2 * Copyright © 2011 Tim Wiederhake
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 * \brief Demonstrate the use of Wayland's toytoolkit.
27 * Heavily commented demo program that can report all events that are
28 * dispatched to the window. For other functionality, eg. opengl/egl,
29 * drag and drop, etc. have a look at the other demos.
30 * \author Tim Wiederhake
43 static char *title = "EventDemo";
46 static int width = 500;
49 static int height = 400;
51 /** set if window has no borders */
52 static int noborder = 0;
54 /** if non-zero, maximum window width */
55 static int width_max = 0;
57 /** if non-zero, maximum window height */
58 static int height_max = 0;
60 /** set to log redrawing */
61 static int log_redraw = 0;
63 /** set to log resizing */
64 static int log_resize = 0;
66 /** set to log keyboard focus */
67 static int log_focus = 0;
69 /** set to log key events */
70 static int log_key = 0;
72 /** set to log button events */
73 static int log_button = 0;
75 /** set to log axis events */
76 static int log_axis = 0;
78 /** set to log motion events */
79 static int log_motion = 0;
83 * \brief Holds all data the program needs per window
85 * In this demo the struct holds the position of a
86 * red rectangle that is drawn in the window's area.
89 struct window *window;
90 struct widget *widget;
91 struct display *display;
97 * \brief CALLBACK function, Wayland requests the window to redraw.
98 * \param widget widget to be redrawn
99 * \param data user data associated to the window
101 * Draws a red rectangle as demonstration of per-window data.
104 redraw_handler(struct widget *widget, void *data)
106 struct eventdemo *e = data;
107 cairo_surface_t *surface;
109 struct rectangle rect;
114 widget_get_allocation(e->widget, &rect);
115 surface = window_get_surface(e->window);
117 cr = cairo_create(surface);
118 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
120 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
121 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
124 cairo_rectangle(cr, e->x, e->y, e->w, e->h);
125 cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
129 cairo_surface_destroy(surface);
133 * \brief CALLBACK function, Wayland requests the window to resize.
134 * \param widget widget to be resized
135 * \param width desired width
136 * \param height desired height
137 * \param data user data associated to the window
141 resize_handler(struct widget *widget,
142 int32_t width, int32_t height, void *data)
144 struct eventdemo *e = data;
146 printf("resize width: %d, height: %d\n", width, height);
148 /* if a maximum width is set, constrain to it */
149 if (width_max && width_max < width)
152 /* if a maximum height is set, constrain to it */
153 if (height_max && height_max < height)
156 /* set the new window dimensions */
157 widget_set_size(e->widget, width, height);
161 * \brief CALLBACK function, Wayland informs about keyboard focus change
162 * \param window window
163 * \param device device that caused the focus change
164 * \param data user data associated to the window
167 keyboard_focus_handler(struct window *window,
168 struct input *device, void *data)
171 struct eventdemo *e = data;
175 input_get_position(device, &x, &y);
176 printf("focus x: %d, y: %d\n", x, y);
178 printf("focus lost\n");
182 window_schedule_redraw(e->window);
186 * \brief CALLBACK function, Wayland informs about key event
187 * \param window window
189 * \param unicode associated character
190 * \param state pressed or released
191 * \param modifiers modifiers: ctrl, alt, meta etc.
192 * \param data user data associated to the window
195 key_handler(struct window *window, struct input *input, uint32_t time,
196 uint32_t key, uint32_t unicode, enum wl_keyboard_key_state state,
199 uint32_t modifiers = input_get_modifiers(input);
204 printf("key key: %d, unicode: %d, state: %s, modifiers: 0x%x\n",
206 (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? "pressed" :
212 * \brief CALLBACK function, Wayland informs about button event
213 * \param widget widget
214 * \param input input device that caused the button event
215 * \param time time the event happened
216 * \param button button
217 * \param state pressed or released
218 * \param data user data associated to the window
221 button_handler(struct widget *widget, struct input *input, uint32_t time,
222 uint32_t button, enum wl_pointer_button_state state, void *data)
229 input_get_position(input, &x, &y);
230 printf("button time: %d, button: %d, state: %s, x: %d, y: %d\n",
232 (state == WL_POINTER_BUTTON_STATE_PRESSED) ? "pressed" :
238 * \brief CALLBACK function, Wayland informs about axis event
239 * \param widget widget
240 * \param input input device that caused the axis event
241 * \param time time the event happened
242 * \param axis vertical or horizontal
243 * \param value amount of scrolling
244 * \param data user data associated to the widget
247 axis_handler(struct widget *widget, struct input *input, uint32_t time,
248 uint32_t axis, wl_fixed_t value, void *data)
253 printf("axis time: %d, axis: %s, value: %f\n",
255 axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? "vertical" :
257 wl_fixed_to_double(value));
261 * \brief CALLBACK function, Waylands informs about pointer motion
262 * \param widget widget
263 * \param input input device that caused the motion event
264 * \param time time the event happened
265 * \param x absolute x position
266 * \param y absolute y position
267 * \param sx x position relative to the window
268 * \param sy y position relative to the window
269 * \param data user data associated to the window
271 * Demonstrates the use of different cursors
274 motion_handler(struct widget *widget, struct input *input, uint32_t time,
275 float x, float y, void *data)
277 struct eventdemo *e = data;
280 printf("motion time: %d, x: %f, y: %f\n", time, x, y);
283 if (x > e->x && x < e->x + e->w)
284 if (y > e->y && y < e->y + e->h)
287 return CURSOR_LEFT_PTR;
291 * \brief Create and initialise a new eventdemo window.
292 * The returned eventdemo instance should be destroyed using \c eventdemo_destroy().
293 * \param d associated display
295 static struct eventdemo *
296 eventdemo_create(struct display *d)
300 e = malloc(sizeof (struct eventdemo));
304 e->window = window_create(d);
307 /* Demonstrate how to create a borderless window.
308 * Move windows with META + left mouse button.
310 e->widget = window_add_widget(e->window, e);
312 e->widget = window_frame_create(e->window, e);
313 window_set_title(e->window, title);
317 /* The eventdemo window draws a red rectangle as a demonstration
318 * of per-window data. The dimensions of that rectangle are set
321 e->x = width * 1.0 / 4.0;
322 e->w = width * 2.0 / 4.0;
323 e->y = height * 1.0 / 4.0;
324 e->h = height * 2.0 / 4.0;
326 /* Connect the user data to the window */
327 window_set_user_data(e->window, e);
329 /* Set the callback redraw handler for the window */
330 widget_set_redraw_handler(e->widget, redraw_handler);
332 /* Set the callback resize handler for the window */
333 widget_set_resize_handler(e->widget, resize_handler);
335 /* Set the callback focus handler for the window */
336 window_set_keyboard_focus_handler(e->window,
337 keyboard_focus_handler);
339 /* Set the callback key handler for the window */
340 window_set_key_handler(e->window, key_handler);
342 /* Set the callback button handler for the window */
343 widget_set_button_handler(e->widget, button_handler);
345 /* Set the callback motion handler for the window */
346 widget_set_motion_handler(e->widget, motion_handler);
348 /* Set the callback axis handler for the window */
349 widget_set_axis_handler(e->widget, axis_handler);
351 /* Initial drawing of the window */
352 window_schedule_resize(e->window, width, height);
357 * \brief Destroy eventdemo instance previously created by \c eventdemo_create().
358 * \param eventdemo eventdemo instance to destroy
360 static void eventdemo_destroy(struct eventdemo * eventdemo)
362 widget_destroy(eventdemo->widget);
363 window_destroy(eventdemo->window);
367 * \brief command line options for eventdemo
369 static const struct weston_option eventdemo_options[] = {
370 { WESTON_OPTION_STRING, "title", 0, &title },
371 { WESTON_OPTION_INTEGER, "width", 'w', &width },
372 { WESTON_OPTION_INTEGER, "height", 'h', &height },
373 { WESTON_OPTION_INTEGER, "max-width", 0, &width_max },
374 { WESTON_OPTION_INTEGER, "max-height", 0, &height_max },
375 { WESTON_OPTION_BOOLEAN, "no-border", 'b', &noborder },
376 { WESTON_OPTION_BOOLEAN, "log-redraw", 0, &log_redraw },
377 { WESTON_OPTION_BOOLEAN, "log-resize", 0, &log_resize },
378 { WESTON_OPTION_BOOLEAN, "log-focus", 0, &log_focus },
379 { WESTON_OPTION_BOOLEAN, "log-key", 0, &log_key },
380 { WESTON_OPTION_BOOLEAN, "log-button", 0, &log_button },
381 { WESTON_OPTION_BOOLEAN, "log-axis", 0, &log_axis },
382 { WESTON_OPTION_BOOLEAN, "log-motion", 0, &log_motion },
386 * \brief Connects to the display, creates the window and hands over
390 main(int argc, char *argv[])
395 if (parse_options(eventdemo_options,
396 ARRAY_LENGTH(eventdemo_options), &argc, argv) > 1) {
398 printf("Usage: %s [OPTIONS]\n\n", argv[0]);
399 for (k = 0; k < ARRAY_LENGTH(eventdemo_options); k++) {
400 const struct weston_option* p = &eventdemo_options[k];
402 printf(" --%s", p->name);
403 if (p->type != WESTON_OPTION_BOOLEAN)
408 printf(" -%c", p->short_name);
409 if (p->type != WESTON_OPTION_BOOLEAN)
417 if (!log_redraw && !log_resize && !log_focus && !log_key &&
418 !log_button && !log_axis && !log_motion)
419 log_redraw = log_resize = log_focus = log_key =
420 log_button = log_axis = log_motion = 1;
422 /* Connect to the display and have the arguments parsed */
423 d = display_create(&argc, argv);
425 fprintf(stderr, "failed to create display: %m\n");
429 /* Create new eventdemo window */
430 e = eventdemo_create(d);
432 fprintf(stderr, "failed to create eventdemo: %m\n");
438 /* Release resources */
439 eventdemo_destroy(e);