1 /* 2 * Copyright 2008 Kristian Hgsberg 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining 5 * a copy of this software and associated documentation files (the 6 * "Software"), to deal in the Software without restriction, including 7 * without limitation the rights to use, copy, modify, merge, publish, 8 * distribute, sublicense, and/or sell copies of the Software, and to 9 * permit persons to whom the Software is furnished to do so, subject to 10 * the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the 13 * next paragraph) shall be included in all copies or substantial 14 * portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 * SOFTWARE. 24 */ 25 26 #ifndef WAYLAND_SERVER_CORE_H 27 #define WAYLAND_SERVER_CORE_H 28 29 #include <sys/types.h> 30 #include <stdint.h> 31 #include "wayland-util.h" 32 #include "wayland-version.h" 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 enum { 39 WL_EVENT_READABLE = 0x01, 40 WL_EVENT_WRITABLE = 0x02, 41 WL_EVENT_HANGUP = 0x04, 42 WL_EVENT_ERROR = 0x08 43 }; 44 45 typedef int (*wl_event_loop_fd_func_t)(int fd, uint32_t mask, void *data); 46 typedef int (*wl_event_loop_timer_func_t)(void *data); 47 typedef int (*wl_event_loop_signal_func_t)(int signal_number, void *data); 48 typedef void (*wl_event_loop_idle_func_t)(void *data); 49 50 struct wl_event_loop * 51 wl_event_loop_create(void); 52 53 void 54 wl_event_loop_destroy(struct wl_event_loop *loop); 55 56 struct wl_event_source * 57 wl_event_loop_add_fd(struct wl_event_loop *loop, 58 int fd, uint32_t mask, 59 wl_event_loop_fd_func_t func, 60 void *data); 61 62 int 63 wl_event_source_fd_update(struct wl_event_source *source, uint32_t mask); 64 65 struct wl_event_source * 66 wl_event_loop_add_timer(struct wl_event_loop *loop, 67 wl_event_loop_timer_func_t func, 68 void *data); 69 70 struct wl_event_source * 71 wl_event_loop_add_signal(struct wl_event_loop *loop, 72 int signal_number, 73 wl_event_loop_signal_func_t func, 74 void *data); 75 76 int 77 wl_event_source_timer_update(struct wl_event_source *source, 78 int ms_delay); 79 80 int 81 wl_event_source_remove(struct wl_event_source *source); 82 83 void 84 wl_event_source_check(struct wl_event_source *source); 85 86 int 87 wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout); 88 89 void 90 wl_event_loop_dispatch_idle(struct wl_event_loop *loop); 91 92 struct wl_event_source * 93 wl_event_loop_add_idle(struct wl_event_loop *loop, 94 wl_event_loop_idle_func_t func, 95 void *data); 96 97 int 98 wl_event_loop_get_fd(struct wl_event_loop *loop); 99 100 struct wl_listener; 101 102 typedef void (*wl_notify_func_t)(struct wl_listener *listener, void *data); 103 104 void 105 wl_event_loop_add_destroy_listener(struct wl_event_loop *loop, 106 struct wl_listener *listener); 107 108 struct wl_listener * 109 wl_event_loop_get_destroy_listener(struct wl_event_loop *loop, 110 wl_notify_func_t notify); 111 112 struct wl_display * 113 wl_display_create(void); 114 115 void 116 wl_display_destroy(struct wl_display *display); 117 118 struct wl_event_loop * 119 wl_display_get_event_loop(struct wl_display *display); 120 121 int 122 wl_display_add_socket(struct wl_display *display, const char *name); 123 124 const char * 125 wl_display_add_socket_auto(struct wl_display *display); 126 127 int 128 wl_display_add_socket_fd(struct wl_display *display, int sock_fd); 129 130 void 131 wl_display_terminate(struct wl_display *display); 132 133 void 134 wl_display_run(struct wl_display *display); 135 136 void 137 wl_display_flush_clients(struct wl_display *display); 138 139 struct wl_client; 140 141 typedef void (*wl_global_bind_func_t)(struct wl_client *client, void *data, 142 uint32_t version, uint32_t id); 143 144 uint32_t 145 wl_display_get_serial(struct wl_display *display); 146 147 uint32_t 148 wl_display_next_serial(struct wl_display *display); 149 150 void 151 wl_display_add_destroy_listener(struct wl_display *display, 152 struct wl_listener *listener); 153 154 void 155 wl_display_add_client_created_listener(struct wl_display *display, 156 struct wl_listener *listener); 157 158 struct wl_listener * 159 wl_display_get_destroy_listener(struct wl_display *display, 160 wl_notify_func_t notify); 161 162 struct wl_global * 163 wl_global_create(struct wl_display *display, 164 const struct wl_interface *interface, 165 int version, 166 void *data, wl_global_bind_func_t bind); 167 168 void 169 wl_global_destroy(struct wl_global *global); 170 171 struct wl_client * 172 wl_client_create(struct wl_display *display, int fd); 173 174 struct wl_list * 175 wl_display_get_client_list(struct wl_display *display); 176 177 struct wl_list * 178 wl_client_get_link(struct wl_client *client); 179 180 struct wl_client * 181 wl_client_from_link(struct wl_list *link); 182 183 /** Iterate over a list of clients. */ 184 #define wl_client_for_each(client, list) \ 185 for (client = wl_client_from_link((list)->next); \ 186 wl_client_get_link(client) != (list); \ 187 client = wl_client_from_link(wl_client_get_link(client)->next)) 188 189 void 190 wl_client_destroy(struct wl_client *client); 191 192 void 193 wl_client_flush(struct wl_client *client); 194 195 void 196 wl_client_get_credentials(struct wl_client *client, 197 pid_t *pid, uid_t *uid, gid_t *gid); 198 199 int 200 wl_client_get_fd(struct wl_client *client); 201 202 void 203 wl_client_add_destroy_listener(struct wl_client *client, 204 struct wl_listener *listener); 205 206 struct wl_listener * 207 wl_client_get_destroy_listener(struct wl_client *client, 208 wl_notify_func_t notify); 209 210 struct wl_resource * 211 wl_client_get_object(struct wl_client *client, uint32_t id); 212 213 void 214 wl_client_post_no_memory(struct wl_client *client); 215 216 void 217 wl_client_add_resource_created_listener(struct wl_client *client, 218 struct wl_listener *listener); 219 220 typedef enum wl_iterator_result (*wl_client_for_each_resource_iterator_func_t)( 221 struct wl_resource *resource, 222 void *user_data); 223 224 void 225 wl_client_for_each_resource(struct wl_client *client, 226 wl_client_for_each_resource_iterator_func_t iterator, 227 void *user_data); 228 229 /** \class wl_listener 230 * 231 * \brief A single listener for Wayland signals 232 * 233 * wl_listener provides the means to listen for wl_signal notifications. Many 234 * Wayland objects use wl_listener for notification of significant events like 235 * object destruction. 236 * 237 * Clients should create wl_listener objects manually and can register them as 238 * listeners to signals using #wl_signal_add, assuming the signal is 239 * directly accessible. For opaque structs like wl_event_loop, adding a 240 * listener should be done through provided accessor methods. A listener can 241 * only listen to one signal at a time. 242 * 243 * \code 244 * struct wl_listener your_listener; 245 * 246 * your_listener.notify = your_callback_method; 247 * 248 * // Direct access 249 * wl_signal_add(&some_object->destroy_signal, &your_listener); 250 * 251 * // Accessor access 252 * wl_event_loop *loop = ...; 253 * wl_event_loop_add_destroy_listener(loop, &your_listener); 254 * \endcode 255 * 256 * If the listener is part of a larger struct, #wl_container_of can be used 257 * to retrieve a pointer to it: 258 * 259 * \code 260 * void your_listener(struct wl_listener *listener, void *data) 261 * { 262 * struct your_data *data; 263 * 264 * your_data = wl_container_of(listener, data, your_member_name); 265 * } 266 * \endcode 267 * 268 * If you need to remove a listener from a signal, use wl_list_remove(). 269 * 270 * \code 271 * wl_list_remove(&your_listener.link); 272 * \endcode 273 * 274 * \sa wl_signal 275 */ 276 struct wl_listener { 277 struct wl_list link; 278 wl_notify_func_t notify; 279 }; 280 281 /** \class wl_signal 282 * 283 * \brief A source of a type of observable event 284 * 285 * Signals are recognized points where significant events can be observed. 286 * Compositors as well as the server can provide signals. Observers are 287 * wl_listener's that are added through #wl_signal_add. Signals are emitted 288 * using #wl_signal_emit, which will invoke all listeners until that 289 * listener is removed by wl_list_remove() (or whenever the signal is 290 * destroyed). 291 * 292 * \sa wl_listener for more information on using wl_signal 293 */ 294 struct wl_signal { 295 struct wl_list listener_list; 296 }; 297 298 /** Initialize a new \ref wl_signal for use. 299 * 300 * \param signal The signal that will be initialized 301 * 302 * \memberof wl_signal 303 */ 304 static inline void 305 wl_signal_init(struct wl_signal *signal) 306 { 307 wl_list_init(&signal->listener_list); 308 } 309 310 /** Add the specified listener to this signal. 311 * 312 * \param signal The signal that will emit events to the listener 313 * \param listener The listener to add 314 * 315 * \memberof wl_signal 316 */ 317 static inline void 318 wl_signal_add(struct wl_signal *signal, struct wl_listener *listener) 319 { 320 wl_list_insert(signal->listener_list.prev, &listener->link); 321 } 322 323 /** Gets the listener struct for the specified callback. 324 * 325 * \param signal The signal that contains the specified listener 326 * \param notify The listener that is the target of this search 327 * \return the list item that corresponds to the specified listener, or NULL 328 * if none was found 329 * 330 * \memberof wl_signal 331 */ 332 static inline struct wl_listener * 333 wl_signal_get(struct wl_signal *signal, wl_notify_func_t notify) 334 { 335 struct wl_listener *l; 336 337 wl_list_for_each(l, &signal->listener_list, link) 338 if (l->notify == notify) 339 return l; 340 341 return NULL; 342 } 343 344 /** Emits this signal, notifying all registered listeners. 345 * 346 * \param signal The signal object that will emit the signal 347 * \param data The data that will be emitted with the signal 348 * 349 * \memberof wl_signal 350 */ 351 static inline void 352 wl_signal_emit(struct wl_signal *signal, void *data) 353 { 354 struct wl_listener *l, *next; 355 356 wl_list_for_each_safe(l, next, &signal->listener_list, link) 357 l->notify(l, data); 358 } 359 360 typedef void (*wl_resource_destroy_func_t)(struct wl_resource *resource); 361 362 /* 363 * Post an event to the client's object referred to by 'resource'. 364 * 'opcode' is the event number generated from the protocol XML 365 * description (the event name). The variable arguments are the event 366 * parameters, in the order they appear in the protocol XML specification. 367 * 368 * The variable arguments' types are: 369 * - type=uint: uint32_t 370 * - type=int: int32_t 371 * - type=fixed: wl_fixed_t 372 * - type=string: (const char *) to a nil-terminated string 373 * - type=array: (struct wl_array *) 374 * - type=fd: int, that is an open file descriptor 375 * - type=new_id: (struct wl_object *) or (struct wl_resource *) 376 * - type=object: (struct wl_object *) or (struct wl_resource *) 377 */ 378 void 379 wl_resource_post_event(struct wl_resource *resource, 380 uint32_t opcode, ...); 381 382 void 383 wl_resource_post_event_array(struct wl_resource *resource, 384 uint32_t opcode, union wl_argument *args); 385 386 void 387 wl_resource_queue_event(struct wl_resource *resource, 388 uint32_t opcode, ...); 389 390 void 391 wl_resource_queue_event_array(struct wl_resource *resource, 392 uint32_t opcode, union wl_argument *args); 393 394 /* msg is a printf format string, variable args are its args. */ 395 void 396 wl_resource_post_error(struct wl_resource *resource, 397 uint32_t code, const char *msg, ...) WL_PRINTF(3, 4); 398 399 void 400 wl_resource_post_no_memory(struct wl_resource *resource); 401 402 struct wl_display * 403 wl_client_get_display(struct wl_client *client); 404 405 struct wl_resource * 406 wl_resource_create(struct wl_client *client, 407 const struct wl_interface *interface, 408 int version, uint32_t id); 409 410 void 411 wl_resource_set_implementation(struct wl_resource *resource, 412 const void *implementation, 413 void *data, 414 wl_resource_destroy_func_t destroy); 415 416 void 417 wl_resource_set_dispatcher(struct wl_resource *resource, 418 wl_dispatcher_func_t dispatcher, 419 const void *implementation, 420 void *data, 421 wl_resource_destroy_func_t destroy); 422 423 void 424 wl_resource_destroy(struct wl_resource *resource); 425 426 uint32_t 427 wl_resource_get_id(struct wl_resource *resource); 428 429 struct wl_list * 430 wl_resource_get_link(struct wl_resource *resource); 431 432 struct wl_resource * 433 wl_resource_from_link(struct wl_list *resource); 434 435 struct wl_resource * 436 wl_resource_find_for_client(struct wl_list *list, struct wl_client *client); 437 438 struct wl_client * 439 wl_resource_get_client(struct wl_resource *resource); 440 441 void 442 wl_resource_set_user_data(struct wl_resource *resource, void *data); 443 444 void * 445 wl_resource_get_user_data(struct wl_resource *resource); 446 447 int 448 wl_resource_get_version(struct wl_resource *resource); 449 450 void 451 wl_resource_set_destructor(struct wl_resource *resource, 452 wl_resource_destroy_func_t destroy); 453 454 int 455 wl_resource_instance_of(struct wl_resource *resource, 456 const struct wl_interface *interface, 457 const void *implementation); 458 const char * 459 wl_resource_get_class(struct wl_resource *resource); 460 461 void 462 wl_resource_add_destroy_listener(struct wl_resource *resource, 463 struct wl_listener *listener); 464 465 struct wl_listener * 466 wl_resource_get_destroy_listener(struct wl_resource *resource, 467 wl_notify_func_t notify); 468 469 #define wl_resource_for_each(resource, list) \ 470 for (resource = 0, resource = wl_resource_from_link((list)->next); \ 471 wl_resource_get_link(resource) != (list); \ 472 resource = wl_resource_from_link(wl_resource_get_link(resource)->next)) 473 474 #define wl_resource_for_each_safe(resource, tmp, list) \ 475 for (resource = 0, tmp = 0, \ 476 resource = wl_resource_from_link((list)->next), \ 477 tmp = wl_resource_from_link((list)->next->next); \ 478 wl_resource_get_link(resource) != (list); \ 479 resource = tmp, \ 480 tmp = wl_resource_from_link(wl_resource_get_link(resource)->next)) 481 482 struct wl_shm_buffer * 483 wl_shm_buffer_get(struct wl_resource *resource); 484 485 void 486 wl_shm_buffer_begin_access(struct wl_shm_buffer *buffer); 487 488 void 489 wl_shm_buffer_end_access(struct wl_shm_buffer *buffer); 490 491 void * 492 wl_shm_buffer_get_data(struct wl_shm_buffer *buffer); 493 494 int32_t 495 wl_shm_buffer_get_stride(struct wl_shm_buffer *buffer); 496 497 uint32_t 498 wl_shm_buffer_get_format(struct wl_shm_buffer *buffer); 499 500 int32_t 501 wl_shm_buffer_get_width(struct wl_shm_buffer *buffer); 502 503 int32_t 504 wl_shm_buffer_get_height(struct wl_shm_buffer *buffer); 505 506 struct wl_shm_pool * 507 wl_shm_buffer_ref_pool(struct wl_shm_buffer *buffer); 508 509 void 510 wl_shm_pool_unref(struct wl_shm_pool *pool); 511 512 int 513 wl_display_init_shm(struct wl_display *display); 514 515 uint32_t * 516 wl_display_add_shm_format(struct wl_display *display, uint32_t format); 517 518 struct wl_shm_buffer * 519 wl_shm_buffer_create(struct wl_client *client, 520 uint32_t id, int32_t width, int32_t height, 521 int32_t stride, uint32_t format) WL_DEPRECATED; 522 523 void 524 wl_log_set_handler_server(wl_log_func_t handler); 525 526 enum wl_protocol_logger_type { 527 WL_PROTOCOL_LOGGER_REQUEST, 528 WL_PROTOCOL_LOGGER_EVENT, 529 }; 530 531 struct wl_protocol_logger_message { 532 struct wl_resource *resource; 533 int message_opcode; 534 const struct wl_message *message; 535 int arguments_count; 536 const union wl_argument *arguments; 537 }; 538 539 typedef void (*wl_protocol_logger_func_t)(void *user_data, 540 enum wl_protocol_logger_type direction, 541 const struct wl_protocol_logger_message *message); 542 543 struct wl_protocol_logger * 544 wl_display_add_protocol_logger(struct wl_display *display, 545 wl_protocol_logger_func_t, void *user_data); 546 547 void 548 wl_protocol_logger_destroy(struct wl_protocol_logger *logger); 549 550 #ifdef __cplusplus 551 } 552 #endif 553 554 #endif 555