blob: 54daf774645f89f3f3480e52a699f37fa848b0cc [file] [log] [blame]
Tim Wiederhake503ccca2011-01-21 16:56:07 +01001/*
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 */
42static char *title = "EventDemo";
43
44/** window width */
45static int width = 500;
46
47/** window height */
48static int height = 400;
49
50/** set if window has no borders */
51static int noborder = 0;
52
53/** if non-zero, maximum window width */
54static int width_max = 0;
55
56/** if non-zero, maximum window height */
57static int height_max = 0;
58
59/** set to log redrawing */
60static int log_redraw = 0;
61
62/** set to log resizing */
63static int log_resize = 0;
64
65/** set to log keyboard focus */
66static int log_focus = 0;
67
68/** set to log key events */
69static int log_key = 0;
70
71/** set to log button events */
72static int log_button = 0;
73
74/** set to log motion events */
75static 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 */
84struct 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 */
96static void
97eventdemo_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 */
130static void
131redraw_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
145static void
146resize_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 */
174static void
175keyboard_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 */
202static void
203key_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 */
224static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500225button_handler(struct widget *widget, struct input *input, uint32_t time,
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100226 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 */
251static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500252motion_handler(struct widget *widget, struct input *input, uint32_t time,
253 int32_t x, int32_t y, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100254{
255 struct eventdemo *e = data;
256
257 if (log_motion) {
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500258 printf("motion time: %d, x: %d, y: %d\n", time, x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100259 }
260
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500261 if (x > e->x && x < e->x + e->w)
262 if (y > e->y && y < e->y + e->h)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100263 return POINTER_HAND1;
264
265 return POINTER_LEFT_PTR;
266}
267
268/**
269 * \brief Create and initialise a new eventdemo window.
270 * \param d associated display
271 */
272static struct eventdemo *
273eventdemo_create(struct display *d)
274{
275 struct eventdemo *e;
276
277 e = malloc(sizeof (struct eventdemo));
278 if(e == NULL)
279 return NULL;
280
281 /* Creates a new window with the given title, width and height.
282 * To set the size of the window for a given usable areas width
283 * and height in a window decoration agnostic way use:
284 * window_set_child_size(struct window *window,
285 * int32_t width, int32_t height);
286 */
287 e->window = window_create(d, width, height);
288 window_set_title(e->window, title);
289 e->display = d;
290
291 /* The eventdemo window draws a red rectangle as a demonstration
292 * of per-window data. The dimensions of that rectangle are set
293 * here.
294 */
295 e->x = width * 1.0 / 4.0;
296 e->w = width * 2.0 / 4.0;
297 e->y = height * 1.0 / 4.0;
298 e->h = height * 2.0 / 4.0;
299
300 /* Connect the user data to the window */
301 window_set_user_data(e->window, e);
302
303 /* Set the callback redraw handler for the window */
304 window_set_redraw_handler(e->window, redraw_handler);
305
306 /* Set the callback resize handler for the window */
307 window_set_resize_handler(e->window, resize_handler);
308
309 /* Set the callback focus handler for the window */
310 window_set_keyboard_focus_handler(e->window,
311 keyboard_focus_handler);
312
313 /* Set the callback key handler for the window */
314 window_set_key_handler(e->window, key_handler);
315
316 /* Set the callback button handler for the window */
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500317 widget_set_button_handler(window_get_widget(e->window),
318 button_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100319
320 /* Set the callback motion handler for the window */
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500321 widget_set_motion_handler(window_get_widget(e->window),
322 motion_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100323
324 /* Demonstrate how to create a borderless window.
325 Move windows with META + left mouse button.
326 */
327 if(noborder) {
328 window_set_decoration(e->window, 0);
329 }
330
331 /* Initial drawing of the window */
332 eventdemo_draw(e);
333
334 return e;
335}
336/**
337 * \brief command line options for eventdemo
338 *
339 * see
340 * http://developer.gimp.org/api/2.0/glib/glib-Commandline-option-parser.html
341 */
342static const GOptionEntry option_entries[] = {
343 {"title", 0, 0, G_OPTION_ARG_STRING,
344 &title, "Set the window title to TITLE", "TITLE"},
345 {"width", 'w', 0, G_OPTION_ARG_INT,
346 &width, "Set the window's width to W", "W"},
347 {"height", 'h', 0, G_OPTION_ARG_INT,
348 &height, "Set the window's height to H", "H"},
349 {"maxwidth", 0, 0, G_OPTION_ARG_INT,
350 &width_max, "Set the window's maximum width to W", "W"},
351 {"maxheight", 0, 0, G_OPTION_ARG_INT,
352 &height_max, "Set the window's maximum height to H", "H"},
353 {"noborder", 'b', 0, G_OPTION_ARG_NONE,
354 &noborder, "Don't draw window borders", 0},
355 {"log-redraw", 0, 0, G_OPTION_ARG_NONE,
356 &log_redraw, "Log redraw events to stdout", 0},
357 {"log-resize", 0, 0, G_OPTION_ARG_NONE,
358 &log_resize, "Log resize events to stdout", 0},
359 {"log-focus", 0, 0, G_OPTION_ARG_NONE,
360 &log_focus, "Log keyboard focus events to stdout", 0},
361 {"log-key", 0, 0, G_OPTION_ARG_NONE,
362 &log_key, "Log key events to stdout", 0},
363 {"log-button", 0, 0, G_OPTION_ARG_NONE,
364 &log_button, "Log button events to stdout", 0},
365 {"log-motion", 0, 0, G_OPTION_ARG_NONE,
366 &log_motion, "Log motion events to stdout", 0},
367 {NULL}
368};
369
370/**
371 * \brief Connects to the display, creates the window and hands over
372 * to the main loop.
373 */
374int
375main(int argc, char *argv[])
376{
377 struct display *d;
378 struct eventdemo *e;
379
380 /* Connect to the display and have the arguments parsed */
Kristian Høgsberg9de79a92011-08-24 11:09:53 -0400381 d = display_create(&argc, &argv, option_entries);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100382 if (d == NULL) {
383 fprintf(stderr, "failed to create display: %m\n");
384 return -1;
385 }
386
387 /* Create new eventdemo window */
388 e = eventdemo_create(d);
389 if (e == NULL) {
390 fprintf(stderr, "failed to create eventdemo: %m\n");
391 return -1;
392 }
393
394 display_run(d);
395
396 return 0;
397}