Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Collabora Ltd. |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <stdbool.h> |
| 30 | |
| 31 | #include <linux/input.h> |
| 32 | #include <cairo.h> |
| 33 | |
| 34 | #include "window.h" |
| 35 | |
| 36 | struct stacking { |
| 37 | struct display *display; |
| 38 | struct window *root_window; |
| 39 | }; |
| 40 | |
| 41 | static void |
| 42 | button_handler(struct widget *widget, |
| 43 | struct input *input, uint32_t time, |
| 44 | uint32_t button, |
| 45 | enum wl_pointer_button_state state, void *data); |
| 46 | static void |
| 47 | key_handler(struct window *window, |
| 48 | struct input *input, uint32_t time, |
| 49 | uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, |
| 50 | void *data); |
| 51 | static void |
Kristian Høgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 52 | keyboard_focus_handler(struct window *window, |
| 53 | struct input *device, void *data); |
| 54 | static void |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 55 | fullscreen_handler(struct window *window, void *data); |
| 56 | static void |
| 57 | redraw_handler(struct widget *widget, void *data); |
| 58 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 59 | static struct window * |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 60 | new_window(struct stacking *stacking) |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 61 | { |
| 62 | struct window *new_window; |
| 63 | struct widget *new_widget; |
| 64 | |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 65 | new_window = window_create(stacking->display); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 66 | new_widget = window_frame_create(new_window, new_window); |
| 67 | |
| 68 | window_set_title(new_window, "Stacking Test"); |
| 69 | window_set_key_handler(new_window, key_handler); |
Kristian Høgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 70 | window_set_keyboard_focus_handler(new_window, keyboard_focus_handler); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 71 | window_set_fullscreen_handler(new_window, fullscreen_handler); |
| 72 | widget_set_button_handler(new_widget, button_handler); |
| 73 | widget_set_redraw_handler(new_widget, redraw_handler); |
| 74 | window_set_user_data(new_window, stacking); |
| 75 | |
| 76 | window_schedule_resize(new_window, 300, 300); |
| 77 | |
| 78 | return new_window; |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | show_popup_cb(struct window *window, struct input *input, int index, void *data) |
| 83 | { |
| 84 | /* Ignore the selected menu item. */ |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | show_popup(struct stacking *stacking, struct input *input, uint32_t time, |
| 89 | struct window *window) |
| 90 | { |
| 91 | int32_t x, y; |
| 92 | static const char *entries[] = { |
| 93 | "Test Entry", |
| 94 | "Another Test Entry", |
| 95 | }; |
| 96 | |
| 97 | input_get_position(input, &x, &y); |
| 98 | window_show_menu(stacking->display, input, time, window, x, y, |
| 99 | show_popup_cb, entries, ARRAY_LENGTH(entries)); |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | button_handler(struct widget *widget, |
| 104 | struct input *input, uint32_t time, |
| 105 | uint32_t button, |
| 106 | enum wl_pointer_button_state state, void *data) |
| 107 | { |
| 108 | struct stacking *stacking = data; |
| 109 | |
| 110 | switch (button) { |
| 111 | case BTN_RIGHT: |
| 112 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) |
| 113 | show_popup(stacking, input, time, |
| 114 | widget_get_user_data(widget)); |
| 115 | break; |
| 116 | |
| 117 | case BTN_LEFT: |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | key_handler(struct window *window, |
| 125 | struct input *input, uint32_t time, |
| 126 | uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, |
| 127 | void *data) |
| 128 | { |
| 129 | struct stacking *stacking = data; |
| 130 | |
| 131 | if (state != WL_KEYBOARD_KEY_STATE_PRESSED) |
| 132 | return; |
| 133 | |
| 134 | switch (sym) { |
| 135 | case XKB_KEY_f: |
| 136 | fullscreen_handler(window, data); |
| 137 | break; |
| 138 | |
| 139 | case XKB_KEY_m: |
| 140 | window_set_maximized(window, !window_is_maximized(window)); |
| 141 | break; |
| 142 | |
| 143 | case XKB_KEY_n: |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 144 | new_window(stacking); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 145 | break; |
| 146 | |
| 147 | case XKB_KEY_p: |
| 148 | show_popup(stacking, input, time, window); |
| 149 | break; |
| 150 | |
| 151 | case XKB_KEY_q: |
| 152 | exit (0); |
| 153 | break; |
| 154 | |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 155 | default: |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void |
Kristian Høgsberg | acdae2e | 2013-12-05 15:14:45 -0800 | [diff] [blame] | 161 | keyboard_focus_handler(struct window *window, |
| 162 | struct input *device, void *data) |
| 163 | { |
| 164 | window_schedule_redraw(window); |
| 165 | } |
| 166 | |
| 167 | static void |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 168 | fullscreen_handler(struct window *window, void *data) |
| 169 | { |
| 170 | window_set_fullscreen(window, !window_is_fullscreen(window)); |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | draw_string(cairo_t *cr, |
| 175 | const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3))); |
| 176 | |
| 177 | static void |
| 178 | draw_string(cairo_t *cr, |
| 179 | const char *fmt, ...) |
| 180 | { |
| 181 | char buffer[4096]; |
| 182 | char *p, *end; |
| 183 | va_list argp; |
| 184 | cairo_text_extents_t text_extents; |
| 185 | cairo_font_extents_t font_extents; |
| 186 | |
| 187 | cairo_save(cr); |
| 188 | |
| 189 | cairo_select_font_face(cr, "sans", |
| 190 | CAIRO_FONT_SLANT_NORMAL, |
| 191 | CAIRO_FONT_WEIGHT_NORMAL); |
| 192 | cairo_set_font_size(cr, 14); |
| 193 | |
| 194 | cairo_font_extents(cr, &font_extents); |
| 195 | |
| 196 | va_start(argp, fmt); |
| 197 | |
| 198 | vsnprintf(buffer, sizeof(buffer), fmt, argp); |
| 199 | |
| 200 | p = buffer; |
| 201 | while (*p) { |
| 202 | end = strchr(p, '\n'); |
| 203 | if (end) |
| 204 | *end = 0; |
| 205 | |
| 206 | cairo_show_text(cr, p); |
| 207 | cairo_text_extents(cr, p, &text_extents); |
| 208 | cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height); |
| 209 | |
| 210 | if (end) |
| 211 | p = end + 1; |
| 212 | else |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | va_end(argp); |
| 217 | |
| 218 | cairo_restore(cr); |
| 219 | } |
| 220 | |
| 221 | static void |
| 222 | set_window_background_colour(cairo_t *cr, struct window *window) |
| 223 | { |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 224 | if (window_is_maximized(window)) |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 225 | cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6); |
| 226 | else if (window_is_fullscreen(window)) |
| 227 | cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6); |
| 228 | else |
| 229 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); |
| 230 | } |
| 231 | |
| 232 | static void |
| 233 | redraw_handler(struct widget *widget, void *data) |
| 234 | { |
| 235 | struct window *window; |
| 236 | struct rectangle allocation; |
| 237 | cairo_t *cr; |
| 238 | |
| 239 | widget_get_allocation(widget, &allocation); |
| 240 | window = widget_get_user_data(widget); |
| 241 | |
| 242 | cr = widget_cairo_create(widget); |
| 243 | cairo_translate(cr, allocation.x, allocation.y); |
| 244 | |
| 245 | /* Draw background. */ |
| 246 | cairo_push_group(cr); |
| 247 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 248 | set_window_background_colour(cr, window); |
| 249 | cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); |
| 250 | cairo_fill(cr); |
| 251 | |
| 252 | cairo_pop_group_to_source(cr); |
| 253 | cairo_paint(cr); |
| 254 | |
| 255 | /* Print the instructions. */ |
| 256 | cairo_move_to(cr, 5, 15); |
| 257 | cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); |
| 258 | |
| 259 | draw_string(cr, |
| 260 | "Window: %p\n" |
| 261 | "Fullscreen? %u\n" |
| 262 | "Maximized? %u\n" |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 263 | "Keys: (f)ullscreen, (m)aximize,\n" |
| 264 | " (n)ew window, (p)opup,\n" |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 265 | " (q)uit\n", |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 266 | window, window_is_fullscreen(window), |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 267 | window_is_maximized(window)); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 268 | |
| 269 | cairo_destroy(cr); |
| 270 | } |
| 271 | |
| 272 | int |
| 273 | main(int argc, char *argv[]) |
| 274 | { |
| 275 | struct stacking stacking; |
| 276 | |
| 277 | memset(&stacking, 0, sizeof stacking); |
| 278 | |
| 279 | #ifdef HAVE_PANGO |
| 280 | g_type_init(); |
| 281 | #endif |
| 282 | |
| 283 | stacking.display = display_create(&argc, argv); |
| 284 | if (stacking.display == NULL) { |
| 285 | fprintf(stderr, "Failed to create display: %m\n"); |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | display_set_user_data(stacking.display, &stacking); |
| 290 | |
Jasper St. Pierre | b0d604f | 2013-12-09 14:52:33 -0500 | [diff] [blame] | 291 | stacking.root_window = new_window(&stacking); |
Philip Withnall | 17c2fb4 | 2013-11-25 18:01:34 +0000 | [diff] [blame] | 292 | |
| 293 | display_run(stacking.display); |
| 294 | |
| 295 | return 0; |
| 296 | } |