1ecce36beSmrg/*
2ecce36beSmrg * Copyright © 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#include <assert.h>
30ecce36beSmrg#include <stdlib.h>
31ecce36beSmrg#include <string.h>
32ecce36beSmrg
33ecce36beSmrg#include "xcb_event.h"
34ecce36beSmrg#include "../xcb-util-common.h"
35ecce36beSmrg
36ecce36beSmrgvoid
37ecce36beSmrgxcb_event_handlers_init(xcb_connection_t *c, xcb_event_handlers_t *evenths)
38ecce36beSmrg{
39ecce36beSmrg    memset(evenths, 0, sizeof(xcb_event_handlers_t));
40ecce36beSmrg    evenths->c = c;
41ecce36beSmrg}
42ecce36beSmrg
43ecce36beSmrgxcb_connection_t *
44ecce36beSmrgxcb_event_get_xcb_connection(xcb_event_handlers_t *evenths)
45ecce36beSmrg{
46ecce36beSmrg    return evenths->c;
47ecce36beSmrg}
48ecce36beSmrg
49ecce36beSmrgstatic xcb_event_handler_t *
50ecce36beSmrgget_event_handler(xcb_event_handlers_t *evenths, int event)
51ecce36beSmrg{
52ecce36beSmrg    assert(event < 256);
53ecce36beSmrg    event &= XCB_EVENT_RESPONSE_TYPE_MASK;
54ecce36beSmrg    assert(event >= 2);
55ecce36beSmrg    return &evenths->event[event - 2];
56ecce36beSmrg}
57ecce36beSmrg
58ecce36beSmrgstatic xcb_event_handler_t *
59ecce36beSmrgget_error_handler(xcb_event_handlers_t *evenths, int error)
60ecce36beSmrg{
61ecce36beSmrg    assert(error >= 0 && error < 256);
62ecce36beSmrg    return &evenths->error[error];
63ecce36beSmrg}
64ecce36beSmrg
65ecce36beSmrgint
66ecce36beSmrgxcb_event_handle(xcb_event_handlers_t *evenths, xcb_generic_event_t *event)
67ecce36beSmrg{
68ecce36beSmrg    xcb_event_handler_t *eventh = 0;
69ecce36beSmrg    assert(event->response_type != 1);
70ecce36beSmrg
71ecce36beSmrg    if(event->response_type == 0)
72ecce36beSmrg        eventh = get_error_handler(evenths, ((xcb_generic_error_t *) event)->error_code);
73ecce36beSmrg    else
74ecce36beSmrg        eventh = get_event_handler(evenths, event->response_type);
75ecce36beSmrg
76ecce36beSmrg    if(eventh->handler)
77ecce36beSmrg        return eventh->handler(eventh->data, evenths->c, event);
78ecce36beSmrg    return 0;
79ecce36beSmrg}
80ecce36beSmrg
81ecce36beSmrgvoid
82ecce36beSmrgxcb_event_wait_for_event_loop(xcb_event_handlers_t *evenths)
83ecce36beSmrg{
84ecce36beSmrg    xcb_generic_event_t *event;
85ecce36beSmrg    while((event = xcb_wait_for_event(evenths->c)))
86ecce36beSmrg    {
87ecce36beSmrg        xcb_event_handle(evenths, event);
88ecce36beSmrg        free(event);
89ecce36beSmrg    }
90ecce36beSmrg}
91ecce36beSmrg
92ecce36beSmrgvoid
93ecce36beSmrgxcb_event_poll_for_event_loop(xcb_event_handlers_t *evenths)
94ecce36beSmrg{
95ecce36beSmrg    xcb_generic_event_t *event;
96ecce36beSmrg    while ((event = xcb_poll_for_event(evenths->c)))
97ecce36beSmrg    {
98ecce36beSmrg        xcb_event_handle(evenths, event);
99ecce36beSmrg        free(event);
100ecce36beSmrg    }
101ecce36beSmrg}
102ecce36beSmrg
103ecce36beSmrgstatic void
104ecce36beSmrgset_handler(xcb_generic_event_handler_t handler, void *data, xcb_event_handler_t *place)
105ecce36beSmrg{
106ecce36beSmrg    xcb_event_handler_t eventh = { handler, data };
107ecce36beSmrg    *place = eventh;
108ecce36beSmrg}
109ecce36beSmrg
110ecce36beSmrgvoid
111ecce36beSmrgxcb_event_set_handler(xcb_event_handlers_t *evenths, int event, xcb_generic_event_handler_t handler, void *data)
112ecce36beSmrg{
113ecce36beSmrg    set_handler(handler, data, get_event_handler(evenths, event));
114ecce36beSmrg}
115ecce36beSmrg
116ecce36beSmrgvoid
117ecce36beSmrgxcb_event_set_error_handler(xcb_event_handlers_t *evenths, int error, xcb_generic_error_handler_t handler, void *data)
118ecce36beSmrg{
119ecce36beSmrg    set_handler((xcb_generic_event_handler_t) handler, data, get_error_handler(evenths, error));
120ecce36beSmrg}
121ecce36beSmrg
122ecce36beSmrgstatic const char *labelError[] =
123ecce36beSmrg{
124ecce36beSmrg    "Success",
125ecce36beSmrg    "BadRequest",
126ecce36beSmrg    "BadValue",
127ecce36beSmrg    "BadWindow",
128ecce36beSmrg    "BadPixmap",
129ecce36beSmrg    "BadAtom",
130ecce36beSmrg    "BadCursor",
131ecce36beSmrg    "BadFont",
132ecce36beSmrg    "BadMatch",
133ecce36beSmrg    "BadDrawable",
134ecce36beSmrg    "BadAccess",
135ecce36beSmrg    "BadAlloc",
136ecce36beSmrg    "BadColor",
137ecce36beSmrg    "BadGC",
138ecce36beSmrg    "BadIDChoice",
139ecce36beSmrg    "BadName",
140ecce36beSmrg    "BadLength",
141ecce36beSmrg    "BadImplementation",
142ecce36beSmrg};
143ecce36beSmrg
144ecce36beSmrgstatic const char *labelRequest[] =
145ecce36beSmrg{
146ecce36beSmrg    "no request",
147ecce36beSmrg    "CreateWindow",
148ecce36beSmrg    "ChangeWindowAttributes",
149ecce36beSmrg    "GetWindowAttributes",
150ecce36beSmrg    "DestroyWindow",
151ecce36beSmrg    "DestroySubwindows",
152ecce36beSmrg    "ChangeSaveSet",
153ecce36beSmrg    "ReparentWindow",
154ecce36beSmrg    "MapWindow",
155ecce36beSmrg    "MapSubwindows",
156ecce36beSmrg    "UnmapWindow",
157ecce36beSmrg    "UnmapSubwindows",
158ecce36beSmrg    "ConfigureWindow",
159ecce36beSmrg    "CirculateWindow",
160ecce36beSmrg    "GetGeometry",
161ecce36beSmrg    "QueryTree",
162ecce36beSmrg    "InternAtom",
163ecce36beSmrg    "GetAtomName",
164ecce36beSmrg    "ChangeProperty",
165ecce36beSmrg    "DeleteProperty",
166ecce36beSmrg    "GetProperty",
167ecce36beSmrg    "ListProperties",
168ecce36beSmrg    "SetSelectionOwner",
169ecce36beSmrg    "GetSelectionOwner",
170ecce36beSmrg    "ConvertSelection",
171ecce36beSmrg    "SendEvent",
172ecce36beSmrg    "GrabPointer",
173ecce36beSmrg    "UngrabPointer",
174ecce36beSmrg    "GrabButton",
175ecce36beSmrg    "UngrabButton",
176ecce36beSmrg    "ChangeActivePointerGrab",
177ecce36beSmrg    "GrabKeyboard",
178ecce36beSmrg    "UngrabKeyboard",
179ecce36beSmrg    "GrabKey",
180ecce36beSmrg    "UngrabKey",
181ecce36beSmrg    "AllowEvents",
182ecce36beSmrg    "GrabServer",
183ecce36beSmrg    "UngrabServer",
184ecce36beSmrg    "QueryPointer",
185ecce36beSmrg    "GetMotionEvents",
186ecce36beSmrg    "TranslateCoords",
187ecce36beSmrg    "WarpPointer",
188ecce36beSmrg    "SetInputFocus",
189ecce36beSmrg    "GetInputFocus",
190ecce36beSmrg    "QueryKeymap",
191ecce36beSmrg    "OpenFont",
192ecce36beSmrg    "CloseFont",
193ecce36beSmrg    "QueryFont",
194ecce36beSmrg    "QueryTextExtents",
195ecce36beSmrg    "ListFonts",
196ecce36beSmrg    "ListFontsWithInfo",
197ecce36beSmrg    "SetFontPath",
198ecce36beSmrg    "GetFontPath",
199ecce36beSmrg    "CreatePixmap",
200ecce36beSmrg    "FreePixmap",
201ecce36beSmrg    "CreateGC",
202ecce36beSmrg    "ChangeGC",
203ecce36beSmrg    "CopyGC",
204ecce36beSmrg    "SetDashes",
205ecce36beSmrg    "SetClipRectangles",
206ecce36beSmrg    "FreeGC",
207ecce36beSmrg    "ClearArea",
208ecce36beSmrg    "CopyArea",
209ecce36beSmrg    "CopyPlane",
210ecce36beSmrg    "PolyPoint",
211ecce36beSmrg    "PolyLine",
212ecce36beSmrg    "PolySegment",
213ecce36beSmrg    "PolyRectangle",
214ecce36beSmrg    "PolyArc",
215ecce36beSmrg    "FillPoly",
216ecce36beSmrg    "PolyFillRectangle",
217ecce36beSmrg    "PolyFillArc",
218ecce36beSmrg    "PutImage",
219ecce36beSmrg    "GetImage",
220ecce36beSmrg    "PolyText",
221ecce36beSmrg    "PolyText",
222ecce36beSmrg    "ImageText",
223ecce36beSmrg    "ImageText",
224ecce36beSmrg    "CreateColormap",
225ecce36beSmrg    "FreeColormap",
226ecce36beSmrg    "CopyColormapAndFree",
227ecce36beSmrg    "InstallColormap",
228ecce36beSmrg    "UninstallColormap",
229ecce36beSmrg    "ListInstalledColormaps",
230ecce36beSmrg    "AllocColor",
231ecce36beSmrg    "AllocNamedColor",
232ecce36beSmrg    "AllocColorCells",
233ecce36beSmrg    "AllocColorPlanes",
234ecce36beSmrg    "FreeColors",
235ecce36beSmrg    "StoreColors",
236ecce36beSmrg    "StoreNamedColor",
237ecce36beSmrg    "QueryColors",
238ecce36beSmrg    "LookupColor",
239ecce36beSmrg    "CreateCursor",
240ecce36beSmrg    "CreateGlyphCursor",
241ecce36beSmrg    "FreeCursor",
242ecce36beSmrg    "RecolorCursor",
243ecce36beSmrg    "QueryBestSize",
244ecce36beSmrg    "QueryExtension",
245ecce36beSmrg    "ListExtensions",
246ecce36beSmrg    "ChangeKeyboardMapping",
247ecce36beSmrg    "GetKeyboardMapping",
248ecce36beSmrg    "ChangeKeyboardControl",
249ecce36beSmrg    "GetKeyboardControl",
250ecce36beSmrg    "Bell",
251ecce36beSmrg    "ChangePointerControl",
252ecce36beSmrg    "GetPointerControl",
253ecce36beSmrg    "SetScreenSaver",
254ecce36beSmrg    "GetScreenSaver",
255ecce36beSmrg    "ChangeHosts",
256ecce36beSmrg    "ListHosts",
257ecce36beSmrg    "SetAccessControl",
258ecce36beSmrg    "SetCloseDownMode",
259ecce36beSmrg    "KillClient",
260ecce36beSmrg    "RotateProperties",
261ecce36beSmrg    "ForceScreenSaver",
262ecce36beSmrg    "SetPointerMapping",
263ecce36beSmrg    "GetPointerMapping",
264ecce36beSmrg    "SetModifierMapping",
265ecce36beSmrg    "GetModifierMapping",
266ecce36beSmrg    "major 120",
267ecce36beSmrg    "major 121",
268ecce36beSmrg    "major 122",
269ecce36beSmrg    "major 123",
270ecce36beSmrg    "major 124",
271ecce36beSmrg    "major 125",
272ecce36beSmrg    "major 126",
273ecce36beSmrg    "NoOperation",
274ecce36beSmrg};
275ecce36beSmrg
276ecce36beSmrgstatic const char *labelEvent[] =
277ecce36beSmrg{
278ecce36beSmrg    "error",
279ecce36beSmrg    "reply",
280ecce36beSmrg    "KeyPress",
281ecce36beSmrg    "KeyRelease",
282ecce36beSmrg    "ButtonPress",
283ecce36beSmrg    "ButtonRelease",
284ecce36beSmrg    "MotionNotify",
285ecce36beSmrg    "EnterNotify",
286ecce36beSmrg    "LeaveNotify",
287ecce36beSmrg    "FocusIn",
288ecce36beSmrg    "FocusOut",
289ecce36beSmrg    "KeymapNotify",
290ecce36beSmrg    "Expose",
291ecce36beSmrg    "GraphicsExpose",
292ecce36beSmrg    "NoExpose",
293ecce36beSmrg    "VisibilityNotify",
294ecce36beSmrg    "CreateNotify",
295ecce36beSmrg    "DestroyNotify",
296ecce36beSmrg    "UnmapNotify",
297ecce36beSmrg    "MapNotify",
298ecce36beSmrg    "MapRequest",
299ecce36beSmrg    "ReparentNotify",
300ecce36beSmrg    "ConfigureNotify",
301ecce36beSmrg    "ConfigureRequest",
302ecce36beSmrg    "GravityNotify",
303ecce36beSmrg    "ResizeRequest",
304ecce36beSmrg    "CirculateNotify",
305ecce36beSmrg    "CirculateRequest",
306ecce36beSmrg    "PropertyNotify",
307ecce36beSmrg    "SelectionClear",
308ecce36beSmrg    "SelectionRequest",
309ecce36beSmrg    "SelectionNotify",
310ecce36beSmrg    "ColormapNotify",
311ecce36beSmrg    "ClientMessage",
312ecce36beSmrg    "MappingNotify",
313ecce36beSmrg};
314ecce36beSmrg
315ecce36beSmrgconst char *
316ecce36beSmrgxcb_event_get_label(uint8_t type)
317ecce36beSmrg{
318ecce36beSmrg    if(type < countof(labelEvent))
319ecce36beSmrg        return labelEvent[type];
320ecce36beSmrg    return NULL;
321ecce36beSmrg}
322ecce36beSmrg
323ecce36beSmrgconst char *
324ecce36beSmrgxcb_event_get_error_label(uint8_t type)
325ecce36beSmrg{
326ecce36beSmrg    if(type < countof(labelError))
327ecce36beSmrg        return labelError[type];
328ecce36beSmrg    return NULL;
329ecce36beSmrg}
330ecce36beSmrg
331ecce36beSmrgconst char *
332ecce36beSmrgxcb_event_get_request_label(uint8_t type)
333ecce36beSmrg{
334ecce36beSmrg    if(type < countof(labelRequest))
335ecce36beSmrg        return labelRequest[type];
336ecce36beSmrg    return NULL;
337ecce36beSmrg}
338