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