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