blob: ae9c3326d4a6297fc29fdec28ff42d26e4752418 [file] [log] [blame]
Philip Withnall17c2fb42013-11-25 18:01:34 +00001/*
2 * Copyright © 2013 Collabora Ltd.
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Philip Withnall17c2fb42013-11-25 18:01:34 +000010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Philip Withnall17c2fb42013-11-25 18:01:34 +000022 */
23
24#include "config.h"
25
26#include <assert.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdbool.h>
31
32#include <linux/input.h>
33#include <cairo.h>
34
Jon Cruz35b2eaa2015-06-15 15:37:08 -070035#include "shared/helpers.h"
Philip Withnall17c2fb42013-11-25 18:01:34 +000036#include "window.h"
37
38struct stacking {
39 struct display *display;
40 struct window *root_window;
41};
42
43static void
44button_handler(struct widget *widget,
45 struct input *input, uint32_t time,
46 uint32_t button,
47 enum wl_pointer_button_state state, void *data);
48static void
49key_handler(struct window *window,
50 struct input *input, uint32_t time,
51 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
52 void *data);
53static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080054keyboard_focus_handler(struct window *window,
55 struct input *device, void *data);
56static void
Philip Withnall17c2fb42013-11-25 18:01:34 +000057fullscreen_handler(struct window *window, void *data);
58static void
59redraw_handler(struct widget *widget, void *data);
60
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050061/* Iff parent_window is set, the new window will be transient. */
Philip Withnall17c2fb42013-11-25 18:01:34 +000062static struct window *
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050063new_window(struct stacking *stacking, struct window *parent_window)
Philip Withnall17c2fb42013-11-25 18:01:34 +000064{
65 struct window *new_window;
66 struct widget *new_widget;
67
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -050068 new_window = window_create(stacking->display);
Jasper St. Pierrec815d622014-04-10 18:37:54 -070069 window_set_parent(new_window, parent_window);
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050070
Philip Withnall17c2fb42013-11-25 18:01:34 +000071 new_widget = window_frame_create(new_window, new_window);
72
73 window_set_title(new_window, "Stacking Test");
74 window_set_key_handler(new_window, key_handler);
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080075 window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
Philip Withnall17c2fb42013-11-25 18:01:34 +000076 window_set_fullscreen_handler(new_window, fullscreen_handler);
77 widget_set_button_handler(new_widget, button_handler);
78 widget_set_redraw_handler(new_widget, redraw_handler);
79 window_set_user_data(new_window, stacking);
80
81 window_schedule_resize(new_window, 300, 300);
82
83 return new_window;
84}
85
86static void
Jasper St. Pierredda93132014-03-13 12:06:00 -040087show_popup_cb(void *data, struct input *input, int index)
Philip Withnall17c2fb42013-11-25 18:01:34 +000088{
89 /* Ignore the selected menu item. */
90}
91
92static void
93show_popup(struct stacking *stacking, struct input *input, uint32_t time,
94 struct window *window)
95{
96 int32_t x, y;
97 static const char *entries[] = {
98 "Test Entry",
99 "Another Test Entry",
100 };
101
102 input_get_position(input, &x, &y);
103 window_show_menu(stacking->display, input, time, window, x, y,
104 show_popup_cb, entries, ARRAY_LENGTH(entries));
105}
106
107static void
108button_handler(struct widget *widget,
109 struct input *input, uint32_t time,
110 uint32_t button,
111 enum wl_pointer_button_state state, void *data)
112{
113 struct stacking *stacking = data;
114
115 switch (button) {
116 case BTN_RIGHT:
117 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
118 show_popup(stacking, input, time,
119 widget_get_user_data(widget));
120 break;
121
122 case BTN_LEFT:
123 default:
124 break;
125 }
126}
127
128static void
129key_handler(struct window *window,
130 struct input *input, uint32_t time,
131 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
132 void *data)
133{
134 struct stacking *stacking = data;
135
136 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
137 return;
138
139 switch (sym) {
140 case XKB_KEY_f:
141 fullscreen_handler(window, data);
142 break;
143
144 case XKB_KEY_m:
145 window_set_maximized(window, !window_is_maximized(window));
146 break;
147
148 case XKB_KEY_n:
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500149 /* New top-level window. */
150 new_window(stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000151 break;
152
153 case XKB_KEY_p:
154 show_popup(stacking, input, time, window);
155 break;
156
157 case XKB_KEY_q:
158 exit (0);
159 break;
160
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500161 case XKB_KEY_t:
162 /* New transient window. */
163 new_window(stacking, window);
164 break;
165
Philip Withnall17c2fb42013-11-25 18:01:34 +0000166 default:
167 break;
168 }
169}
170
171static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -0800172keyboard_focus_handler(struct window *window,
173 struct input *device, void *data)
174{
175 window_schedule_redraw(window);
176}
177
178static void
Philip Withnall17c2fb42013-11-25 18:01:34 +0000179fullscreen_handler(struct window *window, void *data)
180{
181 window_set_fullscreen(window, !window_is_fullscreen(window));
182}
183
184static void
185draw_string(cairo_t *cr,
186 const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
187
188static void
189draw_string(cairo_t *cr,
190 const char *fmt, ...)
191{
192 char buffer[4096];
193 char *p, *end;
194 va_list argp;
195 cairo_text_extents_t text_extents;
196 cairo_font_extents_t font_extents;
197
198 cairo_save(cr);
199
200 cairo_select_font_face(cr, "sans",
201 CAIRO_FONT_SLANT_NORMAL,
202 CAIRO_FONT_WEIGHT_NORMAL);
203 cairo_set_font_size(cr, 14);
204
205 cairo_font_extents(cr, &font_extents);
206
207 va_start(argp, fmt);
208
209 vsnprintf(buffer, sizeof(buffer), fmt, argp);
210
211 p = buffer;
212 while (*p) {
213 end = strchr(p, '\n');
214 if (end)
215 *end = 0;
216
217 cairo_show_text(cr, p);
218 cairo_text_extents(cr, p, &text_extents);
219 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
220
221 if (end)
222 p = end + 1;
223 else
224 break;
225 }
226
227 va_end(argp);
228
229 cairo_restore(cr);
230}
231
232static void
233set_window_background_colour(cairo_t *cr, struct window *window)
234{
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700235 if (window_get_parent(window))
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500236 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
237 else if (window_is_maximized(window))
Philip Withnall17c2fb42013-11-25 18:01:34 +0000238 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
239 else if (window_is_fullscreen(window))
240 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
241 else
242 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
243}
244
245static void
246redraw_handler(struct widget *widget, void *data)
247{
248 struct window *window;
249 struct rectangle allocation;
250 cairo_t *cr;
251
252 widget_get_allocation(widget, &allocation);
253 window = widget_get_user_data(widget);
254
255 cr = widget_cairo_create(widget);
256 cairo_translate(cr, allocation.x, allocation.y);
257
258 /* Draw background. */
259 cairo_push_group(cr);
260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
261 set_window_background_colour(cr, window);
262 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
263 cairo_fill(cr);
264
265 cairo_pop_group_to_source(cr);
266 cairo_paint(cr);
267
268 /* Print the instructions. */
269 cairo_move_to(cr, 5, 15);
270 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
271
272 draw_string(cr,
273 "Window: %p\n"
274 "Fullscreen? %u\n"
275 "Maximized? %u\n"
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500276 "Transient? %u\n"
Philip Withnall17c2fb42013-11-25 18:01:34 +0000277 "Keys: (f)ullscreen, (m)aximize,\n"
278 " (n)ew window, (p)opup,\n"
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500279 " (q)uit, (t)ransient window\n",
Philip Withnall17c2fb42013-11-25 18:01:34 +0000280 window, window_is_fullscreen(window),
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700281 window_is_maximized(window), window_get_parent(window) ? 1 : 0);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000282
283 cairo_destroy(cr);
284}
285
286int
287main(int argc, char *argv[])
288{
289 struct stacking stacking;
290
291 memset(&stacking, 0, sizeof stacking);
292
293#ifdef HAVE_PANGO
294 g_type_init();
295#endif
296
297 stacking.display = display_create(&argc, argv);
298 if (stacking.display == NULL) {
299 fprintf(stderr, "Failed to create display: %m\n");
300 return -1;
301 }
302
303 display_set_user_data(stacking.display, &stacking);
304
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500305 stacking.root_window = new_window(&stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000306
307 display_run(stacking.display);
308
vivek31732f72014-05-15 18:58:16 +0530309 window_destroy(stacking.root_window);
310 display_destroy(stacking.display);
311
Philip Withnall17c2fb42013-11-25 18:01:34 +0000312 return 0;
313}