blob: e323aa507b116b8337d4fa5b692cd56e4906aa68 [file] [log] [blame]
Tim Wiederhake503ccca2011-01-21 16:56:07 +01001/*
2 * Copyright © 2011 Tim Wiederhake
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * 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 Wiederhake503ccca2011-01-21 16:56:07 +010010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * 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 Wiederhake503ccca2011-01-21 16:56:07 +010022 */
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 Wedgbury9cd661e2014-04-07 12:40:35 +010034#include "config.h"
35
Tim Wiederhake503ccca2011-01-21 16:56:07 +010036#include <stdio.h>
37#include <stdlib.h>
38
39#include <cairo.h>
Tim Wiederhake503ccca2011-01-21 16:56:07 +010040
Jon Cruz35b2eaa2015-06-15 15:37:08 -070041#include "shared/helpers.h"
Tim Wiederhake503ccca2011-01-21 16:56:07 +010042#include "window.h"
43
44/** window title */
45static char *title = "EventDemo";
46
47/** window width */
48static int width = 500;
49
50/** window height */
51static int height = 400;
52
53/** set if window has no borders */
54static int noborder = 0;
55
56/** if non-zero, maximum window width */
57static int width_max = 0;
58
59/** if non-zero, maximum window height */
60static int height_max = 0;
61
62/** set to log redrawing */
63static int log_redraw = 0;
64
65/** set to log resizing */
66static int log_resize = 0;
67
68/** set to log keyboard focus */
69static int log_focus = 0;
70
71/** set to log key events */
72static int log_key = 0;
73
74/** set to log button events */
75static int log_button = 0;
76
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +020077/** set to log axis events */
78static int log_axis = 0;
79
Tim Wiederhake503ccca2011-01-21 16:56:07 +010080/** set to log motion events */
81static int log_motion = 0;
82
83/**
84 * \struct eventdemo
85 * \brief Holds all data the program needs per window
86 *
87 * In this demo the struct holds the position of a
88 * red rectangle that is drawn in the window's area.
89 */
90struct eventdemo {
91 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050092 struct widget *widget;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010093 struct display *display;
94
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040095 int x, y, w, h;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010096};
97
98/**
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050099 * \brief CALLBACK function, Wayland requests the window to redraw.
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200100 * \param widget widget to be redrawn
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500101 * \param data user data associated to the window
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100102 *
103 * Draws a red rectangle as demonstration of per-window data.
104 */
105static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500106redraw_handler(struct widget *widget, void *data)
107{
108 struct eventdemo *e = data;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100109 cairo_surface_t *surface;
110 cairo_t *cr;
111 struct rectangle rect;
112
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500113 if (log_redraw)
114 printf("redraw\n");
115
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500116 widget_get_allocation(e->widget, &rect);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100117 surface = window_get_surface(e->window);
118
119 cr = cairo_create(surface);
120 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
121
122 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
123 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
124 cairo_fill(cr);
125
126 cairo_rectangle(cr, e->x, e->y, e->w, e->h);
127 cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
128 cairo_fill(cr);
129
130 cairo_destroy(cr);
131 cairo_surface_destroy(surface);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100132}
133
134/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100135 * \brief CALLBACK function, Wayland requests the window to resize.
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200136 * \param widget widget to be resized
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100137 * \param width desired width
138 * \param height desired height
139 * \param data user data associated to the window
140 */
141
142static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500143resize_handler(struct widget *widget,
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100144 int32_t width, int32_t height, void *data)
145{
146 struct eventdemo *e = data;
147 if (log_resize)
148 printf("resize width: %d, height: %d\n", width, height);
149
150 /* if a maximum width is set, constrain to it */
151 if (width_max && width_max < width)
152 width = width_max;
153
154 /* if a maximum height is set, constrain to it */
155 if (height_max && height_max < height)
156 height = height_max;
157
158 /* set the new window dimensions */
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500159 widget_set_size(e->widget, width, height);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100160}
161
162/**
163 * \brief CALLBACK function, Wayland informs about keyboard focus change
164 * \param window window
165 * \param device device that caused the focus change
166 * \param data user data associated to the window
167 */
168static void
169keyboard_focus_handler(struct window *window,
170 struct input *device, void *data)
171{
172 int32_t x, y;
173 struct eventdemo *e = data;
174
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300175 if (log_focus) {
176 if (device) {
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100177 input_get_position(device, &x, &y);
178 printf("focus x: %d, y: %d\n", x, y);
179 } else {
180 printf("focus lost\n");
181 }
182 }
183
184 window_schedule_redraw(e->window);
185}
186
187/**
188 * \brief CALLBACK function, Wayland informs about key event
189 * \param window window
190 * \param key keycode
191 * \param unicode associated character
192 * \param state pressed or released
193 * \param modifiers modifiers: ctrl, alt, meta etc.
194 * \param data user data associated to the window
195 */
196static void
197key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100198 uint32_t key, uint32_t unicode, enum wl_keyboard_key_state state,
199 void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100200{
201 uint32_t modifiers = input_get_modifiers(input);
202
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300203 if (!log_key)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100204 return;
205
Daniel Stonec9785ea2012-05-30 16:31:52 +0100206 printf("key key: %d, unicode: %d, state: %s, modifiers: 0x%x\n",
207 key, unicode,
208 (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? "pressed" :
209 "released",
210 modifiers);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100211}
212
213/**
214 * \brief CALLBACK function, Wayland informs about button event
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200215 * \param widget widget
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100216 * \param input input device that caused the button event
Martin Olsson3b132e32012-09-29 15:13:56 +0200217 * \param time time the event happened
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100218 * \param button button
219 * \param state pressed or released
220 * \param data user data associated to the window
221 */
222static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500223button_handler(struct widget *widget, struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100224 uint32_t button, enum wl_pointer_button_state state, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100225{
226 int32_t x, y;
227
228 if (!log_button)
229 return;
230
231 input_get_position(input, &x, &y);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100232 printf("button time: %d, button: %d, state: %s, x: %d, y: %d\n",
233 time, button,
234 (state == WL_POINTER_BUTTON_STATE_PRESSED) ? "pressed" :
235 "released",
236 x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100237}
238
239/**
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200240 * \brief CALLBACK function, Wayland informs about axis event
241 * \param widget widget
242 * \param input input device that caused the axis event
243 * \param time time the event happened
244 * \param axis vertical or horizontal
245 * \param value amount of scrolling
246 * \param data user data associated to the widget
247 */
248static void
249axis_handler(struct widget *widget, struct input *input, uint32_t time,
250 uint32_t axis, wl_fixed_t value, void *data)
251{
252 if (!log_axis)
253 return;
254
255 printf("axis time: %d, axis: %s, value: %f\n",
256 time,
257 axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? "vertical" :
258 "horizontal",
259 wl_fixed_to_double(value));
260}
261
Peter Hutterer87743e92016-01-18 16:38:22 +1000262static void
263pointer_frame_handler(struct widget *widget, struct input *input, void *data)
264{
265 printf("pointer frame\n");
266}
267
268static void
269axis_source_handler(struct widget *widget, struct input *input,
270 uint32_t source, void *data)
271{
272 const char *axis_source;
273
274 switch (source) {
275 case WL_POINTER_AXIS_SOURCE_WHEEL:
276 axis_source = "wheel";
277 break;
278 case WL_POINTER_AXIS_SOURCE_FINGER:
279 axis_source = "finger";
280 break;
281 case WL_POINTER_AXIS_SOURCE_CONTINUOUS:
282 axis_source = "continuous";
283 break;
284 default:
285 axis_source = "<invalid source value>";
286 break;
287 }
288
289 printf("axis source: %s\n", axis_source);
290}
291
292static void
293axis_stop_handler(struct widget *widget, struct input *input,
294 uint32_t time, uint32_t axis,
295 void *data)
296{
297 printf("axis stop time: %d, axis: %s\n",
298 time,
299 axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? "vertical" :
300 "horizontal");
301}
302
303static void
304axis_discrete_handler(struct widget *widget, struct input *input,
305 uint32_t axis, int32_t discrete, void *data)
306{
307 printf("axis discrete axis: %d value: %d\n", axis, discrete);
308}
309
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200310/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100311 * \brief CALLBACK function, Waylands informs about pointer motion
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200312 * \param widget widget
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100313 * \param input input device that caused the motion event
Martin Olsson3b132e32012-09-29 15:13:56 +0200314 * \param time time the event happened
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100315 * \param x absolute x position
316 * \param y absolute y position
317 * \param sx x position relative to the window
318 * \param sy y position relative to the window
319 * \param data user data associated to the window
320 *
321 * Demonstrates the use of different cursors
322 */
323static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500324motion_handler(struct widget *widget, struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400325 float x, float y, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100326{
327 struct eventdemo *e = data;
328
329 if (log_motion) {
Daniel Stoneb230a7e2012-05-08 17:17:54 +0100330 printf("motion time: %d, x: %f, y: %f\n", time, x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100331 }
332
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500333 if (x > e->x && x < e->x + e->w)
334 if (y > e->y && y < e->y + e->h)
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300335 return CURSOR_HAND1;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100336
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300337 return CURSOR_LEFT_PTR;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100338}
339
340/**
341 * \brief Create and initialise a new eventdemo window.
Martin Olssonc663b222012-07-08 11:11:18 +0200342 * The returned eventdemo instance should be destroyed using \c eventdemo_destroy().
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100343 * \param d associated display
344 */
345static struct eventdemo *
346eventdemo_create(struct display *d)
347{
348 struct eventdemo *e;
349
350 e = malloc(sizeof (struct eventdemo));
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300351 if (e == NULL)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100352 return NULL;
353
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500354 e->window = window_create(d);
Philipp Brüschweilera306a262012-08-14 11:02:40 +0200355
356 if (noborder) {
357 /* Demonstrate how to create a borderless window.
358 * Move windows with META + left mouse button.
359 */
360 e->widget = window_add_widget(e->window, e);
361 } else {
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500362 e->widget = window_frame_create(e->window, e);
Philipp Brüschweilera306a262012-08-14 11:02:40 +0200363 window_set_title(e->window, title);
364 }
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100365 e->display = d;
366
367 /* The eventdemo window draws a red rectangle as a demonstration
368 * of per-window data. The dimensions of that rectangle are set
369 * here.
370 */
371 e->x = width * 1.0 / 4.0;
372 e->w = width * 2.0 / 4.0;
373 e->y = height * 1.0 / 4.0;
374 e->h = height * 2.0 / 4.0;
375
376 /* Connect the user data to the window */
377 window_set_user_data(e->window, e);
378
379 /* Set the callback redraw handler for the window */
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500380 widget_set_redraw_handler(e->widget, redraw_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100381
382 /* Set the callback resize handler for the window */
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500383 widget_set_resize_handler(e->widget, resize_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100384
385 /* Set the callback focus handler for the window */
386 window_set_keyboard_focus_handler(e->window,
387 keyboard_focus_handler);
388
389 /* Set the callback key handler for the window */
390 window_set_key_handler(e->window, key_handler);
391
392 /* Set the callback button handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500393 widget_set_button_handler(e->widget, button_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100394
395 /* Set the callback motion handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500396 widget_set_motion_handler(e->widget, motion_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100397
Peter Hutterer87743e92016-01-18 16:38:22 +1000398 /* Set the callback pointer frame handler for the window */
399 widget_set_pointer_frame_handler(e->widget, pointer_frame_handler);
400
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200401 /* Set the callback axis handler for the window */
Peter Hutterer87743e92016-01-18 16:38:22 +1000402 widget_set_axis_handlers(e->widget,
403 axis_handler,
404 axis_source_handler,
405 axis_stop_handler,
406 axis_discrete_handler);
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200407
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100408 /* Initial drawing of the window */
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500409 window_schedule_resize(e->window, width, height);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100410
411 return e;
412}
413/**
Martin Olssonc663b222012-07-08 11:11:18 +0200414 * \brief Destroy eventdemo instance previously created by \c eventdemo_create().
415 * \param eventdemo eventdemo instance to destroy
416 */
417static void eventdemo_destroy(struct eventdemo * eventdemo)
418{
419 widget_destroy(eventdemo->widget);
420 window_destroy(eventdemo->window);
421 free(eventdemo);
422}
423/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100424 * \brief command line options for eventdemo
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100425 */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400426static const struct weston_option eventdemo_options[] = {
427 { WESTON_OPTION_STRING, "title", 0, &title },
428 { WESTON_OPTION_INTEGER, "width", 'w', &width },
429 { WESTON_OPTION_INTEGER, "height", 'h', &height },
430 { WESTON_OPTION_INTEGER, "max-width", 0, &width_max },
431 { WESTON_OPTION_INTEGER, "max-height", 0, &height_max },
432 { WESTON_OPTION_BOOLEAN, "no-border", 'b', &noborder },
Bill Spitzak4fb84912014-08-08 12:59:51 -0700433 { WESTON_OPTION_BOOLEAN, "log-redraw", 0, &log_redraw },
434 { WESTON_OPTION_BOOLEAN, "log-resize", 0, &log_resize },
435 { WESTON_OPTION_BOOLEAN, "log-focus", 0, &log_focus },
436 { WESTON_OPTION_BOOLEAN, "log-key", 0, &log_key },
437 { WESTON_OPTION_BOOLEAN, "log-button", 0, &log_button },
438 { WESTON_OPTION_BOOLEAN, "log-axis", 0, &log_axis },
439 { WESTON_OPTION_BOOLEAN, "log-motion", 0, &log_motion },
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100440};
441
442/**
443 * \brief Connects to the display, creates the window and hands over
444 * to the main loop.
445 */
446int
447main(int argc, char *argv[])
448{
449 struct display *d;
450 struct eventdemo *e;
451
Bill Spitzak4fb84912014-08-08 12:59:51 -0700452 if (parse_options(eventdemo_options,
453 ARRAY_LENGTH(eventdemo_options), &argc, argv) > 1) {
454 unsigned k;
455 printf("Usage: %s [OPTIONS]\n\n", argv[0]);
456 for (k = 0; k < ARRAY_LENGTH(eventdemo_options); k++) {
457 const struct weston_option* p = &eventdemo_options[k];
458 if (p->name) {
459 printf(" --%s", p->name);
460 if (p->type != WESTON_OPTION_BOOLEAN)
461 printf("=VALUE");
462 putchar('\n');
463 }
464 if (p->short_name) {
465 printf(" -%c", p->short_name);
466 if (p->type != WESTON_OPTION_BOOLEAN)
467 printf("VALUE");
468 putchar('\n');
469 }
470 }
471 return 1;
472 }
473
474 if (!log_redraw && !log_resize && !log_focus && !log_key &&
475 !log_button && !log_axis && !log_motion)
476 log_redraw = log_resize = log_focus = log_key =
477 log_button = log_axis = log_motion = 1;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400478
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100479 /* Connect to the display and have the arguments parsed */
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500480 d = display_create(&argc, argv);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100481 if (d == NULL) {
482 fprintf(stderr, "failed to create display: %m\n");
483 return -1;
484 }
485
486 /* Create new eventdemo window */
487 e = eventdemo_create(d);
488 if (e == NULL) {
489 fprintf(stderr, "failed to create eventdemo: %m\n");
490 return -1;
491 }
492
493 display_run(d);
494
Martin Olssonc663b222012-07-08 11:11:18 +0200495 /* Release resources */
496 eventdemo_destroy(e);
497 display_destroy(d);
498
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100499 return 0;
500}