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