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