blob: fc8f8fb2e5efdeef96baf1bd7a1f381e81a52484 [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
35#include "window.h"
36
37struct stacking {
38 struct display *display;
39 struct window *root_window;
40};
41
42static void
43button_handler(struct widget *widget,
44 struct input *input, uint32_t time,
45 uint32_t button,
46 enum wl_pointer_button_state state, void *data);
47static void
48key_handler(struct window *window,
49 struct input *input, uint32_t time,
50 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
51 void *data);
52static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080053keyboard_focus_handler(struct window *window,
54 struct input *device, void *data);
55static void
Philip Withnall17c2fb42013-11-25 18:01:34 +000056fullscreen_handler(struct window *window, void *data);
57static void
58redraw_handler(struct widget *widget, void *data);
59
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050060/* Iff parent_window is set, the new window will be transient. */
Philip Withnall17c2fb42013-11-25 18:01:34 +000061static struct window *
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050062new_window(struct stacking *stacking, struct window *parent_window)
Philip Withnall17c2fb42013-11-25 18:01:34 +000063{
64 struct window *new_window;
65 struct widget *new_widget;
66
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -050067 new_window = window_create(stacking->display);
Jasper St. Pierrec815d622014-04-10 18:37:54 -070068 window_set_parent(new_window, parent_window);
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050069
Philip Withnall17c2fb42013-11-25 18:01:34 +000070 new_widget = window_frame_create(new_window, new_window);
71
72 window_set_title(new_window, "Stacking Test");
73 window_set_key_handler(new_window, key_handler);
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080074 window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
Philip Withnall17c2fb42013-11-25 18:01:34 +000075 window_set_fullscreen_handler(new_window, fullscreen_handler);
76 widget_set_button_handler(new_widget, button_handler);
77 widget_set_redraw_handler(new_widget, redraw_handler);
78 window_set_user_data(new_window, stacking);
79
80 window_schedule_resize(new_window, 300, 300);
81
82 return new_window;
83}
84
85static void
Jasper St. Pierredda93132014-03-13 12:06:00 -040086show_popup_cb(void *data, struct input *input, int index)
Philip Withnall17c2fb42013-11-25 18:01:34 +000087{
88 /* Ignore the selected menu item. */
89}
90
91static void
92show_popup(struct stacking *stacking, struct input *input, uint32_t time,
93 struct window *window)
94{
95 int32_t x, y;
96 static const char *entries[] = {
97 "Test Entry",
98 "Another Test Entry",
99 };
100
101 input_get_position(input, &x, &y);
102 window_show_menu(stacking->display, input, time, window, x, y,
103 show_popup_cb, entries, ARRAY_LENGTH(entries));
104}
105
106static void
107button_handler(struct widget *widget,
108 struct input *input, uint32_t time,
109 uint32_t button,
110 enum wl_pointer_button_state state, void *data)
111{
112 struct stacking *stacking = data;
113
114 switch (button) {
115 case BTN_RIGHT:
116 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
117 show_popup(stacking, input, time,
118 widget_get_user_data(widget));
119 break;
120
121 case BTN_LEFT:
122 default:
123 break;
124 }
125}
126
127static void
128key_handler(struct window *window,
129 struct input *input, uint32_t time,
130 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
131 void *data)
132{
133 struct stacking *stacking = data;
134
135 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
136 return;
137
138 switch (sym) {
139 case XKB_KEY_f:
140 fullscreen_handler(window, data);
141 break;
142
143 case XKB_KEY_m:
144 window_set_maximized(window, !window_is_maximized(window));
145 break;
146
147 case XKB_KEY_n:
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500148 /* New top-level window. */
149 new_window(stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000150 break;
151
152 case XKB_KEY_p:
153 show_popup(stacking, input, time, window);
154 break;
155
156 case XKB_KEY_q:
157 exit (0);
158 break;
159
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500160 case XKB_KEY_t:
161 /* New transient window. */
162 new_window(stacking, window);
163 break;
164
Philip Withnall17c2fb42013-11-25 18:01:34 +0000165 default:
166 break;
167 }
168}
169
170static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -0800171keyboard_focus_handler(struct window *window,
172 struct input *device, void *data)
173{
174 window_schedule_redraw(window);
175}
176
177static void
Philip Withnall17c2fb42013-11-25 18:01:34 +0000178fullscreen_handler(struct window *window, void *data)
179{
180 window_set_fullscreen(window, !window_is_fullscreen(window));
181}
182
183static void
184draw_string(cairo_t *cr,
185 const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
186
187static void
188draw_string(cairo_t *cr,
189 const char *fmt, ...)
190{
191 char buffer[4096];
192 char *p, *end;
193 va_list argp;
194 cairo_text_extents_t text_extents;
195 cairo_font_extents_t font_extents;
196
197 cairo_save(cr);
198
199 cairo_select_font_face(cr, "sans",
200 CAIRO_FONT_SLANT_NORMAL,
201 CAIRO_FONT_WEIGHT_NORMAL);
202 cairo_set_font_size(cr, 14);
203
204 cairo_font_extents(cr, &font_extents);
205
206 va_start(argp, fmt);
207
208 vsnprintf(buffer, sizeof(buffer), fmt, argp);
209
210 p = buffer;
211 while (*p) {
212 end = strchr(p, '\n');
213 if (end)
214 *end = 0;
215
216 cairo_show_text(cr, p);
217 cairo_text_extents(cr, p, &text_extents);
218 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
219
220 if (end)
221 p = end + 1;
222 else
223 break;
224 }
225
226 va_end(argp);
227
228 cairo_restore(cr);
229}
230
231static void
232set_window_background_colour(cairo_t *cr, struct window *window)
233{
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700234 if (window_get_parent(window))
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500235 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
236 else if (window_is_maximized(window))
Philip Withnall17c2fb42013-11-25 18:01:34 +0000237 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
238 else if (window_is_fullscreen(window))
239 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
240 else
241 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
242}
243
244static void
245redraw_handler(struct widget *widget, void *data)
246{
247 struct window *window;
248 struct rectangle allocation;
249 cairo_t *cr;
250
251 widget_get_allocation(widget, &allocation);
252 window = widget_get_user_data(widget);
253
254 cr = widget_cairo_create(widget);
255 cairo_translate(cr, allocation.x, allocation.y);
256
257 /* Draw background. */
258 cairo_push_group(cr);
259 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
260 set_window_background_colour(cr, window);
261 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
262 cairo_fill(cr);
263
264 cairo_pop_group_to_source(cr);
265 cairo_paint(cr);
266
267 /* Print the instructions. */
268 cairo_move_to(cr, 5, 15);
269 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
270
271 draw_string(cr,
272 "Window: %p\n"
273 "Fullscreen? %u\n"
274 "Maximized? %u\n"
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500275 "Transient? %u\n"
Philip Withnall17c2fb42013-11-25 18:01:34 +0000276 "Keys: (f)ullscreen, (m)aximize,\n"
277 " (n)ew window, (p)opup,\n"
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500278 " (q)uit, (t)ransient window\n",
Philip Withnall17c2fb42013-11-25 18:01:34 +0000279 window, window_is_fullscreen(window),
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700280 window_is_maximized(window), window_get_parent(window) ? 1 : 0);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000281
282 cairo_destroy(cr);
283}
284
285int
286main(int argc, char *argv[])
287{
288 struct stacking stacking;
289
290 memset(&stacking, 0, sizeof stacking);
291
292#ifdef HAVE_PANGO
293 g_type_init();
294#endif
295
296 stacking.display = display_create(&argc, argv);
297 if (stacking.display == NULL) {
298 fprintf(stderr, "Failed to create display: %m\n");
299 return -1;
300 }
301
302 display_set_user_data(stacking.display, &stacking);
303
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500304 stacking.root_window = new_window(&stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000305
306 display_run(stacking.display);
307
vivek31732f72014-05-15 18:58:16 +0530308 window_destroy(stacking.root_window);
309 display_destroy(stacking.display);
310
Philip Withnall17c2fb42013-11-25 18:01:34 +0000311 return 0;
312}