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