]> icculus.org git repositories - mikachu/openbox.git/blob - otk/eventdispatcher.cc
add next/prev desktop, give them the no_wrap optional arg
[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 (it != _map.end())
89         it->second->handle(e);
90       else {
91         // unhandled configure requests must be used to configure the window
92         // directly
93         XWindowChanges xwc;
94
95         xwc.x = e.xconfigurerequest.x;
96         xwc.y = e.xconfigurerequest.y;
97         xwc.width = e.xconfigurerequest.width;
98         xwc.height = e.xconfigurerequest.height;
99         xwc.border_width = e.xconfigurerequest.border_width;
100         xwc.sibling = e.xconfigurerequest.above;
101         xwc.stack_mode = e.xconfigurerequest.detail;
102
103         XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
104                          e.xconfigurerequest.value_mask, &xwc);
105       }
106     // madly compress all focus events
107     } else if (e.type == FocusIn) {
108       // any other types are not ones we're interested in
109       if (e.xfocus.detail == NotifyNonlinear) {
110         focus = e.xfocus.window;
111         unfocus = None;
112         //printf("FocusIn focus=%lx unfocus=%lx\n", focus, unfocus);
113       }
114     } else if (e.type == FocusOut) {
115       // any other types are not ones we're interested in
116       if (e.xfocus.detail == NotifyNonlinear) {
117         unfocus = e.xfocus.window;
118         focus = None;
119         //printf("FocusOut focus=%lx unfocus=%lx\n", focus, unfocus);
120       }
121     // madly compress all crossing events
122     } else if (e.type == EnterNotify) {
123       // any other types are not ones we're interested in
124       if (e.xcrossing.mode == NotifyNormal) {
125         // any other types are not ones we're interested in
126         enter = e.xcrossing.window;
127         enter_root = e.xcrossing.root;
128         //printf("Enter enter=%lx leave=%lx\n", enter, leave);
129       }
130     } else if (e.type == LeaveNotify) {
131       // any other types are not ones we're interested in
132       if (e.xcrossing.mode == NotifyNormal) {
133         leave = e.xcrossing.window;
134         leave_root = e.xcrossing.root;
135         //printf("Leave enter=%lx leave=%lx\n", enter, leave);
136       }
137     } else {
138       // normal events
139       dispatch(e);
140     }
141   }
142
143   if (unfocus != None) {
144     // the last focus event was an FocusOut, so where the hell is the focus at?
145     //printf("UNFOCUSING: %lx\n", unfocus);
146     _focus_e.xfocus.type = FocusOut;
147     _focus_e.xfocus.window = unfocus;
148     dispatch(_focus_e);
149
150     _focus = None;
151   } else if (focus != None && focus != _focus) {
152     // the last focus event was a FocusIn, so unfocus what used to be focus and
153     // focus this new target
154     //printf("FOCUSING: %lx\n", focus);
155     _focus_e.xfocus.type = FocusIn;
156     _focus_e.xfocus.window = focus;
157     dispatch(_focus_e);
158
159     if (_focus != None) {
160       //printf("UNFOCUSING: %lx\n", _focus);
161       _focus_e.xfocus.type = FocusOut;
162       _focus_e.xfocus.window = _focus;
163       dispatch(_focus_e);
164     }
165     
166     _focus = focus;
167   }
168   
169   if (leave != None) {
170     _crossing_e.xcrossing.type = LeaveNotify;
171     _crossing_e.xcrossing.window = leave;
172     _crossing_e.xcrossing.root = leave_root;
173     dispatch(_crossing_e);
174   }
175   if (enter != None) {
176     _crossing_e.xcrossing.type = EnterNotify;
177     _crossing_e.xcrossing.window = enter;
178     _crossing_e.xcrossing.root = enter_root;
179     dispatch(_crossing_e);
180   }
181 }
182
183 void OtkEventDispatcher::dispatch(const XEvent &e) {
184   OtkEventHandler *handler;
185   OtkEventMap::iterator it;
186
187   if (_master)
188     _master->handle(e);
189
190   it = _map.find(e.xany.window);
191   
192   if (it != _map.end())
193     handler = it->second;
194   else
195     handler = _fallback;
196
197   if (handler)
198     handler->handle(e);
199 }
200
201 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
202 {
203   OtkEventMap::iterator it = _map.find(win);
204   if (it != _map.end())
205     return it->second;
206   return 0;
207 }
208
209 }