2 * Copyright (C) 2008-2009 Julien Danjou <julien@danjou.info>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
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.
30 * @defgroup xcb__event_t XCB Event Functions
32 * These functions ease the handling of X events received.
37 #ifndef __XCB_EVENT_H__
38 #define __XCB_EVENT_H__
47 * @brief Bit mask to find event type regardless of event source.
49 * Each event in the X11 protocol contains an 8-bit type code.
50 * The most-significant bit in this code is set if the event was
51 * generated from a SendEvent request. This mask can be used to
52 * determine the type of event regardless of how the event was
53 * generated. See the X11R6 protocol specification for details.
55 #define XCB_EVENT_RESPONSE_TYPE_MASK (0x7f)
56 #define XCB_EVENT_RESPONSE_TYPE(e) (e->response_type & XCB_EVENT_RESPONSE_TYPE_MASK)
57 #define XCB_EVENT_SENT(e) (e->response_type & ~XCB_EVENT_RESPONSE_TYPE_MASK)
59 typedef int (*xcb_generic_event_handler_t)(void *data, xcb_connection_t *c, xcb_generic_event_t *event);
60 typedef int (*xcb_generic_error_handler_t)(void *data, xcb_connection_t *c, xcb_generic_error_t *error);
62 typedef struct xcb_event_handler xcb_event_handler_t;
63 struct xcb_event_handler
65 xcb_generic_event_handler_t handler;
69 typedef struct xcb_event_handlers xcb_event_handlers_t;
70 struct xcb_event_handlers
72 xcb_event_handler_t event[126];
73 xcb_event_handler_t error[256];
78 * @brief Initialize event handlers data structure.
79 * @param c The connection to the X server.
80 * @param evenths A pointer to the event handler data structure to initialize.
82 void xcb_event_handlers_init(xcb_connection_t *c, xcb_event_handlers_t *evenths);
85 * @brief Get X connection used in event handlers.
86 * @param evenths The event handlers.
87 * @return The connection to the X server.
89 xcb_connection_t *xcb_event_get_xcb_connection(xcb_event_handlers_t *evenths);
92 * @brief Wait for event and handle it with event handler.
93 * @param evenths The event handlers.
95 void xcb_event_wait_for_event_loop(xcb_event_handlers_t *evenths);
98 * @brief Poll for event and handle it with event handler.
99 * @param evenths The event handlers.
101 void xcb_event_poll_for_event_loop(xcb_event_handlers_t *evenths);
104 * @brief Handle an event using event handlers from event handlers data
106 * @param evenths The event handlers.
107 * @param event The event to handle.
108 * @return The return value of the handler, or 0 if no handler exists for this
111 int xcb_event_handle(xcb_event_handlers_t *evenths, xcb_generic_event_t *event);
114 * @brief Set an event handler for an event type.
115 * @param evenths The event handlers data structure.
116 * @param event The event type.
117 * @param handler The callback function to call for this event type.
118 * @param data Optional data pointer to pass to handler callback function.
120 void xcb_event_set_handler(xcb_event_handlers_t *evenths, int event, xcb_generic_event_handler_t handler, void *data);
123 * @brief Set an error handler for an error type.
124 * @param evenths The error handlers data structure.
125 * @param error The error type.
126 * @param handler The callback function to call for this error type.
127 * @param data Optional data pointer to pass to handler callback function.
129 void xcb_event_set_error_handler(xcb_event_handlers_t *evenths, int error, xcb_generic_error_handler_t handler, void *data);
131 #define XCB_EVENT_MAKE_EVENT_HANDLER(lkind, ukind) \
132 static inline void xcb_event_set_##lkind##_handler(xcb_event_handlers_t *evenths, int (*handler)(void *, xcb_connection_t *, xcb_##lkind##_event_t *), void *data) \
134 xcb_event_set_handler(evenths, XCB_##ukind, (xcb_generic_event_handler_t) handler, data); \
137 XCB_EVENT_MAKE_EVENT_HANDLER(key_press, KEY_PRESS)
138 XCB_EVENT_MAKE_EVENT_HANDLER(key_release, KEY_RELEASE)
139 XCB_EVENT_MAKE_EVENT_HANDLER(button_press, BUTTON_PRESS)
140 XCB_EVENT_MAKE_EVENT_HANDLER(button_release, BUTTON_RELEASE)
141 XCB_EVENT_MAKE_EVENT_HANDLER(motion_notify, MOTION_NOTIFY)
142 XCB_EVENT_MAKE_EVENT_HANDLER(enter_notify, ENTER_NOTIFY)
143 XCB_EVENT_MAKE_EVENT_HANDLER(leave_notify, LEAVE_NOTIFY)
144 XCB_EVENT_MAKE_EVENT_HANDLER(focus_in, FOCUS_IN)
145 XCB_EVENT_MAKE_EVENT_HANDLER(focus_out, FOCUS_OUT)
146 XCB_EVENT_MAKE_EVENT_HANDLER(keymap_notify, KEYMAP_NOTIFY)
147 XCB_EVENT_MAKE_EVENT_HANDLER(expose, EXPOSE)
148 XCB_EVENT_MAKE_EVENT_HANDLER(graphics_exposure, GRAPHICS_EXPOSURE)
149 XCB_EVENT_MAKE_EVENT_HANDLER(no_exposure, NO_EXPOSURE)
150 XCB_EVENT_MAKE_EVENT_HANDLER(visibility_notify, VISIBILITY_NOTIFY)
151 XCB_EVENT_MAKE_EVENT_HANDLER(create_notify, CREATE_NOTIFY)
152 XCB_EVENT_MAKE_EVENT_HANDLER(destroy_notify, DESTROY_NOTIFY)
153 XCB_EVENT_MAKE_EVENT_HANDLER(unmap_notify, UNMAP_NOTIFY)
154 XCB_EVENT_MAKE_EVENT_HANDLER(map_notify, MAP_NOTIFY)
155 XCB_EVENT_MAKE_EVENT_HANDLER(map_request, MAP_REQUEST)
156 XCB_EVENT_MAKE_EVENT_HANDLER(reparent_notify, REPARENT_NOTIFY)
157 XCB_EVENT_MAKE_EVENT_HANDLER(configure_notify, CONFIGURE_NOTIFY)
158 XCB_EVENT_MAKE_EVENT_HANDLER(configure_request, CONFIGURE_REQUEST)
159 XCB_EVENT_MAKE_EVENT_HANDLER(gravity_notify, GRAVITY_NOTIFY)
160 XCB_EVENT_MAKE_EVENT_HANDLER(resize_request, RESIZE_REQUEST)
161 XCB_EVENT_MAKE_EVENT_HANDLER(circulate_notify, CIRCULATE_NOTIFY)
162 XCB_EVENT_MAKE_EVENT_HANDLER(circulate_request, CIRCULATE_REQUEST)
163 XCB_EVENT_MAKE_EVENT_HANDLER(property_notify, PROPERTY_NOTIFY)
164 XCB_EVENT_MAKE_EVENT_HANDLER(selection_clear, SELECTION_CLEAR)
165 XCB_EVENT_MAKE_EVENT_HANDLER(selection_request, SELECTION_REQUEST)
166 XCB_EVENT_MAKE_EVENT_HANDLER(selection_notify, SELECTION_NOTIFY)
167 XCB_EVENT_MAKE_EVENT_HANDLER(colormap_notify, COLORMAP_NOTIFY)
168 XCB_EVENT_MAKE_EVENT_HANDLER(client_message, CLIENT_MESSAGE)
169 XCB_EVENT_MAKE_EVENT_HANDLER(mapping_notify, MAPPING_NOTIFY)
171 /** Everyting is ok */
172 #define XCB_EVENT_ERROR_SUCESS 0
173 /** Bad request code */
174 #define XCB_EVENT_ERROR_BAD_REQUEST 1
175 /** Int parameter out of range */
176 #define XCB_EVENT_ERROR_BAD_VALUE 2
177 /** Parameter not a Window */
178 #define XCB_EVENT_ERROR_BAD_WINDOW 3
179 /** Parameter not a Pixmap */
180 #define XCB_EVENT_ERROR_BAD_PIXMAP 4
181 /** Parameter not an Atom */
182 #define XCB_EVENT_ERROR_BAD_ATOM 5
183 /** Parameter not a Cursor */
184 #define XCB_EVENT_ERROR_BAD_CURSOR 6
185 /** Parameter not a Font */
186 #define XCB_EVENT_ERROR_BAD_FONT 7
187 /** Parameter mismatch */
188 #define XCB_EVENT_ERROR_BAD_MATCH 8
189 /** Parameter not a Pixmap or Window */
190 #define XCB_EVENT_ERROR_BAD_DRAWABLE 9
191 /* Depending on context:
192 - key/button already grabbed
193 - attempt to free an illegal
195 - attempt to store into a read-only
197 - attempt to modify the access control
198 list from other than the local host.
200 #define XCB_EVENT_ERROR_BAD_ACCESS 10
201 /** Insufficient resources */
202 #define XCB_EVENT_ERROR_BAD_ALLOC 11
203 /** No such colormap */
204 #define XCB_EVENT_ERROR_BAD_COLOR 12
205 /** Parameter not a GC */
206 #define XCB_EVENT_ERROR_BAD_GC 13
207 /** Choice not in range or already used */
208 #define XCB_EVENT_ERROR_BAD_ID_CHOICE 14
209 /** Font or color name doesn't exist */
210 #define XCB_EVENT_ERROR_BAD_NAME 15
211 /** Request length incorrect */
212 #define XCB_EVENT_ERROR_BAD_LENGTH 16
213 /** Server is defective */
214 #define XCB_EVENT_ERROR_BAD_IMPLEMENTATION 17
217 * @brief Convert an event reponse type to a label.
218 * @param type The event type.
219 * @return A string with the event name, or NULL if unknown.
221 const char * xcb_event_get_label(uint8_t type);
224 * @brief Convert an event error type to a label.
225 * @param type The erro type.
226 * @return A string with the event name, or NULL if unknown or if the event is
229 const char * xcb_event_get_error_label(uint8_t type);
232 * @brief Convert an event request type to a label.
233 * @param type The request type.
234 * @return A string with the event name, or NULL if unknown or if the event is
237 const char * xcb_event_get_request_label(uint8_t type);
247 #endif /* __XCB_EVENT_H__ */