2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010, 2013 Intel Corporation
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.
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
40 #include <wayland-client.h>
41 #include "../shared/os-compatibility.h"
44 enum { KEYBOARD, POINTER } type;
51 struct wl_keyboard *keyboard;
52 struct wl_pointer *pointer;
57 struct wl_display *display;
58 struct wl_registry *registry;
59 struct wl_compositor *compositor;
60 struct wl_shell *shell;
64 struct wl_list devices;
68 struct display *display;
70 struct wl_surface *surface;
71 struct wl_shell_surface *shell_surface;
75 buffer_release(void *data, struct wl_buffer *buffer)
77 wl_buffer_destroy(buffer);
80 static const struct wl_buffer_listener buffer_listener = {
91 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
99 attach_buffer(struct window *window, int width, int height)
101 struct wl_shm_pool *pool;
102 struct wl_buffer *buffer;
103 int fd, size, stride;
106 size = stride * height;
108 fd = os_create_anonymous_file(size);
110 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
115 pool = wl_shm_create_pool(window->display->shm, fd, size);
116 buffer = wl_shm_pool_create_buffer(pool, 0,
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);
129 handle_ping(void *data, struct wl_shell_surface *shell_surface,
132 wl_shell_surface_pong(shell_surface, serial);
136 handle_configure(void *data, struct wl_shell_surface *shell_surface,
137 uint32_t edges, int32_t width, int32_t height)
142 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
146 static const struct wl_shell_surface_listener shell_surface_listener = {
152 static struct window *
153 create_window(struct display *display, int width, int height)
155 struct window *window;
157 window = xzalloc(sizeof *window);
158 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,
165 if (window->shell_surface)
166 wl_shell_surface_add_listener(window->shell_surface,
167 &shell_surface_listener, window);
169 wl_shell_surface_set_title(window->shell_surface, "simple-shm");
171 wl_shell_surface_set_toplevel(window->shell_surface);
173 wl_surface_damage(window->surface, 0, 0, width, height);
174 attach_buffer(window, width, height);
175 wl_surface_commit(window->surface);
181 destroy_window(struct window *window)
183 wl_shell_surface_destroy(window->shell_surface);
184 wl_surface_destroy(window->surface);
189 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
191 struct display *d = data;
193 d->formats |= (1 << format);
196 struct wl_shm_listener shm_listener = {
201 registry_handle_global(void *data, struct wl_registry *registry,
202 uint32_t id, const char *interface, uint32_t version)
204 struct display *d = data;
206 if (strcmp(interface, "wl_compositor") == 0) {
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 &&
219 d->seat = wl_registry_bind(registry,
220 id, &wl_seat_interface, 3);
225 registry_handle_global_remove(void *data, struct wl_registry *registry,
230 static const struct wl_registry_listener registry_listener = {
231 registry_handle_global,
232 registry_handle_global_remove
235 static struct display *
238 struct display *display;
240 display = xzalloc(sizeof *display);
241 display->display = wl_display_connect(NULL);
242 assert(display->display);
244 display->formats = 0;
245 display->registry = wl_display_get_registry(display->display);
246 wl_registry_add_listener(display->registry,
247 ®istry_listener, display);
248 wl_display_roundtrip(display->display);
249 if (display->shm == NULL) {
250 fprintf(stderr, "No wl_shm global\n");
254 wl_display_roundtrip(display->display);
256 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
257 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
261 wl_list_init(&display->devices);
267 pointer_handle_enter(void *data, struct wl_pointer *pointer,
268 uint32_t serial, struct wl_surface *surface,
269 wl_fixed_t sx_w, wl_fixed_t sy_w)
274 pointer_handle_leave(void *data, struct wl_pointer *pointer,
275 uint32_t serial, struct wl_surface *surface)
280 pointer_handle_motion(void *data, struct wl_pointer *pointer,
281 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
286 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
287 uint32_t time, uint32_t button, uint32_t state_w)
292 pointer_handle_axis(void *data, struct wl_pointer *pointer,
293 uint32_t time, uint32_t axis, wl_fixed_t value)
297 static const struct wl_pointer_listener pointer_listener = {
298 pointer_handle_enter,
299 pointer_handle_leave,
300 pointer_handle_motion,
301 pointer_handle_button,
306 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
307 uint32_t format, int fd, uint32_t size)
312 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
313 uint32_t serial, struct wl_surface *surface,
314 struct wl_array *keys)
319 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
320 uint32_t serial, struct wl_surface *surface)
325 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
326 uint32_t serial, uint32_t time, uint32_t key,
332 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
333 uint32_t serial, uint32_t mods_depressed,
334 uint32_t mods_latched, uint32_t mods_locked,
339 static const struct wl_keyboard_listener keyboard_listener = {
340 keyboard_handle_keymap,
341 keyboard_handle_enter,
342 keyboard_handle_leave,
344 keyboard_handle_modifiers,
348 start_device(struct display *display, struct device *device)
350 if (display->seat == NULL)
353 switch (device->type) {
355 if (device->p.keyboard == NULL) {
357 wl_seat_get_keyboard(display->seat);
358 wl_keyboard_add_listener(device->p.keyboard,
364 if (device->p.pointer == NULL) {
366 wl_seat_get_pointer(display->seat);
367 wl_pointer_add_listener(device->p.pointer,
376 destroy_device(struct device *device)
378 switch (device->type) {
380 if (device->p.keyboard)
381 wl_keyboard_release(device->p.keyboard);
384 if (device->p.pointer)
385 wl_pointer_release(device->p.pointer);
389 wl_list_remove(&device->link);
394 destroy_devices(struct display *display)
396 struct device *device, *tmp;
398 wl_list_for_each_safe(device, tmp, &display->devices, link)
399 destroy_device(device);
403 destroy_display(struct display *display)
405 destroy_devices(display);
408 wl_shm_destroy(display->shm);
411 wl_shell_destroy(display->shell);
414 wl_seat_destroy(display->seat);
416 if (display->compositor)
417 wl_compositor_destroy(display->compositor);
419 wl_registry_destroy(display->registry);
420 wl_display_flush(display->display);
421 wl_display_disconnect(display->display);
425 static int running = 1;
428 signal_int(int signum)
434 create_device(struct display *display, const char *time_desc, int type)
439 struct device *device;
441 if (time_desc == NULL) {
442 fprintf(stderr, "missing time description\n");
447 start_time = strtoul(time_desc, &tail, 10);
452 end_time = strtoul(tail + 1, &tail, 10);
453 if (errno || *tail != '\0')
455 } else if (*tail != '\0') {
459 device = xzalloc(sizeof *device);
461 device->start_time = start_time;
462 device->end_time = end_time;
463 wl_list_insert(&display->devices, &device->link);
468 fprintf(stderr, "invalid time description\n");
472 static struct timespec begin_time;
477 clock_gettime(CLOCK_MONOTONIC, &begin_time);
485 clock_gettime(CLOCK_MONOTONIC, &t);
486 return (double)(t.tv_sec - begin_time.tv_sec) +
487 1e-9 * (t.tv_nsec - begin_time.tv_nsec);
491 main_loop(struct display *display)
496 struct device *device, *tmp;
497 struct pollfd fds[1];
498 double sleep_time = DBL_MAX;
501 if (wl_display_dispatch_pending(display->display) == -1)
503 if (wl_display_flush(display->display) == -1)
508 wl_list_for_each(device, &display->devices, link) {
509 double next_time = device->start_time - now;
510 if (next_time < 0.0) {
513 } else if (next_time < sleep_time) {
514 sleep_time = next_time;
516 next_time = device->end_time - now;
517 if (next_time < 0.0) {
520 } else if (next_time < sleep_time) {
521 sleep_time = next_time;
525 fds[0].fd = wl_display_get_fd(display->display);
526 fds[0].events = POLLIN;
530 sizeof fds / sizeof fds[0],
531 sleep_time == DBL_MAX ? -1 : ceil(sleep_time * 1000.0));
533 if (fds[0].revents &&
534 wl_display_dispatch(display->display) == -1)
539 wl_list_for_each_safe(device, tmp, &display->devices, link) {
540 if (device->start_time <= now)
541 start_device(display, device);
542 if (device->end_time >= 0 && device->end_time <= now)
543 destroy_device(device);
549 main(int argc, char **argv)
551 struct sigaction sigint;
552 struct display *display;
553 struct window *window;
556 display = create_display();
557 window = create_window(display, 250, 250);
559 for (i = 1; i < argc; i++) {
560 if (!strncmp(argv[i], "-p", 2)) {
568 if (create_device(display, arg, POINTER) == -1)
570 } else if (!strncmp(argv[i], "-k", 2)) {
578 if (create_device(display, arg, KEYBOARD) == -1)
581 fprintf(stderr, "unknown argument %s\n", argv[i]);
586 sigint.sa_handler = signal_int;
587 sigemptyset(&sigint.sa_mask);
588 sigint.sa_flags = SA_RESETHAND;
589 sigaction(SIGINT, &sigint, NULL);
593 fprintf(stderr, "multi-resource exiting\n");
594 destroy_window(window);
595 destroy_display(display);