1 /*
2  * Copyright © 2011 Benjamin Franzke
3  * Copyright © 2010, 2013 Intel Corporation
4  *
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:
11  *
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.
23  */
24 
25 #include "config.h"
26 
27 #include <stdint.h>
28 #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 <poll.h>
39 #include <float.h>
40 #include <math.h>
41 
42 #include <wayland-client.h>
43 #include "shared/os-compatibility.h"
44 #include "shared/xalloc.h"
45 #include <libweston/zalloc.h>
46 
47 struct 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 
60 struct 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 
71 struct window {
72 	struct display *display;
73 	int width, height;
74 	struct wl_surface *surface;
75 	struct wl_shell_surface *shell_surface;
76 };
77 
78 static void
buffer_release(void * data,struct wl_buffer * buffer)79 buffer_release(void *data, struct wl_buffer *buffer)
80 {
81 	wl_buffer_destroy(buffer);
82 }
83 
84 static const struct wl_buffer_listener buffer_listener = {
85 	buffer_release
86 };
87 
88 static int
attach_buffer(struct window * window,int width,int height)89 attach_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: %s\n",
101 			size, strerror(errno));
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 
118 static void
handle_ping(void * data,struct wl_shell_surface * shell_surface,uint32_t serial)119 handle_ping(void *data, struct wl_shell_surface *shell_surface,
120 							uint32_t serial)
121 {
122 	wl_shell_surface_pong(shell_surface, serial);
123 }
124 
125 static void
handle_configure(void * data,struct wl_shell_surface * shell_surface,uint32_t edges,int32_t width,int32_t height)126 handle_configure(void *data, struct wl_shell_surface *shell_surface,
127 		 uint32_t edges, int32_t width, int32_t height)
128 {
129 }
130 
131 static void
handle_popup_done(void * data,struct wl_shell_surface * shell_surface)132 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
133 {
134 }
135 
136 static const struct wl_shell_surface_listener shell_surface_listener = {
137 	handle_ping,
138 	handle_configure,
139 	handle_popup_done
140 };
141 
142 static struct window *
create_window(struct display * display,int width,int height)143 create_window(struct display *display, int width, int height)
144 {
145 	struct window *window;
146 
147 	window = xzalloc(sizeof *window);
148 	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 
170 static void
destroy_window(struct window * window)171 destroy_window(struct window *window)
172 {
173 	wl_shell_surface_destroy(window->shell_surface);
174 	wl_surface_destroy(window->surface);
175 	free(window);
176 }
177 
178 static void
shm_format(void * data,struct wl_shm * wl_shm,uint32_t format)179 shm_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 
186 struct wl_shm_listener shm_listener = {
187 	shm_format
188 };
189 
190 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)191 registry_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 
214 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)215 registry_handle_global_remove(void *data, struct wl_registry *registry,
216 			      uint32_t name)
217 {
218 }
219 
220 static const struct wl_registry_listener registry_listener = {
221 	registry_handle_global,
222 	registry_handle_global_remove
223 };
224 
225 static struct display *
create_display(void)226 create_display(void)
227 {
228 	struct display *display;
229 
230 	display = xzalloc(sizeof *display);
231 	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 
251 	wl_list_init(&display->devices);
252 
253 	return display;
254 }
255 
256 static void
pointer_handle_enter(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface,wl_fixed_t sx_w,wl_fixed_t sy_w)257 pointer_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 
263 static void
pointer_handle_leave(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface)264 pointer_handle_leave(void *data, struct wl_pointer *pointer,
265 		     uint32_t serial, struct wl_surface *surface)
266 {
267 }
268 
269 static void
pointer_handle_motion(void * data,struct wl_pointer * pointer,uint32_t time,wl_fixed_t sx_w,wl_fixed_t sy_w)270 pointer_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 
275 static void
pointer_handle_button(void * data,struct wl_pointer * pointer,uint32_t serial,uint32_t time,uint32_t button,uint32_t state_w)276 pointer_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 
281 static void
pointer_handle_axis(void * data,struct wl_pointer * pointer,uint32_t time,uint32_t axis,wl_fixed_t value)282 pointer_handle_axis(void *data, struct wl_pointer *pointer,
283 		    uint32_t time, uint32_t axis, wl_fixed_t value)
284 {
285 }
286 
287 static 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 
295 static void
keyboard_handle_keymap(void * data,struct wl_keyboard * keyboard,uint32_t format,int fd,uint32_t size)296 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
297 		       uint32_t format, int fd, uint32_t size)
298 {
299 	/* Just so we don’t leak the keymap fd */
300 	close(fd);
301 }
302 
303 static void
keyboard_handle_enter(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface,struct wl_array * keys)304 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
305 		      uint32_t serial, struct wl_surface *surface,
306 		      struct wl_array *keys)
307 {
308 }
309 
310 static void
keyboard_handle_leave(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface)311 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
312 		      uint32_t serial, struct wl_surface *surface)
313 {
314 }
315 
316 static void
keyboard_handle_key(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t state_w)317 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
318 		    uint32_t serial, uint32_t time, uint32_t key,
319 		    uint32_t state_w)
320 {
321 }
322 
323 static void
keyboard_handle_modifiers(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)324 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
325 			  uint32_t serial, uint32_t mods_depressed,
326 			  uint32_t mods_latched, uint32_t mods_locked,
327 			  uint32_t group)
328 {
329 }
330 
331 static const struct wl_keyboard_listener keyboard_listener = {
332 	keyboard_handle_keymap,
333 	keyboard_handle_enter,
334 	keyboard_handle_leave,
335 	keyboard_handle_key,
336 	keyboard_handle_modifiers,
337 };
338 
339 static void
start_device(struct display * display,struct device * device)340 start_device(struct display *display, struct device *device)
341 {
342 	if (display->seat == NULL)
343 		return;
344 
345 	switch (device->type) {
346 	case KEYBOARD:
347 		if (device->p.keyboard == NULL) {
348 			device->p.keyboard =
349 				wl_seat_get_keyboard(display->seat);
350 			wl_keyboard_add_listener(device->p.keyboard,
351 						 &keyboard_listener,
352 						 NULL);
353 		}
354 		break;
355 	case POINTER:
356 		if (device->p.pointer == NULL) {
357 			device->p.pointer =
358 				wl_seat_get_pointer(display->seat);
359 			wl_pointer_add_listener(device->p.pointer,
360 						&pointer_listener,
361 						NULL);
362 		}
363 		break;
364 	}
365 }
366 
367 static void
destroy_device(struct device * device)368 destroy_device(struct device *device)
369 {
370 	switch (device->type) {
371 	case KEYBOARD:
372 		if (device->p.keyboard)
373 			wl_keyboard_release(device->p.keyboard);
374 		break;
375 	case POINTER:
376 		if (device->p.pointer)
377 			wl_pointer_release(device->p.pointer);
378 		break;
379 	}
380 
381 	wl_list_remove(&device->link);
382 	free(device);
383 }
384 
385 static void
destroy_devices(struct display * display)386 destroy_devices(struct display *display)
387 {
388 	struct device *device, *tmp;
389 
390 	wl_list_for_each_safe(device, tmp, &display->devices, link)
391 	destroy_device(device);
392 }
393 
394 static void
destroy_display(struct display * display)395 destroy_display(struct display *display)
396 {
397 	destroy_devices(display);
398 
399 	if (display->shm)
400 		wl_shm_destroy(display->shm);
401 
402 	if (display->shell)
403 		wl_shell_destroy(display->shell);
404 
405 	if (display->seat)
406 		wl_seat_destroy(display->seat);
407 
408 	if (display->compositor)
409 		wl_compositor_destroy(display->compositor);
410 
411 	wl_registry_destroy(display->registry);
412 	wl_display_flush(display->display);
413 	wl_display_disconnect(display->display);
414 	free(display);
415 }
416 
417 static int running = 1;
418 
419 static void
signal_int(int signum)420 signal_int(int signum)
421 {
422 	running = 0;
423 }
424 
425 static int
create_device(struct display * display,const char * time_desc,int type)426 create_device(struct display *display, const char *time_desc, int type)
427 {
428 	int start_time;
429 	int end_time = -1;
430 	char *tail;
431 	struct device *device;
432 
433 	if (time_desc == NULL) {
434 		fprintf(stderr, "missing time description\n");
435 		return -1;
436 	}
437 
438 	errno = 0;
439 	start_time = strtoul(time_desc, &tail, 10);
440 	if (errno || tail == time_desc)
441 		goto error;
442 
443 	if (*tail == ':') {
444 		time_desc = tail + 1;
445 		end_time = strtoul(time_desc, &tail, 10);
446 		if (errno || tail == time_desc || *tail != '\0')
447 			goto error;
448 	} else if (*tail != '\0') {
449 		goto error;
450 	}
451 
452 	device = xzalloc(sizeof *device);
453 	device->type = type;
454 	device->start_time = start_time;
455 	device->end_time = end_time;
456 	wl_list_insert(&display->devices, &device->link);
457 
458 	return 0;
459 
460 error:
461 	fprintf(stderr, "invalid time description\n");
462 	return -1;
463 }
464 
465 static struct timespec begin_time;
466 
467 static void
reset_timer(void)468 reset_timer(void)
469 {
470 	clock_gettime(CLOCK_MONOTONIC, &begin_time);
471 }
472 
473 static double
read_timer(void)474 read_timer(void)
475 {
476 	struct timespec t;
477 
478 	clock_gettime(CLOCK_MONOTONIC, &t);
479 	return (double)(t.tv_sec - begin_time.tv_sec) +
480 	       1e-9 * (t.tv_nsec - begin_time.tv_nsec);
481 }
482 
483 static void
main_loop(struct display * display)484 main_loop(struct display *display)
485 {
486 	reset_timer();
487 
488 	while (running) {
489 		struct device *device, *tmp;
490 		struct pollfd fds[1];
491 		double sleep_time = DBL_MAX;
492 		double now;
493 
494 		if (wl_display_dispatch_pending(display->display) == -1)
495 			break;
496 		if (wl_display_flush(display->display) == -1)
497 			break;
498 
499 		now = read_timer();
500 
501 		wl_list_for_each(device, &display->devices, link) {
502 			double next_time = device->start_time - now;
503 			if (next_time < 0.0) {
504 				sleep_time = 0.0;
505 				break;
506 			} else if (next_time < sleep_time) {
507 				sleep_time = next_time;
508 			}
509 			next_time = device->end_time - now;
510 			if (next_time < 0.0) {
511 				sleep_time = 0.0;
512 				break;
513 			} else if (next_time < sleep_time) {
514 				sleep_time = next_time;
515 			}
516 		}
517 
518 		fds[0].fd = wl_display_get_fd(display->display);
519 		fds[0].events = POLLIN;
520 		fds[0].revents = 0;
521 
522 		poll(fds,
523 		     sizeof fds / sizeof fds[0],
524 		     sleep_time == DBL_MAX ? -1 : ceil(sleep_time * 1000.0));
525 
526 		if (fds[0].revents &&
527 		    wl_display_dispatch(display->display) == -1)
528 			break;
529 
530 		now = read_timer();
531 
532 		wl_list_for_each_safe(device, tmp, &display->devices, link) {
533 			if (device->start_time <= now)
534 				start_device(display, device);
535 			if (device->end_time >= 0 && device->end_time <= now)
536 				destroy_device(device);
537 		}
538 	}
539 }
540 
541 int
main(int argc,char ** argv)542 main(int argc, char **argv)
543 {
544 	struct sigaction sigint;
545 	struct display *display;
546 	struct window *window;
547 	int i;
548 
549 	display = create_display();
550 	window = create_window(display, 250, 250);
551 
552 	for (i = 1; i < argc; i++) {
553 		if (!strncmp(argv[i], "-p", 2)) {
554 			char *arg;
555 			if (argv[i][2]) {
556 				arg = argv[i] + 2;
557 			} else {
558 				arg = argv[i + 1];
559 				i++;
560 			}
561 			if (create_device(display, arg, POINTER) == -1)
562 				return 1;
563 		} else if (!strncmp(argv[i], "-k", 2)) {
564 			char *arg;
565 			if (argv[i][2]) {
566 				arg = argv[i] + 2;
567 			} else {
568 				arg = argv[i + 1];
569 				i++;
570 			}
571 			if (create_device(display, arg, KEYBOARD) == -1)
572 				return 1;
573 		} else {
574 			fprintf(stderr, "unknown argument %s\n", argv[i]);
575 			return 1;
576 		}
577 	}
578 
579 	sigint.sa_handler = signal_int;
580 	sigemptyset(&sigint.sa_mask);
581 	sigint.sa_flags = SA_RESETHAND;
582 	sigaction(SIGINT, &sigint, NULL);
583 
584 	main_loop(display);
585 
586 	fprintf(stderr, "multi-resource exiting\n");
587 	destroy_window(window);
588 	destroy_display(display);
589 
590 	return 0;
591 }
592