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> |
| 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 Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame^] | 35 | #include "shared/helpers.h" |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 36 | #include "window.h" |
| 37 | |
| 38 | struct stacking { |
| 39 | struct display *display; |
| 40 | struct window *root_window; |
| 41 | }; |
| 42 | |
| 43 | static void |
| 44 | button_handler(struct widget *widget, |
| 45 | struct input *input, uint32_t time, |
| 46 | uint32_t button, |
| 47 | enum wl_pointer_button_state state, void *data); |
| 48 | static void |
| 49 | key_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); |
| 53 | static void |
Kristian Høgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 54 | keyboard_focus_handler(struct window *window, |
| 55 | struct input *device, void *data); |
| 56 | static void |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 57 | fullscreen_handler(struct window *window, void *data); |
| 58 | static void |
| 59 | redraw_handler(struct widget *widget, void *data); |
| 60 | |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 61 | /* Iff parent_window is set, the new window will be transient. */ |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 62 | static struct window * |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 63 | new_window(struct stacking *stacking, struct window *parent_window) |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 64 | { |
| 65 | struct window *new_window; |
| 66 | struct widget *new_widget; |
| 67 | |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 68 | new_window = window_create(stacking->display); |
Jasper St. Pierre | c815d62 | 2014-04-10 18:37:54 -0700 | [diff] [blame] | 69 | window_set_parent(new_window, parent_window); |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 70 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 71 | 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øgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 75 | window_set_keyboard_focus_handler(new_window, keyboard_focus_handler); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 76 | 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 | |
| 86 | static void |
Jasper St. Pierre | dda9313 | 2014-03-13 12:06:00 -0400 | [diff] [blame] | 87 | show_popup_cb(void *data, struct input *input, int index) |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 88 | { |
| 89 | /* Ignore the selected menu item. */ |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | show_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 | |
| 107 | static void |
| 108 | button_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 | |
| 128 | static void |
| 129 | key_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. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 149 | /* New top-level window. */ |
| 150 | new_window(stacking, NULL); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 151 | 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. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 161 | case XKB_KEY_t: |
| 162 | /* New transient window. */ |
| 163 | new_window(stacking, window); |
| 164 | break; |
| 165 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static void |
Kristian Høgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 172 | keyboard_focus_handler(struct window *window, |
| 173 | struct input *device, void *data) |
| 174 | { |
| 175 | window_schedule_redraw(window); |
| 176 | } |
| 177 | |
| 178 | static void |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 179 | fullscreen_handler(struct window *window, void *data) |
| 180 | { |
| 181 | window_set_fullscreen(window, !window_is_fullscreen(window)); |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | draw_string(cairo_t *cr, |
| 186 | const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3))); |
| 187 | |
| 188 | static void |
| 189 | draw_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 | |
| 232 | static void |
| 233 | set_window_background_colour(cairo_t *cr, struct window *window) |
| 234 | { |
Jasper St. Pierre | c815d62 | 2014-04-10 18:37:54 -0700 | [diff] [blame] | 235 | if (window_get_parent(window)) |
Jasper St. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 236 | cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4); |
| 237 | else if (window_is_maximized(window)) |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 238 | 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 | |
| 245 | static void |
| 246 | redraw_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. 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 | |
| 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. Pierre | 1e47a93 | 2013-12-09 15:20:16 -0500 | [diff] [blame] | 305 | stacking.root_window = new_window(&stacking, NULL); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 306 | |
| 307 | display_run(stacking.display); |
| 308 | |
vivek | 31732f7 | 2014-05-15 18:58:16 +0530 | [diff] [blame] | 309 | window_destroy(stacking.root_window); |
| 310 | display_destroy(stacking.display); |
| 311 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 312 | return 0; |
| 313 | } |