Introduce 'shell' object for moving and resizing surfaces in the compositor
[platform/upstream/weston.git] / wayland-client.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <ctype.h>
33 #include <assert.h>
34 #include <sys/poll.h>
35
36 #include "wayland-client-protocol.h"
37 #include "connection.h"
38 #include "wayland-util.h"
39 #include "wayland-client.h"
40
41 static const char socket_name[] = "\0wayland";
42
43 struct wl_global {
44         uint32_t id;
45         char *interface;
46         uint32_t version;
47         struct wl_proxy *proxy;
48         struct wl_list link;
49 };
50
51 struct wl_global_listener {
52         wl_display_global_func_t handler;
53         void *data;
54         struct wl_list link;
55 };
56
57 struct wl_listener {
58         void (**implementation)(void);
59         void *data;
60         struct wl_list link;
61 };
62
63 struct wl_proxy {
64         struct wl_object base;
65         struct wl_display *display;
66         struct wl_list listener_list;
67         void *user_data;
68 };
69
70 struct wl_compositor {
71         struct wl_proxy proxy;
72 };
73
74 struct wl_surface {
75         struct wl_proxy proxy;
76 };
77
78 struct wl_visual {
79         struct wl_proxy proxy;
80 };
81
82 struct wl_output {
83         struct wl_proxy proxy;
84         struct wl_listener listener;
85         int32_t width, height;
86 };
87
88 struct wl_shell {
89         struct wl_proxy proxy;
90 };
91
92 struct wl_input_device {
93         struct wl_proxy proxy;
94 };
95
96 struct wl_display {
97         struct wl_proxy proxy;
98         struct wl_connection *connection;
99         int fd;
100         uint32_t id, id_count, next_range;
101         uint32_t mask;
102         struct wl_hash_table *objects;
103         struct wl_list global_list;
104         struct wl_listener listener;
105         struct wl_list global_listener_list;
106
107         struct wl_visual *argb_visual;
108         struct wl_visual *premultiplied_argb_visual;
109         struct wl_visual *rgb_visual;
110
111         wl_display_update_func_t update;
112         void *update_data;
113
114         wl_display_global_func_t global_handler;
115         void *global_handler_data;
116
117         struct wl_compositor *compositor;
118 };
119
120 static int
121 connection_update(struct wl_connection *connection,
122                   uint32_t mask, void *data)
123 {
124         struct wl_display *display = data;
125
126         display->mask = mask;
127         if (display->update)
128                 return display->update(display->mask,
129                                        display->update_data);
130
131         return 0;
132 }
133
134 WL_EXPORT int
135 wl_object_implements(struct wl_object *object,
136                      const char *interface, int version)
137 {
138         return strcmp(object->interface->name, interface) == 0 &&
139                 object->interface->version >= version;
140 }
141
142 WL_EXPORT struct wl_global_listener *
143 wl_display_add_global_listener(struct wl_display *display,
144                                wl_display_global_func_t handler, void *data)
145 {
146         struct wl_global_listener *listener;
147         struct wl_global *global;
148
149         listener = malloc(sizeof *listener);
150         if (listener == NULL)
151                 return NULL;
152
153         listener->handler = handler;
154         listener->data = data;
155         wl_list_insert(display->global_listener_list.prev, &listener->link);
156
157         /* FIXME: Need a destructor for void *data? */
158
159         wl_list_for_each(global, &display->global_list, link)
160                 if (global->proxy != NULL)
161                         (*handler)(display, &global->proxy->base, data);
162
163         return listener;
164 }
165
166 WL_EXPORT void
167 wl_display_remove_global_listener(struct wl_display *display,
168                                   struct wl_global_listener *listener)
169 {
170         wl_list_remove(&listener->link);
171         free(listener);
172 }
173
174 static struct wl_proxy *
175 wl_proxy_create_for_global(struct wl_display *display,
176                            struct wl_global *global,
177                            const struct wl_interface *interface)
178 {
179         struct wl_proxy *proxy;
180         struct wl_global_listener *listener;
181
182         proxy = malloc(sizeof *proxy);
183         if (proxy == NULL)
184                 return NULL;
185
186         proxy->base.interface = interface;
187         proxy->base.id = global->id;
188         proxy->display = display;
189         global->proxy = proxy;
190         wl_list_init(&proxy->listener_list);
191         wl_hash_table_insert(display->objects, proxy->base.id, proxy);
192
193         wl_list_for_each(listener, &display->global_listener_list, link)
194                 (*listener->handler)(display, &proxy->base, listener->data);
195
196         return proxy;
197 }
198
199 static int
200 wl_proxy_add_listener(struct wl_proxy *proxy, void (**implementation)(void), void *data)
201 {
202         struct wl_listener *listener;
203
204         listener = malloc(sizeof *listener);
205         if (listener == NULL)
206                 return -1;
207
208         listener->implementation = (void (**)(void)) implementation;
209         listener->data = data;
210         wl_list_insert(proxy->listener_list.prev, &listener->link);
211
212         return 0;
213 }
214
215 static void
216 wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
217 {
218         va_list ap;
219
220         va_start(ap, opcode);
221         wl_connection_vmarshal(proxy->display->connection,
222                                &proxy->base, opcode, ap,
223                                &proxy->base.interface->methods[opcode]);
224         va_end(ap);
225 }
226
227 WL_EXPORT int
228 wl_output_add_listener(struct wl_output *output,
229                        const struct wl_output_listener *listener,
230                        void *data)
231 {
232         return wl_proxy_add_listener(&output->proxy,
233                                      (void (**)(void)) listener, data);
234 }
235
236 WL_EXPORT int
237 wl_shell_add_listener(struct wl_shell *shell,
238                       const struct wl_shell_listener *listener,
239                       void *data)
240 {
241         return wl_proxy_add_listener(&shell->proxy,
242                                      (void (**)(void)) listener, data);
243 }
244
245 WL_EXPORT void
246 wl_shell_move(struct wl_shell *shell,
247               struct wl_surface *surface, struct wl_input_device *device,
248               uint32_t time)
249 {
250         wl_proxy_marshal(&shell->proxy,
251                          WL_SHELL_MOVE, surface, device, time);
252 }
253
254 WL_EXPORT void
255 wl_shell_resize(struct wl_shell *shell,
256                 struct wl_surface *surface, struct wl_input_device *device,
257                 uint32_t time, uint32_t edges)
258 {
259         wl_proxy_marshal(&shell->proxy,
260                          WL_SHELL_RESIZE, surface, device, time, edges);
261 }
262
263 static void
264 add_visual(struct wl_display *display, struct wl_global *global)
265 {
266         struct wl_visual *visual;
267
268         visual = (struct wl_visual *)
269                 wl_proxy_create_for_global(display, global,
270                                            &wl_visual_interface);
271         if (display->argb_visual == NULL)
272                 display->argb_visual = visual;
273         else if (display->premultiplied_argb_visual == NULL)
274                 display->premultiplied_argb_visual = visual;
275         else
276                 display->rgb_visual = visual;
277 }
278
279 WL_EXPORT struct wl_visual *
280 wl_display_get_argb_visual(struct wl_display *display)
281 {
282         return display->argb_visual;
283 }
284
285 WL_EXPORT struct wl_visual *
286 wl_display_get_premultiplied_argb_visual(struct wl_display *display)
287 {
288         return display->premultiplied_argb_visual;
289 }
290
291 WL_EXPORT struct wl_visual *
292 wl_display_get_rgb_visual(struct wl_display *display)
293 {
294         return display->rgb_visual;
295 }
296
297 WL_EXPORT int
298 wl_input_device_add_listener(struct wl_input_device *input_device,
299                              const struct wl_input_device_listener *listener,
300                              void *data)
301 {
302         return wl_proxy_add_listener(&input_device->proxy,
303                                      (void (**)(void)) listener, data);
304 }
305
306 static void
307 display_handle_invalid_object(void *data,
308                               struct wl_display *display, uint32_t id)
309 {
310         fprintf(stderr, "sent request to invalid object\n");
311 }
312                               
313 static void
314 display_handle_invalid_method(void *data, 
315                               struct wl_display *display,
316                               uint32_t id, uint32_t opcode)
317 {
318         fprintf(stderr, "sent invalid request opcode\n");
319 }
320
321 static void
322 display_handle_no_memory(void *data,
323                          struct wl_display *display)
324 {
325         fprintf(stderr, "server out of memory\n");
326 }
327
328 static void
329 display_handle_global(void *data,
330                       struct wl_display *display,
331                       uint32_t id, const char *interface, uint32_t version)
332 {
333         struct wl_global *global;
334
335         global = malloc(sizeof *global);
336         if (global == NULL)
337                 return;
338
339         global->id = id;
340         global->interface = strdup(interface);
341         global->version = version;
342         wl_list_insert(display->global_list.prev, &global->link);
343
344         if (strcmp(global->interface, "display") == 0)
345                 wl_hash_table_insert(display->objects,
346                                      id, &display->proxy.base);
347         else if (strcmp(global->interface, "compositor") == 0)
348                 display->compositor = (struct wl_compositor *) 
349                         wl_proxy_create_for_global(display, global,
350                                                    &wl_compositor_interface);
351         else if (strcmp(global->interface, "visual") == 0)
352                 add_visual(display, global);
353         else if (strcmp(global->interface, "output") == 0)
354                 wl_proxy_create_for_global(display, global,
355                                            &wl_output_interface);
356         else if (strcmp(global->interface, "input_device") == 0)
357                 wl_proxy_create_for_global(display, global,
358                                            &wl_input_device_interface);
359         else if (strcmp(global->interface, "shell") == 0)
360                 wl_proxy_create_for_global(display, global,
361                                            &wl_shell_interface);
362 }
363
364 static void
365 display_handle_range(void *data,
366                      struct wl_display *display, uint32_t range)
367 {
368         display->next_range = range;
369 }
370
371 static const struct wl_display_listener display_listener = {
372         display_handle_invalid_object,
373         display_handle_invalid_method,
374         display_handle_no_memory,
375         display_handle_global,
376         display_handle_range
377 };
378
379 WL_EXPORT struct wl_display *
380 wl_display_create(const char *name, size_t name_size)
381 {
382         struct wl_display *display;
383         struct sockaddr_un addr;
384         socklen_t size;
385
386         display = malloc(sizeof *display);
387         if (display == NULL)
388                 return NULL;
389
390         memset(display, 0, sizeof *display);
391         display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
392         if (display->fd < 0) {
393                 free(display);
394                 return NULL;
395         }
396
397         addr.sun_family = AF_LOCAL;
398         memcpy(addr.sun_path, name, name_size);
399
400         size = offsetof (struct sockaddr_un, sun_path) + name_size;
401
402         if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
403                 close(display->fd);
404                 free(display);
405                 return NULL;
406         }
407
408         display->objects = wl_hash_table_create();
409         wl_list_init(&display->global_list);
410         wl_list_init(&display->global_listener_list);
411
412         display->proxy.base.interface = &wl_display_interface;
413         display->proxy.base.id = 1;
414         display->proxy.display = display;
415         wl_list_init(&display->proxy.listener_list);
416
417         display->listener.implementation = (void(**)(void)) &display_listener;
418         wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
419
420         display->connection = wl_connection_create(display->fd,
421                                                    connection_update,
422                                                    display);
423
424         return display;
425 }
426
427 WL_EXPORT void
428 wl_display_destroy(struct wl_display *display)
429 {
430         wl_connection_destroy(display->connection);
431         close(display->fd);
432         free(display);
433 }
434
435 WL_EXPORT uint32_t
436 wl_display_get_object_id(struct wl_display *display,
437                          const char *interface, uint32_t version)
438 {
439         struct wl_global *global;
440
441         wl_list_for_each(global, &display->global_list, link)
442                 if (strcmp(global->interface, interface) == 0 &&
443                     global->version >= version)
444                         return global->id;
445
446         return 0;
447 }
448
449 WL_EXPORT int
450 wl_display_get_fd(struct wl_display *display,
451                   wl_display_update_func_t update, void *data)
452 {
453         display->update = update;
454         display->update_data = data;
455
456         display->update(display->mask, display->update_data);
457
458         return display->fd;
459 }
460
461 static void
462 handle_event(struct wl_display *display,
463              uint32_t id, uint32_t opcode, uint32_t size)
464 {
465         uint32_t p[32];
466         struct wl_listener *listener;
467         struct wl_proxy *proxy;
468
469         wl_connection_copy(display->connection, p, size);
470         if (id == 1)
471                 proxy = &display->proxy;
472         else
473                 proxy = (struct wl_proxy *)
474                         wl_hash_table_lookup(display->objects, id);
475
476         if (proxy != NULL) {
477                 if (wl_list_empty(&proxy->listener_list)) {
478                         printf("proxy found for object %d, opcode %d, but no listeners\n",
479                                id, opcode);
480                 }
481
482                 wl_list_for_each(listener, &proxy->listener_list, link)
483                         wl_connection_demarshal(display->connection,
484                                                 size,
485                                                 display->objects,
486                                                 listener->implementation[opcode],
487                                                 listener->data,
488                                                 &proxy->base, 
489                                                 &proxy->base.interface->events[opcode]);
490
491         }
492
493         wl_connection_consume(display->connection, size);
494 }
495
496 WL_EXPORT void
497 wl_display_iterate(struct wl_display *display, uint32_t mask)
498 {
499         uint32_t p[2], object, opcode, size;
500         int len;
501
502         len = wl_connection_data(display->connection, mask);
503         while (len > 0) {
504                 if (len < sizeof p)
505                         break;
506                 
507                 wl_connection_copy(display->connection, p, sizeof p);
508                 object = p[0];
509                 opcode = p[1] & 0xffff;
510                 size = p[1] >> 16;
511                 if (len < size)
512                         break;
513
514                 handle_event(display, object, opcode, size);
515                 len -= size;
516         }
517
518         if (len < 0) {
519                 fprintf(stderr, "read error: %m\n");
520                 exit(EXIT_FAILURE);
521         }
522 }
523
524 WL_EXPORT uint32_t
525 wl_display_allocate_id(struct wl_display *display)
526 {
527         if (display->id_count == 0) {
528                 display->id_count = 256;
529                 display->id = display->next_range;
530         }
531
532         display->id_count--;
533
534         return display->id++;
535 }
536
537 WL_EXPORT void
538 wl_display_write(struct wl_display *display, const void *data, size_t count)
539 {
540         wl_connection_write(display->connection, data, count);
541 }
542
543 WL_EXPORT struct wl_compositor *
544 wl_display_get_compositor(struct wl_display *display)
545 {
546         return display->compositor;
547 }
548
549 WL_EXPORT int
550 wl_compositor_add_listener(struct wl_compositor *compositor,
551                            const struct wl_compositor_listener *listener,
552                            void *data)
553 {
554         return wl_proxy_add_listener(&compositor->proxy,
555                                      (void (**)(void)) listener, data);
556 }
557
558 WL_EXPORT struct wl_surface *
559 wl_compositor_create_surface(struct wl_compositor *compositor)
560 {
561         struct wl_surface *surface;
562
563         surface = malloc(sizeof *surface);
564         if (surface == NULL)
565                 return NULL;
566
567         surface->proxy.base.interface = &wl_surface_interface;
568         surface->proxy.base.id = wl_display_allocate_id(compositor->proxy.display);
569         surface->proxy.display = compositor->proxy.display;
570         wl_hash_table_insert(compositor->proxy.display->objects,
571                              surface->proxy.base.id, surface);
572         wl_proxy_marshal(&compositor->proxy,
573                           WL_COMPOSITOR_CREATE_SURFACE, surface);
574
575         return surface;
576 }
577
578 WL_EXPORT void
579 wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
580 {
581         wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
582 }
583
584 WL_EXPORT void
585 wl_surface_destroy(struct wl_surface *surface)
586 {
587         wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
588 }
589
590 WL_EXPORT void
591 wl_surface_attach(struct wl_surface *surface, uint32_t name,
592                   int32_t width, int32_t height, uint32_t stride,
593                   struct wl_visual *visual)
594 {
595         wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
596                          name, width, height, stride, visual);
597 }
598
599 WL_EXPORT void
600 wl_surface_map(struct wl_surface *surface,
601                int32_t x, int32_t y, int32_t width, int32_t height)
602 {
603         wl_proxy_marshal(&surface->proxy,
604                          WL_SURFACE_MAP, x, y, width, height);
605 }
606
607 WL_EXPORT void
608 wl_surface_damage(struct wl_surface *surface,
609                   int32_t x, int32_t y, int32_t width, int32_t height)
610 {
611         wl_proxy_marshal(&surface->proxy,
612                          WL_SURFACE_DAMAGE, x, y, width, height);
613 }
614
615 WL_EXPORT void
616 wl_surface_set_user_data(struct wl_surface *surface, void *user_data)
617 {
618         surface->proxy.user_data = user_data;
619 }
620
621 WL_EXPORT void *
622 wl_surface_get_user_data(struct wl_surface *surface)
623 {
624         return surface->proxy.user_data;
625 }