xcb_event.h revision ecce36be
1b8e80941Smrg/*
2b8e80941Smrg * Copyright (C) 2008-2009 Julien Danjou <julien@danjou.info>
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person
5b8e80941Smrg * obtaining a copy of this software and associated documentation
6b8e80941Smrg * files (the "Software"), to deal in the Software without
7b8e80941Smrg * restriction, including without limitation the rights to use, copy,
8b8e80941Smrg * modify, merge, publish, distribute, sublicense, and/or sell copies
9b8e80941Smrg * of the Software, and to permit persons to whom the Software is
10b8e80941Smrg * furnished to do so, subject to the following conditions:
11b8e80941Smrg *
12b8e80941Smrg * The above copyright notice and this permission notice shall be
13b8e80941Smrg * included in all copies or substantial portions of the Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16b8e80941Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17b8e80941Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18b8e80941Smrg * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
19b8e80941Smrg * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20b8e80941Smrg * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21b8e80941Smrg * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg * Except as contained in this notice, the names of the authors or
24b8e80941Smrg * their institutions shall not be used in advertising or otherwise to
25b8e80941Smrg * promote the sale, use or other dealings in this Software without
26b8e80941Smrg * prior written authorization from the authors.
27b8e80941Smrg */
28b8e80941Smrg
29b8e80941Smrg/**
30b8e80941Smrg * @defgroup xcb__event_t XCB Event Functions
31b8e80941Smrg *
32b8e80941Smrg * These functions ease the handling of X events received.
33b8e80941Smrg *
34b8e80941Smrg * @{
35b8e80941Smrg */
36b8e80941Smrg
37b8e80941Smrg#ifndef __XCB_EVENT_H__
38b8e80941Smrg#define __XCB_EVENT_H__
39b8e80941Smrg
40b8e80941Smrg#include <xcb/xcb.h>
41b8e80941Smrg
42b8e80941Smrg#ifdef __cplusplus
43b8e80941Smrgextern "C" {
44b8e80941Smrg#endif
45b8e80941Smrg
46b8e80941Smrg/**
47b8e80941Smrg * @brief Bit mask to find event type regardless of event source.
48b8e80941Smrg *
49b8e80941Smrg * Each event in the X11 protocol contains an 8-bit type code.
50b8e80941Smrg * The most-significant bit in this code is set if the event was
51b8e80941Smrg * generated from a SendEvent request. This mask can be used to
52b8e80941Smrg * determine the type of event regardless of how the event was
53b8e80941Smrg * generated. See the X11R6 protocol specification for details.
54b8e80941Smrg */
55b8e80941Smrg#define XCB_EVENT_RESPONSE_TYPE_MASK (0x7f)
56b8e80941Smrg#define XCB_EVENT_RESPONSE_TYPE(e)   (e->response_type &  XCB_EVENT_RESPONSE_TYPE_MASK)
57b8e80941Smrg#define XCB_EVENT_SENT(e)            (e->response_type & ~XCB_EVENT_RESPONSE_TYPE_MASK)
58b8e80941Smrg
59b8e80941Smrgtypedef int (*xcb_generic_event_handler_t)(void *data, xcb_connection_t *c, xcb_generic_event_t *event);
60b8e80941Smrgtypedef int (*xcb_generic_error_handler_t)(void *data, xcb_connection_t *c, xcb_generic_error_t *error);
61b8e80941Smrg
62b8e80941Smrgtypedef struct xcb_event_handler xcb_event_handler_t;
63b8e80941Smrgstruct xcb_event_handler
64b8e80941Smrg{
65b8e80941Smrg    xcb_generic_event_handler_t handler;
66b8e80941Smrg    void *data;
67b8e80941Smrg};
68b8e80941Smrg
69b8e80941Smrgtypedef struct xcb_event_handlers xcb_event_handlers_t;
70b8e80941Smrgstruct xcb_event_handlers
71b8e80941Smrg{
72b8e80941Smrg    xcb_event_handler_t event[126];
73b8e80941Smrg    xcb_event_handler_t error[256];
74b8e80941Smrg    xcb_connection_t *c;
75b8e80941Smrg};
76b8e80941Smrg
77b8e80941Smrg/**
78b8e80941Smrg * @brief Initialize event handlers data structure.
79b8e80941Smrg * @param c The connection to the X server.
80b8e80941Smrg * @param evenths A pointer to the event handler data structure to initialize.
81b8e80941Smrg */
82b8e80941Smrgvoid xcb_event_handlers_init(xcb_connection_t *c, xcb_event_handlers_t *evenths);
83b8e80941Smrg
84b8e80941Smrg/**
85b8e80941Smrg * @brief Get X connection used in event handlers.
86b8e80941Smrg * @param evenths The event handlers.
87b8e80941Smrg * @return The connection to the X server.
88b8e80941Smrg */
89b8e80941Smrgxcb_connection_t *xcb_event_get_xcb_connection(xcb_event_handlers_t *evenths);
90b8e80941Smrg
91b8e80941Smrg/**
92b8e80941Smrg * @brief Wait for event and handle it with event handler.
93b8e80941Smrg * @param evenths The event handlers.
94b8e80941Smrg */
95b8e80941Smrgvoid xcb_event_wait_for_event_loop(xcb_event_handlers_t *evenths);
96b8e80941Smrg
97b8e80941Smrg/**
98b8e80941Smrg * @brief Poll for event and handle it with event handler.
99b8e80941Smrg * @param evenths The event handlers.
100b8e80941Smrg */
101b8e80941Smrgvoid xcb_event_poll_for_event_loop(xcb_event_handlers_t *evenths);
102b8e80941Smrg
103b8e80941Smrg/**
104b8e80941Smrg * @brief Handle an event using event handlers from event handlers data
105b8e80941Smrg * structure.
106b8e80941Smrg * @param evenths The event handlers.
107b8e80941Smrg * @param event The event to handle.
108b8e80941Smrg * @return The return value of the handler, or 0 if no handler exists for this
109b8e80941Smrg * event.
110b8e80941Smrg */
111b8e80941Smrgint xcb_event_handle(xcb_event_handlers_t *evenths, xcb_generic_event_t *event);
112b8e80941Smrg
113b8e80941Smrg/**
114b8e80941Smrg * @brief Set an event handler for an event type.
115b8e80941Smrg * @param evenths The event handlers data structure.
116b8e80941Smrg * @param event The event type.
117b8e80941Smrg * @param handler The callback function to call for this event type.
118b8e80941Smrg * @param data Optional data pointer to pass to handler callback function.
119b8e80941Smrg */
120b8e80941Smrgvoid xcb_event_set_handler(xcb_event_handlers_t *evenths, int event, xcb_generic_event_handler_t handler, void *data);
121b8e80941Smrg
122b8e80941Smrg/**
123b8e80941Smrg * @brief Set an error handler for an error type.
124b8e80941Smrg * @param evenths The error handlers data structure.
125b8e80941Smrg * @param error The error type.
126b8e80941Smrg * @param handler The callback function to call for this error type.
127b8e80941Smrg * @param data Optional data pointer to pass to handler callback function.
128b8e80941Smrg */
129b8e80941Smrgvoid xcb_event_set_error_handler(xcb_event_handlers_t *evenths, int error, xcb_generic_error_handler_t handler, void *data);
130b8e80941Smrg
131b8e80941Smrg#define XCB_EVENT_MAKE_EVENT_HANDLER(lkind, ukind) \
132b8e80941Smrgstatic inline void xcb_event_set_##lkind##_handler(xcb_event_handlers_t *evenths, int (*handler)(void *, xcb_connection_t *, xcb_##lkind##_event_t *), void *data) \
133b8e80941Smrg{ \
134b8e80941Smrg    xcb_event_set_handler(evenths, XCB_##ukind, (xcb_generic_event_handler_t) handler, data); \
135b8e80941Smrg}
136b8e80941Smrg
137b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(key_press, KEY_PRESS)
138b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(key_release, KEY_RELEASE)
139b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(button_press, BUTTON_PRESS)
140b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(button_release, BUTTON_RELEASE)
141b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(motion_notify, MOTION_NOTIFY)
142b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(enter_notify, ENTER_NOTIFY)
143b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(leave_notify, LEAVE_NOTIFY)
144b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(focus_in, FOCUS_IN)
145b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(focus_out, FOCUS_OUT)
146b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(keymap_notify, KEYMAP_NOTIFY)
147b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(expose, EXPOSE)
148b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(graphics_exposure, GRAPHICS_EXPOSURE)
149b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(no_exposure, NO_EXPOSURE)
150b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(visibility_notify, VISIBILITY_NOTIFY)
151b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(create_notify, CREATE_NOTIFY)
152b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(destroy_notify, DESTROY_NOTIFY)
153b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(unmap_notify, UNMAP_NOTIFY)
154b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(map_notify, MAP_NOTIFY)
155b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(map_request, MAP_REQUEST)
156b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(reparent_notify, REPARENT_NOTIFY)
157b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(configure_notify, CONFIGURE_NOTIFY)
158b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(configure_request, CONFIGURE_REQUEST)
159b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(gravity_notify, GRAVITY_NOTIFY)
160b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(resize_request, RESIZE_REQUEST)
161b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(circulate_notify, CIRCULATE_NOTIFY)
162b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(circulate_request, CIRCULATE_REQUEST)
163b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(property_notify, PROPERTY_NOTIFY)
164b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(selection_clear, SELECTION_CLEAR)
165b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(selection_request, SELECTION_REQUEST)
166b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(selection_notify, SELECTION_NOTIFY)
167b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(colormap_notify, COLORMAP_NOTIFY)
168b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(client_message, CLIENT_MESSAGE)
169b8e80941SmrgXCB_EVENT_MAKE_EVENT_HANDLER(mapping_notify, MAPPING_NOTIFY)
170b8e80941Smrg
171b8e80941Smrg/** Everyting is ok */
172b8e80941Smrg#define XCB_EVENT_ERROR_SUCESS 0
173b8e80941Smrg/** Bad request code */
174b8e80941Smrg#define XCB_EVENT_ERROR_BAD_REQUEST 1
175b8e80941Smrg/** Int parameter out of range */
176b8e80941Smrg#define XCB_EVENT_ERROR_BAD_VALUE 2
177b8e80941Smrg/** Parameter not a Window */
178b8e80941Smrg#define XCB_EVENT_ERROR_BAD_WINDOW 3
179b8e80941Smrg/** Parameter not a Pixmap */
180b8e80941Smrg#define XCB_EVENT_ERROR_BAD_PIXMAP 4
181b8e80941Smrg/** Parameter not an Atom */
182b8e80941Smrg#define XCB_EVENT_ERROR_BAD_ATOM 5
183b8e80941Smrg/** Parameter not a Cursor */
184b8e80941Smrg#define XCB_EVENT_ERROR_BAD_CURSOR 6
185b8e80941Smrg/** Parameter not a Font */
186b8e80941Smrg#define XCB_EVENT_ERROR_BAD_FONT 7
187b8e80941Smrg/** Parameter mismatch */
188b8e80941Smrg#define XCB_EVENT_ERROR_BAD_MATCH 8
189b8e80941Smrg/** Parameter not a Pixmap or Window */
190b8e80941Smrg#define XCB_EVENT_ERROR_BAD_DRAWABLE 9
191b8e80941Smrg/* Depending on context:
192b8e80941Smrg   - key/button already grabbed
193b8e80941Smrg   - attempt to free an illegal
194b8e80941Smrg     cmap entry
195b8e80941Smrg   - attempt to store into a read-only
196b8e80941Smrg     color map entry.
197b8e80941Smrg   - attempt to modify the access control
198b8e80941Smrg     list from other than the local host.
199b8e80941Smrg*/
200b8e80941Smrg#define XCB_EVENT_ERROR_BAD_ACCESS 10
201b8e80941Smrg/** Insufficient resources */
202b8e80941Smrg#define XCB_EVENT_ERROR_BAD_ALLOC 11
203b8e80941Smrg/** No such colormap */
204b8e80941Smrg#define XCB_EVENT_ERROR_BAD_COLOR 12
205b8e80941Smrg/** Parameter not a GC */
206b8e80941Smrg#define XCB_EVENT_ERROR_BAD_GC 13
207b8e80941Smrg/** Choice not in range or already used */
208b8e80941Smrg#define XCB_EVENT_ERROR_BAD_ID_CHOICE 14
209b8e80941Smrg/** Font or color name doesn't exist */
210b8e80941Smrg#define XCB_EVENT_ERROR_BAD_NAME 15
211b8e80941Smrg/** Request length incorrect */
212b8e80941Smrg#define XCB_EVENT_ERROR_BAD_LENGTH 16
213b8e80941Smrg/** Server is defective */
214b8e80941Smrg#define XCB_EVENT_ERROR_BAD_IMPLEMENTATION 17
215b8e80941Smrg
216b8e80941Smrg/**
217b8e80941Smrg * @brief Convert an event reponse type to a label.
218b8e80941Smrg * @param type The event type.
219b8e80941Smrg * @return A string with the event name, or NULL if unknown.
220b8e80941Smrg */
221b8e80941Smrgconst char * xcb_event_get_label(uint8_t type);
222b8e80941Smrg
223b8e80941Smrg/**
224b8e80941Smrg * @brief Convert an event error type to a label.
225b8e80941Smrg * @param type The erro type.
226b8e80941Smrg * @return A string with the event name, or NULL if unknown or if the event is
227b8e80941Smrg * not an error.
228b8e80941Smrg */
229b8e80941Smrgconst char * xcb_event_get_error_label(uint8_t type);
230b8e80941Smrg
231b8e80941Smrg/**
232b8e80941Smrg * @brief Convert an event request type to a label.
233b8e80941Smrg * @param type The request type.
234b8e80941Smrg * @return A string with the event name, or NULL if unknown or if the event is
235b8e80941Smrg * not an error.
236b8e80941Smrg */
237b8e80941Smrgconst char * xcb_event_get_request_label(uint8_t type);
238b8e80941Smrg
239b8e80941Smrg#ifdef __cplusplus
240b8e80941Smrg}
241b8e80941Smrg#endif
242b8e80941Smrg
243b8e80941Smrg/**
244b8e80941Smrg * @}
245b8e80941Smrg */
246b8e80941Smrg
247b8e80941Smrg#endif /* __XCB_EVENT_H__ */
248b8e80941Smrg