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
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 display *display;
88 unsigned int x, y, w, h;
92 * \brief Redraws the window
94 * Draws a red rectangle as demonstration of per-window data.
97 eventdemo_draw(struct eventdemo *e) {
101 cairo_surface_t *surface;
103 struct rectangle rect;
105 window_draw(e->window);
106 window_get_child_allocation(e->window, &rect);
107 surface = window_get_surface(e->window);
109 cr = cairo_create(surface);
110 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
112 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
113 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
116 cairo_rectangle(cr, e->x, e->y, e->w, e->h);
117 cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
121 cairo_surface_destroy(surface);
122 window_flush(e->window);
126 * \brief CALLBACK function, Wayland requests the window to redraw.
127 * \param window window to be redrawn
128 * \param data user data associated to the window
131 redraw_handler(struct window *window, void *data)
133 struct eventdemo *e = data;
138 * \brief CALLBACK function, Wayland requests the window to resize.
139 * \param window window to be resized
140 * \param width desired width
141 * \param height desired height
142 * \param data user data associated to the window
146 resize_handler(struct window *window,
147 int32_t width, int32_t height, void *data)
149 struct eventdemo *e = data;
151 printf("resize width: %d, height: %d\n", width, height);
153 /* if a maximum width is set, constrain to it */
154 if (width_max && width_max < width)
157 /* if a maximum height is set, constrain to it */
158 if (height_max && height_max < height)
161 /* set the new window dimensions */
162 window_set_child_size(e->window, width, height);
164 /* inform Wayland that the window needs to be redrawn */
165 window_schedule_redraw(e->window);
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
175 keyboard_focus_handler(struct window *window,
176 struct input *device, void *data)
179 struct eventdemo *e = data;
183 input_get_position(device, &x, &y);
184 printf("focus x: %d, y: %d\n", x, y);
186 printf("focus lost\n");
190 window_schedule_redraw(e->window);
194 * \brief CALLBACK function, Wayland informs about key event
195 * \param window window
197 * \param unicode associated character
198 * \param state pressed or released
199 * \param modifiers modifiers: ctrl, alt, meta etc.
200 * \param data user data associated to the window
203 key_handler(struct window *window, struct input *input, uint32_t time,
204 uint32_t key, uint32_t unicode, uint32_t state, void *data)
206 uint32_t modifiers = input_get_modifiers(input);
211 printf("key key: %d, unicode: %d, state: %d, modifiers: %d\n",
212 key, unicode, state, modifiers);
216 * \brief CALLBACK function, Wayland informs about button event
217 * \param window window
218 * \param input input device that caused the button event
219 * \param time time the event happend
220 * \param button button
221 * \param state pressed or released
222 * \param data user data associated to the window
225 button_handler(struct window *window, struct input *input, uint32_t time,
226 int button, int state, void *data)
233 input_get_position(input, &x, &y);
234 printf("button time: %d, button: %d, state: %d, x: %d, y: %d\n",
235 time, button, state, x, y);
239 * \brief CALLBACK function, Waylands informs about pointer motion
240 * \param window window
241 * \param input input device that caused the motion event
242 * \param time time the event happend
243 * \param x absolute x position
244 * \param y absolute y position
245 * \param sx x position relative to the window
246 * \param sy y position relative to the window
247 * \param data user data associated to the window
249 * Demonstrates the use of different cursors
252 motion_handler(struct window *window, struct input *input, uint32_t time,
253 int32_t x, int32_t y, int32_t sx, int32_t sy, void *data)
255 struct eventdemo *e = data;
258 printf("motion time: %d, x: %d, y: %d, sx: %d, sy: %d\n",
262 if(sx > e->x && sx < e->x + e->w)
263 if(sy > e->y && sy < e->y + e->h)
264 return POINTER_HAND1;
266 return POINTER_LEFT_PTR;
270 * \brief Create and initialise a new eventdemo window.
271 * \param d associated display
273 static struct eventdemo *
274 eventdemo_create(struct display *d)
278 e = malloc(sizeof (struct eventdemo));
282 /* Creates a new window with the given title, width and height.
283 * To set the size of the window for a given usable areas width
284 * and height in a window decoration agnostic way use:
285 * window_set_child_size(struct window *window,
286 * int32_t width, int32_t height);
288 e->window = window_create(d, width, height);
289 window_set_title(e->window, title);
292 /* The eventdemo window draws a red rectangle as a demonstration
293 * of per-window data. The dimensions of that rectangle are set
296 e->x = width * 1.0 / 4.0;
297 e->w = width * 2.0 / 4.0;
298 e->y = height * 1.0 / 4.0;
299 e->h = height * 2.0 / 4.0;
301 /* Connect the user data to the window */
302 window_set_user_data(e->window, e);
304 /* Set the callback redraw handler for the window */
305 window_set_redraw_handler(e->window, redraw_handler);
307 /* Set the callback resize handler for the window */
308 window_set_resize_handler(e->window, resize_handler);
310 /* Set the callback focus handler for the window */
311 window_set_keyboard_focus_handler(e->window,
312 keyboard_focus_handler);
314 /* Set the callback key handler for the window */
315 window_set_key_handler(e->window, key_handler);
317 /* Set the callback button handler for the window */
318 window_set_button_handler(e->window, button_handler);
320 /* Set the callback motion handler for the window */
321 window_set_motion_handler(e->window, motion_handler);
323 /* Demonstrate how to create a borderless window.
324 Move windows with META + left mouse button.
327 window_set_decoration(e->window, 0);
330 /* Initial drawing of the window */
336 * \brief command line options for eventdemo
339 * http://developer.gimp.org/api/2.0/glib/glib-Commandline-option-parser.html
341 static const GOptionEntry option_entries[] = {
342 {"title", 0, 0, G_OPTION_ARG_STRING,
343 &title, "Set the window title to TITLE", "TITLE"},
344 {"width", 'w', 0, G_OPTION_ARG_INT,
345 &width, "Set the window's width to W", "W"},
346 {"height", 'h', 0, G_OPTION_ARG_INT,
347 &height, "Set the window's height to H", "H"},
348 {"maxwidth", 0, 0, G_OPTION_ARG_INT,
349 &width_max, "Set the window's maximum width to W", "W"},
350 {"maxheight", 0, 0, G_OPTION_ARG_INT,
351 &height_max, "Set the window's maximum height to H", "H"},
352 {"noborder", 'b', 0, G_OPTION_ARG_NONE,
353 &noborder, "Don't draw window borders", 0},
354 {"log-redraw", 0, 0, G_OPTION_ARG_NONE,
355 &log_redraw, "Log redraw events to stdout", 0},
356 {"log-resize", 0, 0, G_OPTION_ARG_NONE,
357 &log_resize, "Log resize events to stdout", 0},
358 {"log-focus", 0, 0, G_OPTION_ARG_NONE,
359 &log_focus, "Log keyboard focus events to stdout", 0},
360 {"log-key", 0, 0, G_OPTION_ARG_NONE,
361 &log_key, "Log key events to stdout", 0},
362 {"log-button", 0, 0, G_OPTION_ARG_NONE,
363 &log_button, "Log button events to stdout", 0},
364 {"log-motion", 0, 0, G_OPTION_ARG_NONE,
365 &log_motion, "Log motion events to stdout", 0},
370 * \brief Connects to the display, creates the window and hands over
374 main(int argc, char *argv[])
379 /* Connect to the display and have the arguments parsed */
380 d = display_create(&argc, &argv, option_entries);
382 fprintf(stderr, "failed to create display: %m\n");
386 /* Create new eventdemo window */
387 e = eventdemo_create(d);
389 fprintf(stderr, "failed to create eventdemo: %m\n");