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