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