1/*
2 * Copyright (C) 2008-2009 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__event_t XCB Event Functions
31 *
32 * These functions ease the handling of X events received.
33 *
34 * @{
35 */
36
37#ifndef __XCB_EVENT_H__
38#define __XCB_EVENT_H__
39
40#include <xcb/xcb.h>
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/**
47 * @brief Bit mask to find event type regardless of event source.
48 *
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.
54 */
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)
58
59typedef int (*xcb_generic_event_handler_t)(void *data, xcb_connection_t *c, xcb_generic_event_t *event);
60typedef int (*xcb_generic_error_handler_t)(void *data, xcb_connection_t *c, xcb_generic_error_t *error);
61
62typedef struct xcb_event_handler xcb_event_handler_t;
63struct xcb_event_handler
64{
65    xcb_generic_event_handler_t handler;
66    void *data;
67};
68
69typedef struct xcb_event_handlers xcb_event_handlers_t;
70struct xcb_event_handlers
71{
72    xcb_event_handler_t event[126];
73    xcb_event_handler_t error[256];
74    xcb_connection_t *c;
75};
76
77/**
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.
81 */
82void xcb_event_handlers_init(xcb_connection_t *c, xcb_event_handlers_t *evenths);
83
84/**
85 * @brief Get X connection used in event handlers.
86 * @param evenths The event handlers.
87 * @return The connection to the X server.
88 */
89xcb_connection_t *xcb_event_get_xcb_connection(xcb_event_handlers_t *evenths);
90
91/**
92 * @brief Wait for event and handle it with event handler.
93 * @param evenths The event handlers.
94 */
95void xcb_event_wait_for_event_loop(xcb_event_handlers_t *evenths);
96
97/**
98 * @brief Poll for event and handle it with event handler.
99 * @param evenths The event handlers.
100 */
101void xcb_event_poll_for_event_loop(xcb_event_handlers_t *evenths);
102
103/**
104 * @brief Handle an event using event handlers from event handlers data
105 * structure.
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
109 * event.
110 */
111int xcb_event_handle(xcb_event_handlers_t *evenths, xcb_generic_event_t *event);
112
113/**
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.
119 */
120void xcb_event_set_handler(xcb_event_handlers_t *evenths, int event, xcb_generic_event_handler_t handler, void *data);
121
122/**
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.
128 */
129void xcb_event_set_error_handler(xcb_event_handlers_t *evenths, int error, xcb_generic_error_handler_t handler, void *data);
130
131#define XCB_EVENT_MAKE_EVENT_HANDLER(lkind, ukind) \
132static inline void xcb_event_set_##lkind##_handler(xcb_event_handlers_t *evenths, int (*handler)(void *, xcb_connection_t *, xcb_##lkind##_event_t *), void *data) \
133{ \
134    xcb_event_set_handler(evenths, XCB_##ukind, (xcb_generic_event_handler_t) handler, data); \
135}
136
137XCB_EVENT_MAKE_EVENT_HANDLER(key_press, KEY_PRESS)
138XCB_EVENT_MAKE_EVENT_HANDLER(key_release, KEY_RELEASE)
139XCB_EVENT_MAKE_EVENT_HANDLER(button_press, BUTTON_PRESS)
140XCB_EVENT_MAKE_EVENT_HANDLER(button_release, BUTTON_RELEASE)
141XCB_EVENT_MAKE_EVENT_HANDLER(motion_notify, MOTION_NOTIFY)
142XCB_EVENT_MAKE_EVENT_HANDLER(enter_notify, ENTER_NOTIFY)
143XCB_EVENT_MAKE_EVENT_HANDLER(leave_notify, LEAVE_NOTIFY)
144XCB_EVENT_MAKE_EVENT_HANDLER(focus_in, FOCUS_IN)
145XCB_EVENT_MAKE_EVENT_HANDLER(focus_out, FOCUS_OUT)
146XCB_EVENT_MAKE_EVENT_HANDLER(keymap_notify, KEYMAP_NOTIFY)
147XCB_EVENT_MAKE_EVENT_HANDLER(expose, EXPOSE)
148XCB_EVENT_MAKE_EVENT_HANDLER(graphics_exposure, GRAPHICS_EXPOSURE)
149XCB_EVENT_MAKE_EVENT_HANDLER(no_exposure, NO_EXPOSURE)
150XCB_EVENT_MAKE_EVENT_HANDLER(visibility_notify, VISIBILITY_NOTIFY)
151XCB_EVENT_MAKE_EVENT_HANDLER(create_notify, CREATE_NOTIFY)
152XCB_EVENT_MAKE_EVENT_HANDLER(destroy_notify, DESTROY_NOTIFY)
153XCB_EVENT_MAKE_EVENT_HANDLER(unmap_notify, UNMAP_NOTIFY)
154XCB_EVENT_MAKE_EVENT_HANDLER(map_notify, MAP_NOTIFY)
155XCB_EVENT_MAKE_EVENT_HANDLER(map_request, MAP_REQUEST)
156XCB_EVENT_MAKE_EVENT_HANDLER(reparent_notify, REPARENT_NOTIFY)
157XCB_EVENT_MAKE_EVENT_HANDLER(configure_notify, CONFIGURE_NOTIFY)
158XCB_EVENT_MAKE_EVENT_HANDLER(configure_request, CONFIGURE_REQUEST)
159XCB_EVENT_MAKE_EVENT_HANDLER(gravity_notify, GRAVITY_NOTIFY)
160XCB_EVENT_MAKE_EVENT_HANDLER(resize_request, RESIZE_REQUEST)
161XCB_EVENT_MAKE_EVENT_HANDLER(circulate_notify, CIRCULATE_NOTIFY)
162XCB_EVENT_MAKE_EVENT_HANDLER(circulate_request, CIRCULATE_REQUEST)
163XCB_EVENT_MAKE_EVENT_HANDLER(property_notify, PROPERTY_NOTIFY)
164XCB_EVENT_MAKE_EVENT_HANDLER(selection_clear, SELECTION_CLEAR)
165XCB_EVENT_MAKE_EVENT_HANDLER(selection_request, SELECTION_REQUEST)
166XCB_EVENT_MAKE_EVENT_HANDLER(selection_notify, SELECTION_NOTIFY)
167XCB_EVENT_MAKE_EVENT_HANDLER(colormap_notify, COLORMAP_NOTIFY)
168XCB_EVENT_MAKE_EVENT_HANDLER(client_message, CLIENT_MESSAGE)
169XCB_EVENT_MAKE_EVENT_HANDLER(mapping_notify, MAPPING_NOTIFY)
170
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
194     cmap entry
195   - attempt to store into a read-only
196     color map entry.
197   - attempt to modify the access control
198     list from other than the local host.
199*/
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
215
216/**
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.
220 */
221const char * xcb_event_get_label(uint8_t type);
222
223/**
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
227 * not an error.
228 */
229const char * xcb_event_get_error_label(uint8_t type);
230
231/**
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
235 * not an error.
236 */
237const char * xcb_event_get_request_label(uint8_t type);
238
239#ifdef __cplusplus
240}
241#endif
242
243/**
244 * @}
245 */
246
247#endif /* __XCB_EVENT_H__ */
248