xcb_reply.h revision ecce36be
1/*
2 * Copyright (C) 2008 Julien Danjou <julien@danjou.info>
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Except as contained in this notice, the names of the authors or
24 * their institutions shall not be used in advertising or otherwise to
25 * promote the sale, use or other dealings in this Software without
26 * prior written authorization from the authors.
27 */
28
29/**
30 * @defgroup xcb__reply_t XCB Reply Functions
31 *
32 * These functions ease the usage of asynchronous possibility of XCB about
33 * the reply retrieve of sent requests.
34 *
35 * @{
36 */
37
38#ifndef __XCB_REPLY_H__
39#define __XCB_REPLY_H__
40
41#include <xcb/xcb.h>
42#include <pthread.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48typedef void (*xcb_generic_reply_handler_t)(void *data, xcb_connection_t *c, xcb_generic_reply_t *reply, xcb_generic_error_t *error);
49
50struct xcb_reply_node
51{
52    struct xcb_reply_node *next;
53    unsigned int request;
54    xcb_generic_reply_handler_t handler;
55    void *data;
56    char handled;
57};
58
59struct xcb_reply_handlers
60{
61    pthread_mutex_t lock;
62    pthread_cond_t cond;
63    struct xcb_reply_node *head;
64    xcb_connection_t *c;
65    pthread_t thread;
66};
67
68typedef struct xcb_reply_handlers xcb_reply_handlers_t;
69
70/**
71 * @brief Initialize a reply handlers structure.
72 * @param c The connection to the X server.
73 * @param h The reply handlers.
74 */
75void xcb_reply_handlers_init(xcb_connection_t *c, xcb_reply_handlers_t *h);
76
77/**
78 * @brief Get the connection to the X server used in reply handlers.
79 * @param h The reply handlers data structure.
80 * @return The connection to the X server.
81 */
82xcb_connection_t *xcb_reply_get_xcb_connection(xcb_reply_handlers_t *h);
83
84/**
85 * @brief Poll for reply using reply handlers.
86 * @param h The reply handlers data structure.
87 * @return The value return by the handler callback function, or 0 if no
88 * handler was found.
89 */
90int xcb_reply_poll_for_reply(xcb_reply_handlers_t *h);
91
92/**
93 * @brief Start reply handling thread.
94 * This thread will run forever until it is stop with xcb_reply_stop_thread.
95 * @param h The reply handlers.
96 */
97void xcb_reply_start_thread(xcb_reply_handlers_t *h);
98
99/**
100 * @brief Stop reply handling thread.
101 * @param h The reply handlers.
102 */
103void xcb_reply_stop_thread(xcb_reply_handlers_t *h);
104
105/**
106 * @brief Add a reply handler.
107 * @param h The reply handlers data structure to fill.
108 * @param request The request identifier.
109 * @param handler The handler to call for this request.
110 * @param data Optional data passed to the callback function handling request.
111 */
112void xcb_reply_add_handler(xcb_reply_handlers_t *h, unsigned int request, xcb_generic_reply_handler_t handler, void *data);
113
114#ifdef __cplusplus
115}
116#endif
117
118/**
119 * @}
120 */
121
122#endif /* __XCB_REPLY_H__ */
123