blob: 0f98869a7d9af18424b557b473e48354b57dd19d [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
41#include "window.h"
42
43/** window title */
44static char *title = "EventDemo";
45
46/** window width */
47static int width = 500;
48
49/** window height */
50static int height = 400;
51
52/** set if window has no borders */
53static int noborder = 0;
54
55/** if non-zero, maximum window width */
56static int width_max = 0;
57
58/** if non-zero, maximum window height */
59static int height_max = 0;
60
61/** set to log redrawing */
62static int log_redraw = 0;
63
64/** set to log resizing */
65static int log_resize = 0;
66
67/** set to log keyboard focus */
68static int log_focus = 0;
69
70/** set to log key events */
71static int log_key = 0;
72
73/** set to log button events */
74static int log_button = 0;
75
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +020076/** set to log axis events */
77static int log_axis = 0;
78
Tim Wiederhake503ccca2011-01-21 16:56:07 +010079/** set to log motion events */
80static int log_motion = 0;
81
82/**
83 * \struct eventdemo
84 * \brief Holds all data the program needs per window
85 *
86 * In this demo the struct holds the position of a
87 * red rectangle that is drawn in the window's area.
88 */
89struct eventdemo {
90 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050091 struct widget *widget;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010092 struct display *display;
93
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040094 int x, y, w, h;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010095};
96
97/**
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050098 * \brief CALLBACK function, Wayland requests the window to redraw.
Philipp Brüschweiler850f5042012-08-14 11:02:39 +020099 * \param widget widget to be redrawn
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500100 * \param data user data associated to the window
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100101 *
102 * Draws a red rectangle as demonstration of per-window data.
103 */
104static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500105redraw_handler(struct widget *widget, void *data)
106{
107 struct eventdemo *e = data;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100108 cairo_surface_t *surface;
109 cairo_t *cr;
110 struct rectangle rect;
111
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500112 if (log_redraw)
113 printf("redraw\n");
114
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500115 widget_get_allocation(e->widget, &rect);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100116 surface = window_get_surface(e->window);
117
118 cr = cairo_create(surface);
119 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
120
121 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
122 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
123 cairo_fill(cr);
124
125 cairo_rectangle(cr, e->x, e->y, e->w, e->h);
126 cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
127 cairo_fill(cr);
128
129 cairo_destroy(cr);
130 cairo_surface_destroy(surface);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100131}
132
133/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100134 * \brief CALLBACK function, Wayland requests the window to resize.
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200135 * \param widget widget to be resized
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100136 * \param width desired width
137 * \param height desired height
138 * \param data user data associated to the window
139 */
140
141static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500142resize_handler(struct widget *widget,
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100143 int32_t width, int32_t height, void *data)
144{
145 struct eventdemo *e = data;
146 if (log_resize)
147 printf("resize width: %d, height: %d\n", width, height);
148
149 /* if a maximum width is set, constrain to it */
150 if (width_max && width_max < width)
151 width = width_max;
152
153 /* if a maximum height is set, constrain to it */
154 if (height_max && height_max < height)
155 height = height_max;
156
157 /* set the new window dimensions */
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500158 widget_set_size(e->widget, width, height);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100159}
160
161/**
162 * \brief CALLBACK function, Wayland informs about keyboard focus change
163 * \param window window
164 * \param device device that caused the focus change
165 * \param data user data associated to the window
166 */
167static void
168keyboard_focus_handler(struct window *window,
169 struct input *device, void *data)
170{
171 int32_t x, y;
172 struct eventdemo *e = data;
173
174 if(log_focus) {
175 if(device) {
176 input_get_position(device, &x, &y);
177 printf("focus x: %d, y: %d\n", x, y);
178 } else {
179 printf("focus lost\n");
180 }
181 }
182
183 window_schedule_redraw(e->window);
184}
185
186/**
187 * \brief CALLBACK function, Wayland informs about key event
188 * \param window window
189 * \param key keycode
190 * \param unicode associated character
191 * \param state pressed or released
192 * \param modifiers modifiers: ctrl, alt, meta etc.
193 * \param data user data associated to the window
194 */
195static void
196key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100197 uint32_t key, uint32_t unicode, enum wl_keyboard_key_state state,
198 void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100199{
200 uint32_t modifiers = input_get_modifiers(input);
201
202 if(!log_key)
203 return;
204
Daniel Stonec9785ea2012-05-30 16:31:52 +0100205 printf("key key: %d, unicode: %d, state: %s, modifiers: 0x%x\n",
206 key, unicode,
207 (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? "pressed" :
208 "released",
209 modifiers);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100210}
211
212/**
213 * \brief CALLBACK function, Wayland informs about button event
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200214 * \param widget widget
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100215 * \param input input device that caused the button event
Martin Olsson3b132e32012-09-29 15:13:56 +0200216 * \param time time the event happened
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100217 * \param button button
218 * \param state pressed or released
219 * \param data user data associated to the window
220 */
221static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500222button_handler(struct widget *widget, struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100223 uint32_t button, enum wl_pointer_button_state state, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100224{
225 int32_t x, y;
226
227 if (!log_button)
228 return;
229
230 input_get_position(input, &x, &y);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100231 printf("button time: %d, button: %d, state: %s, x: %d, y: %d\n",
232 time, button,
233 (state == WL_POINTER_BUTTON_STATE_PRESSED) ? "pressed" :
234 "released",
235 x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100236}
237
238/**
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200239 * \brief CALLBACK function, Wayland informs about axis event
240 * \param widget widget
241 * \param input input device that caused the axis event
242 * \param time time the event happened
243 * \param axis vertical or horizontal
244 * \param value amount of scrolling
245 * \param data user data associated to the widget
246 */
247static void
248axis_handler(struct widget *widget, struct input *input, uint32_t time,
249 uint32_t axis, wl_fixed_t value, void *data)
250{
251 if (!log_axis)
252 return;
253
254 printf("axis time: %d, axis: %s, value: %f\n",
255 time,
256 axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? "vertical" :
257 "horizontal",
258 wl_fixed_to_double(value));
259}
260
261/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100262 * \brief CALLBACK function, Waylands informs about pointer motion
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200263 * \param widget widget
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100264 * \param input input device that caused the motion event
Martin Olsson3b132e32012-09-29 15:13:56 +0200265 * \param time time the event happened
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100266 * \param x absolute x position
267 * \param y absolute y position
268 * \param sx x position relative to the window
269 * \param sy y position relative to the window
270 * \param data user data associated to the window
271 *
272 * Demonstrates the use of different cursors
273 */
274static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500275motion_handler(struct widget *widget, struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400276 float x, float y, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100277{
278 struct eventdemo *e = data;
279
280 if (log_motion) {
Daniel Stoneb230a7e2012-05-08 17:17:54 +0100281 printf("motion time: %d, x: %f, y: %f\n", time, x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100282 }
283
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500284 if (x > e->x && x < e->x + e->w)
285 if (y > e->y && y < e->y + e->h)
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300286 return CURSOR_HAND1;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100287
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300288 return CURSOR_LEFT_PTR;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100289}
290
291/**
292 * \brief Create and initialise a new eventdemo window.
Martin Olssonc663b222012-07-08 11:11:18 +0200293 * The returned eventdemo instance should be destroyed using \c eventdemo_destroy().
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100294 * \param d associated display
295 */
296static struct eventdemo *
297eventdemo_create(struct display *d)
298{
299 struct eventdemo *e;
300
301 e = malloc(sizeof (struct eventdemo));
302 if(e == NULL)
303 return NULL;
304
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500305 e->window = window_create(d);
Philipp Brüschweilera306a262012-08-14 11:02:40 +0200306
307 if (noborder) {
308 /* Demonstrate how to create a borderless window.
309 * Move windows with META + left mouse button.
310 */
311 e->widget = window_add_widget(e->window, e);
312 } else {
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500313 e->widget = window_frame_create(e->window, e);
Philipp Brüschweilera306a262012-08-14 11:02:40 +0200314 window_set_title(e->window, title);
315 }
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100316 e->display = d;
317
318 /* The eventdemo window draws a red rectangle as a demonstration
319 * of per-window data. The dimensions of that rectangle are set
320 * here.
321 */
322 e->x = width * 1.0 / 4.0;
323 e->w = width * 2.0 / 4.0;
324 e->y = height * 1.0 / 4.0;
325 e->h = height * 2.0 / 4.0;
326
327 /* Connect the user data to the window */
328 window_set_user_data(e->window, e);
329
330 /* Set the callback redraw handler for the window */
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500331 widget_set_redraw_handler(e->widget, redraw_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100332
333 /* Set the callback resize handler for the window */
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500334 widget_set_resize_handler(e->widget, resize_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100335
336 /* Set the callback focus handler for the window */
337 window_set_keyboard_focus_handler(e->window,
338 keyboard_focus_handler);
339
340 /* Set the callback key handler for the window */
341 window_set_key_handler(e->window, key_handler);
342
343 /* Set the callback button handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500344 widget_set_button_handler(e->widget, button_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100345
346 /* Set the callback motion handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500347 widget_set_motion_handler(e->widget, motion_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100348
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200349 /* Set the callback axis handler for the window */
350 widget_set_axis_handler(e->widget, axis_handler);
351
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100352 /* Initial drawing of the window */
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500353 window_schedule_resize(e->window, width, height);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100354
355 return e;
356}
357/**
Martin Olssonc663b222012-07-08 11:11:18 +0200358 * \brief Destroy eventdemo instance previously created by \c eventdemo_create().
359 * \param eventdemo eventdemo instance to destroy
360 */
361static void eventdemo_destroy(struct eventdemo * eventdemo)
362{
363 widget_destroy(eventdemo->widget);
364 window_destroy(eventdemo->window);
365 free(eventdemo);
366}
367/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100368 * \brief command line options for eventdemo
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100369 */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400370static const struct weston_option eventdemo_options[] = {
371 { WESTON_OPTION_STRING, "title", 0, &title },
372 { WESTON_OPTION_INTEGER, "width", 'w', &width },
373 { WESTON_OPTION_INTEGER, "height", 'h', &height },
374 { WESTON_OPTION_INTEGER, "max-width", 0, &width_max },
375 { WESTON_OPTION_INTEGER, "max-height", 0, &height_max },
376 { WESTON_OPTION_BOOLEAN, "no-border", 'b', &noborder },
Bill Spitzak4fb84912014-08-08 12:59:51 -0700377 { WESTON_OPTION_BOOLEAN, "log-redraw", 0, &log_redraw },
378 { WESTON_OPTION_BOOLEAN, "log-resize", 0, &log_resize },
379 { WESTON_OPTION_BOOLEAN, "log-focus", 0, &log_focus },
380 { WESTON_OPTION_BOOLEAN, "log-key", 0, &log_key },
381 { WESTON_OPTION_BOOLEAN, "log-button", 0, &log_button },
382 { WESTON_OPTION_BOOLEAN, "log-axis", 0, &log_axis },
383 { WESTON_OPTION_BOOLEAN, "log-motion", 0, &log_motion },
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100384};
385
386/**
387 * \brief Connects to the display, creates the window and hands over
388 * to the main loop.
389 */
390int
391main(int argc, char *argv[])
392{
393 struct display *d;
394 struct eventdemo *e;
395
Bill Spitzak4fb84912014-08-08 12:59:51 -0700396 if (parse_options(eventdemo_options,
397 ARRAY_LENGTH(eventdemo_options), &argc, argv) > 1) {
398 unsigned k;
399 printf("Usage: %s [OPTIONS]\n\n", argv[0]);
400 for (k = 0; k < ARRAY_LENGTH(eventdemo_options); k++) {
401 const struct weston_option* p = &eventdemo_options[k];
402 if (p->name) {
403 printf(" --%s", p->name);
404 if (p->type != WESTON_OPTION_BOOLEAN)
405 printf("=VALUE");
406 putchar('\n');
407 }
408 if (p->short_name) {
409 printf(" -%c", p->short_name);
410 if (p->type != WESTON_OPTION_BOOLEAN)
411 printf("VALUE");
412 putchar('\n');
413 }
414 }
415 return 1;
416 }
417
418 if (!log_redraw && !log_resize && !log_focus && !log_key &&
419 !log_button && !log_axis && !log_motion)
420 log_redraw = log_resize = log_focus = log_key =
421 log_button = log_axis = log_motion = 1;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400422
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100423 /* Connect to the display and have the arguments parsed */
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500424 d = display_create(&argc, argv);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100425 if (d == NULL) {
426 fprintf(stderr, "failed to create display: %m\n");
427 return -1;
428 }
429
430 /* Create new eventdemo window */
431 e = eventdemo_create(d);
432 if (e == NULL) {
433 fprintf(stderr, "failed to create eventdemo: %m\n");
434 return -1;
435 }
436
437 display_run(d);
438
Martin Olssonc663b222012-07-08 11:11:18 +0200439 /* Release resources */
440 eventdemo_destroy(e);
441 display_destroy(d);
442
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100443 return 0;
444}