blob: 8e63540e2243f63128dddc1582d9200cdf719d85 [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
73/** set to log motion events */
74static int log_motion = 0;
75
76/**
77 * \struct eventdemo
78 * \brief Holds all data the program needs per window
79 *
80 * In this demo the struct holds the position of a
81 * red rectangle that is drawn in the window's area.
82 */
83struct eventdemo {
84 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050085 struct widget *widget;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010086 struct display *display;
87
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040088 int x, y, w, h;
Tim Wiederhake503ccca2011-01-21 16:56:07 +010089};
90
91/**
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050092 * \brief CALLBACK function, Wayland requests the window to redraw.
93 * \param window window to be redrawn
94 * \param data user data associated to the window
Tim Wiederhake503ccca2011-01-21 16:56:07 +010095 *
96 * Draws a red rectangle as demonstration of per-window data.
97 */
98static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050099redraw_handler(struct widget *widget, void *data)
100{
101 struct eventdemo *e = data;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100102 cairo_surface_t *surface;
103 cairo_t *cr;
104 struct rectangle rect;
105
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500106 if (log_redraw)
107 printf("redraw\n");
108
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500109 widget_get_allocation(e->widget, &rect);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100110 surface = window_get_surface(e->window);
111
112 cr = cairo_create(surface);
113 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
114
115 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
116 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
117 cairo_fill(cr);
118
119 cairo_rectangle(cr, e->x, e->y, e->w, e->h);
120 cairo_set_source_rgba(cr, 1.0, 0, 0, 1);
121 cairo_fill(cr);
122
123 cairo_destroy(cr);
124 cairo_surface_destroy(surface);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100125}
126
127/**
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100128 * \brief CALLBACK function, Wayland requests the window to resize.
129 * \param window window to be resized
130 * \param width desired width
131 * \param height desired height
132 * \param data user data associated to the window
133 */
134
135static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500136resize_handler(struct widget *widget,
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100137 int32_t width, int32_t height, void *data)
138{
139 struct eventdemo *e = data;
140 if (log_resize)
141 printf("resize width: %d, height: %d\n", width, height);
142
143 /* if a maximum width is set, constrain to it */
144 if (width_max && width_max < width)
145 width = width_max;
146
147 /* if a maximum height is set, constrain to it */
148 if (height_max && height_max < height)
149 height = height_max;
150
151 /* set the new window dimensions */
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500152 widget_set_size(e->widget, width, height);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100153}
154
155/**
156 * \brief CALLBACK function, Wayland informs about keyboard focus change
157 * \param window window
158 * \param device device that caused the focus change
159 * \param data user data associated to the window
160 */
161static void
162keyboard_focus_handler(struct window *window,
163 struct input *device, void *data)
164{
165 int32_t x, y;
166 struct eventdemo *e = data;
167
168 if(log_focus) {
169 if(device) {
170 input_get_position(device, &x, &y);
171 printf("focus x: %d, y: %d\n", x, y);
172 } else {
173 printf("focus lost\n");
174 }
175 }
176
177 window_schedule_redraw(e->window);
178}
179
180/**
181 * \brief CALLBACK function, Wayland informs about key event
182 * \param window window
183 * \param key keycode
184 * \param unicode associated character
185 * \param state pressed or released
186 * \param modifiers modifiers: ctrl, alt, meta etc.
187 * \param data user data associated to the window
188 */
189static void
190key_handler(struct window *window, struct input *input, uint32_t time,
191 uint32_t key, uint32_t unicode, uint32_t state, void *data)
192{
193 uint32_t modifiers = input_get_modifiers(input);
194
195 if(!log_key)
196 return;
197
198 printf("key key: %d, unicode: %d, state: %d, modifiers: %d\n",
199 key, unicode, state, modifiers);
200}
201
202/**
203 * \brief CALLBACK function, Wayland informs about button event
204 * \param window window
205 * \param input input device that caused the button event
206 * \param time time the event happend
207 * \param button button
208 * \param state pressed or released
209 * \param data user data associated to the window
210 */
211static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500212button_handler(struct widget *widget, struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +0100213 uint32_t button, uint32_t state, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100214{
215 int32_t x, y;
216
217 if (!log_button)
218 return;
219
220 input_get_position(input, &x, &y);
221 printf("button time: %d, button: %d, state: %d, x: %d, y: %d\n",
222 time, button, state, x, y);
223}
224
225/**
226 * \brief CALLBACK function, Waylands informs about pointer motion
227 * \param window window
228 * \param input input device that caused the motion event
229 * \param time time the event happend
230 * \param x absolute x position
231 * \param y absolute y position
232 * \param sx x position relative to the window
233 * \param sy y position relative to the window
234 * \param data user data associated to the window
235 *
236 * Demonstrates the use of different cursors
237 */
238static int
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500239motion_handler(struct widget *widget, struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400240 float x, float y, void *data)
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100241{
242 struct eventdemo *e = data;
243
244 if (log_motion) {
Daniel Stoneb230a7e2012-05-08 17:17:54 +0100245 printf("motion time: %d, x: %f, y: %f\n", time, x, y);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100246 }
247
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -0500248 if (x > e->x && x < e->x + e->w)
249 if (y > e->y && y < e->y + e->h)
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300250 return POINTER_HAND1;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100251
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300252 return POINTER_LEFT_PTR;
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100253}
254
255/**
256 * \brief Create and initialise a new eventdemo window.
257 * \param d associated display
258 */
259static struct eventdemo *
260eventdemo_create(struct display *d)
261{
262 struct eventdemo *e;
263
264 e = malloc(sizeof (struct eventdemo));
265 if(e == NULL)
266 return NULL;
267
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500268 e->window = window_create(d);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500269 e->widget = frame_create(e->window, e);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100270 window_set_title(e->window, title);
271 e->display = d;
272
273 /* The eventdemo window draws a red rectangle as a demonstration
274 * of per-window data. The dimensions of that rectangle are set
275 * here.
276 */
277 e->x = width * 1.0 / 4.0;
278 e->w = width * 2.0 / 4.0;
279 e->y = height * 1.0 / 4.0;
280 e->h = height * 2.0 / 4.0;
281
282 /* Connect the user data to the window */
283 window_set_user_data(e->window, e);
284
285 /* Set the callback redraw handler for the window */
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500286 widget_set_redraw_handler(e->widget, redraw_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100287
288 /* Set the callback resize handler for the window */
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500289 widget_set_resize_handler(e->widget, resize_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100290
291 /* Set the callback focus handler for the window */
292 window_set_keyboard_focus_handler(e->window,
293 keyboard_focus_handler);
294
295 /* Set the callback key handler for the window */
296 window_set_key_handler(e->window, key_handler);
297
298 /* Set the callback button handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500299 widget_set_button_handler(e->widget, button_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100300
301 /* Set the callback motion handler for the window */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500302 widget_set_motion_handler(e->widget, motion_handler);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100303
304 /* Demonstrate how to create a borderless window.
305 Move windows with META + left mouse button.
306 */
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500307 if (noborder) {
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100308 }
309
310 /* Initial drawing of the window */
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500311 window_schedule_resize(e->window, width, height);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100312
313 return e;
314}
315/**
316 * \brief command line options for eventdemo
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100317 */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400318static const struct weston_option eventdemo_options[] = {
319 { WESTON_OPTION_STRING, "title", 0, &title },
320 { WESTON_OPTION_INTEGER, "width", 'w', &width },
321 { WESTON_OPTION_INTEGER, "height", 'h', &height },
322 { WESTON_OPTION_INTEGER, "max-width", 0, &width_max },
323 { WESTON_OPTION_INTEGER, "max-height", 0, &height_max },
324 { WESTON_OPTION_BOOLEAN, "no-border", 'b', &noborder },
325 { WESTON_OPTION_BOOLEAN, "log-redraw", '0', &log_redraw },
326 { WESTON_OPTION_BOOLEAN, "log-resize", '0', &log_resize },
327 { WESTON_OPTION_BOOLEAN, "log-focus", '0', &log_focus },
328 { WESTON_OPTION_BOOLEAN, "log-key", '0', &log_key },
329 { WESTON_OPTION_BOOLEAN, "log-button", '0', &log_button },
330 { WESTON_OPTION_BOOLEAN, "log-motion", '0', &log_motion },
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100331};
332
333/**
334 * \brief Connects to the display, creates the window and hands over
335 * to the main loop.
336 */
337int
338main(int argc, char *argv[])
339{
340 struct display *d;
341 struct eventdemo *e;
342
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400343 argc = parse_options(eventdemo_options,
344 ARRAY_LENGTH(eventdemo_options), argc, argv);
345
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100346 /* Connect to the display and have the arguments parsed */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400347 d = display_create(argc, argv);
Tim Wiederhake503ccca2011-01-21 16:56:07 +0100348 if (d == NULL) {
349 fprintf(stderr, "failed to create display: %m\n");
350 return -1;
351 }
352
353 /* Create new eventdemo window */
354 e = eventdemo_create(d);
355 if (e == NULL) {
356 fprintf(stderr, "failed to create eventdemo: %m\n");
357 return -1;
358 }
359
360 display_run(d);
361
362 return 0;
363}