Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Collabora Ltd. |
| 3 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 4 | * 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 Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 10 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 11 | * 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 Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include "config.h" |
| 25 | |
| 26 | #include <assert.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 27 | #include <stdint.h> |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <stdbool.h> |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 32 | #include <errno.h> |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 33 | |
| 34 | #include <linux/input.h> |
| 35 | #include <cairo.h> |
Armin Krezović | eaf5841 | 2016-09-29 00:18:10 +0200 | [diff] [blame] | 36 | #include <wayland-util.h> |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 37 | |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 38 | #include "shared/helpers.h" |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 39 | #include "window.h" |
| 40 | |
| 41 | struct stacking { |
| 42 | struct display *display; |
| 43 | struct window *root_window; |
| 44 | }; |
| 45 | |
| 46 | static void |
| 47 | button_handler(struct widget *widget, |
| 48 | struct input *input, uint32_t time, |
| 49 | uint32_t button, |
| 50 | enum wl_pointer_button_state state, void *data); |
| 51 | static void |
| 52 | key_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); |
| 56 | static void |
Kristian Høgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 57 | keyboard_focus_handler(struct window *window, |
| 58 | struct input *device, void *data); |
| 59 | static void |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 60 | fullscreen_handler(struct window *window, void *data); |
| 61 | static void |
| 62 | redraw_handler(struct widget *widget, void *data); |
| 63 | |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 64 | /* Iff parent_window is set, the new window will be transient. */ |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 65 | static struct window * |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 66 | new_window(struct stacking *stacking, struct window *parent_window) |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 67 | { |
| 68 | struct window *new_window; |
| 69 | struct widget *new_widget; |
| 70 | |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 71 | new_window = window_create(stacking->display); |
Jasper St. Pierre | c815d62 | 2014-04-10 18:37:54 -0700 | [diff] [blame] | 72 | window_set_parent(new_window, parent_window); |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 73 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 74 | new_widget = window_frame_create(new_window, new_window); |
| 75 | |
| 76 | window_set_title(new_window, "Stacking Test"); |
Marius Vlad | b3544c2 | 2021-09-09 13:52:18 +0300 | [diff] [blame] | 77 | window_set_appid(new_window, "org.freedesktop.weston.stacking-test"); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 78 | window_set_key_handler(new_window, key_handler); |
Kristian Høgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 79 | window_set_keyboard_focus_handler(new_window, keyboard_focus_handler); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 80 | 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 | |
| 90 | static void |
Jasper St. Pierre | dda9313 | 2014-03-13 12:06:00 -0400 | [diff] [blame] | 91 | show_popup_cb(void *data, struct input *input, int index) |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 92 | { |
| 93 | /* Ignore the selected menu item. */ |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | show_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 | |
| 111 | static void |
| 112 | button_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 | |
| 132 | static void |
| 133 | key_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. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 153 | /* New top-level window. */ |
| 154 | new_window(stacking, NULL); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 155 | 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. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 165 | case XKB_KEY_t: |
| 166 | /* New transient window. */ |
| 167 | new_window(stacking, window); |
| 168 | break; |
| 169 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 170 | default: |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void |
Kristian Høgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 176 | keyboard_focus_handler(struct window *window, |
| 177 | struct input *device, void *data) |
| 178 | { |
| 179 | window_schedule_redraw(window); |
| 180 | } |
| 181 | |
| 182 | static void |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 183 | fullscreen_handler(struct window *window, void *data) |
| 184 | { |
| 185 | window_set_fullscreen(window, !window_is_fullscreen(window)); |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | draw_string(cairo_t *cr, |
Armin Krezović | eaf5841 | 2016-09-29 00:18:10 +0200 | [diff] [blame] | 190 | const char *fmt, ...) WL_PRINTF(2, 3); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 191 | |
| 192 | static void |
| 193 | draw_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 Ross | 7c12182 | 2021-11-17 17:25:17 +0000 | [diff] [blame] | 204 | cairo_select_font_face(cr, "sans-serif", |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 205 | 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 | |
| 236 | static void |
| 237 | set_window_background_colour(cairo_t *cr, struct window *window) |
| 238 | { |
Jasper St. Pierre | c815d62 | 2014-04-10 18:37:54 -0700 | [diff] [blame] | 239 | if (window_get_parent(window)) |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 240 | cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4); |
| 241 | else if (window_is_maximized(window)) |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 242 | 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 | |
| 249 | static void |
| 250 | redraw_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 Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 263 | 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 Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 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. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 276 | "Transient? %u\n" |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 277 | "Keys: (f)ullscreen, (m)aximize,\n" |
| 278 | " (n)ew window, (p)opup,\n" |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 279 | " (q)uit, (t)ransient window\n", |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 280 | window, window_is_fullscreen(window), |
Jasper St. Pierre | c815d62 | 2014-04-10 18:37:54 -0700 | [diff] [blame] | 281 | window_is_maximized(window), window_get_parent(window) ? 1 : 0); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 282 | |
| 283 | cairo_destroy(cr); |
| 284 | } |
| 285 | |
| 286 | int |
| 287 | main(int argc, char *argv[]) |
| 288 | { |
| 289 | struct stacking stacking; |
| 290 | |
| 291 | memset(&stacking, 0, sizeof stacking); |
| 292 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 293 | stacking.display = display_create(&argc, argv); |
| 294 | if (stacking.display == NULL) { |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 295 | fprintf(stderr, "Failed to create display: %s\n", |
| 296 | strerror(errno)); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 297 | return -1; |
| 298 | } |
| 299 | |
| 300 | display_set_user_data(stacking.display, &stacking); |
| 301 | |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 302 | stacking.root_window = new_window(&stacking, NULL); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 303 | |
| 304 | display_run(stacking.display); |
| 305 | |
vivek | 31732f7 | 2014-05-15 18:58:16 +0530 | [diff] [blame] | 306 | window_destroy(stacking.root_window); |
| 307 | display_destroy(stacking.display); |
| 308 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 309 | return 0; |
| 310 | } |