wayland-server-core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright © 2008 Kristian Høgsberg
  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. #ifndef WAYLAND_SERVER_CORE_H
  26. #define WAYLAND_SERVER_CORE_H
  27. #include <sys/types.h>
  28. #include <stdint.h>
  29. #include "wayland-util.h"
  30. #include "wayland-version.h"
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. enum {
  35. WL_EVENT_READABLE = 0x01,
  36. WL_EVENT_WRITABLE = 0x02,
  37. WL_EVENT_HANGUP = 0x04,
  38. WL_EVENT_ERROR = 0x08
  39. };
  40. struct wl_event_loop;
  41. struct wl_event_source;
  42. typedef int (*wl_event_loop_fd_func_t)(int fd, uint32_t mask, void *data);
  43. typedef int (*wl_event_loop_timer_func_t)(void *data);
  44. typedef int (*wl_event_loop_signal_func_t)(int signal_number, void *data);
  45. typedef void (*wl_event_loop_idle_func_t)(void *data);
  46. struct wl_event_loop *
  47. wl_event_loop_create(void);
  48. void
  49. wl_event_loop_destroy(struct wl_event_loop *loop);
  50. struct wl_event_source *
  51. 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
  56. wl_event_source_fd_update(struct wl_event_source *source, uint32_t mask);
  57. struct wl_event_source *
  58. wl_event_loop_add_timer(struct wl_event_loop *loop,
  59. wl_event_loop_timer_func_t func,
  60. void *data);
  61. struct wl_event_source *
  62. wl_event_loop_add_signal(struct wl_event_loop *loop,
  63. int signal_number,
  64. wl_event_loop_signal_func_t func,
  65. void *data);
  66. int
  67. wl_event_source_timer_update(struct wl_event_source *source,
  68. int ms_delay);
  69. int
  70. wl_event_source_remove(struct wl_event_source *source);
  71. void
  72. wl_event_source_check(struct wl_event_source *source);
  73. int
  74. wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout);
  75. void
  76. wl_event_loop_dispatch_idle(struct wl_event_loop *loop);
  77. struct wl_event_source *
  78. wl_event_loop_add_idle(struct wl_event_loop *loop,
  79. wl_event_loop_idle_func_t func,
  80. void *data);
  81. int
  82. wl_event_loop_get_fd(struct wl_event_loop *loop);
  83. struct wl_client;
  84. struct wl_display;
  85. struct wl_listener;
  86. struct wl_resource;
  87. struct wl_global;
  88. typedef void (*wl_notify_func_t)(struct wl_listener *listener, void *data);
  89. void
  90. wl_event_loop_add_destroy_listener(struct wl_event_loop *loop,
  91. struct wl_listener * listener);
  92. struct wl_listener *
  93. wl_event_loop_get_destroy_listener(
  94. struct wl_event_loop *loop,
  95. wl_notify_func_t notify);
  96. struct wl_display *
  97. wl_display_create(void);
  98. void
  99. wl_display_destroy(struct wl_display *display);
  100. struct wl_event_loop *
  101. wl_display_get_event_loop(struct wl_display *display);
  102. int
  103. wl_display_add_socket(struct wl_display *display, const char *name);
  104. const char *
  105. wl_display_add_socket_auto(struct wl_display *display);
  106. int
  107. wl_display_add_socket_fd(struct wl_display *display, int sock_fd);
  108. void
  109. wl_display_terminate(struct wl_display *display);
  110. void
  111. wl_display_run(struct wl_display *display);
  112. void
  113. wl_display_flush_clients(struct wl_display *display);
  114. typedef void (*wl_global_bind_func_t)(struct wl_client *client, void *data,
  115. uint32_t version, uint32_t id);
  116. uint32_t
  117. wl_display_get_serial(struct wl_display *display);
  118. uint32_t
  119. wl_display_next_serial(struct wl_display *display);
  120. void
  121. wl_display_add_destroy_listener(struct wl_display *display,
  122. struct wl_listener *listener);
  123. struct wl_listener *
  124. wl_display_get_destroy_listener(struct wl_display *display,
  125. wl_notify_func_t notify);
  126. struct wl_global *
  127. wl_global_create(struct wl_display *display,
  128. const struct wl_interface *interface,
  129. int version,
  130. void *data, wl_global_bind_func_t bind);
  131. void
  132. wl_global_destroy(struct wl_global *global);
  133. struct wl_client *
  134. wl_client_create(struct wl_display *display, int fd);
  135. void
  136. wl_client_destroy(struct wl_client *client);
  137. void
  138. wl_client_flush(struct wl_client *client);
  139. void
  140. wl_client_get_credentials(struct wl_client *client,
  141. pid_t *pid, uid_t *uid, gid_t *gid);
  142. int
  143. wl_client_get_fd(struct wl_client *client);
  144. void
  145. wl_client_add_destroy_listener(struct wl_client *client,
  146. struct wl_listener *listener);
  147. struct wl_listener *
  148. wl_client_get_destroy_listener(struct wl_client *client,
  149. wl_notify_func_t notify);
  150. struct wl_resource *
  151. wl_client_get_object(struct wl_client *client, uint32_t id);
  152. void
  153. wl_client_post_no_memory(struct wl_client *client);
  154. /** \class wl_listener
  155. *
  156. * \brief A single listener for Wayland signals
  157. *
  158. * wl_listener provides the means to listen for wl_signal notifications. Many
  159. * Wayland objects use wl_listener for notification of significant events like
  160. * object destruction.
  161. *
  162. * Clients should create wl_listener objects manually and can register them as
  163. * listeners to signals using #wl_signal_add, assuming the signal is
  164. * directly accessible. For opaque structs like wl_event_loop, adding a
  165. * listener should be done through provided accessor methods. A listener can
  166. * only listen to one signal at a time.
  167. *
  168. * \code
  169. * struct wl_listener your_listener;
  170. *
  171. * your_listener.notify = your_callback_method;
  172. *
  173. * // Direct access
  174. * wl_signal_add(&some_object->destroy_signal, &your_listener);
  175. *
  176. * // Accessor access
  177. * wl_event_loop *loop = ...;
  178. * wl_event_loop_add_destroy_listener(loop, &your_listener);
  179. * \endcode
  180. *
  181. * If the listener is part of a larger struct, #wl_container_of can be used
  182. * to retrieve a pointer to it:
  183. *
  184. * \code
  185. * void your_listener(struct wl_listener *listener, void *data)
  186. * {
  187. * struct your_data *data;
  188. *
  189. * your_data = wl_container_of(listener, data, your_member_name);
  190. * }
  191. * \endcode
  192. *
  193. * If you need to remove a listener from a signal, use wl_list_remove().
  194. *
  195. * \code
  196. * wl_list_remove(&your_listener.link);
  197. * \endcode
  198. *
  199. * \sa wl_signal
  200. */
  201. struct wl_listener {
  202. struct wl_list link;
  203. wl_notify_func_t notify;
  204. };
  205. /** \class wl_signal
  206. *
  207. * \brief A source of a type of observable event
  208. *
  209. * Signals are recognized points where significant events can be observed.
  210. * Compositors as well as the server can provide signals. Observers are
  211. * wl_listener's that are added through #wl_signal_add. Signals are emitted
  212. * using #wl_signal_emit, which will invoke all listeners until that
  213. * listener is removed by wl_list_remove() (or whenever the signal is
  214. * destroyed).
  215. *
  216. * \sa wl_listener for more information on using wl_signal
  217. */
  218. struct wl_signal {
  219. struct wl_list listener_list;
  220. };
  221. /** Initialize a new \ref wl_signal for use.
  222. *
  223. * \param signal The signal that will be initialized
  224. *
  225. * \memberof wl_signal
  226. */
  227. static inline void
  228. wl_signal_init(struct wl_signal *signal)
  229. {
  230. wl_list_init(&signal->listener_list);
  231. }
  232. /** Add the specified listener to this signal.
  233. *
  234. * \param signal The signal that will emit events to the listener
  235. * \param listener The listener to add
  236. *
  237. * \memberof wl_signal
  238. */
  239. static inline void
  240. wl_signal_add(struct wl_signal *signal, struct wl_listener *listener)
  241. {
  242. wl_list_insert(signal->listener_list.prev, &listener->link);
  243. }
  244. /** Gets the listener struct for the specified callback.
  245. *
  246. * \param signal The signal that contains the specified listener
  247. * \param notify The listener that is the target of this search
  248. * \return the list item that corresponds to the specified listener, or NULL
  249. * if none was found
  250. *
  251. * \memberof wl_signal
  252. */
  253. static inline struct wl_listener *
  254. wl_signal_get(struct wl_signal *signal, wl_notify_func_t notify)
  255. {
  256. struct wl_listener *l;
  257. wl_list_for_each(l, &signal->listener_list, link)
  258. if (l->notify == notify)
  259. return l;
  260. return NULL;
  261. }
  262. /** Emits this signal, notifying all registered listeners.
  263. *
  264. * \param signal The signal object that will emit the signal
  265. * \param data The data that will be emitted with the signal
  266. *
  267. * \memberof wl_signal
  268. */
  269. static inline void
  270. wl_signal_emit(struct wl_signal *signal, void *data)
  271. {
  272. struct wl_listener *l, *next;
  273. wl_list_for_each_safe(l, next, &signal->listener_list, link)
  274. l->notify(l, data);
  275. }
  276. typedef void (*wl_resource_destroy_func_t)(struct wl_resource *resource);
  277. /*
  278. * Post an event to the client's object referred to by 'resource'.
  279. * 'opcode' is the event number generated from the protocol XML
  280. * description (the event name). The variable arguments are the event
  281. * parameters, in the order they appear in the protocol XML specification.
  282. *
  283. * The variable arguments' types are:
  284. * - type=uint: uint32_t
  285. * - type=int: int32_t
  286. * - type=fixed: wl_fixed_t
  287. * - type=string: (const char *) to a nil-terminated string
  288. * - type=array: (struct wl_array *)
  289. * - type=fd: int, that is an open file descriptor
  290. * - type=new_id: (struct wl_object *) or (struct wl_resource *)
  291. * - type=object: (struct wl_object *) or (struct wl_resource *)
  292. */
  293. void
  294. wl_resource_post_event(struct wl_resource *resource,
  295. uint32_t opcode, ...);
  296. void
  297. wl_resource_post_event_array(struct wl_resource *resource,
  298. uint32_t opcode, union wl_argument *args);
  299. void
  300. wl_resource_queue_event(struct wl_resource *resource,
  301. uint32_t opcode, ...);
  302. void wl_resource_queue_event_array(struct wl_resource *resource,
  303. uint32_t opcode, union wl_argument *args);
  304. /* msg is a printf format string, variable args are its args. */
  305. void
  306. wl_resource_post_error(struct wl_resource *resource,
  307. uint32_t code, const char *msg, ...)
  308. __attribute__ ((format (printf, 3, 4)));
  309. void wl_resource_post_no_memory(struct wl_resource *resource);
  310. struct wl_display *
  311. wl_client_get_display(struct wl_client *client);
  312. struct wl_resource *
  313. wl_resource_create(struct wl_client *client,
  314. const struct wl_interface *interface,
  315. int version, uint32_t id);
  316. void
  317. wl_resource_set_implementation(struct wl_resource *resource,
  318. const void *implementation,
  319. void *data,
  320. wl_resource_destroy_func_t destroy);
  321. void
  322. wl_resource_set_dispatcher(struct wl_resource *resource,
  323. wl_dispatcher_func_t dispatcher,
  324. const void *implementation,
  325. void *data,
  326. wl_resource_destroy_func_t destroy);
  327. void
  328. wl_resource_destroy(struct wl_resource *resource);
  329. uint32_t
  330. wl_resource_get_id(struct wl_resource *resource);
  331. struct wl_list *
  332. wl_resource_get_link(struct wl_resource *resource);
  333. struct wl_resource *
  334. wl_resource_from_link(struct wl_list *resource);
  335. struct wl_resource *
  336. wl_resource_find_for_client(struct wl_list *list, struct wl_client *client);
  337. struct wl_client *
  338. wl_resource_get_client(struct wl_resource *resource);
  339. void
  340. wl_resource_set_user_data(struct wl_resource *resource, void *data);
  341. void *
  342. wl_resource_get_user_data(struct wl_resource *resource);
  343. int
  344. wl_resource_get_version(struct wl_resource *resource);
  345. void
  346. wl_resource_set_destructor(struct wl_resource *resource,
  347. wl_resource_destroy_func_t destroy);
  348. int
  349. wl_resource_instance_of(struct wl_resource *resource,
  350. const struct wl_interface *interface,
  351. const void *implementation);
  352. void
  353. wl_resource_add_destroy_listener(struct wl_resource *resource,
  354. struct wl_listener * listener);
  355. struct wl_listener *
  356. wl_resource_get_destroy_listener(struct wl_resource *resource,
  357. wl_notify_func_t notify);
  358. #define wl_resource_for_each(resource, list) \
  359. for (resource = 0, resource = wl_resource_from_link((list)->next); \
  360. wl_resource_get_link(resource) != (list); \
  361. resource = wl_resource_from_link(wl_resource_get_link(resource)->next))
  362. #define wl_resource_for_each_safe(resource, tmp, list) \
  363. for (resource = 0, tmp = 0, \
  364. resource = wl_resource_from_link((list)->next), \
  365. tmp = wl_resource_from_link((list)->next->next); \
  366. wl_resource_get_link(resource) != (list); \
  367. resource = tmp, \
  368. tmp = wl_resource_from_link(wl_resource_get_link(resource)->next))
  369. struct wl_shm_pool;
  370. struct wl_shm_buffer;
  371. void
  372. wl_shm_buffer_begin_access(struct wl_shm_buffer *buffer);
  373. void
  374. wl_shm_buffer_end_access(struct wl_shm_buffer *buffer);
  375. struct wl_shm_buffer *
  376. wl_shm_buffer_get(struct wl_resource *resource);
  377. void *
  378. wl_shm_buffer_get_data(struct wl_shm_buffer *buffer);
  379. int32_t
  380. wl_shm_buffer_get_stride(struct wl_shm_buffer *buffer);
  381. uint32_t
  382. wl_shm_buffer_get_format(struct wl_shm_buffer *buffer);
  383. int32_t
  384. wl_shm_buffer_get_width(struct wl_shm_buffer *buffer);
  385. int32_t
  386. wl_shm_buffer_get_height(struct wl_shm_buffer *buffer);
  387. struct wl_shm_pool *
  388. wl_shm_buffer_ref_pool(struct wl_shm_buffer *buffer);
  389. void
  390. wl_shm_pool_unref(struct wl_shm_pool *pool);
  391. int
  392. wl_display_init_shm(struct wl_display *display);
  393. uint32_t *
  394. wl_display_add_shm_format(struct wl_display *display, uint32_t format);
  395. struct wl_shm_buffer *
  396. wl_shm_buffer_create(struct wl_client *client,
  397. uint32_t id, int32_t width, int32_t height,
  398. int32_t stride, uint32_t format) WL_DEPRECATED;
  399. void wl_log_set_handler_server(wl_log_func_t handler);
  400. #ifdef __cplusplus
  401. }
  402. #endif
  403. #endif