]> Dogcows Code - chaz/openbox/blob - otk/eventdispatcher.cc
b3c8a587cb4e690b1e801d060f361df676e4727c
[chaz/openbox] / 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 }
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 if (e.type == FocusIn || e.type == FocusOut) {
49 // focus events are a beast all their own.. yuk, hate, etc.
50 dispatchFocus(e);
51 } else {
52 Window win;
53
54 // pick a window
55 switch (e.type) {
56 case UnmapNotify:
57 win = e.xunmap.window;
58 break;
59 case DestroyNotify:
60 win = e.xdestroywindow.window;
61 break;
62 case ConfigureRequest:
63 win = e.xconfigurerequest.window;
64 break;
65 default:
66 win = e.xany.window;
67 }
68
69 // grab the lasttime and hack up the modifiers
70 switch (e.type) {
71 case ButtonPress:
72 case ButtonRelease:
73 _lasttime = e.xbutton.time;
74 e.xbutton.state &= ~(LockMask | OBDisplay::numLockMask() |
75 OBDisplay::scrollLockMask());
76 break;
77 case KeyPress:
78 e.xkey.state &= ~(LockMask | OBDisplay::numLockMask() |
79 OBDisplay::scrollLockMask());
80 break;
81 case MotionNotify:
82 _lasttime = e.xmotion.time;
83 e.xmotion.state &= ~(LockMask | OBDisplay::numLockMask() |
84 OBDisplay::scrollLockMask());
85 break;
86 case PropertyNotify:
87 _lasttime = e.xproperty.time;
88 break;
89 case EnterNotify:
90 case LeaveNotify:
91 _lasttime = e.xcrossing.time;
92 break;
93 }
94
95 dispatch(win, e);
96 }
97 }
98 }
99
100 void OtkEventDispatcher::dispatchFocus(const XEvent &e)
101 {
102 Window newfocus = None;
103
104 // any other types are not ones we're interested in
105 if (e.xfocus.detail != NotifyNonlinear)
106 return;
107
108 if (e.type == FocusIn) {
109 printf("---\n");
110 printf("Got FocusIn!\n");
111 printf("Using FocusIn\n");
112 newfocus = e.xfocus.window;
113
114 if (newfocus != _focus) {
115 // send a FocusIn to whatever was just focused
116 dispatch(newfocus, e);
117 printf("Sent FocusIn 0x%lx\n", newfocus);
118
119 // send a FocusOut to whatever used to be focused
120 if (_focus) {
121 XEvent ev;
122 ev.xfocus = e.xfocus;
123 ev.xfocus.window = _focus;
124 ev.type = FocusOut;
125 dispatch(_focus, ev);
126 printf("Sent FocusOut 0x%lx\n", _focus);
127 }
128
129 // store the new focused window
130 _focus = newfocus;
131 }
132
133 } else if (e.type == FocusOut) {
134 bool focused = false; // found a new focus target?
135 printf("---\n");
136 printf("Got FocusOut!\n");
137
138 // FocusOut events just make us look for FocusIn events. They are ignored
139 // otherwise.
140 XEvent fi;
141 while (XCheckTypedEvent(OBDisplay::display, FocusIn, &fi)) {
142 if (e.xfocus.detail == NotifyNonlinear) {
143 printf("Found FocusIn\n");
144 dispatchFocus(fi);
145 focused = true;
146 break;
147 }
148 }
149 if (!focused) {
150 // send a FocusOut to whatever used to be focused
151 if (_focus) {
152 XEvent ev;
153 ev.xfocus = e.xfocus;
154 ev.xfocus.window = _focus;
155 dispatch(_focus, ev);
156 printf("Sent FocusOut 0x%lx\n", _focus);
157 }
158 // store that no window has focus anymore
159 _focus = None;
160 }
161 }
162 }
163
164 void OtkEventDispatcher::dispatch(Window win, const XEvent &e)
165 {
166 OtkEventHandler *handler = 0;
167 OtkEventMap::iterator it;
168
169 // master gets everything first
170 if (_master)
171 _master->handle(e);
172
173 // find handler for the chosen window
174 it = _map.find(win);
175
176 if (it != _map.end()) {
177 // if we found a handler
178 handler = it->second;
179 } else if (e.type == ConfigureRequest) {
180 // unhandled configure requests must be used to configure the window
181 // directly
182 XWindowChanges xwc;
183
184 xwc.x = e.xconfigurerequest.x;
185 xwc.y = e.xconfigurerequest.y;
186 xwc.width = e.xconfigurerequest.width;
187 xwc.height = e.xconfigurerequest.height;
188 xwc.border_width = e.xconfigurerequest.border_width;
189 xwc.sibling = e.xconfigurerequest.above;
190 xwc.stack_mode = e.xconfigurerequest.detail;
191
192 XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
193 e.xconfigurerequest.value_mask, &xwc);
194 } else {
195 // grab a falback if it exists
196 handler = _fallback;
197 }
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 }
This page took 0.041009 seconds and 4 git commands to generate.