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