blob: 4ed4d5002ef4aefd65fa9c9eb322a5d770391022 [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"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080043#include "shared/zalloc.h"
Neil Robertsf428d252013-09-19 17:32:01 +010044
45struct device {
46 enum { KEYBOARD, POINTER } type;
47
48 int start_time;
49 int end_time;
50 struct wl_list link;
51
52 union {
53 struct wl_keyboard *keyboard;
54 struct wl_pointer *pointer;
55 } p;
56};
57
58struct display {
59 struct wl_display *display;
60 struct wl_registry *registry;
61 struct wl_compositor *compositor;
62 struct wl_shell *shell;
63 struct wl_seat *seat;
64 struct wl_shm *shm;
65 uint32_t formats;
66 struct wl_list devices;
67};
68
69struct window {
70 struct display *display;
71 int width, height;
72 struct wl_surface *surface;
73 struct wl_shell_surface *shell_surface;
74};
75
76static void
77buffer_release(void *data, struct wl_buffer *buffer)
78{
79 wl_buffer_destroy(buffer);
80}
81
82static const struct wl_buffer_listener buffer_listener = {
83 buffer_release
84};
85
Kristian Høgsbergcab9aea2013-10-10 19:21:05 -070086static inline void *
87xzalloc(size_t s)
88{
89 void *p;
90
Bryce Harrington0d1a6222016-02-11 16:42:49 -080091 p = zalloc(s);
Kristian Høgsbergcab9aea2013-10-10 19:21:05 -070092 if (p == NULL) {
93 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
94 exit(EXIT_FAILURE);
95 }
96
97 return p;
98}
99
Neil Robertsf428d252013-09-19 17:32:01 +0100100static int
101attach_buffer(struct window *window, int width, int height)
102{
103 struct wl_shm_pool *pool;
104 struct wl_buffer *buffer;
105 int fd, size, stride;
106
107 stride = width * 4;
108 size = stride * height;
109
110 fd = os_create_anonymous_file(size);
111 if (fd < 0) {
112 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
113 size);
114 return -1;
115 }
116
117 pool = wl_shm_create_pool(window->display->shm, fd, size);
118 buffer = wl_shm_pool_create_buffer(pool, 0,
119 width, height,
120 stride,
121 WL_SHM_FORMAT_XRGB8888);
122 wl_surface_attach(window->surface, buffer, 0, 0);
123 wl_buffer_add_listener(buffer, &buffer_listener, buffer);
124 wl_shm_pool_destroy(pool);
125 close(fd);
126
127 return 0;
128}
129
130static void
131handle_ping(void *data, struct wl_shell_surface *shell_surface,
132 uint32_t serial)
133{
134 wl_shell_surface_pong(shell_surface, serial);
135}
136
137static void
138handle_configure(void *data, struct wl_shell_surface *shell_surface,
139 uint32_t edges, int32_t width, int32_t height)
140{
141}
142
143static void
144handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
145{
146}
147
148static const struct wl_shell_surface_listener shell_surface_listener = {
149 handle_ping,
150 handle_configure,
151 handle_popup_done
152};
153
154static struct window *
155create_window(struct display *display, int width, int height)
156{
157 struct window *window;
158
Kristian Høgsbergb24d5902013-10-09 13:09:51 -0700159 window = xzalloc(sizeof *window);
Neil Robertsf428d252013-09-19 17:32:01 +0100160 window->display = display;
161 window->width = width;
162 window->height = height;
163 window->surface = wl_compositor_create_surface(display->compositor);
164 window->shell_surface = wl_shell_get_shell_surface(display->shell,
165 window->surface);
166
167 if (window->shell_surface)
168 wl_shell_surface_add_listener(window->shell_surface,
169 &shell_surface_listener, window);
170
171 wl_shell_surface_set_title(window->shell_surface, "simple-shm");
172
173 wl_shell_surface_set_toplevel(window->shell_surface);
174
175 wl_surface_damage(window->surface, 0, 0, width, height);
176 attach_buffer(window, width, height);
177 wl_surface_commit(window->surface);
178
179 return window;
180}
181
182static void
183destroy_window(struct window *window)
184{
185 wl_shell_surface_destroy(window->shell_surface);
186 wl_surface_destroy(window->surface);
187 free(window);
188}
189
190static void
191shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
192{
193 struct display *d = data;
194
195 d->formats |= (1 << format);
196}
197
198struct wl_shm_listener shm_listener = {
199 shm_format
200};
201
202static void
203registry_handle_global(void *data, struct wl_registry *registry,
204 uint32_t id, const char *interface, uint32_t version)
205{
206 struct display *d = data;
207
208 if (strcmp(interface, "wl_compositor") == 0) {
209 d->compositor =
210 wl_registry_bind(registry,
211 id, &wl_compositor_interface, 1);
212 } else if (strcmp(interface, "wl_shell") == 0) {
213 d->shell = wl_registry_bind(registry,
214 id, &wl_shell_interface, 1);
215 } else if (strcmp(interface, "wl_shm") == 0) {
216 d->shm = wl_registry_bind(registry,
217 id, &wl_shm_interface, 1);
218 wl_shm_add_listener(d->shm, &shm_listener, d);
219 } else if (strcmp(interface, "wl_seat") == 0 &&
220 d->seat == NULL) {
221 d->seat = wl_registry_bind(registry,
222 id, &wl_seat_interface, 3);
223 }
224}
225
226static void
227registry_handle_global_remove(void *data, struct wl_registry *registry,
228 uint32_t name)
229{
230}
231
232static const struct wl_registry_listener registry_listener = {
233 registry_handle_global,
234 registry_handle_global_remove
235};
236
237static struct display *
238create_display(void)
239{
240 struct display *display;
241
Kristian Høgsbergb24d5902013-10-09 13:09:51 -0700242 display = xzalloc(sizeof *display);
Neil Robertsf428d252013-09-19 17:32:01 +0100243 display->display = wl_display_connect(NULL);
244 assert(display->display);
245
246 display->formats = 0;
247 display->registry = wl_display_get_registry(display->display);
248 wl_registry_add_listener(display->registry,
249 &registry_listener, display);
250 wl_display_roundtrip(display->display);
251 if (display->shm == NULL) {
252 fprintf(stderr, "No wl_shm global\n");
253 exit(1);
254 }
255
256 wl_display_roundtrip(display->display);
257
258 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
259 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
260 exit(1);
261 }
262
Neil Robertsf428d252013-09-19 17:32:01 +0100263 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}