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