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 #include <stdbool.h>
35
36 #define WL_HIDE_DEPRECATED 1
37
38 #include "wayland-util.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 WL_MAP_SERVER_SIDE 0
46 #define WL_MAP_CLIENT_SIDE 1
47 #define WL_SERVER_ID_START 0xff000000
48 #define WL_MAP_MAX_OBJECTS 0x00f00000
49 #define WL_CLOSURE_MAX_ARGS 20
50
51 struct wl_object {
52 const struct wl_interface *interface;
53 const void *implementation;
54 uint32_t id;
55 };
56
57 int
58 wl_interface_equal(const struct wl_interface *iface1,
59 const struct wl_interface *iface2);
60
61 /* Flags for wl_map_insert_new and wl_map_insert_at. Flags can be queried with
62 * wl_map_lookup_flags. The current implementation has room for 1 bit worth of
63 * flags. If more flags are ever added, the implementation of wl_map will have
64 * to change to allow for new flags */
65 enum wl_map_entry_flags {
66 WL_MAP_ENTRY_LEGACY = (1 << 0), /* Server side only */
67 WL_MAP_ENTRY_ZOMBIE = (1 << 0) /* Client side only */
68 };
69
70 struct wl_map {
71 struct wl_array client_entries;
72 struct wl_array server_entries;
73 uint32_t side;
74 uint32_t free_list;
75 };
76
77 typedef enum wl_iterator_result (*wl_iterator_func_t)(void *element,
78 void *data,
79 uint32_t flags);
80
81 void
82 wl_map_init(struct wl_map *map, uint32_t side);
83
84 void
85 wl_map_release(struct wl_map *map);
86
87 uint32_t
88 wl_map_insert_new(struct wl_map *map, uint32_t flags, void *data);
89
90 int
91 wl_map_insert_at(struct wl_map *map, uint32_t flags, uint32_t i, void *data);
92
93 int
94 wl_map_reserve_new(struct wl_map *map, uint32_t i);
95
96 void
97 wl_map_remove(struct wl_map *map, uint32_t i);
98
99 void *
100 wl_map_lookup(struct wl_map *map, uint32_t i);
101
102 uint32_t
103 wl_map_lookup_flags(struct wl_map *map, uint32_t i);
104
105 void
106 wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data);
107
108 struct wl_connection *
109 wl_connection_create(int fd);
110
111 int
112 wl_connection_destroy(struct wl_connection *connection);
113
114 void
115 wl_connection_copy(struct wl_connection *connection, void *data, size_t size);
116
117 void
118 wl_connection_consume(struct wl_connection *connection, size_t size);
119
120 int
121 wl_connection_flush(struct wl_connection *connection);
122
123 uint32_t
124 wl_connection_pending_input(struct wl_connection *connection);
125
126 int
127 wl_connection_read(struct wl_connection *connection);
128
129 int
130 wl_connection_write(struct wl_connection *connection,
131 const void *data, size_t count);
132
133 int
134 wl_connection_queue(struct wl_connection *connection,
135 const void *data, size_t count);
136
137 int
138 wl_connection_get_fd(struct wl_connection *connection);
139
140 struct wl_closure {
141 int count;
142 const struct wl_message *message;
143 uint32_t opcode;
144 uint32_t sender_id;
145 union wl_argument args[WL_CLOSURE_MAX_ARGS];
146 struct wl_list link;
147 struct wl_proxy *proxy;
148 struct wl_array extra[0];
149 };
150
151 struct argument_details {
152 char type;
153 int nullable;
154 };
155
156 const char *
157 get_next_argument(const char *signature, struct argument_details *details);
158
159 int
160 arg_count_for_signature(const char *signature);
161
162 int
163 wl_message_count_arrays(const struct wl_message *message);
164
165 int
166 wl_message_get_since(const struct wl_message *message);
167
168 void
169 wl_argument_from_va_list(const char *signature, union wl_argument *args,
170 int count, va_list ap);
171
172 struct wl_closure *
173 wl_closure_marshal(struct wl_object *sender,
174 uint32_t opcode, union wl_argument *args,
175 const struct wl_message *message);
176
177 struct wl_closure *
178 wl_closure_vmarshal(struct wl_object *sender,
179 uint32_t opcode, va_list ap,
180 const struct wl_message *message);
181
182 struct wl_closure *
183 wl_connection_demarshal(struct wl_connection *connection,
184 uint32_t size,
185 struct wl_map *objects,
186 const struct wl_message *message);
187
188 bool
189 wl_object_is_zombie(struct wl_map *map, uint32_t id);
190
191 int
192 wl_closure_lookup_objects(struct wl_closure *closure, struct wl_map *objects);
193
194 enum wl_closure_invoke_flag {
195 WL_CLOSURE_INVOKE_CLIENT = (1 << 0),
196 WL_CLOSURE_INVOKE_SERVER = (1 << 1)
197 };
198
199 void
200 wl_closure_invoke(struct wl_closure *closure, uint32_t flags,
201 struct wl_object *target, uint32_t opcode, void *data);
202
203 void
204 wl_closure_dispatch(struct wl_closure *closure, wl_dispatcher_func_t dispatcher,
205 struct wl_object *target, uint32_t opcode);
206
207 int
208 wl_closure_send(struct wl_closure *closure, struct wl_connection *connection);
209
210 int
211 wl_closure_queue(struct wl_closure *closure, struct wl_connection *connection);
212
213 void
214 wl_closure_print(struct wl_closure *closure, struct wl_object *target,
215 bool send, const char *discarded_reason);
216
217 void
218 wl_closure_destroy(struct wl_closure *closure);
219
220 extern wl_log_func_t wl_log_handler;
221
222 void wl_log(const char *fmt, ...);
223 void wl_abort(const char *fmt, ...);
224
225 struct wl_display;
226
227 struct wl_array *
228 wl_display_get_additional_shm_formats(struct wl_display *display);
229
230 static inline void *
zalloc(size_t s)231 zalloc(size_t s)
232 {
233 return calloc(1, s);
234 }
235
236 void
237 wl_connection_close_fds_in(struct wl_connection *connection, int max);
238
239 #endif
240