wmail: updated the copyright notices.
[dockapps.git] / libdockapp / src / daevent.c
blob21fad98225a17f0370fd95eba94bf36255439d52
1 /*
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>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <string.h>
28 #include "dockapp.h"
29 #include "daargs.h"
30 #include "dautil.h"
32 extern struct DAContext *_daContext;
33 extern Atom WM_DELETE_WINDOW;
35 Bool
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
42 * dockapp window?
44 return DAProcessEventForWindow(DALeader, event);
45 else
46 /* XXX: What about handling events for child windows? */
47 return False;
50 Bool
51 DAProcessEventForWindow(Window window, XEvent *event)
53 if (event->xany.window != window)
54 return False;
56 switch (event->type) {
57 case ClientMessage:
58 if (event->xclient.data.l[0] != WM_DELETE_WINDOW)
59 break;
60 /* fallthrough */
61 case DestroyNotify:
62 if (_daContext->callbacks.destroy)
63 (*_daContext->callbacks.destroy)();
65 DAFreeContext();
66 XCloseDisplay(DADisplay);
67 #ifdef DEBUG
68 debug("%s: DestroyNotify\n", _daContext->programName);
69 #endif
71 exit(0);
72 break;
73 case ButtonPress:
74 if (_daContext->callbacks.buttonPress)
75 (*_daContext->callbacks.buttonPress)(event->xbutton.button,
76 event->xbutton.state,
77 event->xbutton.x,
78 event->xbutton.y);
79 break;
80 case ButtonRelease:
81 if (_daContext->callbacks.buttonRelease)
82 (*_daContext->callbacks.buttonRelease)(event->xbutton.button,
83 event->xbutton.state,
84 event->xbutton.x,
85 event->xbutton.y);
86 break;
87 case MotionNotify:
88 if (_daContext->callbacks.motion)
89 (*_daContext->callbacks.motion)(event->xmotion.x,
90 event->xmotion.y);
91 break;
92 case EnterNotify:
93 if (_daContext->callbacks.enter)
94 (*_daContext->callbacks.enter)();
95 break;
96 case LeaveNotify:
97 if (_daContext->callbacks.leave)
98 (*_daContext->callbacks.leave)();
99 break;
100 default:
101 return False;
104 return True;
107 void
108 DAEventLoop(void)
110 DAEventLoopForWindow(DAWindow);
113 void
114 DAEventLoopForWindow(Window window)
116 XEvent event;
118 for (;; ) {
119 if (_daContext->timeOut >= 0) {
120 if (!DANextEventOrTimeout(&event, _daContext->timeOut)) {
121 if (_daContext->callbacks.timeout)
122 (*_daContext->callbacks.timeout)();
123 continue;
125 } else
126 XNextEvent(DADisplay, &event);
128 DAProcessEventForWindow(window, &event);
132 Bool
133 DANextEventOrTimeout(XEvent *event, unsigned long milliseconds)
135 struct timeval timeout;
136 fd_set rset;
138 XSync(DADisplay, False);
139 if (XPending(DADisplay)) {
140 XNextEvent(DADisplay, event);
141 return True;
144 timeout.tv_sec = milliseconds / 1000;
145 timeout.tv_usec = (milliseconds % 1000) * 1000;
147 FD_ZERO(&rset);
148 FD_SET(ConnectionNumber(DADisplay), &rset);
150 if (select(ConnectionNumber(DADisplay)+1, &rset, NULL, NULL, &timeout) > 0) {
151 XNextEvent(DADisplay, event);
152 return True;
155 return False;