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