]> icculus.org git repositories - mikachu/openbox.git/blob - otk/eventdispatcher.cc
stop compressing enter/leave events.. this might be responsible for the focu sfuckups?
[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     /*
117     // madly compress all crossing events
118     } else if (e.type == EnterNotify) {
119       // any other types are not ones we're interested in
120       if (e.xcrossing.mode == NotifyNormal) {
121         // any other types are not ones we're interested in
122         enter = e.xcrossing.window;
123         enter_root = e.xcrossing.root;
124         //printf("Enter enter=%lx leave=%lx\n", enter, leave);
125       }
126     } else if (e.type == LeaveNotify) {
127       // any other types are not ones we're interested in
128       if (e.xcrossing.mode == NotifyNormal) {
129         leave = e.xcrossing.window;
130         leave_root = e.xcrossing.root;
131         //printf("Leave enter=%lx leave=%lx\n", enter, leave);
132       }
133     */
134     } else {
135       // normal events
136       dispatch(win, e);
137     }
138   }
139
140   if (unfocus != None) {
141     // the last focus event was an FocusOut, so where the hell is the focus at?
142     //printf("UNFOCUSING: %lx\n", unfocus);
143     _focus_e.xfocus.type = FocusOut;
144     _focus_e.xfocus.window = unfocus;
145     dispatch(_focus_e.xfocus.window, _focus_e);
146
147     _focus = None;
148   } else if (focus != None && focus != _focus) {
149     // the last focus event was a FocusIn, so unfocus what used to be focus and
150     // focus this new target
151     //printf("FOCUSING: %lx\n", focus);
152     _focus_e.xfocus.type = FocusIn;
153     _focus_e.xfocus.window = focus;
154     dispatch(_focus_e.xfocus.window, _focus_e);
155
156     if (_focus != None) {
157       //printf("UNFOCUSING: %lx\n", _focus);
158       _focus_e.xfocus.type = FocusOut;
159       _focus_e.xfocus.window = _focus;
160       dispatch(_focus_e.xfocus.window, _focus_e);
161     }
162     
163     _focus = focus;
164   }
165   /*
166   if (leave != None) {
167     _crossing_e.xcrossing.type = LeaveNotify;
168     _crossing_e.xcrossing.window = leave;
169     _crossing_e.xcrossing.root = leave_root;
170     dispatch(_crossing_e.xcrossing.window, _crossing_e);
171   }
172   if (enter != None) {
173     _crossing_e.xcrossing.type = EnterNotify;
174     _crossing_e.xcrossing.window = enter;
175     _crossing_e.xcrossing.root = enter_root;
176     dispatch(_crossing_e.xcrossing.window, _crossing_e);
177     }*/
178 }
179
180 void OtkEventDispatcher::dispatch(Window win, const XEvent &e) {
181   OtkEventHandler *handler = 0;
182   OtkEventMap::iterator it;
183
184   if (_master)
185     _master->handle(e);
186
187   it = _map.find(win);
188   
189   if (it != _map.end())
190     handler = it->second;
191   // these ConfigureRequests require some special attention
192   else if (e.type == ConfigureRequest) {
193     // unhandled configure requests must be used to configure the window
194     // directly
195     XWindowChanges xwc;
196       
197     xwc.x = e.xconfigurerequest.x;
198     xwc.y = e.xconfigurerequest.y;
199     xwc.width = e.xconfigurerequest.width;
200     xwc.height = e.xconfigurerequest.height;
201     xwc.border_width = e.xconfigurerequest.border_width;
202     xwc.sibling = e.xconfigurerequest.above;
203     xwc.stack_mode = e.xconfigurerequest.detail;
204       
205     XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
206                      e.xconfigurerequest.value_mask, &xwc);
207   } else
208     handler = _fallback;
209
210   if (handler)
211     handler->handle(e);
212 }
213
214 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
215 {
216   OtkEventMap::iterator it = _map.find(win);
217   if (it != _map.end())
218     return it->second;
219   return 0;
220 }
221
222 }