]> icculus.org git repositories - mikachu/openbox.git/blob - otk/eventdispatcher.cc
ignore num/caps/scroll lock in 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   OtkEventMap::iterator it;
47   XEvent e;
48   Window focus = None, unfocus = None;
49   Window enter = None, leave = None;
50   Window enter_root = None, leave_root = None;
51
52   while (XPending(OBDisplay::display)) {
53     XNextEvent(OBDisplay::display, &e);
54
55 #if 0
56     printf("Event %d window %lx\n", e.type, e.xany.window);
57 #endif
58
59     printf("num=%u scroll=%u\n", OBDisplay::numLockMask(), OBDisplay::scrollLockMask());
60
61     // grab the lasttime and hack up the modifiers
62     switch (e.type) {
63     case ButtonPress:
64     case ButtonRelease:
65       _lasttime = e.xbutton.time;
66       e.xbutton.state &= ~(LockMask | OBDisplay::numLockMask() |
67                            OBDisplay::scrollLockMask());
68       break;
69     case KeyPress:
70       e.xkey.state &= ~(LockMask | OBDisplay::numLockMask() |
71                         OBDisplay::scrollLockMask());
72       break;
73     case MotionNotify:
74       _lasttime = e.xmotion.time;
75       e.xmotion.state &= ~(LockMask | OBDisplay::numLockMask() |
76                            OBDisplay::scrollLockMask());
77       break;
78     case PropertyNotify:
79       _lasttime = e.xproperty.time; break;
80     case EnterNotify:
81     case LeaveNotify:
82       _lasttime = e.xcrossing.time; break;
83     }
84
85     // these ConfigureRequests require some special attention
86     if (e.type == ConfigureRequest) {
87       // find the actual window! e.xany.window is the parent window
88       it = _map.find(e.xconfigurerequest.window);
89
90       if (it != _map.end())
91         it->second->handle(e);
92       else {
93         // unhandled configure requests must be used to configure the window
94         // directly
95         XWindowChanges xwc;
96
97         xwc.x = e.xconfigurerequest.x;
98         xwc.y = e.xconfigurerequest.y;
99         xwc.width = e.xconfigurerequest.width;
100         xwc.height = e.xconfigurerequest.height;
101         xwc.border_width = e.xconfigurerequest.border_width;
102         xwc.sibling = e.xconfigurerequest.above;
103         xwc.stack_mode = e.xconfigurerequest.detail;
104
105         XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
106                          e.xconfigurerequest.value_mask, &xwc);
107       }
108     // madly compress all focus events
109     } else if (e.type == FocusIn) {
110       // any other types are not ones we're interested in
111       if (e.xfocus.detail == NotifyNonlinear) {
112         focus = e.xfocus.window;
113         unfocus = None;
114         //printf("FocusIn focus=%lx unfocus=%lx\n", focus, unfocus);
115       }
116     } else if (e.type == FocusOut) {
117       // any other types are not ones we're interested in
118       if (e.xfocus.detail == NotifyNonlinear) {
119         unfocus = e.xfocus.window;
120         focus = None;
121         //printf("FocusOut focus=%lx unfocus=%lx\n", focus, unfocus);
122       }
123     // madly compress all crossing events
124     } else if (e.type == EnterNotify) {
125       // any other types are not ones we're interested in
126       if (e.xcrossing.mode == NotifyNormal) {
127         // any other types are not ones we're interested in
128         enter = e.xcrossing.window;
129         enter_root = e.xcrossing.root;
130         //printf("Enter enter=%lx leave=%lx\n", enter, leave);
131       }
132     } else if (e.type == LeaveNotify) {
133       // any other types are not ones we're interested in
134       if (e.xcrossing.mode == NotifyNormal) {
135         leave = e.xcrossing.window;
136         leave_root = e.xcrossing.root;
137         //printf("Leave enter=%lx leave=%lx\n", enter, leave);
138       }
139     } else {
140       // normal events
141       dispatch(e);
142     }
143   }
144
145   if (unfocus != None) {
146     // the last focus event was an FocusOut, so where the hell is the focus at?
147     //printf("UNFOCUSING: %lx\n", unfocus);
148     _focus_e.xfocus.type = FocusOut;
149     _focus_e.xfocus.window = unfocus;
150     dispatch(_focus_e);
151
152     _focus = None;
153   } else if (focus != None && focus != _focus) {
154     // the last focus event was a FocusIn, so unfocus what used to be focus and
155     // focus this new target
156     //printf("FOCUSING: %lx\n", focus);
157     _focus_e.xfocus.type = FocusIn;
158     _focus_e.xfocus.window = focus;
159     dispatch(_focus_e);
160
161     if (_focus != None) {
162       //printf("UNFOCUSING: %lx\n", _focus);
163       _focus_e.xfocus.type = FocusOut;
164       _focus_e.xfocus.window = _focus;
165       dispatch(_focus_e);
166     }
167     
168     _focus = focus;
169   }
170   
171   if (leave != None) {
172     _crossing_e.xcrossing.type = LeaveNotify;
173     _crossing_e.xcrossing.window = leave;
174     _crossing_e.xcrossing.root = leave_root;
175     dispatch(_crossing_e);
176   }
177   if (enter != None) {
178     _crossing_e.xcrossing.type = EnterNotify;
179     _crossing_e.xcrossing.window = enter;
180     _crossing_e.xcrossing.root = enter_root;
181     dispatch(_crossing_e);
182   }
183 }
184
185 void OtkEventDispatcher::dispatch(const XEvent &e) {
186   OtkEventHandler *handler;
187   OtkEventMap::iterator it;
188
189   if (_master)
190     _master->handle(e);
191
192   it = _map.find(e.xany.window);
193   
194   if (it != _map.end())
195     handler = it->second;
196   else
197     handler = _fallback;
198
199   if (handler)
200     handler->handle(e);
201 }
202
203 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
204 {
205   OtkEventMap::iterator it = _map.find(win);
206   if (it != _map.end())
207     return it->second;
208   return 0;
209 }
210
211 }