client: Don't forget to init and destroy mutex
[profile/ivi/wayland.git] / src / wayland-server.h
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 #ifndef WAYLAND_H
24 #define WAYLAND_H
25
26 #ifdef  __cplusplus
27 extern "C" {
28 #endif
29
30 #include <sys/types.h>
31 #include <stdint.h>
32 #include "wayland-util.h"
33 #include "wayland-version.h"
34
35 enum {
36         WL_EVENT_READABLE = 0x01,
37         WL_EVENT_WRITABLE = 0x02,
38         WL_EVENT_HANGUP   = 0x04,
39         WL_EVENT_ERROR    = 0x08
40 };
41
42 struct wl_event_loop;
43 struct wl_event_source;
44 typedef int (*wl_event_loop_fd_func_t)(int fd, uint32_t mask, void *data);
45 typedef int (*wl_event_loop_timer_func_t)(void *data);
46 typedef int (*wl_event_loop_signal_func_t)(int signal_number, void *data);
47 typedef void (*wl_event_loop_idle_func_t)(void *data);
48
49 struct wl_event_loop *wl_event_loop_create(void);
50 void wl_event_loop_destroy(struct wl_event_loop *loop);
51 struct wl_event_source *wl_event_loop_add_fd(struct wl_event_loop *loop,
52                                              int fd, uint32_t mask,
53                                              wl_event_loop_fd_func_t func,
54                                              void *data);
55 int wl_event_source_fd_update(struct wl_event_source *source, uint32_t mask);
56 struct wl_event_source *wl_event_loop_add_timer(struct wl_event_loop *loop,
57                                                 wl_event_loop_timer_func_t func,
58                                                 void *data);
59 struct wl_event_source *
60 wl_event_loop_add_signal(struct wl_event_loop *loop,
61                         int signal_number,
62                         wl_event_loop_signal_func_t func,
63                         void *data);
64
65 int wl_event_source_timer_update(struct wl_event_source *source,
66                                  int ms_delay);
67 int wl_event_source_remove(struct wl_event_source *source);
68 void wl_event_source_check(struct wl_event_source *source);
69
70
71 int wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout);
72 void wl_event_loop_dispatch_idle(struct wl_event_loop *loop);
73 struct wl_event_source *wl_event_loop_add_idle(struct wl_event_loop *loop,
74                                                wl_event_loop_idle_func_t func,
75                                                void *data);
76 int wl_event_loop_get_fd(struct wl_event_loop *loop);
77
78 struct wl_client;
79 struct wl_display;
80 struct wl_seat;
81 struct wl_pointer;
82 struct wl_keyboard;
83 struct wl_touch;
84 struct wl_listener;
85 typedef void (*wl_notify_func_t)(struct wl_listener *listener, void *data);
86
87 struct wl_display *wl_display_create(void);
88 void wl_display_destroy(struct wl_display *display);
89 struct wl_event_loop *wl_display_get_event_loop(struct wl_display *display);
90 int wl_display_add_socket(struct wl_display *display, const char *name);
91 void wl_display_terminate(struct wl_display *display);
92 void wl_display_run(struct wl_display *display);
93 void wl_display_flush_clients(struct wl_display *display);
94
95 typedef void (*wl_global_bind_func_t)(struct wl_client *client, void *data,
96                                       uint32_t version, uint32_t id);
97
98 struct wl_global *wl_display_add_global(struct wl_display *display,
99                                         const struct wl_interface *interface,
100                                         void *data,
101                                         wl_global_bind_func_t bind);
102
103 void wl_display_remove_global(struct wl_display *display,
104                               struct wl_global *global);
105
106 uint32_t wl_display_get_serial(struct wl_display *display);
107 uint32_t wl_display_next_serial(struct wl_display *display);
108
109 struct wl_client *wl_client_create(struct wl_display *display, int fd);
110 void wl_client_destroy(struct wl_client *client);
111 void wl_client_flush(struct wl_client *client);
112 void wl_client_get_credentials(struct wl_client *client,
113                                pid_t *pid, uid_t *uid, gid_t *gid);
114
115 void wl_client_add_destroy_listener(struct wl_client *client,
116                                     struct wl_listener *listener);
117 struct wl_listener *wl_client_get_destroy_listener(struct wl_client *client,
118                                                    wl_notify_func_t notify);
119
120 struct wl_resource *
121 wl_client_add_object(struct wl_client *client,
122                      const struct wl_interface *interface,
123                      const void *implementation, uint32_t id, void *data);
124 struct wl_resource *
125 wl_client_new_object(struct wl_client *client,
126                      const struct wl_interface *interface,
127                      const void *implementation, void *data);
128 struct wl_resource *
129 wl_client_get_object(struct wl_client *client, uint32_t id);
130
131 struct wl_listener {
132         struct wl_list link;
133         wl_notify_func_t notify;
134 };
135
136 struct wl_signal {
137         struct wl_list listener_list;
138 };
139
140 static inline void
141 wl_signal_init(struct wl_signal *signal)
142 {
143         wl_list_init(&signal->listener_list);
144 }
145
146 static inline void
147 wl_signal_add(struct wl_signal *signal, struct wl_listener *listener)
148 {
149         wl_list_insert(signal->listener_list.prev, &listener->link);
150 }
151
152 static inline struct wl_listener *
153 wl_signal_get(struct wl_signal *signal, void *notify)
154 {
155         struct wl_listener *l;
156
157         wl_list_for_each(l, &signal->listener_list, link)
158                 if (l->notify == notify)
159                         return l;
160
161         return NULL;
162 }
163
164 static inline void
165 wl_signal_emit(struct wl_signal *signal, void *data)
166 {
167         struct wl_listener *l, *next;
168
169         wl_list_for_each_safe(l, next, &signal->listener_list, link)
170                 l->notify(l, data);
171 }
172
173 struct wl_resource {
174         struct wl_object object;
175         void (*destroy)(struct wl_resource *resource);
176         struct wl_list link;
177         struct wl_signal destroy_signal;
178         struct wl_client *client;
179         void *data;
180 };
181
182 struct wl_buffer {
183         struct wl_resource resource;
184         int32_t width, height;
185         uint32_t busy_count;
186 };
187
188 struct wl_surface {
189         struct wl_resource resource;
190 };
191
192 struct wl_pointer_grab;
193 struct wl_pointer_grab_interface {
194         void (*focus)(struct wl_pointer_grab *grab,
195                       struct wl_surface *surface,
196                       wl_fixed_t x,
197                       wl_fixed_t y);
198         void (*motion)(struct wl_pointer_grab *grab,
199                        uint32_t time,
200                        wl_fixed_t x,
201                        wl_fixed_t y);
202         void (*button)(struct wl_pointer_grab *grab,
203                        uint32_t time, uint32_t button, uint32_t state);
204 };
205
206 struct wl_pointer_grab {
207         const struct wl_pointer_grab_interface *interface;
208         struct wl_pointer *pointer;
209         struct wl_surface *focus;
210         wl_fixed_t x, y;
211 };
212
213 struct wl_keyboard_grab;
214 struct wl_keyboard_grab_interface {
215         void (*key)(struct wl_keyboard_grab *grab, uint32_t time,
216                     uint32_t key, uint32_t state);
217         void (*modifiers)(struct wl_keyboard_grab *grab, uint32_t serial,
218                           uint32_t mods_depressed, uint32_t mods_latched,
219                           uint32_t mods_locked, uint32_t group);
220 };
221
222 struct wl_keyboard_grab {
223         const struct wl_keyboard_grab_interface *interface;
224         struct wl_keyboard *keyboard;
225         struct wl_surface *focus;
226         uint32_t key;
227 };
228
229 struct wl_data_offer {
230         struct wl_resource resource;
231         struct wl_data_source *source;
232         struct wl_listener source_destroy_listener;
233 };
234
235 struct wl_data_source {
236         struct wl_resource resource;
237         struct wl_array mime_types;
238
239         void (*accept)(struct wl_data_source *source,
240                        uint32_t serial, const char *mime_type);
241         void (*send)(struct wl_data_source *source,
242                      const char *mime_type, int32_t fd);
243         void (*cancel)(struct wl_data_source *source);
244 };
245
246 struct wl_pointer {
247         struct wl_seat *seat;
248
249         struct wl_list resource_list;
250         struct wl_surface *focus;
251         struct wl_resource *focus_resource;
252         struct wl_listener focus_listener;
253         uint32_t focus_serial;
254         struct wl_signal focus_signal;
255
256         struct wl_pointer_grab *grab;
257         struct wl_pointer_grab default_grab;
258         wl_fixed_t grab_x, grab_y;
259         uint32_t grab_button;
260         uint32_t grab_serial;
261         uint32_t grab_time;
262
263         wl_fixed_t x, y;
264         struct wl_surface *current;
265         wl_fixed_t current_x, current_y;
266
267         uint32_t button_count;
268 };
269
270 struct wl_keyboard {
271         struct wl_seat *seat;
272
273         struct wl_list resource_list;
274         struct wl_surface *focus;
275         struct wl_resource *focus_resource;
276         struct wl_listener focus_listener;
277         uint32_t focus_serial;
278         struct wl_signal focus_signal;
279
280         struct wl_keyboard_grab *grab;
281         struct wl_keyboard_grab default_grab;
282         uint32_t grab_key;
283         uint32_t grab_serial;
284         uint32_t grab_time;
285
286         struct wl_array keys;
287
288         struct {
289                 uint32_t mods_depressed;
290                 uint32_t mods_latched;
291                 uint32_t mods_locked;
292                 uint32_t group;
293         } modifiers;
294 };
295
296 struct wl_touch {
297         struct wl_seat *seat;
298
299         struct wl_list resource_list;
300         struct wl_surface *focus;
301         struct wl_resource *focus_resource;
302         struct wl_listener focus_listener;
303         uint32_t focus_serial;
304 };
305
306 struct wl_seat {
307         struct wl_list base_resource_list;
308         struct wl_signal destroy_signal;
309
310         struct wl_pointer *pointer;
311         struct wl_keyboard *keyboard;
312         struct wl_touch *touch;
313
314         uint32_t selection_serial;
315         struct wl_data_source *selection_data_source;
316         struct wl_listener selection_data_source_listener;
317         struct wl_signal selection_signal;
318
319         struct wl_list drag_resource_list;
320         struct wl_client *drag_client;
321         struct wl_data_source *drag_data_source;
322         struct wl_listener drag_data_source_listener;
323         struct wl_surface *drag_focus;
324         struct wl_resource *drag_focus_resource;
325         struct wl_listener drag_focus_listener;
326         struct wl_pointer_grab drag_grab;
327         struct wl_surface *drag_surface;
328         struct wl_listener drag_icon_listener;
329         struct wl_signal drag_icon_signal;
330 };
331
332 /*
333  * Post an event to the client's object referred to by 'resource'.
334  * 'opcode' is the event number generated from the protocol XML
335  * description (the event name). The variable arguments are the event
336  * parameters, in the order they appear in the protocol XML specification.
337  *
338  * The variable arguments' types are:
339  * - type=uint:         uint32_t
340  * - type=int:          int32_t
341  * - type=fixed:        wl_fixed_t
342  * - type=string:       (const char *) to a nil-terminated string
343  * - type=array:        (struct wl_array *)
344  * - type=fd:           int, that is an open file descriptor
345  * - type=new_id:       (struct wl_object *) or (struct wl_resource *)
346  * - type=object:       (struct wl_object *) or (struct wl_resource *)
347  */
348 void wl_resource_post_event(struct wl_resource *resource,
349                             uint32_t opcode, ...);
350 void wl_resource_queue_event(struct wl_resource *resource,
351                              uint32_t opcode, ...);
352
353 /* msg is a printf format string, variable args are its args. */
354 void wl_resource_post_error(struct wl_resource *resource,
355                             uint32_t code, const char *msg, ...)
356         __attribute__ ((format (printf, 3, 4)));
357 void wl_resource_post_no_memory(struct wl_resource *resource);
358
359 #include "wayland-server-protocol.h"
360
361 uint32_t
362 wl_client_add_resource(struct wl_client *client,
363                        struct wl_resource *resource);
364
365 struct wl_display *
366 wl_client_get_display(struct wl_client *client);
367
368 void
369 wl_resource_destroy(struct wl_resource *resource);
370
371 void
372 wl_seat_init(struct wl_seat *seat);
373
374 void
375 wl_seat_release(struct wl_seat *seat);
376
377 void
378 wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer);
379 void
380 wl_seat_set_keyboard(struct wl_seat *seat, struct wl_keyboard *keyboard);
381 void
382 wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch);
383
384 void
385 wl_pointer_init(struct wl_pointer *pointer);
386 void
387 wl_pointer_release(struct wl_pointer *pointer);
388 void
389 wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface,
390                      wl_fixed_t sx, wl_fixed_t sy);
391 void
392 wl_pointer_start_grab(struct wl_pointer *pointer,
393                       struct wl_pointer_grab *grab);
394 void
395 wl_pointer_end_grab(struct wl_pointer *pointer);
396
397 void
398 wl_keyboard_init(struct wl_keyboard *keyboard);
399 void
400 wl_keyboard_release(struct wl_keyboard *keyboard);
401 void
402 wl_keyboard_set_focus(struct wl_keyboard *keyboard, struct wl_surface *surface);
403 void
404 wl_keyboard_start_grab(struct wl_keyboard *device,
405                        struct wl_keyboard_grab *grab);
406 void
407 wl_keyboard_end_grab(struct wl_keyboard *keyboard);
408
409 void
410 wl_touch_init(struct wl_touch *touch);
411 void
412 wl_touch_release(struct wl_touch *touch);
413
414 void
415 wl_data_device_set_keyboard_focus(struct wl_seat *seat);
416
417 int
418 wl_data_device_manager_init(struct wl_display *display);
419
420
421 void
422 wl_seat_set_selection(struct wl_seat *seat,
423                       struct wl_data_source *source, uint32_t serial);
424
425
426 void *
427 wl_shm_buffer_get_data(struct wl_buffer *buffer);
428
429 int32_t
430 wl_shm_buffer_get_stride(struct wl_buffer *buffer);
431
432 uint32_t
433 wl_shm_buffer_get_format(struct wl_buffer *buffer);
434
435 int32_t
436 wl_shm_buffer_get_width(struct wl_buffer *buffer);
437
438 int32_t
439 wl_shm_buffer_get_height(struct wl_buffer *buffer);
440
441 int
442 wl_buffer_is_shm(struct wl_buffer *buffer);
443
444 int
445 wl_display_init_shm(struct wl_display *display);
446
447 struct wl_buffer *
448 wl_shm_buffer_create(struct wl_client *client,
449                      uint32_t id, int32_t width, int32_t height,
450                      int32_t stride, uint32_t format);
451
452 void wl_log_set_handler_server(wl_log_func_t handler);
453
454 #ifdef  __cplusplus
455 }
456 #endif
457
458 #endif