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_display_get_fd(display->display);
263 wl_list_init(&display->devices);
269 pointer_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)
276 pointer_handle_leave(void *data, struct wl_pointer *pointer,
277 uint32_t serial, struct wl_surface *surface)
282 pointer_handle_motion(void *data, struct wl_pointer *pointer,
283 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
288 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
289 uint32_t time, uint32_t button, uint32_t state_w)
294 pointer_handle_axis(void *data, struct wl_pointer *pointer,
295 uint32_t time, uint32_t axis, wl_fixed_t value)
299 static const struct wl_pointer_listener pointer_listener = {
300 pointer_handle_enter,
301 pointer_handle_leave,
302 pointer_handle_motion,
303 pointer_handle_button,
308 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
309 uint32_t format, int fd, uint32_t size)
314 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
315 uint32_t serial, struct wl_surface *surface,
316 struct wl_array *keys)
321 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
322 uint32_t serial, struct wl_surface *surface)
327 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
328 uint32_t serial, uint32_t time, uint32_t key,
334 keyboard_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,
341 static const struct wl_keyboard_listener keyboard_listener = {
342 keyboard_handle_keymap,
343 keyboard_handle_enter,
344 keyboard_handle_leave,
346 keyboard_handle_modifiers,
350 start_device(struct display *display, struct device *device)
352 if (display->seat == NULL)
355 switch (device->type) {
357 if (device->p.keyboard == NULL) {
359 wl_seat_get_keyboard(display->seat);
360 wl_keyboard_add_listener(device->p.keyboard,
366 if (device->p.pointer == NULL) {
368 wl_seat_get_pointer(display->seat);
369 wl_pointer_add_listener(device->p.pointer,
378 destroy_device(struct device *device)
380 switch (device->type) {
382 if (device->p.keyboard)
383 wl_keyboard_release(device->p.keyboard);
386 if (device->p.pointer)
387 wl_pointer_release(device->p.pointer);
391 wl_list_remove(&device->link);
396 destroy_devices(struct display *display)
398 struct device *device, *tmp;
400 wl_list_for_each_safe(device, tmp, &display->devices, link)
401 destroy_device(device);
405 destroy_display(struct display *display)
407 destroy_devices(display);
410 wl_shm_destroy(display->shm);
413 wl_shell_destroy(display->shell);
416 wl_seat_destroy(display->seat);
418 if (display->compositor)
419 wl_compositor_destroy(display->compositor);
421 wl_registry_destroy(display->registry);
422 wl_display_flush(display->display);
423 wl_display_disconnect(display->display);
427 static int running = 1;
430 signal_int(int signum)
436 create_device(struct display *display, const char *time_desc, int type)
441 struct device *device;
443 if (time_desc == NULL) {
444 fprintf(stderr, "missing time description\n");
449 start_time = strtoul(time_desc, &tail, 10);
454 end_time = strtoul(tail + 1, &tail, 10);
455 if (errno || *tail != '\0')
457 } else if (*tail != '\0') {
461 device = xzalloc(sizeof *device);
463 device->start_time = start_time;
464 device->end_time = end_time;
465 wl_list_insert(&display->devices, &device->link);
470 fprintf(stderr, "invalid time description\n");
474 static struct timespec begin_time;
479 clock_gettime(CLOCK_MONOTONIC, &begin_time);
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);
493 main_loop(struct display *display)
498 struct device *device, *tmp;
499 struct pollfd fds[1];
500 double sleep_time = DBL_MAX;
503 if (wl_display_dispatch_pending(display->display) == -1)
505 if (wl_display_flush(display->display) == -1)
510 wl_list_for_each(device, &display->devices, link) {
511 double next_time = device->start_time - now;
512 if (next_time < 0.0) {
515 } else if (next_time < sleep_time) {
516 sleep_time = next_time;
518 next_time = device->end_time - now;
519 if (next_time < 0.0) {
522 } else if (next_time < sleep_time) {
523 sleep_time = next_time;
527 fds[0].fd = wl_display_get_fd(display->display);
528 fds[0].events = POLLIN;
532 sizeof fds / sizeof fds[0],
533 sleep_time == DBL_MAX ? -1 : ceil(sleep_time * 1000.0));
535 if (fds[0].revents &&
536 wl_display_dispatch(display->display) == -1)
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);
551 main(int argc, char **argv)
553 struct sigaction sigint;
554 struct display *display;
555 struct window *window;
558 display = create_display();
559 window = create_window(display, 250, 250);
561 for (i = 1; i < argc; i++) {
562 if (!strncmp(argv[i], "-p", 2)) {
570 if (create_device(display, arg, POINTER) == -1)
572 } else if (!strncmp(argv[i], "-k", 2)) {
580 if (create_device(display, arg, KEYBOARD) == -1)
583 fprintf(stderr, "unknown argument %s\n", argv[i]);
588 sigint.sa_handler = signal_int;
589 sigemptyset(&sigint.sa_mask);
590 sigint.sa_flags = SA_RESETHAND;
591 sigaction(SIGINT, &sigint, NULL);
595 fprintf(stderr, "multi-resource exiting\n");
596 destroy_window(window);
597 destroy_display(display);