]> icculus.org git repositories - dana/openbox.git/blob - otk/eventdispatcher.cc
make focus work without races.
[dana/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)
15 {
16 }
17
18 OtkEventDispatcher::~OtkEventDispatcher()
19 {
20 }
21
22 void OtkEventDispatcher::clearAllHandlers(void)
23 {
24   _map.clear();
25 }
26
27 void OtkEventDispatcher::registerHandler(Window id, OtkEventHandler *handler)
28 {
29   _map.insert(std::pair<Window, OtkEventHandler*>(id, handler));
30 }
31
32 void OtkEventDispatcher::clearHandler(Window id)
33 {
34   _map.erase(id);
35 }
36
37 void OtkEventDispatcher::dispatchEvents(void)
38 {
39   XEvent e;
40
41   while (XPending(OBDisplay::display)) {
42     XNextEvent(OBDisplay::display, &e);
43
44 #if 0//defined(DEBUG)
45     printf("Event %d window %lx\n", e.type, e.xany.window);
46 #endif
47
48     Window win;
49
50     // pick a window
51     switch (e.type) {
52     case UnmapNotify:
53       win = e.xunmap.window;
54       break;
55     case DestroyNotify:
56       win = e.xdestroywindow.window;
57       break;
58     case ConfigureRequest:
59       win = e.xconfigurerequest.window;
60       break;
61     default:
62       win = e.xany.window;
63     }
64     
65     // grab the lasttime and hack up the modifiers
66     switch (e.type) {
67     case ButtonPress:
68     case ButtonRelease:
69       _lasttime = e.xbutton.time;
70       e.xbutton.state &= ~(LockMask | OBDisplay::numLockMask() |
71                            OBDisplay::scrollLockMask());
72       break;
73     case KeyPress:
74       e.xkey.state &= ~(LockMask | OBDisplay::numLockMask() |
75                         OBDisplay::scrollLockMask());
76       break;
77     case MotionNotify:
78       _lasttime = e.xmotion.time;
79       e.xmotion.state &= ~(LockMask | OBDisplay::numLockMask() |
80                            OBDisplay::scrollLockMask());
81       break;
82     case PropertyNotify:
83       _lasttime = e.xproperty.time;
84       break;
85     case EnterNotify:
86     case LeaveNotify:
87       _lasttime = e.xcrossing.time;
88       break;
89     }
90
91     if (e.type == FocusIn || e.type == FocusOut)
92       // any other types are not ones we're interested in
93       if (e.xfocus.detail != NotifyNonlinear)
94         continue;
95
96     if (e.type == FocusOut) {
97       XEvent fi;
98       // send a FocusIn first if one exists
99       while (XCheckTypedEvent(OBDisplay::display, FocusIn, &fi)) {
100         // any other types are not ones we're interested in
101         printf("found focusin\n");
102         if (fi.xfocus.detail == NotifyNonlinear) {
103           printf("sending focusin\n");
104           dispatch(fi.xfocus.window, fi);
105           break;
106         }
107       }
108     }
109     
110     dispatch(win, e);
111   }
112 }
113
114 void OtkEventDispatcher::dispatch(Window win, const XEvent &e)
115 {
116   OtkEventHandler *handler = 0;
117   OtkEventMap::iterator it;
118
119   // master gets everything first
120   if (_master)
121     _master->handle(e);
122
123   // find handler for the chosen window
124   it = _map.find(win);
125
126   if (it != _map.end()) {
127     // if we found a handler
128     handler = it->second;
129   } else if (e.type == ConfigureRequest) {
130     // unhandled configure requests must be used to configure the window
131     // directly
132     XWindowChanges xwc;
133       
134     xwc.x = e.xconfigurerequest.x;
135     xwc.y = e.xconfigurerequest.y;
136     xwc.width = e.xconfigurerequest.width;
137     xwc.height = e.xconfigurerequest.height;
138     xwc.border_width = e.xconfigurerequest.border_width;
139     xwc.sibling = e.xconfigurerequest.above;
140     xwc.stack_mode = e.xconfigurerequest.detail;
141       
142     XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
143                      e.xconfigurerequest.value_mask, &xwc);
144   } else {
145     // grab a falback if it exists
146     handler = _fallback;
147   }
148
149   if (handler)
150     handler->handle(e);
151 }
152
153 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
154 {
155   OtkEventMap::iterator it = _map.find(win);
156   if (it != _map.end())
157     return it->second;
158   return 0;
159 }
160
161 }