blob: 569a8c139fcd76ae6affbb599a44ac3d7e311135 [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>
Tim Wiederhake503ccca2011-01-21 16:56:07 +010037
38#include "window.h"
39
40/** window title */
41static char *title = "EventDemo";
42
43/** window width */
44static int width = 500;
45
46/** window height */
47static int height = 400;
48
49/** set if window has no borders */
50static int noborder = 0;
51
52/** if non-zero, maximum window width */
53static int width_max = 0;
54
55/** if non-zero, maximum window height */
56static int height_max = 0;
57
58/** set to log redrawing */
59static int log_redraw = 0;
60
61/** set to log resizing */
62static int log_resize = 0;
63
64/** set to log keyboard focus */
65static int log_focus = 0;
66
67/** set to log key events */
68static int log_key = 0;
69
70/** set to log button events */
71static int log_button = 0;
72
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +020073/** set to log axis events */
74static int log_axis = 0;
75
Tim Wiederhake503ccca2011-01-21 16:56:07 +010076/** set to log motion events */
77static int log_motion = 0;
78
79/**
80 * \struct eventdemo
81 * \brief Holds all data the program needs per window
82 *
83 * In this demo the struct holds the position of a
84 * red rectangle that is drawn in the window's area.
85 */
86struct eventdemo {
87 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050088 struct widget *widget;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010089 struct display *display;
90
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040091 int x, y, w, h;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010092};
93
94/**
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050095 * \brief CALLBACK function, Wayland requests the window to redraw.
Philipp Brüschweiler850f5042012-08-14 11:02:39 +020096 * \param widget widget to be redrawn
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050097 * \param data user data associated to the window
Tim Wiederhake503ccca2011-01-21 16:56:07 +010098 *
99 * Draws a red rectangle as demonstration of per-window data.
100 */
101static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500102redraw_handler(struct widget *widget, void *data)
103{
104 struct eventdemo *e = data;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100105 cairo_surface_t *surface;
106 cairo_t *cr;
107 struct rectangle rect;
108
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500109 if (log_redraw)
110 printf("redraw\n");
111
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500112 widget_get_allocation(e->widget, &rect);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100113 surface = window_get_surface(e->window);
114
115 cr = cairo_create(surface);
116 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
117
118 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
119 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
120 cairo_fill(cr);
121
122 cairo_rectangle(cr, e->x, e->y, e->w, e->h);
123 cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
124 cairo_fill(cr);
125
126 cairo_destroy(cr);
127 cairo_surface_destroy(surface);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100128}
129
130/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100131 * \brief CALLBACK function, Wayland requests the window to resize.
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200132 * \param widget widget to be resized
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100133 * \param width desired width
134 * \param height desired height
135 * \param data user data associated to the window
136 */
137
138static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500139resize_handler(struct widget *widget,
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100140 int32_t width, int32_t height, void *data)
141{
142 struct eventdemo *e = data;
143 if (log_resize)
144 printf("resize width: %d, height: %d\n", width, height);
145
146 /* if a maximum width is set, constrain to it */
147 if (width_max && width_max < width)
148 width = width_max;
149
150 /* if a maximum height is set, constrain to it */
151 if (height_max && height_max < height)
152 height = height_max;
153
154 /* set the new window dimensions */
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500155 widget_set_size(e->widget, width, height);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100156}
157
158/**
159 * \brief CALLBACK function, Wayland informs about keyboard focus change
160 * \param window window
161 * \param device device that caused the focus change
162 * \param data user data associated to the window
163 */
164static void
165keyboard_focus_handler(struct window *window,
166 struct input *device, void *data)
167{
168 int32_t x, y;
169 struct eventdemo *e = data;
170
171 if(log_focus) {
172 if(device) {
173 input_get_position(device, &x, &y);
174 printf("focus x: %d, y: %d\n", x, y);
175 } else {
176 printf("focus lost\n");
177 }
178 }
179
180 window_schedule_redraw(e->window);
181}
182
183/**
184 * \brief CALLBACK function, Wayland informs about key event
185 * \param window window
186 * \param key keycode
187 * \param unicode associated character
188 * \param state pressed or released
189 * \param modifiers modifiers: ctrl, alt, meta etc.
190 * \param data user data associated to the window
191 */
192static void
193key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100194 uint32_t key, uint32_t unicode, enum wl_keyboard_key_state state,
195 void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100196{
197 uint32_t modifiers = input_get_modifiers(input);
198
199 if(!log_key)
200 return;
201
Daniel Stonec9785ea2012-05-30 16:31:52 +0100202 printf("key key: %d, unicode: %d, state: %s, modifiers: 0x%x\n",
203 key, unicode,
204 (state == WL_KEYBOARD_KEY_STATE_PRESSED) ? "pressed" :
205 "released",
206 modifiers);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100207}
208
209/**
210 * \brief CALLBACK function, Wayland informs about button event
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200211 * \param widget widget
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100212 * \param input input device that caused the button event
213 * \param time time the event happend
214 * \param button button
215 * \param state pressed or released
216 * \param data user data associated to the window
217 */
218static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500219button_handler(struct widget *widget, struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100220 uint32_t button, enum wl_pointer_button_state state, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100221{
222 int32_t x, y;
223
224 if (!log_button)
225 return;
226
227 input_get_position(input, &x, &y);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100228 printf("button time: %d, button: %d, state: %s, x: %d, y: %d\n",
229 time, button,
230 (state == WL_POINTER_BUTTON_STATE_PRESSED) ? "pressed" :
231 "released",
232 x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100233}
234
235/**
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200236 * \brief CALLBACK function, Wayland informs about axis event
237 * \param widget widget
238 * \param input input device that caused the axis event
239 * \param time time the event happened
240 * \param axis vertical or horizontal
241 * \param value amount of scrolling
242 * \param data user data associated to the widget
243 */
244static void
245axis_handler(struct widget *widget, struct input *input, uint32_t time,
246 uint32_t axis, wl_fixed_t value, void *data)
247{
248 if (!log_axis)
249 return;
250
251 printf("axis time: %d, axis: %s, value: %f\n",
252 time,
253 axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? "vertical" :
254 "horizontal",
255 wl_fixed_to_double(value));
256}
257
258/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100259 * \brief CALLBACK function, Waylands informs about pointer motion
Philipp Brüschweiler850f5042012-08-14 11:02:39 +0200260 * \param widget widget
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100261 * \param input input device that caused the motion event
262 * \param time time the event happend
263 * \param x absolute x position
264 * \param y absolute y position
265 * \param sx x position relative to the window
266 * \param sy y position relative to the window
267 * \param data user data associated to the window
268 *
269 * Demonstrates the use of different cursors
270 */
271static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500272motion_handler(struct widget *widget, struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400273 float x, float y, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100274{
275 struct eventdemo *e = data;
276
277 if (log_motion) {
Daniel Stoneb230a7e2012-05-08 17:17:54 +0100278 printf("motion time: %d, x: %f, y: %f\n", time, x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100279 }
280
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500281 if (x > e->x && x < e->x + e->w)
282 if (y > e->y && y < e->y + e->h)
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300283 return CURSOR_HAND1;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100284
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300285 return CURSOR_LEFT_PTR;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100286}
287
288/**
289 * \brief Create and initialise a new eventdemo window.
Martin Olssonc663b222012-07-08 11:11:18 +0200290 * The returned eventdemo instance should be destroyed using \c eventdemo_destroy().
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100291 * \param d associated display
292 */
293static struct eventdemo *
294eventdemo_create(struct display *d)
295{
296 struct eventdemo *e;
297
298 e = malloc(sizeof (struct eventdemo));
299 if(e == NULL)
300 return NULL;
301
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500302 e->window = window_create(d);
Philipp Brüschweilera306a262012-08-14 11:02:40 +0200303
304 if (noborder) {
305 /* Demonstrate how to create a borderless window.
306 * Move windows with META + left mouse button.
307 */
308 e->widget = window_add_widget(e->window, e);
309 } else {
310 e->widget = frame_create(e->window, e);
311 window_set_title(e->window, title);
312 }
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100313 e->display = d;
314
315 /* The eventdemo window draws a red rectangle as a demonstration
316 * of per-window data. The dimensions of that rectangle are set
317 * here.
318 */
319 e->x = width * 1.0 / 4.0;
320 e->w = width * 2.0 / 4.0;
321 e->y = height * 1.0 / 4.0;
322 e->h = height * 2.0 / 4.0;
323
324 /* Connect the user data to the window */
325 window_set_user_data(e->window, e);
326
327 /* Set the callback redraw handler for the window */
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500328 widget_set_redraw_handler(e->widget, redraw_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100329
330 /* Set the callback resize handler for the window */
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500331 widget_set_resize_handler(e->widget, resize_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100332
333 /* Set the callback focus handler for the window */
334 window_set_keyboard_focus_handler(e->window,
335 keyboard_focus_handler);
336
337 /* Set the callback key handler for the window */
338 window_set_key_handler(e->window, key_handler);
339
340 /* Set the callback button handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500341 widget_set_button_handler(e->widget, button_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100342
343 /* Set the callback motion handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500344 widget_set_motion_handler(e->widget, motion_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100345
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200346 /* Set the callback axis handler for the window */
347 widget_set_axis_handler(e->widget, axis_handler);
348
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100349 /* Initial drawing of the window */
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500350 window_schedule_resize(e->window, width, height);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100351
352 return e;
353}
354/**
Martin Olssonc663b222012-07-08 11:11:18 +0200355 * \brief Destroy eventdemo instance previously created by \c eventdemo_create().
356 * \param eventdemo eventdemo instance to destroy
357 */
358static void eventdemo_destroy(struct eventdemo * eventdemo)
359{
360 widget_destroy(eventdemo->widget);
361 window_destroy(eventdemo->window);
362 free(eventdemo);
363}
364/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100365 * \brief command line options for eventdemo
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100366 */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400367static const struct weston_option eventdemo_options[] = {
368 { WESTON_OPTION_STRING, "title", 0, &title },
369 { WESTON_OPTION_INTEGER, "width", 'w', &width },
370 { WESTON_OPTION_INTEGER, "height", 'h', &height },
371 { WESTON_OPTION_INTEGER, "max-width", 0, &width_max },
372 { WESTON_OPTION_INTEGER, "max-height", 0, &height_max },
373 { WESTON_OPTION_BOOLEAN, "no-border", 'b', &noborder },
374 { WESTON_OPTION_BOOLEAN, "log-redraw", '0', &log_redraw },
375 { WESTON_OPTION_BOOLEAN, "log-resize", '0', &log_resize },
376 { WESTON_OPTION_BOOLEAN, "log-focus", '0', &log_focus },
377 { WESTON_OPTION_BOOLEAN, "log-key", '0', &log_key },
378 { WESTON_OPTION_BOOLEAN, "log-button", '0', &log_button },
Philipp Brüschweiler70cc73b2012-08-14 11:02:42 +0200379 { WESTON_OPTION_BOOLEAN, "log-axis", '0', &log_axis },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400380 { WESTON_OPTION_BOOLEAN, "log-motion", '0', &log_motion },
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100381};
382
383/**
384 * \brief Connects to the display, creates the window and hands over
385 * to the main loop.
386 */
387int
388main(int argc, char *argv[])
389{
390 struct display *d;
391 struct eventdemo *e;
392
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400393 argc = parse_options(eventdemo_options,
394 ARRAY_LENGTH(eventdemo_options), argc, argv);
395
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100396 /* Connect to the display and have the arguments parsed */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400397 d = display_create(argc, argv);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100398 if (d == NULL) {
399 fprintf(stderr, "failed to create display: %m\n");
400 return -1;
401 }
402
403 /* Create new eventdemo window */
404 e = eventdemo_create(d);
405 if (e == NULL) {
406 fprintf(stderr, "failed to create eventdemo: %m\n");
407 return -1;
408 }
409
410 display_run(d);
411
Martin Olssonc663b222012-07-08 11:11:18 +0200412 /* Release resources */
413 eventdemo_destroy(e);
414 display_destroy(d);
415
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100416 return 0;
417}