blob: 5ea6831b5cc6eb62b951d30c883584cd6754b337 [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
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100106 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);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100122}
123
124/**
125 * \brief CALLBACK function, Wayland requests the window to redraw.
126 * \param window window to be redrawn
127 * \param data user data associated to the window
128 */
129static void
130redraw_handler(struct window *window, void *data)
131{
132 struct eventdemo *e = data;
133 eventdemo_draw(e);
134}
135
136/**
137 * \brief CALLBACK function, Wayland requests the window to resize.
138 * \param window window to be resized
139 * \param width desired width
140 * \param height desired height
141 * \param data user data associated to the window
142 */
143
144static void
145resize_handler(struct window *window,
146 int32_t width, int32_t height, void *data)
147{
148 struct eventdemo *e = data;
149 if (log_resize)
150 printf("resize width: %d, height: %d\n", width, height);
151
152 /* if a maximum width is set, constrain to it */
153 if (width_max && width_max < width)
154 width = width_max;
155
156 /* if a maximum height is set, constrain to it */
157 if (height_max && height_max < height)
158 height = height_max;
159
160 /* set the new window dimensions */
161 window_set_child_size(e->window, width, height);
162
163 /* inform Wayland that the window needs to be redrawn */
164 window_schedule_redraw(e->window);
165}
166
167/**
168 * \brief CALLBACK function, Wayland informs about keyboard focus change
169 * \param window window
170 * \param device device that caused the focus change
171 * \param data user data associated to the window
172 */
173static void
174keyboard_focus_handler(struct window *window,
175 struct input *device, void *data)
176{
177 int32_t x, y;
178 struct eventdemo *e = data;
179
180 if(log_focus) {
181 if(device) {
182 input_get_position(device, &x, &y);
183 printf("focus x: %d, y: %d\n", x, y);
184 } else {
185 printf("focus lost\n");
186 }
187 }
188
189 window_schedule_redraw(e->window);
190}
191
192/**
193 * \brief CALLBACK function, Wayland informs about key event
194 * \param window window
195 * \param key keycode
196 * \param unicode associated character
197 * \param state pressed or released
198 * \param modifiers modifiers: ctrl, alt, meta etc.
199 * \param data user data associated to the window
200 */
201static void
202key_handler(struct window *window, struct input *input, uint32_t time,
203 uint32_t key, uint32_t unicode, uint32_t state, void *data)
204{
205 uint32_t modifiers = input_get_modifiers(input);
206
207 if(!log_key)
208 return;
209
210 printf("key key: %d, unicode: %d, state: %d, modifiers: %d\n",
211 key, unicode, state, modifiers);
212}
213
214/**
215 * \brief CALLBACK function, Wayland informs about button event
216 * \param window window
217 * \param input input device that caused the button event
218 * \param time time the event happend
219 * \param button button
220 * \param state pressed or released
221 * \param data user data associated to the window
222 */
223static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500224button_handler(struct widget *widget, struct input *input, uint32_t time,
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100225 int button, int state, void *data)
226{
227 int32_t x, y;
228
229 if (!log_button)
230 return;
231
232 input_get_position(input, &x, &y);
233 printf("button time: %d, button: %d, state: %d, x: %d, y: %d\n",
234 time, button, state, x, y);
235}
236
237/**
238 * \brief CALLBACK function, Waylands informs about pointer motion
239 * \param window window
240 * \param input input device that caused the motion event
241 * \param time time the event happend
242 * \param x absolute x position
243 * \param y absolute y position
244 * \param sx x position relative to the window
245 * \param sy y position relative to the window
246 * \param data user data associated to the window
247 *
248 * Demonstrates the use of different cursors
249 */
250static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500251motion_handler(struct widget *widget, struct input *input, uint32_t time,
252 int32_t x, int32_t y, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100253{
254 struct eventdemo *e = data;
255
256 if (log_motion) {
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500257 printf("motion time: %d, x: %d, y: %d\n", time, x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100258 }
259
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500260 if (x > e->x && x < e->x + e->w)
261 if (y > e->y && y < e->y + e->h)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100262 return POINTER_HAND1;
263
264 return POINTER_LEFT_PTR;
265}
266
267/**
268 * \brief Create and initialise a new eventdemo window.
269 * \param d associated display
270 */
271static struct eventdemo *
272eventdemo_create(struct display *d)
273{
274 struct eventdemo *e;
275
276 e = malloc(sizeof (struct eventdemo));
277 if(e == NULL)
278 return NULL;
279
280 /* Creates a new window with the given title, width and height.
281 * To set the size of the window for a given usable areas width
282 * and height in a window decoration agnostic way use:
283 * window_set_child_size(struct window *window,
284 * int32_t width, int32_t height);
285 */
286 e->window = window_create(d, width, height);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500287 e->widget = window_add_widget(e->window, e);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100288 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øgsberg75bc6672012-01-10 09:43:58 -0500317 widget_set_button_handler(e->widget, button_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100318
319 /* Set the callback motion handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500320 widget_set_motion_handler(e->widget, motion_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100321
322 /* Demonstrate how to create a borderless window.
323 Move windows with META + left mouse button.
324 */
325 if(noborder) {
326 window_set_decoration(e->window, 0);
327 }
328
329 /* Initial drawing of the window */
Kristian Høgsberg5d129902012-01-10 10:49:41 -0500330 window_schedule_redraw(e->window);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100331
332 return e;
333}
334/**
335 * \brief command line options for eventdemo
336 *
337 * see
338 * http://developer.gimp.org/api/2.0/glib/glib-Commandline-option-parser.html
339 */
340static const GOptionEntry option_entries[] = {
341 {"title", 0, 0, G_OPTION_ARG_STRING,
342 &title, "Set the window title to TITLE", "TITLE"},
343 {"width", 'w', 0, G_OPTION_ARG_INT,
344 &width, "Set the window's width to W", "W"},
345 {"height", 'h', 0, G_OPTION_ARG_INT,
346 &height, "Set the window's height to H", "H"},
347 {"maxwidth", 0, 0, G_OPTION_ARG_INT,
348 &width_max, "Set the window's maximum width to W", "W"},
349 {"maxheight", 0, 0, G_OPTION_ARG_INT,
350 &height_max, "Set the window's maximum height to H", "H"},
351 {"noborder", 'b', 0, G_OPTION_ARG_NONE,
352 &noborder, "Don't draw window borders", 0},
353 {"log-redraw", 0, 0, G_OPTION_ARG_NONE,
354 &log_redraw, "Log redraw events to stdout", 0},
355 {"log-resize", 0, 0, G_OPTION_ARG_NONE,
356 &log_resize, "Log resize events to stdout", 0},
357 {"log-focus", 0, 0, G_OPTION_ARG_NONE,
358 &log_focus, "Log keyboard focus events to stdout", 0},
359 {"log-key", 0, 0, G_OPTION_ARG_NONE,
360 &log_key, "Log key events to stdout", 0},
361 {"log-button", 0, 0, G_OPTION_ARG_NONE,
362 &log_button, "Log button events to stdout", 0},
363 {"log-motion", 0, 0, G_OPTION_ARG_NONE,
364 &log_motion, "Log motion events to stdout", 0},
365 {NULL}
366};
367
368/**
369 * \brief Connects to the display, creates the window and hands over
370 * to the main loop.
371 */
372int
373main(int argc, char *argv[])
374{
375 struct display *d;
376 struct eventdemo *e;
377
378 /* Connect to the display and have the arguments parsed */
Kristian Høgsberg9de79a92011-08-24 11:09:53 -0400379 d = display_create(&argc, &argv, option_entries);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100380 if (d == NULL) {
381 fprintf(stderr, "failed to create display: %m\n");
382 return -1;
383 }
384
385 /* Create new eventdemo window */
386 e = eventdemo_create(d);
387 if (e == NULL) {
388 fprintf(stderr, "failed to create eventdemo: %m\n");
389 return -1;
390 }
391
392 display_run(d);
393
394 return 0;
395}