document the usage of libtoytoolkit
[profile/ivi/wayland.git] / clients / eventdemo.c
1 /*
2  * Copyright © 2011 Tim Wiederhake
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22
23 /**
24  * \file eventdemo.c
25  * \brief Demonstrate the use of Wayland's toytoolkit.
26  *
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
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include <cairo.h>
37 #include <glib.h>
38
39 #include "window.h"
40
41 /** window title */
42 static char *title = "EventDemo";
43
44 /** window width */
45 static int width = 500;
46
47 /** window height */
48 static int height = 400;
49
50 /** set if window has no borders */
51 static int noborder = 0;
52
53 /** if non-zero, maximum window width */
54 static int width_max = 0;
55
56 /** if non-zero, maximum window height */
57 static int height_max = 0;
58
59 /** set to log redrawing */
60 static int log_redraw = 0;
61
62 /** set to log resizing */
63 static int log_resize = 0;
64
65 /** set to log keyboard focus */
66 static int log_focus = 0;
67
68 /** set to log key events */
69 static int log_key = 0;
70
71 /** set to log button events */
72 static int log_button = 0;
73
74 /** set to log motion events */
75 static int log_motion = 0;
76
77 /**
78  * \struct eventdemo
79  * \brief Holds all data the program needs per window
80  *
81  * In this demo the struct holds the position of a
82  * red rectangle that is drawn in the window's area.
83  */
84 struct eventdemo {
85         struct window *window;
86         struct display *display;
87
88         unsigned int x, y, w, h;
89 };
90
91 /**
92  * \brief Redraws the window
93  *
94  * Draws a red rectangle as demonstration of per-window data.
95  */
96 static void
97 eventdemo_draw(struct eventdemo *e) {
98         if (log_redraw)
99                 printf("redraw\n");
100
101         cairo_surface_t *surface;
102         cairo_t *cr;
103         struct rectangle rect;
104
105         window_draw(e->window);
106         window_get_child_allocation(e->window, &rect);
107         surface = window_get_surface(e->window);
108
109         cr = cairo_create(surface);
110         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
111
112         cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
113         cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
114         cairo_fill(cr);
115
116         cairo_rectangle(cr, e->x, e->y, e->w, e->h);
117         cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
118         cairo_fill(cr);
119
120         cairo_destroy(cr);
121         cairo_surface_destroy(surface);
122         window_flush(e->window);
123 }
124
125 /**
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
129  */
130 static void
131 redraw_handler(struct window *window, void *data)
132 {
133         struct eventdemo *e = data;
134         eventdemo_draw(e);
135 }
136
137 /**
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
143  */
144
145 static void
146 resize_handler(struct window *window,
147                int32_t width, int32_t height, void *data)
148 {
149         struct eventdemo *e = data;
150         if (log_resize)
151                 printf("resize width: %d, height: %d\n", width, height);
152
153         /* if a maximum width is set, constrain to it */
154         if (width_max && width_max < width)
155                 width = width_max;
156
157         /* if a maximum height is set, constrain to it */
158         if (height_max && height_max < height)
159                 height = height_max;
160
161         /* set the new window dimensions */
162         window_set_child_size(e->window, width, height);
163
164         /* inform Wayland that the window needs to be redrawn */
165         window_schedule_redraw(e->window);
166 }
167
168 /**
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
173  */
174 static void
175 keyboard_focus_handler(struct window *window,
176                        struct input *device, void *data)
177 {
178         int32_t x, y;
179         struct eventdemo *e = data;
180
181         if(log_focus) {
182                 if(device) {
183                         input_get_position(device, &x, &y);
184                         printf("focus x: %d, y: %d\n", x, y);
185                 } else {
186                         printf("focus lost\n");
187                 }
188         }
189
190         window_schedule_redraw(e->window);
191 }
192
193 /**
194  * \brief CALLBACK function, Wayland informs about key event
195  * \param window window
196  * \param key keycode
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
201  */
202 static void
203 key_handler(struct window *window, struct input *input, uint32_t time,
204             uint32_t key, uint32_t unicode, uint32_t state, void *data)
205 {
206         uint32_t modifiers = input_get_modifiers(input);
207
208         if(!log_key)
209                 return;
210
211         printf("key key: %d, unicode: %d, state: %d, modifiers: %d\n",
212                key, unicode, state, modifiers);
213 }
214
215 /**
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
223  */
224 static void
225 button_handler(struct window *window, struct input *input, uint32_t time,
226                int button, int state, void *data)
227 {
228         int32_t x, y;
229
230         if (!log_button)
231                 return;
232
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);
236 }
237
238 /**
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
248  *
249  * Demonstrates the use of different cursors
250  */
251 static int
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)
254 {
255         struct eventdemo *e = data;
256
257         if (log_motion) {
258                 printf("motion time: %d, x: %d, y: %d, sx: %d, sy: %d\n",
259                        time, x, y, sx, sy);
260         }
261
262         if(sx > e->x && sx < e->x + e->w)
263                 if(sy > e->y && sy < e->y + e->h)
264                         return POINTER_HAND1;
265
266         return POINTER_LEFT_PTR;
267 }
268
269 /**
270  * \brief Create and initialise a new eventdemo window.
271  * \param d associated display
272  */
273 static struct eventdemo *
274 eventdemo_create(struct display *d)
275 {
276         struct eventdemo *e;
277
278         e = malloc(sizeof (struct eventdemo));
279         if(e == NULL)
280                 return NULL;
281
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);
287          */
288         e->window = window_create(d, width, height);
289         window_set_title(e->window, title);
290         e->display = d;
291
292         /* The eventdemo window draws a red rectangle as a demonstration
293          * of per-window data. The dimensions of that rectangle are set
294          * here.
295          */
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;
300
301         /* Connect the user data to the window */
302         window_set_user_data(e->window, e);
303
304         /* Set the callback redraw handler for the window */
305         window_set_redraw_handler(e->window, redraw_handler);
306
307         /* Set the callback resize handler for the window */
308         window_set_resize_handler(e->window, resize_handler);
309
310         /* Set the callback focus handler for the window */
311         window_set_keyboard_focus_handler(e->window,
312                                           keyboard_focus_handler);
313
314         /* Set the callback key handler for the window */
315         window_set_key_handler(e->window, key_handler);
316
317         /* Set the callback button handler for the window */
318         window_set_button_handler(e->window, button_handler);
319
320         /* Set the callback motion handler for the window */
321         window_set_motion_handler(e->window, motion_handler);
322
323         /* Demonstrate how to create a borderless window.
324            Move windows with META + left mouse button.
325          */
326         if(noborder) {
327                 window_set_decoration(e->window, 0);
328         }
329
330         /* Initial drawing of the window */
331         eventdemo_draw(e);
332
333         return e;
334 }
335 /**
336  * \brief command line options for eventdemo
337  *
338  * see
339  * http://developer.gimp.org/api/2.0/glib/glib-Commandline-option-parser.html
340  */
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},
366         {NULL}
367 };
368
369 /**
370  * \brief Connects to the display, creates the window and hands over
371  * to the main loop.
372  */
373 int
374 main(int argc, char *argv[])
375 {
376         struct display *d;
377         struct eventdemo *e;
378
379         /* Connect to the display and have the arguments parsed */
380         d = display_create(&argc, &argv, option_entries);
381         if (d == NULL) {
382                 fprintf(stderr, "failed to create display: %m\n");
383                 return -1;
384         }
385
386         /* Create new eventdemo window */
387         e = eventdemo_create(d);
388         if (e == NULL) {
389                 fprintf(stderr, "failed to create eventdemo: %m\n");
390                 return -1;
391         }
392
393         display_run(d);
394
395         return 0;
396 }