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