blob: 544094c60c346ed76d5f804eb1a5045fd15cc693 [file] [log] [blame]
Philip Withnall17c2fb42013-11-25 18:01:34 +00001/*
2 * Copyright © 2013 Collabora Ltd.
3 *
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.
13 *
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.
21 */
22
23#include "config.h"
24
25#include <assert.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdbool.h>
30
31#include <linux/input.h>
32#include <cairo.h>
33
34#include "window.h"
35
36struct stacking {
37 struct display *display;
38 struct window *root_window;
39};
40
41static void
42button_handler(struct widget *widget,
43 struct input *input, uint32_t time,
44 uint32_t button,
45 enum wl_pointer_button_state state, void *data);
46static void
47key_handler(struct window *window,
48 struct input *input, uint32_t time,
49 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
50 void *data);
51static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080052keyboard_focus_handler(struct window *window,
53 struct input *device, void *data);
54static void
Philip Withnall17c2fb42013-11-25 18:01:34 +000055fullscreen_handler(struct window *window, void *data);
56static void
57redraw_handler(struct widget *widget, void *data);
58
59/* Iff parent_window is set, the new window will be transient. */
60static struct window *
61new_window(struct stacking *stacking, struct window *parent_window)
62{
63 struct window *new_window;
64 struct widget *new_widget;
65
66 if (parent_window == NULL) {
67 new_window = window_create(stacking->display);
68 } else {
69 new_window = window_create_transient(stacking->display,
70 parent_window, 50, 50, 0);
71 }
72
73 new_widget = window_frame_create(new_window, new_window);
74
75 window_set_title(new_window, "Stacking Test");
76 window_set_key_handler(new_window, key_handler);
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080077 window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
Philip Withnall17c2fb42013-11-25 18:01:34 +000078 window_set_fullscreen_handler(new_window, fullscreen_handler);
79 widget_set_button_handler(new_widget, button_handler);
80 widget_set_redraw_handler(new_widget, redraw_handler);
81 window_set_user_data(new_window, stacking);
82
83 window_schedule_resize(new_window, 300, 300);
84
85 return new_window;
86}
87
88static void
89show_popup_cb(struct window *window, struct input *input, int index, void *data)
90{
91 /* Ignore the selected menu item. */
92}
93
94static void
95show_popup(struct stacking *stacking, struct input *input, uint32_t time,
96 struct window *window)
97{
98 int32_t x, y;
99 static const char *entries[] = {
100 "Test Entry",
101 "Another Test Entry",
102 };
103
104 input_get_position(input, &x, &y);
105 window_show_menu(stacking->display, input, time, window, x, y,
106 show_popup_cb, entries, ARRAY_LENGTH(entries));
107}
108
109static void
110button_handler(struct widget *widget,
111 struct input *input, uint32_t time,
112 uint32_t button,
113 enum wl_pointer_button_state state, void *data)
114{
115 struct stacking *stacking = data;
116
117 switch (button) {
118 case BTN_RIGHT:
119 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
120 show_popup(stacking, input, time,
121 widget_get_user_data(widget));
122 break;
123
124 case BTN_LEFT:
125 default:
126 break;
127 }
128}
129
130static void
131key_handler(struct window *window,
132 struct input *input, uint32_t time,
133 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
134 void *data)
135{
136 struct stacking *stacking = data;
137
138 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
139 return;
140
141 switch (sym) {
142 case XKB_KEY_f:
143 fullscreen_handler(window, data);
144 break;
145
146 case XKB_KEY_m:
147 window_set_maximized(window, !window_is_maximized(window));
148 break;
149
150 case XKB_KEY_n:
151 /* New top-level window. */
152 new_window(stacking, NULL);
153 break;
154
155 case XKB_KEY_p:
156 show_popup(stacking, input, time, window);
157 break;
158
159 case XKB_KEY_q:
160 exit (0);
161 break;
162
163 case XKB_KEY_t:
164 /* New transient window. */
165 new_window(stacking, window);
166 break;
167
168 default:
169 break;
170 }
171}
172
173static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -0800174keyboard_focus_handler(struct window *window,
175 struct input *device, void *data)
176{
177 window_schedule_redraw(window);
178}
179
180static void
Philip Withnall17c2fb42013-11-25 18:01:34 +0000181fullscreen_handler(struct window *window, void *data)
182{
183 window_set_fullscreen(window, !window_is_fullscreen(window));
184}
185
186static void
187draw_string(cairo_t *cr,
188 const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
189
190static void
191draw_string(cairo_t *cr,
192 const char *fmt, ...)
193{
194 char buffer[4096];
195 char *p, *end;
196 va_list argp;
197 cairo_text_extents_t text_extents;
198 cairo_font_extents_t font_extents;
199
200 cairo_save(cr);
201
202 cairo_select_font_face(cr, "sans",
203 CAIRO_FONT_SLANT_NORMAL,
204 CAIRO_FONT_WEIGHT_NORMAL);
205 cairo_set_font_size(cr, 14);
206
207 cairo_font_extents(cr, &font_extents);
208
209 va_start(argp, fmt);
210
211 vsnprintf(buffer, sizeof(buffer), fmt, argp);
212
213 p = buffer;
214 while (*p) {
215 end = strchr(p, '\n');
216 if (end)
217 *end = 0;
218
219 cairo_show_text(cr, p);
220 cairo_text_extents(cr, p, &text_extents);
221 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
222
223 if (end)
224 p = end + 1;
225 else
226 break;
227 }
228
229 va_end(argp);
230
231 cairo_restore(cr);
232}
233
234static void
235set_window_background_colour(cairo_t *cr, struct window *window)
236{
237 if (window_is_transient(window))
238 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
239 else if (window_is_maximized(window))
240 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
241 else if (window_is_fullscreen(window))
242 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
243 else
244 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
245}
246
247static void
248redraw_handler(struct widget *widget, void *data)
249{
250 struct window *window;
251 struct rectangle allocation;
252 cairo_t *cr;
253
254 widget_get_allocation(widget, &allocation);
255 window = widget_get_user_data(widget);
256
257 cr = widget_cairo_create(widget);
258 cairo_translate(cr, allocation.x, allocation.y);
259
260 /* Draw background. */
261 cairo_push_group(cr);
262 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
263 set_window_background_colour(cr, window);
264 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
265 cairo_fill(cr);
266
267 cairo_pop_group_to_source(cr);
268 cairo_paint(cr);
269
270 /* Print the instructions. */
271 cairo_move_to(cr, 5, 15);
272 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
273
274 draw_string(cr,
275 "Window: %p\n"
276 "Fullscreen? %u\n"
277 "Maximized? %u\n"
278 "Transient? %u\n"
279 "Keys: (f)ullscreen, (m)aximize,\n"
280 " (n)ew window, (p)opup,\n"
281 " (q)uit, (t)ransient window\n",
282 window, window_is_fullscreen(window),
283 window_is_maximized(window), window_is_transient(window));
284
285 cairo_destroy(cr);
286}
287
288int
289main(int argc, char *argv[])
290{
291 struct stacking stacking;
292
293 memset(&stacking, 0, sizeof stacking);
294
295#ifdef HAVE_PANGO
296 g_type_init();
297#endif
298
299 stacking.display = display_create(&argc, argv);
300 if (stacking.display == NULL) {
301 fprintf(stderr, "Failed to create display: %m\n");
302 return -1;
303 }
304
305 display_set_user_data(stacking.display, &stacking);
306
307 stacking.root_window = new_window(&stacking, NULL);
308
309 display_run(stacking.display);
310
311 return 0;
312}