2 * Copyright (c) 1999-2005 Alfredo K. Kojima, Alban Hertroys
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <sys/types.h>
32 extern struct DAContext
*_daContext
;
33 extern Atom WM_DELETE_WINDOW
;
36 DAProcessEvent(XEvent
*event
)
38 if (event
->xany
.window
== DAWindow
)
39 return DAProcessEventForWindow(DAWindow
, event
);
40 else if (event
->xany
.window
== DALeader
)
41 /* XXX: Is this superfluous now that DAWindow always references the
44 return DAProcessEventForWindow(DALeader
, event
);
46 /* XXX: What about handling events for child windows? */
51 DAProcessEventForWindow(Window window
, XEvent
*event
)
53 if (event
->xany
.window
!= window
)
56 switch (event
->type
) {
58 if (event
->xclient
.data
.l
[0] != WM_DELETE_WINDOW
)
62 if (_daContext
->callbacks
.destroy
)
63 (*_daContext
->callbacks
.destroy
)();
66 XCloseDisplay(DADisplay
);
68 debug("%s: DestroyNotify\n", _daContext
->programName
);
74 if (_daContext
->callbacks
.buttonPress
)
75 (*_daContext
->callbacks
.buttonPress
)(event
->xbutton
.button
,
81 if (_daContext
->callbacks
.buttonRelease
)
82 (*_daContext
->callbacks
.buttonRelease
)(event
->xbutton
.button
,
88 if (_daContext
->callbacks
.motion
)
89 (*_daContext
->callbacks
.motion
)(event
->xmotion
.x
,
93 if (_daContext
->callbacks
.enter
)
94 (*_daContext
->callbacks
.enter
)();
97 if (_daContext
->callbacks
.leave
)
98 (*_daContext
->callbacks
.leave
)();
110 DAEventLoopForWindow(DAWindow
);
114 DAEventLoopForWindow(Window window
)
119 if (_daContext
->timeOut
>= 0) {
120 if (!DANextEventOrTimeout(&event
, _daContext
->timeOut
)) {
121 if (_daContext
->callbacks
.timeout
)
122 (*_daContext
->callbacks
.timeout
)();
126 XNextEvent(DADisplay
, &event
);
128 DAProcessEventForWindow(window
, &event
);
133 DANextEventOrTimeout(XEvent
*event
, unsigned long milliseconds
)
135 struct timeval timeout
;
138 XSync(DADisplay
, False
);
139 if (XPending(DADisplay
)) {
140 XNextEvent(DADisplay
, event
);
144 timeout
.tv_sec
= milliseconds
/ 1000;
145 timeout
.tv_usec
= (milliseconds
% 1000) * 1000;
148 FD_SET(ConnectionNumber(DADisplay
), &rset
);
150 if (select(ConnectionNumber(DADisplay
)+1, &rset
, NULL
, NULL
, &timeout
) > 0) {
151 XNextEvent(DADisplay
, event
);