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