]> icculus.org git repositories - mikachu/openbox.git/blob - otk/eventdispatcher.cc
handle unmaps better. all thanks to acroread sending wacky unmap events
[mikachu/openbox.git] / otk / eventdispatcher.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "eventdispatcher.hh"
8 #include "display.hh"
9 #include <iostream>
10
11 namespace otk {
12
13 OtkEventDispatcher::OtkEventDispatcher()
14   : _fallback(0), _master(0), _focus(None)
15 {
16   _focus_e.xfocus.display = OBDisplay::display;
17   _focus_e.xfocus.mode = NotifyNormal;
18   _focus_e.xfocus.detail = NotifyNonlinear;
19
20   _crossing_e.xcrossing.display = OBDisplay::display;
21   _crossing_e.xcrossing.mode = NotifyNormal;
22   _crossing_e.xcrossing.detail = NotifyNonlinear;
23 }
24
25 OtkEventDispatcher::~OtkEventDispatcher()
26 {
27 }
28
29 void OtkEventDispatcher::clearAllHandlers(void)
30 {
31   _map.clear();
32 }
33
34 void OtkEventDispatcher::registerHandler(Window id, OtkEventHandler *handler)
35 {
36   _map.insert(std::pair<Window, OtkEventHandler*>(id, handler));
37 }
38
39 void OtkEventDispatcher::clearHandler(Window id)
40 {
41   _map.erase(id);
42 }
43
44 void OtkEventDispatcher::dispatchEvents(void)
45 {
46   XEvent e;
47   Window focus = None, unfocus = None;
48   Window enter = None, leave = None;
49   Window enter_root = None, leave_root = None;
50
51   while (XPending(OBDisplay::display)) {
52     XNextEvent(OBDisplay::display, &e);
53
54 #if 0//defined(DEBUG)
55     printf("Event %d window %lx\n", e.type, e.xany.window);
56 #endif
57
58     Window win;
59
60     // pick a window
61     switch (e.type) {
62     case UnmapNotify:
63       win = e.xunmap.window;
64       break;
65     case DestroyNotify:
66       win = e.xdestroywindow.window;
67       break;
68     case ConfigureRequest:
69       win = e.xconfigurerequest.window;
70       break;
71     default:
72       win = e.xany.window;
73     }
74     
75     // grab the lasttime and hack up the modifiers
76     switch (e.type) {
77     case ButtonPress:
78     case ButtonRelease:
79       _lasttime = e.xbutton.time;
80       e.xbutton.state &= ~(LockMask | OBDisplay::numLockMask() |
81                            OBDisplay::scrollLockMask());
82       break;
83     case KeyPress:
84       e.xkey.state &= ~(LockMask | OBDisplay::numLockMask() |
85                         OBDisplay::scrollLockMask());
86       break;
87     case MotionNotify:
88       _lasttime = e.xmotion.time;
89       e.xmotion.state &= ~(LockMask | OBDisplay::numLockMask() |
90                            OBDisplay::scrollLockMask());
91       break;
92     case PropertyNotify:
93       _lasttime = e.xproperty.time;
94       break;
95     case EnterNotify:
96     case LeaveNotify:
97       _lasttime = e.xcrossing.time;
98       break;
99     }
100
101     // madly compress all focus events
102     if (e.type == FocusIn) {
103       // any other types are not ones we're interested in
104       if (e.xfocus.detail == NotifyNonlinear) {
105         focus = e.xfocus.window;
106         unfocus = None;
107         //printf("FocusIn focus=%lx unfocus=%lx\n", focus, unfocus);
108       }
109     } else if (e.type == FocusOut) {
110       // any other types are not ones we're interested in
111       if (e.xfocus.detail == NotifyNonlinear) {
112         unfocus = e.xfocus.window;
113         focus = None;
114         //printf("FocusOut focus=%lx unfocus=%lx\n", focus, unfocus);
115       }
116     // madly compress all crossing events
117     } else if (e.type == EnterNotify) {
118       // any other types are not ones we're interested in
119       if (e.xcrossing.mode == NotifyNormal) {
120         // any other types are not ones we're interested in
121         enter = e.xcrossing.window;
122         enter_root = e.xcrossing.root;
123         //printf("Enter enter=%lx leave=%lx\n", enter, leave);
124       }
125     } else if (e.type == LeaveNotify) {
126       // any other types are not ones we're interested in
127       if (e.xcrossing.mode == NotifyNormal) {
128         leave = e.xcrossing.window;
129         leave_root = e.xcrossing.root;
130         //printf("Leave enter=%lx leave=%lx\n", enter, leave);
131       }
132     } else {
133       // normal events
134       dispatch(win, e);
135     }
136   }
137
138   if (unfocus != None) {
139     // the last focus event was an FocusOut, so where the hell is the focus at?
140     //printf("UNFOCUSING: %lx\n", unfocus);
141     _focus_e.xfocus.type = FocusOut;
142     _focus_e.xfocus.window = unfocus;
143     dispatch(_focus_e.xfocus.window, _focus_e);
144
145     _focus = None;
146   } else if (focus != None && focus != _focus) {
147     // the last focus event was a FocusIn, so unfocus what used to be focus and
148     // focus this new target
149     //printf("FOCUSING: %lx\n", focus);
150     _focus_e.xfocus.type = FocusIn;
151     _focus_e.xfocus.window = focus;
152     dispatch(_focus_e.xfocus.window, _focus_e);
153
154     if (_focus != None) {
155       //printf("UNFOCUSING: %lx\n", _focus);
156       _focus_e.xfocus.type = FocusOut;
157       _focus_e.xfocus.window = _focus;
158       dispatch(_focus_e.xfocus.window, _focus_e);
159     }
160     
161     _focus = focus;
162   }
163   
164   if (leave != None) {
165     _crossing_e.xcrossing.type = LeaveNotify;
166     _crossing_e.xcrossing.window = leave;
167     _crossing_e.xcrossing.root = leave_root;
168     dispatch(_crossing_e.xcrossing.window, _crossing_e);
169   }
170   if (enter != None) {
171     _crossing_e.xcrossing.type = EnterNotify;
172     _crossing_e.xcrossing.window = enter;
173     _crossing_e.xcrossing.root = enter_root;
174     dispatch(_crossing_e.xcrossing.window, _crossing_e);
175   }
176 }
177
178 void OtkEventDispatcher::dispatch(Window win, const XEvent &e) {
179   OtkEventHandler *handler = 0;
180   OtkEventMap::iterator it;
181
182   if (_master)
183     _master->handle(e);
184
185   it = _map.find(win);
186   
187   if (it != _map.end())
188     handler = it->second;
189   // these ConfigureRequests require some special attention
190   else if (e.type == ConfigureRequest) {
191     // unhandled configure requests must be used to configure the window
192     // directly
193     XWindowChanges xwc;
194       
195     xwc.x = e.xconfigurerequest.x;
196     xwc.y = e.xconfigurerequest.y;
197     xwc.width = e.xconfigurerequest.width;
198     xwc.height = e.xconfigurerequest.height;
199     xwc.border_width = e.xconfigurerequest.border_width;
200     xwc.sibling = e.xconfigurerequest.above;
201     xwc.stack_mode = e.xconfigurerequest.detail;
202       
203     XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
204                      e.xconfigurerequest.value_mask, &xwc);
205   } else
206     handler = _fallback;
207
208   if (handler)
209     handler->handle(e);
210 }
211
212 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
213 {
214   OtkEventMap::iterator it = _map.find(win);
215   if (it != _map.end())
216     return it->second;
217   return 0;
218 }
219
220 }