71b4fee2a4557797f1917f9b1783f73e3064e5d3
[profile/ivi/wayland.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_listener {
44         wl_display_global_func_t handler;
45         void *data;
46         struct wl_list link;
47 };
48
49 struct wl_listener {
50         void (**implementation)(void);
51         void *data;
52         struct wl_list link;
53 };
54
55 struct wl_proxy {
56         struct wl_object base;
57         struct wl_display *display;
58         struct wl_list listener_list;
59         void *user_data;
60 };
61
62 struct wl_display {
63         struct wl_proxy proxy;
64         struct wl_connection *connection;
65         int fd;
66         uint32_t id, id_count, next_range;
67         uint32_t mask;
68         struct wl_hash_table *objects;
69         struct wl_listener listener;
70         struct wl_list global_listener_list;
71
72         struct wl_visual *argb_visual;
73         struct wl_visual *premultiplied_argb_visual;
74         struct wl_visual *rgb_visual;
75
76         wl_display_update_func_t update;
77         void *update_data;
78
79         wl_display_global_func_t global_handler;
80         void *global_handler_data;
81 };
82
83 static int
84 connection_update(struct wl_connection *connection,
85                   uint32_t mask, void *data)
86 {
87         struct wl_display *display = data;
88
89         display->mask = mask;
90         if (display->update)
91                 return display->update(display->mask,
92                                        display->update_data);
93
94         return 0;
95 }
96
97 WL_EXPORT struct wl_global_listener *
98 wl_display_add_global_listener(struct wl_display *display,
99                                wl_display_global_func_t handler, void *data)
100 {
101         struct wl_global_listener *listener;
102
103         listener = malloc(sizeof *listener);
104         if (listener == NULL)
105                 return NULL;
106
107         listener->handler = handler;
108         listener->data = data;
109         wl_list_insert(display->global_listener_list.prev, &listener->link);
110
111         return listener;
112 }
113
114 WL_EXPORT void
115 wl_display_remove_global_listener(struct wl_display *display,
116                                   struct wl_global_listener *listener)
117 {
118         wl_list_remove(&listener->link);
119         free(listener);
120 }
121
122 WL_EXPORT struct wl_proxy *
123 wl_proxy_create_for_id(struct wl_display *display,
124                        const struct wl_interface *interface, uint32_t id)
125 {
126         struct wl_proxy *proxy;
127
128         proxy = malloc(sizeof *proxy);
129         if (proxy == NULL)
130                 return NULL;
131
132         proxy->base.interface = interface;
133         proxy->base.id = id;
134         proxy->display = display;
135         wl_list_init(&proxy->listener_list);
136         wl_hash_table_insert(display->objects, proxy->base.id, proxy);
137
138         return proxy;
139 }
140
141 WL_EXPORT struct wl_proxy *
142 wl_proxy_create(struct wl_proxy *factory,
143                 const struct wl_interface *interface)
144 {
145         return wl_proxy_create_for_id(factory->display, interface,
146                                       wl_display_allocate_id(factory->display));
147 }
148
149 WL_EXPORT int
150 wl_proxy_add_listener(struct wl_proxy *proxy,
151                       void (**implementation)(void), void *data)
152 {
153         struct wl_listener *listener;
154
155         listener = malloc(sizeof *listener);
156         if (listener == NULL)
157                 return -1;
158
159         listener->implementation = (void (**)(void)) implementation;
160         listener->data = data;
161         wl_list_insert(proxy->listener_list.prev, &listener->link);
162
163         return 0;
164 }
165
166 WL_EXPORT void
167 wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
168 {
169         va_list ap;
170
171         va_start(ap, opcode);
172         wl_connection_vmarshal(proxy->display->connection,
173                                &proxy->base, opcode, ap,
174                                &proxy->base.interface->methods[opcode]);
175         va_end(ap);
176 }
177
178 static void
179 add_visual(struct wl_display *display, uint32_t id)
180 {
181         struct wl_visual *visual;
182
183         visual = (struct wl_visual *)
184                 wl_proxy_create_for_id(display, &wl_visual_interface, id);
185         if (display->argb_visual == NULL)
186                 display->argb_visual = visual;
187         else if (display->premultiplied_argb_visual == NULL)
188                 display->premultiplied_argb_visual = visual;
189         else
190                 display->rgb_visual = visual;
191 }
192
193 WL_EXPORT struct wl_visual *
194 wl_display_get_argb_visual(struct wl_display *display)
195 {
196         return display->argb_visual;
197 }
198
199 WL_EXPORT struct wl_visual *
200 wl_display_get_premultiplied_argb_visual(struct wl_display *display)
201 {
202         return display->premultiplied_argb_visual;
203 }
204
205 WL_EXPORT struct wl_visual *
206 wl_display_get_rgb_visual(struct wl_display *display)
207 {
208         return display->rgb_visual;
209 }
210
211 static void
212 display_handle_invalid_object(void *data,
213                               struct wl_display *display, uint32_t id)
214 {
215         fprintf(stderr, "sent request to invalid object\n");
216         abort();
217 }
218                               
219 static void
220 display_handle_invalid_method(void *data, 
221                               struct wl_display *display,
222                               uint32_t id, uint32_t opcode)
223 {
224         fprintf(stderr, "sent invalid request opcode\n");
225         abort();
226 }
227
228 static void
229 display_handle_no_memory(void *data,
230                          struct wl_display *display)
231 {
232         fprintf(stderr, "server out of memory\n");
233         abort();
234 }
235
236 static void
237 display_handle_global(void *data,
238                       struct wl_display *display,
239                       uint32_t id, const char *interface, uint32_t version)
240 {
241         struct wl_global_listener *listener;
242
243         if (strcmp(interface, "display") == 0)
244                 wl_hash_table_insert(display->objects,
245                                      id, &display->proxy.base);
246         else if (strcmp(interface, "visual") == 0)
247                 add_visual(display, id);
248
249         wl_list_for_each(listener, &display->global_listener_list, link)
250                 (*listener->handler)(display,
251                                      id, interface, version, listener->data);
252 }
253
254 static void
255 display_handle_range(void *data,
256                      struct wl_display *display, uint32_t range)
257 {
258         display->next_range = range;
259 }
260
261 static const struct wl_display_listener display_listener = {
262         display_handle_invalid_object,
263         display_handle_invalid_method,
264         display_handle_no_memory,
265         display_handle_global,
266         display_handle_range
267 };
268
269 WL_EXPORT struct wl_display *
270 wl_display_create(const char *name, size_t name_size)
271 {
272         struct wl_display *display;
273         struct sockaddr_un addr;
274         socklen_t size;
275
276         display = malloc(sizeof *display);
277         if (display == NULL)
278                 return NULL;
279
280         memset(display, 0, sizeof *display);
281         display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
282         if (display->fd < 0) {
283                 free(display);
284                 return NULL;
285         }
286
287         addr.sun_family = AF_LOCAL;
288         memcpy(addr.sun_path, name, name_size);
289
290         size = offsetof (struct sockaddr_un, sun_path) + name_size;
291
292         if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
293                 close(display->fd);
294                 free(display);
295                 return NULL;
296         }
297
298         display->objects = wl_hash_table_create();
299         wl_list_init(&display->global_listener_list);
300
301         display->proxy.base.interface = &wl_display_interface;
302         display->proxy.base.id = 1;
303         display->proxy.display = display;
304         wl_list_init(&display->proxy.listener_list);
305
306         display->listener.implementation = (void(**)(void)) &display_listener;
307         wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
308
309         display->connection = wl_connection_create(display->fd,
310                                                    connection_update,
311                                                    display);
312
313         return display;
314 }
315
316 WL_EXPORT void
317 wl_display_destroy(struct wl_display *display)
318 {
319         wl_connection_destroy(display->connection);
320         close(display->fd);
321         free(display);
322 }
323
324 WL_EXPORT int
325 wl_display_get_fd(struct wl_display *display,
326                   wl_display_update_func_t update, void *data)
327 {
328         display->update = update;
329         display->update_data = data;
330
331         display->update(display->mask, display->update_data);
332
333         return display->fd;
334 }
335
336 static void
337 handle_event(struct wl_display *display,
338              uint32_t id, uint32_t opcode, uint32_t size)
339 {
340         uint32_t p[32];
341         struct wl_listener *listener;
342         struct wl_proxy *proxy;
343
344         wl_connection_copy(display->connection, p, size);
345         if (id == 1)
346                 proxy = &display->proxy;
347         else
348                 proxy = (struct wl_proxy *)
349                         wl_hash_table_lookup(display->objects, id);
350
351         if (proxy != NULL) {
352                 if (wl_list_empty(&proxy->listener_list)) {
353                         printf("proxy found for object %d, opcode %d, but no listeners\n",
354                                id, opcode);
355                 }
356
357                 wl_list_for_each(listener, &proxy->listener_list, link)
358                         wl_connection_demarshal(display->connection,
359                                                 size,
360                                                 display->objects,
361                                                 listener->implementation[opcode],
362                                                 listener->data,
363                                                 &proxy->base, 
364                                                 &proxy->base.interface->events[opcode]);
365
366         }
367
368         wl_connection_consume(display->connection, size);
369 }
370
371 WL_EXPORT void
372 wl_display_iterate(struct wl_display *display, uint32_t mask)
373 {
374         uint32_t p[2], object, opcode, size;
375         int len;
376
377         len = wl_connection_data(display->connection, mask);
378         while (len > 0) {
379                 if (len < sizeof p)
380                         break;
381                 
382                 wl_connection_copy(display->connection, p, sizeof p);
383                 object = p[0];
384                 opcode = p[1] & 0xffff;
385                 size = p[1] >> 16;
386                 if (len < size)
387                         break;
388
389                 handle_event(display, object, opcode, size);
390                 len -= size;
391         }
392
393         if (len < 0) {
394                 fprintf(stderr, "read error: %m\n");
395                 exit(EXIT_FAILURE);
396         }
397 }
398
399 WL_EXPORT uint32_t
400 wl_display_allocate_id(struct wl_display *display)
401 {
402         if (display->id_count == 0) {
403                 display->id_count = 256;
404                 display->id = display->next_range;
405         }
406
407         display->id_count--;
408
409         return display->id++;
410 }
411
412 WL_EXPORT void
413 wl_surface_set_user_data(struct wl_surface *surface, void *user_data)
414 {
415         struct wl_proxy *proxy = (struct wl_proxy *) surface;
416
417         proxy->user_data = user_data;
418 }
419
420 WL_EXPORT void *
421 wl_surface_get_user_data(struct wl_surface *surface)
422 {
423         struct wl_proxy *proxy = (struct wl_proxy *) surface;
424
425         return proxy->user_data;
426 }