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
38 #include <wayland-cursor.h>
42 static char *title = "EventDemo";
45 static int width = 500;
48 static int height = 400;
50 /** set if window has no borders */
51 static int noborder = 0;
53 /** if non-zero, maximum window width */
54 static int width_max = 0;
56 /** if non-zero, maximum window height */
57 static int height_max = 0;
59 /** set to log redrawing */
60 static int log_redraw = 0;
62 /** set to log resizing */
63 static int log_resize = 0;
65 /** set to log keyboard focus */
66 static int log_focus = 0;
68 /** set to log key events */
69 static int log_key = 0;
71 /** set to log button events */
72 static int log_button = 0;
74 /** set to log motion events */
75 static int log_motion = 0;
79 * \brief Holds all data the program needs per window
81 * In this demo the struct holds the position of a
82 * red rectangle that is drawn in the window's area.
85 struct window *window;
86 struct widget *widget;
87 struct display *display;
93 * \brief CALLBACK function, Wayland requests the window to redraw.
94 * \param window window to be redrawn
95 * \param data user data associated to the window
97 * Draws a red rectangle as demonstration of per-window data.
100 redraw_handler(struct widget *widget, void *data)
102 struct eventdemo *e = data;
103 cairo_surface_t *surface;
105 struct rectangle rect;
110 widget_get_allocation(e->widget, &rect);
111 surface = window_get_surface(e->window);
113 cr = cairo_create(surface);
114 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
116 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
117 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
120 cairo_rectangle(cr, e->x, e->y, e->w, e->h);
121 cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
125 cairo_surface_destroy(surface);
129 * \brief CALLBACK function, Wayland requests the window to resize.
130 * \param window window to be resized
131 * \param width desired width
132 * \param height desired height
133 * \param data user data associated to the window
137 resize_handler(struct widget *widget,
138 int32_t width, int32_t height, void *data)
140 struct eventdemo *e = data;
142 printf("resize width: %d, height: %d\n", width, height);
144 /* if a maximum width is set, constrain to it */
145 if (width_max && width_max < width)
148 /* if a maximum height is set, constrain to it */
149 if (height_max && height_max < height)
152 /* set the new window dimensions */
153 widget_set_size(e->widget, width, height);
157 * \brief CALLBACK function, Wayland informs about keyboard focus change
158 * \param window window
159 * \param device device that caused the focus change
160 * \param data user data associated to the window
163 keyboard_focus_handler(struct window *window,
164 struct input *device, void *data)
167 struct eventdemo *e = data;
171 input_get_position(device, &x, &y);
172 printf("focus x: %d, y: %d\n", x, y);
174 printf("focus lost\n");
178 window_schedule_redraw(e->window);
182 * \brief CALLBACK function, Wayland informs about key event
183 * \param window window
185 * \param unicode associated character
186 * \param state pressed or released
187 * \param modifiers modifiers: ctrl, alt, meta etc.
188 * \param data user data associated to the window
191 key_handler(struct window *window, struct input *input, uint32_t time,
192 uint32_t key, uint32_t unicode, uint32_t state, void *data)
194 uint32_t modifiers = input_get_modifiers(input);
199 printf("key key: %d, unicode: %d, state: %d, modifiers: %d\n",
200 key, unicode, state, modifiers);
204 * \brief CALLBACK function, Wayland informs about button event
205 * \param window window
206 * \param input input device that caused the button event
207 * \param time time the event happend
208 * \param button button
209 * \param state pressed or released
210 * \param data user data associated to the window
213 button_handler(struct widget *widget, struct input *input, uint32_t time,
214 uint32_t button, uint32_t state, void *data)
221 input_get_position(input, &x, &y);
222 printf("button time: %d, button: %d, state: %d, x: %d, y: %d\n",
223 time, button, state, x, y);
227 * \brief CALLBACK function, Waylands informs about pointer motion
228 * \param window window
229 * \param input input device that caused the motion event
230 * \param time time the event happend
231 * \param x absolute x position
232 * \param y absolute y position
233 * \param sx x position relative to the window
234 * \param sy y position relative to the window
235 * \param data user data associated to the window
237 * Demonstrates the use of different cursors
240 motion_handler(struct widget *widget, struct input *input, uint32_t time,
241 float x, float y, void *data)
243 struct eventdemo *e = data;
246 printf("motion time: %d, x: %f, y: %f\n", time, x, y);
249 if (x > e->x && x < e->x + e->w)
250 if (y > e->y && y < e->y + e->h)
251 return WL_CURSOR_HAND1;
253 return WL_CURSOR_LEFT_PTR;
257 * \brief Create and initialise a new eventdemo window.
258 * \param d associated display
260 static struct eventdemo *
261 eventdemo_create(struct display *d)
265 e = malloc(sizeof (struct eventdemo));
269 e->window = window_create(d);
270 e->widget = frame_create(e->window, e);
271 window_set_title(e->window, title);
274 /* The eventdemo window draws a red rectangle as a demonstration
275 * of per-window data. The dimensions of that rectangle are set
278 e->x = width * 1.0 / 4.0;
279 e->w = width * 2.0 / 4.0;
280 e->y = height * 1.0 / 4.0;
281 e->h = height * 2.0 / 4.0;
283 /* Connect the user data to the window */
284 window_set_user_data(e->window, e);
286 /* Set the callback redraw handler for the window */
287 widget_set_redraw_handler(e->widget, redraw_handler);
289 /* Set the callback resize handler for the window */
290 widget_set_resize_handler(e->widget, resize_handler);
292 /* Set the callback focus handler for the window */
293 window_set_keyboard_focus_handler(e->window,
294 keyboard_focus_handler);
296 /* Set the callback key handler for the window */
297 window_set_key_handler(e->window, key_handler);
299 /* Set the callback button handler for the window */
300 widget_set_button_handler(e->widget, button_handler);
302 /* Set the callback motion handler for the window */
303 widget_set_motion_handler(e->widget, motion_handler);
305 /* Demonstrate how to create a borderless window.
306 Move windows with META + left mouse button.
311 /* Initial drawing of the window */
312 window_schedule_resize(e->window, width, height);
317 * \brief command line options for eventdemo
319 static const struct weston_option eventdemo_options[] = {
320 { WESTON_OPTION_STRING, "title", 0, &title },
321 { WESTON_OPTION_INTEGER, "width", 'w', &width },
322 { WESTON_OPTION_INTEGER, "height", 'h', &height },
323 { WESTON_OPTION_INTEGER, "max-width", 0, &width_max },
324 { WESTON_OPTION_INTEGER, "max-height", 0, &height_max },
325 { WESTON_OPTION_BOOLEAN, "no-border", 'b', &noborder },
326 { WESTON_OPTION_BOOLEAN, "log-redraw", '0', &log_redraw },
327 { WESTON_OPTION_BOOLEAN, "log-resize", '0', &log_resize },
328 { WESTON_OPTION_BOOLEAN, "log-focus", '0', &log_focus },
329 { WESTON_OPTION_BOOLEAN, "log-key", '0', &log_key },
330 { WESTON_OPTION_BOOLEAN, "log-button", '0', &log_button },
331 { WESTON_OPTION_BOOLEAN, "log-motion", '0', &log_motion },
335 * \brief Connects to the display, creates the window and hands over
339 main(int argc, char *argv[])
344 argc = parse_options(eventdemo_options,
345 ARRAY_LENGTH(eventdemo_options), argc, argv);
347 /* Connect to the display and have the arguments parsed */
348 d = display_create(argc, argv);
350 fprintf(stderr, "failed to create display: %m\n");
354 /* Create new eventdemo window */
355 e = eventdemo_create(d);
357 fprintf(stderr, "failed to create eventdemo: %m\n");