blob: 928cfdde1e5dcbf9979cd2e7093d9131eef4b3d6 [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;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050086 struct widget *widget;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010087 struct display *display;
88
89 unsigned int x, y, w, h;
90};
91
92/**
93 * \brief Redraws the window
94 *
95 * Draws a red rectangle as demonstration of per-window data.
96 */
97static void
98eventdemo_draw(struct eventdemo *e) {
99 if (log_redraw)
100 printf("redraw\n");
101
102 cairo_surface_t *surface;
103 cairo_t *cr;
104 struct rectangle rect;
105
106 window_draw(e->window);
107 window_get_child_allocation(e->window, &rect);
108 surface = window_get_surface(e->window);
109
110 cr = cairo_create(surface);
111 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
112
113 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
114 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
115 cairo_fill(cr);
116
117 cairo_rectangle(cr, e->x, e->y, e->w, e->h);
118 cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
119 cairo_fill(cr);
120
121 cairo_destroy(cr);
122 cairo_surface_destroy(surface);
123 window_flush(e->window);
124}
125
126/**
127 * \brief CALLBACK function, Wayland requests the window to redraw.
128 * \param window window to be redrawn
129 * \param data user data associated to the window
130 */
131static void
132redraw_handler(struct window *window, void *data)
133{
134 struct eventdemo *e = data;
135 eventdemo_draw(e);
136}
137
138/**
139 * \brief CALLBACK function, Wayland requests the window to resize.
140 * \param window window to be resized
141 * \param width desired width
142 * \param height desired height
143 * \param data user data associated to the window
144 */
145
146static void
147resize_handler(struct window *window,
148 int32_t width, int32_t height, void *data)
149{
150 struct eventdemo *e = data;
151 if (log_resize)
152 printf("resize width: %d, height: %d\n", width, height);
153
154 /* if a maximum width is set, constrain to it */
155 if (width_max && width_max < width)
156 width = width_max;
157
158 /* if a maximum height is set, constrain to it */
159 if (height_max && height_max < height)
160 height = height_max;
161
162 /* set the new window dimensions */
163 window_set_child_size(e->window, width, height);
164
165 /* inform Wayland that the window needs to be redrawn */
166 window_schedule_redraw(e->window);
167}
168
169/**
170 * \brief CALLBACK function, Wayland informs about keyboard focus change
171 * \param window window
172 * \param device device that caused the focus change
173 * \param data user data associated to the window
174 */
175static void
176keyboard_focus_handler(struct window *window,
177 struct input *device, void *data)
178{
179 int32_t x, y;
180 struct eventdemo *e = data;
181
182 if(log_focus) {
183 if(device) {
184 input_get_position(device, &x, &y);
185 printf("focus x: %d, y: %d\n", x, y);
186 } else {
187 printf("focus lost\n");
188 }
189 }
190
191 window_schedule_redraw(e->window);
192}
193
194/**
195 * \brief CALLBACK function, Wayland informs about key event
196 * \param window window
197 * \param key keycode
198 * \param unicode associated character
199 * \param state pressed or released
200 * \param modifiers modifiers: ctrl, alt, meta etc.
201 * \param data user data associated to the window
202 */
203static void
204key_handler(struct window *window, struct input *input, uint32_t time,
205 uint32_t key, uint32_t unicode, uint32_t state, void *data)
206{
207 uint32_t modifiers = input_get_modifiers(input);
208
209 if(!log_key)
210 return;
211
212 printf("key key: %d, unicode: %d, state: %d, modifiers: %d\n",
213 key, unicode, state, modifiers);
214}
215
216/**
217 * \brief CALLBACK function, Wayland informs about button event
218 * \param window window
219 * \param input input device that caused the button event
220 * \param time time the event happend
221 * \param button button
222 * \param state pressed or released
223 * \param data user data associated to the window
224 */
225static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500226button_handler(struct widget *widget, struct input *input, uint32_t time,
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100227 int button, int state, void *data)
228{
229 int32_t x, y;
230
231 if (!log_button)
232 return;
233
234 input_get_position(input, &x, &y);
235 printf("button time: %d, button: %d, state: %d, x: %d, y: %d\n",
236 time, button, state, x, y);
237}
238
239/**
240 * \brief CALLBACK function, Waylands informs about pointer motion
241 * \param window window
242 * \param input input device that caused the motion event
243 * \param time time the event happend
244 * \param x absolute x position
245 * \param y absolute y position
246 * \param sx x position relative to the window
247 * \param sy y position relative to the window
248 * \param data user data associated to the window
249 *
250 * Demonstrates the use of different cursors
251 */
252static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500253motion_handler(struct widget *widget, struct input *input, uint32_t time,
254 int32_t x, int32_t y, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100255{
256 struct eventdemo *e = data;
257
258 if (log_motion) {
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500259 printf("motion time: %d, x: %d, y: %d\n", time, x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100260 }
261
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500262 if (x > e->x && x < e->x + e->w)
263 if (y > e->y && y < e->y + e->h)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100264 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 */
273static struct eventdemo *
274eventdemo_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);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500289 e->widget = window_add_widget(e->window, e);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100290 window_set_title(e->window, title);
291 e->display = d;
292
293 /* The eventdemo window draws a red rectangle as a demonstration
294 * of per-window data. The dimensions of that rectangle are set
295 * here.
296 */
297 e->x = width * 1.0 / 4.0;
298 e->w = width * 2.0 / 4.0;
299 e->y = height * 1.0 / 4.0;
300 e->h = height * 2.0 / 4.0;
301
302 /* Connect the user data to the window */
303 window_set_user_data(e->window, e);
304
305 /* Set the callback redraw handler for the window */
306 window_set_redraw_handler(e->window, redraw_handler);
307
308 /* Set the callback resize handler for the window */
309 window_set_resize_handler(e->window, resize_handler);
310
311 /* Set the callback focus handler for the window */
312 window_set_keyboard_focus_handler(e->window,
313 keyboard_focus_handler);
314
315 /* Set the callback key handler for the window */
316 window_set_key_handler(e->window, key_handler);
317
318 /* Set the callback button handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500319 widget_set_button_handler(e->widget, button_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100320
321 /* Set the callback motion handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500322 widget_set_motion_handler(e->widget, 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}