1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2013 Jason Ekstrand
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #ifndef WAYLAND_PRIVATE_H
29 #define WAYLAND_PRIVATE_H
30
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34
35 #define WL_HIDE_DEPRECATED 1
36
37 #include "wayland-util.h"
38 #include "wayland-server-core.h"
39
40 /* Invalid memory address */
41 #define WL_ARRAY_POISON_PTR (void *) 4
42
43 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
44
45 #define container_of(ptr, type, member) ({ \
46 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
47 (type *)( (char *)__mptr - offsetof(type,member) );})
48
49 #define WL_MAP_SERVER_SIDE 0
50 #define WL_MAP_CLIENT_SIDE 1
51 #define WL_SERVER_ID_START 0xff000000
52 #define WL_CLOSURE_MAX_ARGS 20
53
54 struct wl_object {
55 const struct wl_interface *interface;
56 const void *implementation;
57 uint32_t id;
58 };
59
60 int
61 wl_interface_equal(const struct wl_interface *iface1,
62 const struct wl_interface *iface2);
63
64 /* Flags for wl_map_insert_new and wl_map_insert_at. Flags can be queried with
65 * wl_map_lookup_flags. The current implementation has room for 1 bit worth of
66 * flags. If more flags are ever added, the implementation of wl_map will have
67 * to change to allow for new flags */
68 enum wl_map_entry_flags {
69 WL_MAP_ENTRY_LEGACY = (1 << 0), /* Server side only */
70 WL_MAP_ENTRY_ZOMBIE = (1 << 0) /* Client side only */
71 };
72
73 struct wl_map {
74 struct wl_array client_entries;
75 struct wl_array server_entries;
76 uint32_t side;
77 uint32_t free_list;
78 };
79
80 typedef enum wl_iterator_result (*wl_iterator_func_t)(void *element,
81 void *data,
82 uint32_t flags);
83
84 void
85 wl_map_init(struct wl_map *map, uint32_t side);
86
87 void
88 wl_map_release(struct wl_map *map);
89
90 uint32_t
91 wl_map_insert_new(struct wl_map *map, uint32_t flags, void *data);
92
93 int
94 wl_map_insert_at(struct wl_map *map, uint32_t flags, uint32_t i, void *data);
95
96 int
97 wl_map_reserve_new(struct wl_map *map, uint32_t i);
98
99 void
100 wl_map_remove(struct wl_map *map, uint32_t i);
101
102 void *
103 wl_map_lookup(struct wl_map *map, uint32_t i);
104
105 uint32_t
106 wl_map_lookup_flags(struct wl_map *map, uint32_t i);
107
108 void
109 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data);
110
111 struct wl_connection *
112 wl_connection_create(int fd);
113
114 int
115 wl_connection_destroy(struct wl_connection *connection);
116
117 void
118 wl_connection_copy(struct wl_connection *connection, void *data, size_t size);
119
120 void
121 wl_connection_consume(struct wl_connection *connection, size_t size);
122
123 int
124 wl_connection_flush(struct wl_connection *connection);
125
126 uint32_t
127 wl_connection_pending_input(struct wl_connection *connection);
128
129 int
130 wl_connection_read(struct wl_connection *connection);
131
132 int
133 wl_connection_write(struct wl_connection *connection,
134 const void *data, size_t count);
135
136 int
137 wl_connection_queue(struct wl_connection *connection,
138 const void *data, size_t count);
139
140 int
141 wl_connection_get_fd(struct wl_connection *connection);
142
143 struct wl_closure {
144 int count;
145 const struct wl_message *message;
146 uint32_t opcode;
147 uint32_t sender_id;
148 union wl_argument args[WL_CLOSURE_MAX_ARGS];
149 struct wl_list link;
150 struct wl_proxy *proxy;
151 struct wl_array extra[0];
152 };
153
154 struct argument_details {
155 char type;
156 int nullable;
157 };
158
159 const char *
160 get_next_argument(const char *signature, struct argument_details *details);
161
162 int
163 arg_count_for_signature(const char *signature);
164
165 int
166 wl_message_count_arrays(const struct wl_message *message);
167
168 int
169 wl_message_get_since(const struct wl_message *message);
170
171 void
172 wl_argument_from_va_list(const char *signature, union wl_argument *args,
173 int count, va_list ap);
174
175 struct wl_closure *
176 wl_closure_marshal(struct wl_object *sender,
177 uint32_t opcode, union wl_argument *args,
178 const struct wl_message *message);
179
180 struct wl_closure *
181 wl_closure_vmarshal(struct wl_object *sender,
182 uint32_t opcode, va_list ap,
183 const struct wl_message *message);
184
185 struct wl_closure *
186 wl_connection_demarshal(struct wl_connection *connection,
187 uint32_t size,
188 struct wl_map *objects,
189 const struct wl_message *message);
190
191 bool
192 wl_object_is_zombie(struct wl_map *map, uint32_t id);
193
194 int
195 wl_closure_lookup_objects(struct wl_closure *closure, struct wl_map *objects);
196
197 enum wl_closure_invoke_flag {
198 WL_CLOSURE_INVOKE_CLIENT = (1 << 0),
199 WL_CLOSURE_INVOKE_SERVER = (1 << 1)
200 };
201
202 void
203 wl_closure_invoke(struct wl_closure *closure, uint32_t flags,
204 struct wl_object *target, uint32_t opcode, void *data);
205
206 void
207 wl_closure_dispatch(struct wl_closure *closure, wl_dispatcher_func_t dispatcher,
208 struct wl_object *target, uint32_t opcode);
209
210 int
211 wl_closure_send(struct wl_closure *closure, struct wl_connection *connection);
212
213 int
214 wl_closure_queue(struct wl_closure *closure, struct wl_connection *connection);
215
216 void
217 wl_closure_print(struct wl_closure *closure,
218 struct wl_object *target, int send);
219
220 void
221 wl_closure_destroy(struct wl_closure *closure);
222
223 extern wl_log_func_t wl_log_handler;
224
225 void wl_log(const char *fmt, ...);
226 void wl_abort(const char *fmt, ...);
227
228 struct wl_display;
229
230 struct wl_array *
231 wl_display_get_additional_shm_formats(struct wl_display *display);
232
233 static inline void *
zalloc(size_t s)234 zalloc(size_t s)
235 {
236 return calloc(1, s);
237 }
238
239 struct wl_priv_signal {
240 struct wl_list listener_list;
241 struct wl_list emit_list;
242 };
243
244 void
245 wl_priv_signal_init(struct wl_priv_signal *signal);
246
247 void
248 wl_priv_signal_add(struct wl_priv_signal *signal, struct wl_listener *listener);
249
250 struct wl_listener *
251 wl_priv_signal_get(struct wl_priv_signal *signal, wl_notify_func_t notify);
252
253 void
254 wl_priv_signal_emit(struct wl_priv_signal *signal, void *data);
255
256 void
257 wl_priv_signal_final_emit(struct wl_priv_signal *signal, void *data);
258
259 void
260 wl_connection_close_fds_in(struct wl_connection *connection, int max);
261
262 #endif
263