blob: 0682e60ac644dcc7c2f7b253cd8b0cb64b6155ad [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030027#include <stdint.h>
Philip Withnall17c2fb42013-11-25 18:01:34 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdbool.h>
32
33#include <linux/input.h>
34#include <cairo.h>
Armin Krezovićeaf58412016-09-29 00:18:10 +020035#include <wayland-util.h>
Philip Withnall17c2fb42013-11-25 18:01:34 +000036
Jon Cruz35b2eaa2015-06-15 15:37:08 -070037#include "shared/helpers.h"
Philip Withnall17c2fb42013-11-25 18:01:34 +000038#include "window.h"
39
40struct stacking {
41 struct display *display;
42 struct window *root_window;
43};
44
45static void
46button_handler(struct widget *widget,
47 struct input *input, uint32_t time,
48 uint32_t button,
49 enum wl_pointer_button_state state, void *data);
50static void
51key_handler(struct window *window,
52 struct input *input, uint32_t time,
53 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
54 void *data);
55static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080056keyboard_focus_handler(struct window *window,
57 struct input *device, void *data);
58static void
Philip Withnall17c2fb42013-11-25 18:01:34 +000059fullscreen_handler(struct window *window, void *data);
60static void
61redraw_handler(struct widget *widget, void *data);
62
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050063/* Iff parent_window is set, the new window will be transient. */
Philip Withnall17c2fb42013-11-25 18:01:34 +000064static struct window *
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050065new_window(struct stacking *stacking, struct window *parent_window)
Philip Withnall17c2fb42013-11-25 18:01:34 +000066{
67 struct window *new_window;
68 struct widget *new_widget;
69
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -050070 new_window = window_create(stacking->display);
Jasper St. Pierrec815d622014-04-10 18:37:54 -070071 window_set_parent(new_window, parent_window);
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050072
Philip Withnall17c2fb42013-11-25 18:01:34 +000073 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
Jasper St. Pierredda93132014-03-13 12:06:00 -040089show_popup_cb(void *data, struct input *input, int index)
Philip Withnall17c2fb42013-11-25 18:01:34 +000090{
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:
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500151 /* New top-level window. */
152 new_window(stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000153 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
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500163 case XKB_KEY_t:
164 /* New transient window. */
165 new_window(stacking, window);
166 break;
167
Philip Withnall17c2fb42013-11-25 18:01:34 +0000168 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,
Armin Krezovićeaf58412016-09-29 00:18:10 +0200188 const char *fmt, ...) WL_PRINTF(2, 3);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000189
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{
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700237 if (window_get_parent(window))
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500238 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
239 else if (window_is_maximized(window))
Philip Withnall17c2fb42013-11-25 18:01:34 +0000240 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. */
Philip Withnall17c2fb42013-11-25 18:01:34 +0000261 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
262 set_window_background_colour(cr, window);
263 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
264 cairo_fill(cr);
265
Philip Withnall17c2fb42013-11-25 18:01:34 +0000266 /* Print the instructions. */
267 cairo_move_to(cr, 5, 15);
268 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
269
270 draw_string(cr,
271 "Window: %p\n"
272 "Fullscreen? %u\n"
273 "Maximized? %u\n"
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500274 "Transient? %u\n"
Philip Withnall17c2fb42013-11-25 18:01:34 +0000275 "Keys: (f)ullscreen, (m)aximize,\n"
276 " (n)ew window, (p)opup,\n"
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500277 " (q)uit, (t)ransient window\n",
Philip Withnall17c2fb42013-11-25 18:01:34 +0000278 window, window_is_fullscreen(window),
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700279 window_is_maximized(window), window_get_parent(window) ? 1 : 0);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000280
281 cairo_destroy(cr);
282}
283
284int
285main(int argc, char *argv[])
286{
287 struct stacking stacking;
288
289 memset(&stacking, 0, sizeof stacking);
290
291#ifdef HAVE_PANGO
292 g_type_init();
293#endif
294
295 stacking.display = display_create(&argc, argv);
296 if (stacking.display == NULL) {
297 fprintf(stderr, "Failed to create display: %m\n");
298 return -1;
299 }
300
301 display_set_user_data(stacking.display, &stacking);
302
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500303 stacking.root_window = new_window(&stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000304
305 display_run(stacking.display);
306
vivek31732f72014-05-15 18:58:16 +0530307 window_destroy(stacking.root_window);
308 display_destroy(stacking.display);
309
Philip Withnall17c2fb42013-11-25 18:01:34 +0000310 return 0;
311}