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 fullscreen_handler(struct window *window, void *data);
54 redraw_handler(struct widget *widget, void *data);
56 /* Iff parent_window is set, the new window will be transient. */
57 static struct window *
58 new_window(struct stacking *stacking, struct window *parent_window)
60 struct window *new_window;
61 struct widget *new_widget;
63 if (parent_window == NULL) {
64 new_window = window_create(stacking->display);
66 new_window = window_create_transient(stacking->display,
67 parent_window, 50, 50, 0);
70 new_widget = window_frame_create(new_window, new_window);
72 window_set_title(new_window, "Stacking Test");
73 window_set_key_handler(new_window, key_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(struct window *window, struct input *input, int index, void *data)
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 fullscreen_handler(struct window *window, void *data)
172 window_set_fullscreen(window, !window_is_fullscreen(window));
176 draw_string(cairo_t *cr,
177 const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
180 draw_string(cairo_t *cr,
181 const char *fmt, ...)
186 cairo_text_extents_t text_extents;
187 cairo_font_extents_t font_extents;
191 cairo_select_font_face(cr, "sans",
192 CAIRO_FONT_SLANT_NORMAL,
193 CAIRO_FONT_WEIGHT_NORMAL);
194 cairo_set_font_size(cr, 14);
196 cairo_font_extents(cr, &font_extents);
200 vsnprintf(buffer, sizeof(buffer), fmt, argp);
204 end = strchr(p, '\n');
208 cairo_show_text(cr, p);
209 cairo_text_extents(cr, p, &text_extents);
210 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
224 set_window_background_colour(cairo_t *cr, struct window *window)
226 if (window_is_transient(window))
227 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
228 else if (window_is_maximized(window))
229 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
230 else if (window_is_fullscreen(window))
231 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
233 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
237 redraw_handler(struct widget *widget, void *data)
239 struct window *window;
240 struct rectangle allocation;
243 widget_get_allocation(widget, &allocation);
244 window = widget_get_user_data(widget);
246 cr = widget_cairo_create(widget);
247 cairo_translate(cr, allocation.x, allocation.y);
249 /* Draw background. */
250 cairo_push_group(cr);
251 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
252 set_window_background_colour(cr, window);
253 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
256 cairo_pop_group_to_source(cr);
259 /* Print the instructions. */
260 cairo_move_to(cr, 5, 15);
261 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
268 "Keys: (f)ullscreen, (m)aximize,\n"
269 " (n)ew window, (p)opup,\n"
270 " (q)uit, (t)ransient window\n",
271 window, window_is_fullscreen(window),
272 window_is_maximized(window), window_is_transient(window));
278 main(int argc, char *argv[])
280 struct stacking stacking;
282 memset(&stacking, 0, sizeof stacking);
288 stacking.display = display_create(&argc, argv);
289 if (stacking.display == NULL) {
290 fprintf(stderr, "Failed to create display: %m\n");
294 display_set_user_data(stacking.display, &stacking);
296 stacking.root_window = new_window(&stacking, NULL);
298 display_run(stacking.display);