2 * Copyright © 2013 Collabora Ltd.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #include <linux/input.h>
37 struct display *display;
38 struct window *root_window;
42 button_handler(struct widget *widget,
43 struct input *input, uint32_t time,
45 enum wl_pointer_button_state state, void *data);
47 key_handler(struct window *window,
48 struct input *input, uint32_t time,
49 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
52 keyboard_focus_handler(struct window *window,
53 struct input *device, void *data);
55 fullscreen_handler(struct window *window, void *data);
57 redraw_handler(struct widget *widget, void *data);
59 /* Iff parent_window is set, the new window will be transient. */
60 static struct window *
61 new_window(struct stacking *stacking, struct window *parent_window)
63 struct window *new_window;
64 struct widget *new_widget;
66 new_window = window_create(stacking->display);
67 window_set_parent(new_window, parent_window);
69 new_widget = window_frame_create(new_window, new_window);
71 window_set_title(new_window, "Stacking Test");
72 window_set_key_handler(new_window, key_handler);
73 window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
74 window_set_fullscreen_handler(new_window, fullscreen_handler);
75 widget_set_button_handler(new_widget, button_handler);
76 widget_set_redraw_handler(new_widget, redraw_handler);
77 window_set_user_data(new_window, stacking);
79 window_schedule_resize(new_window, 300, 300);
85 show_popup_cb(void *data, struct input *input, int index)
87 /* Ignore the selected menu item. */
91 show_popup(struct stacking *stacking, struct input *input, uint32_t time,
92 struct window *window)
95 static const char *entries[] = {
100 input_get_position(input, &x, &y);
101 window_show_menu(stacking->display, input, time, window, x, y,
102 show_popup_cb, entries, ARRAY_LENGTH(entries));
106 button_handler(struct widget *widget,
107 struct input *input, uint32_t time,
109 enum wl_pointer_button_state state, void *data)
111 struct stacking *stacking = data;
115 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
116 show_popup(stacking, input, time,
117 widget_get_user_data(widget));
127 key_handler(struct window *window,
128 struct input *input, uint32_t time,
129 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
132 struct stacking *stacking = data;
134 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
139 fullscreen_handler(window, data);
143 window_set_maximized(window, !window_is_maximized(window));
147 /* New top-level window. */
148 new_window(stacking, NULL);
152 show_popup(stacking, input, time, window);
160 /* New transient window. */
161 new_window(stacking, window);
170 keyboard_focus_handler(struct window *window,
171 struct input *device, void *data)
173 window_schedule_redraw(window);
177 fullscreen_handler(struct window *window, void *data)
179 window_set_fullscreen(window, !window_is_fullscreen(window));
183 draw_string(cairo_t *cr,
184 const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
187 draw_string(cairo_t *cr,
188 const char *fmt, ...)
193 cairo_text_extents_t text_extents;
194 cairo_font_extents_t font_extents;
198 cairo_select_font_face(cr, "sans",
199 CAIRO_FONT_SLANT_NORMAL,
200 CAIRO_FONT_WEIGHT_NORMAL);
201 cairo_set_font_size(cr, 14);
203 cairo_font_extents(cr, &font_extents);
207 vsnprintf(buffer, sizeof(buffer), fmt, argp);
211 end = strchr(p, '\n');
215 cairo_show_text(cr, p);
216 cairo_text_extents(cr, p, &text_extents);
217 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
231 set_window_background_colour(cairo_t *cr, struct window *window)
233 if (window_get_parent(window))
234 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
235 else if (window_is_maximized(window))
236 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
237 else if (window_is_fullscreen(window))
238 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
240 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
244 redraw_handler(struct widget *widget, void *data)
246 struct window *window;
247 struct rectangle allocation;
250 widget_get_allocation(widget, &allocation);
251 window = widget_get_user_data(widget);
253 cr = widget_cairo_create(widget);
254 cairo_translate(cr, allocation.x, allocation.y);
256 /* Draw background. */
257 cairo_push_group(cr);
258 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
259 set_window_background_colour(cr, window);
260 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
263 cairo_pop_group_to_source(cr);
266 /* Print the instructions. */
267 cairo_move_to(cr, 5, 15);
268 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
275 "Keys: (f)ullscreen, (m)aximize,\n"
276 " (n)ew window, (p)opup,\n"
277 " (q)uit, (t)ransient window\n",
278 window, window_is_fullscreen(window),
279 window_is_maximized(window), window_get_parent(window) ? 1 : 0);
285 main(int argc, char *argv[])
287 struct stacking stacking;
289 memset(&stacking, 0, sizeof stacking);
295 stacking.display = display_create(&argc, argv);
296 if (stacking.display == NULL) {
297 fprintf(stderr, "Failed to create display: %m\n");
301 display_set_user_data(stacking.display, &stacking);
303 stacking.root_window = new_window(&stacking, NULL);
305 display_run(stacking.display);
307 window_destroy(stacking.root_window);
308 display_destroy(stacking.display);