Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Tim Wiederhake |
| 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: |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [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. |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file eventdemo.c |
| 26 | * \brief Demonstrate the use of Wayland's toytoolkit. |
| 27 | * |
| 28 | * Heavily commented demo program that can report all events that are |
| 29 | * dispatched to the window. For other functionality, eg. opengl/egl, |
| 30 | * drag and drop, etc. have a look at the other demos. |
| 31 | * \author Tim Wiederhake |
| 32 | */ |
| 33 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 34 | #include "config.h" |
| 35 | |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 38 | #include <stdbool.h> |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 39 | |
| 40 | #include <cairo.h> |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 41 | |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 42 | #include "shared/helpers.h" |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 43 | #include "window.h" |
| 44 | |
| 45 | /** window title */ |
| 46 | static char *title = "EventDemo"; |
| 47 | |
| 48 | /** window width */ |
| 49 | static int width = 500; |
| 50 | |
| 51 | /** window height */ |
| 52 | static int height = 400; |
| 53 | |
| 54 | /** set if window has no borders */ |
| 55 | static int noborder = 0; |
| 56 | |
| 57 | /** if non-zero, maximum window width */ |
| 58 | static int width_max = 0; |
| 59 | |
| 60 | /** if non-zero, maximum window height */ |
| 61 | static int height_max = 0; |
| 62 | |
| 63 | /** set to log redrawing */ |
| 64 | static int log_redraw = 0; |
| 65 | |
| 66 | /** set to log resizing */ |
| 67 | static int log_resize = 0; |
| 68 | |
| 69 | /** set to log keyboard focus */ |
| 70 | static int log_focus = 0; |
| 71 | |
| 72 | /** set to log key events */ |
| 73 | static int log_key = 0; |
| 74 | |
| 75 | /** set to log button events */ |
| 76 | static int log_button = 0; |
| 77 | |
Philipp Brüschweiler | 70cc73b | 2012-08-14 11:02:42 +0200 | [diff] [blame] | 78 | /** set to log axis events */ |
| 79 | static int log_axis = 0; |
| 80 | |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 81 | /** set to log motion events */ |
| 82 | static int log_motion = 0; |
| 83 | |
| 84 | /** |
| 85 | * \struct eventdemo |
| 86 | * \brief Holds all data the program needs per window |
| 87 | * |
| 88 | * In this demo the struct holds the position of a |
| 89 | * red rectangle that is drawn in the window's area. |
| 90 | */ |
| 91 | struct eventdemo { |
| 92 | struct window *window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 93 | struct widget *widget; |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 94 | struct display *display; |
| 95 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 96 | int x, y, w, h; |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 97 | |
| 98 | bool print_pointer_frame; |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /** |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 102 | * \brief CALLBACK function, Wayland requests the window to redraw. |
Philipp Brüschweiler | 850f504 | 2012-08-14 11:02:39 +0200 | [diff] [blame] | 103 | * \param widget widget to be redrawn |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 104 | * \param data user data associated to the window |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 105 | * |
| 106 | * Draws a red rectangle as demonstration of per-window data. |
| 107 | */ |
| 108 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 109 | redraw_handler(struct widget *widget, void *data) |
| 110 | { |
| 111 | struct eventdemo *e = data; |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 112 | cairo_surface_t *surface; |
| 113 | cairo_t *cr; |
| 114 | struct rectangle rect; |
| 115 | |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 116 | if (log_redraw) |
| 117 | printf("redraw\n"); |
| 118 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 119 | widget_get_allocation(e->widget, &rect); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 120 | surface = window_get_surface(e->window); |
| 121 | |
| 122 | cr = cairo_create(surface); |
| 123 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 124 | |
| 125 | cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height); |
| 126 | cairo_set_source_rgba(cr, 0, 0, 0, 0.8); |
| 127 | cairo_fill(cr); |
| 128 | |
| 129 | cairo_rectangle(cr, e->x, e->y, e->w, e->h); |
| 130 | cairo_set_source_rgba(cr, 1.0, 0, 0, 1); |
| 131 | cairo_fill(cr); |
| 132 | |
| 133 | cairo_destroy(cr); |
| 134 | cairo_surface_destroy(surface); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 138 | * \brief CALLBACK function, Wayland requests the window to resize. |
Philipp Brüschweiler | 850f504 | 2012-08-14 11:02:39 +0200 | [diff] [blame] | 139 | * \param widget widget to be resized |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 140 | * \param width desired width |
| 141 | * \param height desired height |
| 142 | * \param data user data associated to the window |
| 143 | */ |
| 144 | |
| 145 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 146 | resize_handler(struct widget *widget, |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 147 | int32_t width, int32_t height, void *data) |
| 148 | { |
| 149 | struct eventdemo *e = data; |
| 150 | if (log_resize) |
| 151 | printf("resize width: %d, height: %d\n", width, height); |
| 152 | |
| 153 | /* if a maximum width is set, constrain to it */ |
| 154 | if (width_max && width_max < width) |
| 155 | width = width_max; |
| 156 | |
| 157 | /* if a maximum height is set, constrain to it */ |
| 158 | if (height_max && height_max < height) |
| 159 | height = height_max; |
| 160 | |
| 161 | /* set the new window dimensions */ |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 162 | widget_set_size(e->widget, width, height); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * \brief CALLBACK function, Wayland informs about keyboard focus change |
| 167 | * \param window window |
| 168 | * \param device device that caused the focus change |
| 169 | * \param data user data associated to the window |
| 170 | */ |
| 171 | static void |
| 172 | keyboard_focus_handler(struct window *window, |
| 173 | struct input *device, void *data) |
| 174 | { |
| 175 | int32_t x, y; |
| 176 | struct eventdemo *e = data; |
| 177 | |
Dawid Gajownik | 74a635b | 2015-08-06 17:12:19 -0300 | [diff] [blame] | 178 | if (log_focus) { |
| 179 | if (device) { |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 180 | input_get_position(device, &x, &y); |
| 181 | printf("focus x: %d, y: %d\n", x, y); |
| 182 | } else { |
| 183 | printf("focus lost\n"); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | window_schedule_redraw(e->window); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * \brief CALLBACK function, Wayland informs about key event |
| 192 | * \param window window |
| 193 | * \param key keycode |
| 194 | * \param unicode associated character |
| 195 | * \param state pressed or released |
| 196 | * \param modifiers modifiers: ctrl, alt, meta etc. |
| 197 | * \param data user data associated to the window |
| 198 | */ |
| 199 | static void |
| 200 | key_handler(struct window *window, struct input *input, uint32_t time, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 201 | uint32_t key, uint32_t unicode, enum wl_keyboard_key_state state, |
| 202 | void *data) |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 203 | { |
| 204 | uint32_t modifiers = input_get_modifiers(input); |
| 205 | |
Dawid Gajownik | 74a635b | 2015-08-06 17:12:19 -0300 | [diff] [blame] | 206 | if (!log_key) |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 207 | return; |
| 208 | |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 209 | printf("key key: %d, unicode: %d, state: %s, modifiers: 0x%x\n", |
| 210 | key, unicode, |
| 211 | (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? "pressed" : |
| 212 | "released", |
| 213 | modifiers); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * \brief CALLBACK function, Wayland informs about button event |
Philipp Brüschweiler | 850f504 | 2012-08-14 11:02:39 +0200 | [diff] [blame] | 218 | * \param widget widget |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 219 | * \param input input device that caused the button event |
Martin Olsson | 3b132e3 | 2012-09-29 15:13:56 +0200 | [diff] [blame] | 220 | * \param time time the event happened |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 221 | * \param button button |
| 222 | * \param state pressed or released |
| 223 | * \param data user data associated to the window |
| 224 | */ |
| 225 | static void |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 226 | button_handler(struct widget *widget, struct input *input, uint32_t time, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 227 | uint32_t button, enum wl_pointer_button_state state, void *data) |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 228 | { |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 229 | struct eventdemo *e = data; |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 230 | int32_t x, y; |
| 231 | |
| 232 | if (!log_button) |
| 233 | return; |
| 234 | |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 235 | e->print_pointer_frame = true; |
| 236 | |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 237 | input_get_position(input, &x, &y); |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 238 | printf("button time: %d, button: %d, state: %s, x: %d, y: %d\n", |
| 239 | time, button, |
| 240 | (state == WL_POINTER_BUTTON_STATE_PRESSED) ? "pressed" : |
| 241 | "released", |
| 242 | x, y); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
Philipp Brüschweiler | 70cc73b | 2012-08-14 11:02:42 +0200 | [diff] [blame] | 246 | * \brief CALLBACK function, Wayland informs about axis event |
| 247 | * \param widget widget |
| 248 | * \param input input device that caused the axis event |
| 249 | * \param time time the event happened |
| 250 | * \param axis vertical or horizontal |
| 251 | * \param value amount of scrolling |
| 252 | * \param data user data associated to the widget |
| 253 | */ |
| 254 | static void |
| 255 | axis_handler(struct widget *widget, struct input *input, uint32_t time, |
| 256 | uint32_t axis, wl_fixed_t value, void *data) |
| 257 | { |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 258 | struct eventdemo *e = data; |
| 259 | |
Philipp Brüschweiler | 70cc73b | 2012-08-14 11:02:42 +0200 | [diff] [blame] | 260 | if (!log_axis) |
| 261 | return; |
| 262 | |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 263 | e->print_pointer_frame = true; |
| 264 | |
Philipp Brüschweiler | 70cc73b | 2012-08-14 11:02:42 +0200 | [diff] [blame] | 265 | printf("axis time: %d, axis: %s, value: %f\n", |
| 266 | time, |
| 267 | axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? "vertical" : |
| 268 | "horizontal", |
| 269 | wl_fixed_to_double(value)); |
| 270 | } |
| 271 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 272 | static void |
| 273 | pointer_frame_handler(struct widget *widget, struct input *input, void *data) |
| 274 | { |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 275 | struct eventdemo *e = data; |
| 276 | |
| 277 | if (!e->print_pointer_frame) |
| 278 | return; |
| 279 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 280 | printf("pointer frame\n"); |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 281 | e->print_pointer_frame = false; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void |
| 285 | axis_source_handler(struct widget *widget, struct input *input, |
| 286 | uint32_t source, void *data) |
| 287 | { |
| 288 | const char *axis_source; |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 289 | struct eventdemo *e = data; |
| 290 | |
| 291 | e->print_pointer_frame = true; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 292 | |
| 293 | switch (source) { |
| 294 | case WL_POINTER_AXIS_SOURCE_WHEEL: |
| 295 | axis_source = "wheel"; |
| 296 | break; |
| 297 | case WL_POINTER_AXIS_SOURCE_FINGER: |
| 298 | axis_source = "finger"; |
| 299 | break; |
| 300 | case WL_POINTER_AXIS_SOURCE_CONTINUOUS: |
| 301 | axis_source = "continuous"; |
| 302 | break; |
| 303 | default: |
| 304 | axis_source = "<invalid source value>"; |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | printf("axis source: %s\n", axis_source); |
| 309 | } |
| 310 | |
| 311 | static void |
| 312 | axis_stop_handler(struct widget *widget, struct input *input, |
| 313 | uint32_t time, uint32_t axis, |
| 314 | void *data) |
| 315 | { |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 316 | struct eventdemo *e = data; |
| 317 | |
| 318 | e->print_pointer_frame = true; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 319 | printf("axis stop time: %d, axis: %s\n", |
| 320 | time, |
| 321 | axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? "vertical" : |
| 322 | "horizontal"); |
| 323 | } |
| 324 | |
| 325 | static void |
| 326 | axis_discrete_handler(struct widget *widget, struct input *input, |
| 327 | uint32_t axis, int32_t discrete, void *data) |
| 328 | { |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 329 | struct eventdemo *e = data; |
| 330 | |
| 331 | e->print_pointer_frame = true; |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 332 | printf("axis discrete axis: %d value: %d\n", axis, discrete); |
| 333 | } |
| 334 | |
Philipp Brüschweiler | 70cc73b | 2012-08-14 11:02:42 +0200 | [diff] [blame] | 335 | /** |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 336 | * \brief CALLBACK function, Waylands informs about pointer motion |
Philipp Brüschweiler | 850f504 | 2012-08-14 11:02:39 +0200 | [diff] [blame] | 337 | * \param widget widget |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 338 | * \param input input device that caused the motion event |
Martin Olsson | 3b132e3 | 2012-09-29 15:13:56 +0200 | [diff] [blame] | 339 | * \param time time the event happened |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 340 | * \param x absolute x position |
| 341 | * \param y absolute y position |
| 342 | * \param sx x position relative to the window |
| 343 | * \param sy y position relative to the window |
| 344 | * \param data user data associated to the window |
| 345 | * |
| 346 | * Demonstrates the use of different cursors |
| 347 | */ |
| 348 | static int |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 349 | motion_handler(struct widget *widget, struct input *input, uint32_t time, |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 350 | float x, float y, void *data) |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 351 | { |
| 352 | struct eventdemo *e = data; |
| 353 | |
| 354 | if (log_motion) { |
Daniel Stone | b230a7e | 2012-05-08 17:17:54 +0100 | [diff] [blame] | 355 | printf("motion time: %d, x: %f, y: %f\n", time, x, y); |
Pekka Paalanen | 4386499 | 2016-04-12 13:33:09 +0300 | [diff] [blame] | 356 | e->print_pointer_frame = true; |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 359 | if (x > e->x && x < e->x + e->w) |
| 360 | if (y > e->y && y < e->y + e->h) |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 361 | return CURSOR_HAND1; |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 362 | |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 363 | return CURSOR_LEFT_PTR; |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * \brief Create and initialise a new eventdemo window. |
Martin Olsson | c663b22 | 2012-07-08 11:11:18 +0200 | [diff] [blame] | 368 | * The returned eventdemo instance should be destroyed using \c eventdemo_destroy(). |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 369 | * \param d associated display |
| 370 | */ |
| 371 | static struct eventdemo * |
| 372 | eventdemo_create(struct display *d) |
| 373 | { |
| 374 | struct eventdemo *e; |
| 375 | |
Pekka Paalanen | 10e92db | 2016-04-12 13:49:53 +0300 | [diff] [blame^] | 376 | e = zalloc(sizeof (struct eventdemo)); |
Dawid Gajownik | 74a635b | 2015-08-06 17:12:19 -0300 | [diff] [blame] | 377 | if (e == NULL) |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 378 | return NULL; |
| 379 | |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 380 | e->window = window_create(d); |
Philipp Brüschweiler | a306a26 | 2012-08-14 11:02:40 +0200 | [diff] [blame] | 381 | |
| 382 | if (noborder) { |
| 383 | /* Demonstrate how to create a borderless window. |
| 384 | * Move windows with META + left mouse button. |
| 385 | */ |
| 386 | e->widget = window_add_widget(e->window, e); |
| 387 | } else { |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 388 | e->widget = window_frame_create(e->window, e); |
Philipp Brüschweiler | a306a26 | 2012-08-14 11:02:40 +0200 | [diff] [blame] | 389 | window_set_title(e->window, title); |
| 390 | } |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 391 | e->display = d; |
| 392 | |
| 393 | /* The eventdemo window draws a red rectangle as a demonstration |
| 394 | * of per-window data. The dimensions of that rectangle are set |
| 395 | * here. |
| 396 | */ |
| 397 | e->x = width * 1.0 / 4.0; |
| 398 | e->w = width * 2.0 / 4.0; |
| 399 | e->y = height * 1.0 / 4.0; |
| 400 | e->h = height * 2.0 / 4.0; |
| 401 | |
| 402 | /* Connect the user data to the window */ |
| 403 | window_set_user_data(e->window, e); |
| 404 | |
| 405 | /* Set the callback redraw handler for the window */ |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 406 | widget_set_redraw_handler(e->widget, redraw_handler); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 407 | |
| 408 | /* Set the callback resize handler for the window */ |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 409 | widget_set_resize_handler(e->widget, resize_handler); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 410 | |
| 411 | /* Set the callback focus handler for the window */ |
| 412 | window_set_keyboard_focus_handler(e->window, |
| 413 | keyboard_focus_handler); |
| 414 | |
| 415 | /* Set the callback key handler for the window */ |
| 416 | window_set_key_handler(e->window, key_handler); |
| 417 | |
| 418 | /* Set the callback button handler for the window */ |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 419 | widget_set_button_handler(e->widget, button_handler); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 420 | |
| 421 | /* Set the callback motion handler for the window */ |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 422 | widget_set_motion_handler(e->widget, motion_handler); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 423 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 424 | /* Set the callback pointer frame handler for the window */ |
| 425 | widget_set_pointer_frame_handler(e->widget, pointer_frame_handler); |
| 426 | |
Philipp Brüschweiler | 70cc73b | 2012-08-14 11:02:42 +0200 | [diff] [blame] | 427 | /* Set the callback axis handler for the window */ |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 428 | widget_set_axis_handlers(e->widget, |
| 429 | axis_handler, |
| 430 | axis_source_handler, |
| 431 | axis_stop_handler, |
| 432 | axis_discrete_handler); |
Philipp Brüschweiler | 70cc73b | 2012-08-14 11:02:42 +0200 | [diff] [blame] | 433 | |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 434 | /* Initial drawing of the window */ |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 435 | window_schedule_resize(e->window, width, height); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 436 | |
| 437 | return e; |
| 438 | } |
| 439 | /** |
Martin Olsson | c663b22 | 2012-07-08 11:11:18 +0200 | [diff] [blame] | 440 | * \brief Destroy eventdemo instance previously created by \c eventdemo_create(). |
| 441 | * \param eventdemo eventdemo instance to destroy |
| 442 | */ |
| 443 | static void eventdemo_destroy(struct eventdemo * eventdemo) |
| 444 | { |
| 445 | widget_destroy(eventdemo->widget); |
| 446 | window_destroy(eventdemo->window); |
| 447 | free(eventdemo); |
| 448 | } |
| 449 | /** |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 450 | * \brief command line options for eventdemo |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 451 | */ |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 452 | static const struct weston_option eventdemo_options[] = { |
| 453 | { WESTON_OPTION_STRING, "title", 0, &title }, |
| 454 | { WESTON_OPTION_INTEGER, "width", 'w', &width }, |
| 455 | { WESTON_OPTION_INTEGER, "height", 'h', &height }, |
| 456 | { WESTON_OPTION_INTEGER, "max-width", 0, &width_max }, |
| 457 | { WESTON_OPTION_INTEGER, "max-height", 0, &height_max }, |
| 458 | { WESTON_OPTION_BOOLEAN, "no-border", 'b', &noborder }, |
Bill Spitzak | 4fb8491 | 2014-08-08 12:59:51 -0700 | [diff] [blame] | 459 | { WESTON_OPTION_BOOLEAN, "log-redraw", 0, &log_redraw }, |
| 460 | { WESTON_OPTION_BOOLEAN, "log-resize", 0, &log_resize }, |
| 461 | { WESTON_OPTION_BOOLEAN, "log-focus", 0, &log_focus }, |
| 462 | { WESTON_OPTION_BOOLEAN, "log-key", 0, &log_key }, |
| 463 | { WESTON_OPTION_BOOLEAN, "log-button", 0, &log_button }, |
| 464 | { WESTON_OPTION_BOOLEAN, "log-axis", 0, &log_axis }, |
| 465 | { WESTON_OPTION_BOOLEAN, "log-motion", 0, &log_motion }, |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | /** |
| 469 | * \brief Connects to the display, creates the window and hands over |
| 470 | * to the main loop. |
| 471 | */ |
| 472 | int |
| 473 | main(int argc, char *argv[]) |
| 474 | { |
| 475 | struct display *d; |
| 476 | struct eventdemo *e; |
| 477 | |
Bill Spitzak | 4fb8491 | 2014-08-08 12:59:51 -0700 | [diff] [blame] | 478 | if (parse_options(eventdemo_options, |
| 479 | ARRAY_LENGTH(eventdemo_options), &argc, argv) > 1) { |
| 480 | unsigned k; |
| 481 | printf("Usage: %s [OPTIONS]\n\n", argv[0]); |
| 482 | for (k = 0; k < ARRAY_LENGTH(eventdemo_options); k++) { |
| 483 | const struct weston_option* p = &eventdemo_options[k]; |
| 484 | if (p->name) { |
| 485 | printf(" --%s", p->name); |
| 486 | if (p->type != WESTON_OPTION_BOOLEAN) |
| 487 | printf("=VALUE"); |
| 488 | putchar('\n'); |
| 489 | } |
| 490 | if (p->short_name) { |
| 491 | printf(" -%c", p->short_name); |
| 492 | if (p->type != WESTON_OPTION_BOOLEAN) |
| 493 | printf("VALUE"); |
| 494 | putchar('\n'); |
| 495 | } |
| 496 | } |
| 497 | return 1; |
| 498 | } |
| 499 | |
| 500 | if (!log_redraw && !log_resize && !log_focus && !log_key && |
| 501 | !log_button && !log_axis && !log_motion) |
| 502 | log_redraw = log_resize = log_focus = log_key = |
| 503 | log_button = log_axis = log_motion = 1; |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 504 | |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 505 | /* Connect to the display and have the arguments parsed */ |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 506 | d = display_create(&argc, argv); |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 507 | if (d == NULL) { |
| 508 | fprintf(stderr, "failed to create display: %m\n"); |
| 509 | return -1; |
| 510 | } |
| 511 | |
| 512 | /* Create new eventdemo window */ |
| 513 | e = eventdemo_create(d); |
| 514 | if (e == NULL) { |
| 515 | fprintf(stderr, "failed to create eventdemo: %m\n"); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | display_run(d); |
| 520 | |
Martin Olsson | c663b22 | 2012-07-08 11:11:18 +0200 | [diff] [blame] | 521 | /* Release resources */ |
| 522 | eventdemo_destroy(e); |
| 523 | display_destroy(d); |
| 524 | |
Tim Wiederhake | 503ccca | 2011-01-21 16:56:07 +0100 | [diff] [blame] | 525 | return 0; |
| 526 | } |