blob: c68556435422ec2138b1345fb35a94230fc86de8 [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>
Antonio Borneo39578632019-04-26 23:57:31 +020032#include <errno.h>
Philip Withnall17c2fb42013-11-25 18:01:34 +000033
34#include <linux/input.h>
35#include <cairo.h>
Armin Krezovićeaf58412016-09-29 00:18:10 +020036#include <wayland-util.h>
Philip Withnall17c2fb42013-11-25 18:01:34 +000037
Jon Cruz35b2eaa2015-06-15 15:37:08 -070038#include "shared/helpers.h"
Philip Withnall17c2fb42013-11-25 18:01:34 +000039#include "window.h"
40
41struct stacking {
42 struct display *display;
43 struct window *root_window;
44};
45
46static void
47button_handler(struct widget *widget,
48 struct input *input, uint32_t time,
49 uint32_t button,
50 enum wl_pointer_button_state state, void *data);
51static void
52key_handler(struct window *window,
53 struct input *input, uint32_t time,
54 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
55 void *data);
56static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080057keyboard_focus_handler(struct window *window,
58 struct input *device, void *data);
59static void
Philip Withnall17c2fb42013-11-25 18:01:34 +000060fullscreen_handler(struct window *window, void *data);
61static void
62redraw_handler(struct widget *widget, void *data);
63
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050064/* Iff parent_window is set, the new window will be transient. */
Philip Withnall17c2fb42013-11-25 18:01:34 +000065static struct window *
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050066new_window(struct stacking *stacking, struct window *parent_window)
Philip Withnall17c2fb42013-11-25 18:01:34 +000067{
68 struct window *new_window;
69 struct widget *new_widget;
70
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -050071 new_window = window_create(stacking->display);
Jasper St. Pierrec815d622014-04-10 18:37:54 -070072 window_set_parent(new_window, parent_window);
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050073
Philip Withnall17c2fb42013-11-25 18:01:34 +000074 new_widget = window_frame_create(new_window, new_window);
75
76 window_set_title(new_window, "Stacking Test");
Marius Vladb3544c22021-09-09 13:52:18 +030077 window_set_appid(new_window, "org.freedesktop.weston.stacking-test");
Philip Withnall17c2fb42013-11-25 18:01:34 +000078 window_set_key_handler(new_window, key_handler);
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080079 window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
Philip Withnall17c2fb42013-11-25 18:01:34 +000080 window_set_fullscreen_handler(new_window, fullscreen_handler);
81 widget_set_button_handler(new_widget, button_handler);
82 widget_set_redraw_handler(new_widget, redraw_handler);
83 window_set_user_data(new_window, stacking);
84
85 window_schedule_resize(new_window, 300, 300);
86
87 return new_window;
88}
89
90static void
Jasper St. Pierredda93132014-03-13 12:06:00 -040091show_popup_cb(void *data, struct input *input, int index)
Philip Withnall17c2fb42013-11-25 18:01:34 +000092{
93 /* Ignore the selected menu item. */
94}
95
96static void
97show_popup(struct stacking *stacking, struct input *input, uint32_t time,
98 struct window *window)
99{
100 int32_t x, y;
101 static const char *entries[] = {
102 "Test Entry",
103 "Another Test Entry",
104 };
105
106 input_get_position(input, &x, &y);
107 window_show_menu(stacking->display, input, time, window, x, y,
108 show_popup_cb, entries, ARRAY_LENGTH(entries));
109}
110
111static void
112button_handler(struct widget *widget,
113 struct input *input, uint32_t time,
114 uint32_t button,
115 enum wl_pointer_button_state state, void *data)
116{
117 struct stacking *stacking = data;
118
119 switch (button) {
120 case BTN_RIGHT:
121 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
122 show_popup(stacking, input, time,
123 widget_get_user_data(widget));
124 break;
125
126 case BTN_LEFT:
127 default:
128 break;
129 }
130}
131
132static void
133key_handler(struct window *window,
134 struct input *input, uint32_t time,
135 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
136 void *data)
137{
138 struct stacking *stacking = data;
139
140 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
141 return;
142
143 switch (sym) {
144 case XKB_KEY_f:
145 fullscreen_handler(window, data);
146 break;
147
148 case XKB_KEY_m:
149 window_set_maximized(window, !window_is_maximized(window));
150 break;
151
152 case XKB_KEY_n:
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500153 /* New top-level window. */
154 new_window(stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000155 break;
156
157 case XKB_KEY_p:
158 show_popup(stacking, input, time, window);
159 break;
160
161 case XKB_KEY_q:
162 exit (0);
163 break;
164
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500165 case XKB_KEY_t:
166 /* New transient window. */
167 new_window(stacking, window);
168 break;
169
Philip Withnall17c2fb42013-11-25 18:01:34 +0000170 default:
171 break;
172 }
173}
174
175static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -0800176keyboard_focus_handler(struct window *window,
177 struct input *device, void *data)
178{
179 window_schedule_redraw(window);
180}
181
182static void
Philip Withnall17c2fb42013-11-25 18:01:34 +0000183fullscreen_handler(struct window *window, void *data)
184{
185 window_set_fullscreen(window, !window_is_fullscreen(window));
186}
187
188static void
189draw_string(cairo_t *cr,
Armin Krezovićeaf58412016-09-29 00:18:10 +0200190 const char *fmt, ...) WL_PRINTF(2, 3);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000191
192static void
193draw_string(cairo_t *cr,
194 const char *fmt, ...)
195{
196 char buffer[4096];
197 char *p, *end;
198 va_list argp;
199 cairo_text_extents_t text_extents;
200 cairo_font_extents_t font_extents;
201
202 cairo_save(cr);
203
Alyssa Ross7c121822021-11-17 17:25:17 +0000204 cairo_select_font_face(cr, "sans-serif",
Philip Withnall17c2fb42013-11-25 18:01:34 +0000205 CAIRO_FONT_SLANT_NORMAL,
206 CAIRO_FONT_WEIGHT_NORMAL);
207 cairo_set_font_size(cr, 14);
208
209 cairo_font_extents(cr, &font_extents);
210
211 va_start(argp, fmt);
212
213 vsnprintf(buffer, sizeof(buffer), fmt, argp);
214
215 p = buffer;
216 while (*p) {
217 end = strchr(p, '\n');
218 if (end)
219 *end = 0;
220
221 cairo_show_text(cr, p);
222 cairo_text_extents(cr, p, &text_extents);
223 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
224
225 if (end)
226 p = end + 1;
227 else
228 break;
229 }
230
231 va_end(argp);
232
233 cairo_restore(cr);
234}
235
236static void
237set_window_background_colour(cairo_t *cr, struct window *window)
238{
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700239 if (window_get_parent(window))
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500240 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
241 else if (window_is_maximized(window))
Philip Withnall17c2fb42013-11-25 18:01:34 +0000242 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
243 else if (window_is_fullscreen(window))
244 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
245 else
246 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
247}
248
249static void
250redraw_handler(struct widget *widget, void *data)
251{
252 struct window *window;
253 struct rectangle allocation;
254 cairo_t *cr;
255
256 widget_get_allocation(widget, &allocation);
257 window = widget_get_user_data(widget);
258
259 cr = widget_cairo_create(widget);
260 cairo_translate(cr, allocation.x, allocation.y);
261
262 /* Draw background. */
Philip Withnall17c2fb42013-11-25 18:01:34 +0000263 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
264 set_window_background_colour(cr, window);
265 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
266 cairo_fill(cr);
267
Philip Withnall17c2fb42013-11-25 18:01:34 +0000268 /* 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
Philip Withnall17c2fb42013-11-25 18:01:34 +0000293 stacking.display = display_create(&argc, argv);
294 if (stacking.display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +0200295 fprintf(stderr, "Failed to create display: %s\n",
296 strerror(errno));
Philip Withnall17c2fb42013-11-25 18:01:34 +0000297 return -1;
298 }
299
300 display_set_user_data(stacking.display, &stacking);
301
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500302 stacking.root_window = new_window(&stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000303
304 display_run(stacking.display);
305
vivek31732f72014-05-15 18:58:16 +0530306 window_destroy(stacking.root_window);
307 display_destroy(stacking.display);
308
Philip Withnall17c2fb42013-11-25 18:01:34 +0000309 return 0;
310}