blob: 6f9361864257abe22b9ccdb660900d620c547e7e [file] [log] [blame]
Neil Robertsf428d252013-09-19 17:32:01 +01001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010, 2013 Intel Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#include <config.h>
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <string.h>
30#include <stdbool.h>
31#include <assert.h>
32#include <unistd.h>
33#include <sys/mman.h>
34#include <signal.h>
35#include <time.h>
36#include <sys/poll.h>
37#include <float.h>
Stefan Schmidt639fd862013-09-23 11:25:29 +010038#include <math.h>
Neil Robertsf428d252013-09-19 17:32:01 +010039
40#include <wayland-client.h>
41#include "../shared/os-compatibility.h"
42
43struct device {
44 enum { KEYBOARD, POINTER } type;
45
46 int start_time;
47 int end_time;
48 struct wl_list link;
49
50 union {
51 struct wl_keyboard *keyboard;
52 struct wl_pointer *pointer;
53 } p;
54};
55
56struct display {
57 struct wl_display *display;
58 struct wl_registry *registry;
59 struct wl_compositor *compositor;
60 struct wl_shell *shell;
61 struct wl_seat *seat;
62 struct wl_shm *shm;
63 uint32_t formats;
64 struct wl_list devices;
65};
66
67struct window {
68 struct display *display;
69 int width, height;
70 struct wl_surface *surface;
71 struct wl_shell_surface *shell_surface;
72};
73
74static void
75buffer_release(void *data, struct wl_buffer *buffer)
76{
77 wl_buffer_destroy(buffer);
78}
79
80static const struct wl_buffer_listener buffer_listener = {
81 buffer_release
82};
83
84static int
85attach_buffer(struct window *window, int width, int height)
86{
87 struct wl_shm_pool *pool;
88 struct wl_buffer *buffer;
89 int fd, size, stride;
90
91 stride = width * 4;
92 size = stride * height;
93
94 fd = os_create_anonymous_file(size);
95 if (fd < 0) {
96 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
97 size);
98 return -1;
99 }
100
101 pool = wl_shm_create_pool(window->display->shm, fd, size);
102 buffer = wl_shm_pool_create_buffer(pool, 0,
103 width, height,
104 stride,
105 WL_SHM_FORMAT_XRGB8888);
106 wl_surface_attach(window->surface, buffer, 0, 0);
107 wl_buffer_add_listener(buffer, &buffer_listener, buffer);
108 wl_shm_pool_destroy(pool);
109 close(fd);
110
111 return 0;
112}
113
114static void
115handle_ping(void *data, struct wl_shell_surface *shell_surface,
116 uint32_t serial)
117{
118 wl_shell_surface_pong(shell_surface, serial);
119}
120
121static void
122handle_configure(void *data, struct wl_shell_surface *shell_surface,
123 uint32_t edges, int32_t width, int32_t height)
124{
125}
126
127static void
128handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
129{
130}
131
132static const struct wl_shell_surface_listener shell_surface_listener = {
133 handle_ping,
134 handle_configure,
135 handle_popup_done
136};
137
138static struct window *
139create_window(struct display *display, int width, int height)
140{
141 struct window *window;
142
143 window = calloc(1, sizeof *window);
144 if (!window)
145 return NULL;
146
147 window->display = display;
148 window->width = width;
149 window->height = height;
150 window->surface = wl_compositor_create_surface(display->compositor);
151 window->shell_surface = wl_shell_get_shell_surface(display->shell,
152 window->surface);
153
154 if (window->shell_surface)
155 wl_shell_surface_add_listener(window->shell_surface,
156 &shell_surface_listener, window);
157
158 wl_shell_surface_set_title(window->shell_surface, "simple-shm");
159
160 wl_shell_surface_set_toplevel(window->shell_surface);
161
162 wl_surface_damage(window->surface, 0, 0, width, height);
163 attach_buffer(window, width, height);
164 wl_surface_commit(window->surface);
165
166 return window;
167}
168
169static void
170destroy_window(struct window *window)
171{
172 wl_shell_surface_destroy(window->shell_surface);
173 wl_surface_destroy(window->surface);
174 free(window);
175}
176
177static void
178shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
179{
180 struct display *d = data;
181
182 d->formats |= (1 << format);
183}
184
185struct wl_shm_listener shm_listener = {
186 shm_format
187};
188
189static void
190registry_handle_global(void *data, struct wl_registry *registry,
191 uint32_t id, const char *interface, uint32_t version)
192{
193 struct display *d = data;
194
195 if (strcmp(interface, "wl_compositor") == 0) {
196 d->compositor =
197 wl_registry_bind(registry,
198 id, &wl_compositor_interface, 1);
199 } else if (strcmp(interface, "wl_shell") == 0) {
200 d->shell = wl_registry_bind(registry,
201 id, &wl_shell_interface, 1);
202 } else if (strcmp(interface, "wl_shm") == 0) {
203 d->shm = wl_registry_bind(registry,
204 id, &wl_shm_interface, 1);
205 wl_shm_add_listener(d->shm, &shm_listener, d);
206 } else if (strcmp(interface, "wl_seat") == 0 &&
207 d->seat == NULL) {
208 d->seat = wl_registry_bind(registry,
209 id, &wl_seat_interface, 3);
210 }
211}
212
213static void
214registry_handle_global_remove(void *data, struct wl_registry *registry,
215 uint32_t name)
216{
217}
218
219static const struct wl_registry_listener registry_listener = {
220 registry_handle_global,
221 registry_handle_global_remove
222};
223
224static struct display *
225create_display(void)
226{
227 struct display *display;
228
229 display = malloc(sizeof *display);
230 if (display == NULL) {
231 fprintf(stderr, "out of memory\n");
232 exit(1);
233 }
234 memset(display, 0, sizeof *display);
235 display->display = wl_display_connect(NULL);
236 assert(display->display);
237
238 display->formats = 0;
239 display->registry = wl_display_get_registry(display->display);
240 wl_registry_add_listener(display->registry,
241 &registry_listener, display);
242 wl_display_roundtrip(display->display);
243 if (display->shm == NULL) {
244 fprintf(stderr, "No wl_shm global\n");
245 exit(1);
246 }
247
248 wl_display_roundtrip(display->display);
249
250 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
251 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
252 exit(1);
253 }
254
255 wl_display_get_fd(display->display);
256
257 wl_list_init(&display->devices);
258
259 return display;
260}
261
262static void
263pointer_handle_enter(void *data, struct wl_pointer *pointer,
264 uint32_t serial, struct wl_surface *surface,
265 wl_fixed_t sx_w, wl_fixed_t sy_w)
266{
267}
268
269static void
270pointer_handle_leave(void *data, struct wl_pointer *pointer,
271 uint32_t serial, struct wl_surface *surface)
272{
273}
274
275static void
276pointer_handle_motion(void *data, struct wl_pointer *pointer,
277 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
278{
279}
280
281static void
282pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
283 uint32_t time, uint32_t button, uint32_t state_w)
284{
285}
286
287static void
288pointer_handle_axis(void *data, struct wl_pointer *pointer,
289 uint32_t time, uint32_t axis, wl_fixed_t value)
290{
291}
292
293static const struct wl_pointer_listener pointer_listener = {
294 pointer_handle_enter,
295 pointer_handle_leave,
296 pointer_handle_motion,
297 pointer_handle_button,
298 pointer_handle_axis,
299};
300
301static void
302keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
303 uint32_t format, int fd, uint32_t size)
304{
305}
306
307static void
308keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
309 uint32_t serial, struct wl_surface *surface,
310 struct wl_array *keys)
311{
312}
313
314static void
315keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
316 uint32_t serial, struct wl_surface *surface)
317{
318}
319
320static void
321keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
322 uint32_t serial, uint32_t time, uint32_t key,
323 uint32_t state_w)
324{
325}
326
327static void
328keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
329 uint32_t serial, uint32_t mods_depressed,
330 uint32_t mods_latched, uint32_t mods_locked,
331 uint32_t group)
332{
333}
334
335static const struct wl_keyboard_listener keyboard_listener = {
336 keyboard_handle_keymap,
337 keyboard_handle_enter,
338 keyboard_handle_leave,
339 keyboard_handle_key,
340 keyboard_handle_modifiers,
341};
342
343static void
344start_device(struct display *display, struct device *device)
345{
346 if (display->seat == NULL)
347 return;
348
349 switch (device->type) {
350 case KEYBOARD:
351 if (device->p.keyboard == NULL) {
352 device->p.keyboard =
353 wl_seat_get_keyboard(display->seat);
354 wl_keyboard_add_listener(device->p.keyboard,
355 &keyboard_listener,
356 NULL);
357 }
358 break;
359 case POINTER:
360 if (device->p.pointer == NULL) {
361 device->p.pointer =
362 wl_seat_get_pointer(display->seat);
363 wl_pointer_add_listener(device->p.pointer,
364 &pointer_listener,
365 NULL);
366 }
367 break;
368 }
369}
370
371static void
372destroy_device(struct device *device)
373{
374 switch (device->type) {
375 case KEYBOARD:
376 if (device->p.keyboard)
377 wl_keyboard_release(device->p.keyboard);
378 break;
379 case POINTER:
380 if (device->p.pointer)
381 wl_pointer_release(device->p.pointer);
382 break;
383 }
384
385 wl_list_remove(&device->link);
386 free(device);
387}
388
389static void
390destroy_devices(struct display *display)
391{
392 struct device *device, *tmp;
393
394 wl_list_for_each_safe(device, tmp, &display->devices, link)
395 destroy_device(device);
396}
397
398static void
399destroy_display(struct display *display)
400{
401 destroy_devices(display);
402
403 if (display->shm)
404 wl_shm_destroy(display->shm);
405
406 if (display->shell)
407 wl_shell_destroy(display->shell);
408
409 if (display->seat)
410 wl_seat_destroy(display->seat);
411
412 if (display->compositor)
413 wl_compositor_destroy(display->compositor);
414
415 wl_registry_destroy(display->registry);
416 wl_display_flush(display->display);
417 wl_display_disconnect(display->display);
418 free(display);
419}
420
421static int running = 1;
422
423static void
424signal_int(int signum)
425{
426 running = 0;
427}
428
429static int
430create_device(struct display *display, const char *time_desc, int type)
431{
432 int start_time;
433 int end_time = -1;
434 char *tail;
435 struct device *device;
436
437 if (time_desc == NULL) {
438 fprintf(stderr, "missing time description\n");
439 return -1;
440 }
441
442 errno = 0;
443 start_time = strtoul(time_desc, &tail, 10);
444 if (errno)
445 goto error;
446
447 if (*tail == ':') {
448 end_time = strtoul(tail + 1, &tail, 10);
449 if (errno || *tail != '\0')
450 goto error;
451 } else if (*tail != '\0') {
452 goto error;
453 }
454
455 device = malloc(sizeof *device);
456 memset(device, 0, sizeof(*device));
457 device->type = type;
458 device->start_time = start_time;
459 device->end_time = end_time;
460 wl_list_insert(&display->devices, &device->link);
461
462 return 0;
463
464error:
465 fprintf(stderr, "invalid time description\n");
466 return -1;
467}
468
469static struct timespec begin_time;
470
471static void
472reset_timer(void)
473{
474 clock_gettime(CLOCK_MONOTONIC, &begin_time);
475}
476
477static double
478read_timer(void)
479{
480 struct timespec t;
481
482 clock_gettime(CLOCK_MONOTONIC, &t);
483 return (double)(t.tv_sec - begin_time.tv_sec) +
484 1e-9 * (t.tv_nsec - begin_time.tv_nsec);
485}
486
487static void
488main_loop(struct display *display)
489{
490 reset_timer();
491
492 while (running) {
493 struct device *device, *tmp;
494 struct pollfd fds[1];
495 double sleep_time = DBL_MAX;
496 double now;
497
498 if (wl_display_dispatch_pending(display->display) == -1)
499 break;
500 if (wl_display_flush(display->display) == -1)
501 break;
502
503 now = read_timer();
504
505 wl_list_for_each(device, &display->devices, link) {
506 double next_time = device->start_time - now;
507 if (next_time < 0.0) {
508 sleep_time = 0.0;
509 break;
510 } else if (next_time < sleep_time) {
511 sleep_time = next_time;
512 }
513 next_time = device->end_time - now;
514 if (next_time < 0.0) {
515 sleep_time = 0.0;
516 break;
517 } else if (next_time < sleep_time) {
518 sleep_time = next_time;
519 }
520 }
521
522 fds[0].fd = wl_display_get_fd(display->display);
523 fds[0].events = POLLIN;
524 fds[0].revents = 0;
525
526 poll(fds,
527 sizeof fds / sizeof fds[0],
528 sleep_time == DBL_MAX ? -1 : ceil(sleep_time * 1000.0));
529
530 if (fds[0].revents &&
531 wl_display_dispatch(display->display) == -1)
532 break;
533
534 now = read_timer();
535
536 wl_list_for_each_safe(device, tmp, &display->devices, link) {
537 if (device->start_time <= now)
538 start_device(display, device);
539 if (device->end_time >= 0 && device->end_time <= now)
540 destroy_device(device);
541 }
542 }
543}
544
545int
546main(int argc, char **argv)
547{
548 struct sigaction sigint;
549 struct display *display;
550 struct window *window;
551 int i;
552
553 display = create_display();
554 window = create_window(display, 250, 250);
555 if (!window)
556 return 1;
557
558 for (i = 1; i < argc; i++) {
559 if (!strncmp(argv[i], "-p", 2)) {
560 char *arg;
561 if (argv[i][2]) {
562 arg = argv[i] + 2;
563 } else {
564 arg = argv[i + 1];
565 i++;
566 }
567 if (create_device(display, arg, POINTER) == -1)
568 return 1;
569 } else if (!strncmp(argv[i], "-k", 2)) {
570 char *arg;
571 if (argv[i][2]) {
572 arg = argv[i] + 2;
573 } else {
574 arg = argv[i + 1];
575 i++;
576 }
577 if (create_device(display, arg, KEYBOARD) == -1)
578 return 1;
579 } else {
580 fprintf(stderr, "unknown argument %s\n", argv[i]);
581 return 1;
582 }
583 }
584
585 sigint.sa_handler = signal_int;
586 sigemptyset(&sigint.sa_mask);
587 sigint.sa_flags = SA_RESETHAND;
588 sigaction(SIGINT, &sigint, NULL);
589
590 main_loop(display);
591
592 fprintf(stderr, "multi-resource exiting\n");
593 destroy_window(window);
594 destroy_display(display);
595
596 return 0;
597}