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