1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
7 #include "eventdispatcher.hh"
13 OtkEventDispatcher::OtkEventDispatcher()
14 : _fallback(0), _master(0), _focus(None)
16 _focus_e.xfocus.display = OBDisplay::display;
17 _focus_e.xfocus.mode = NotifyNormal;
18 _focus_e.xfocus.detail = NotifyNonlinear;
20 _crossing_e.xcrossing.display = OBDisplay::display;
21 _crossing_e.xcrossing.mode = NotifyNormal;
22 _crossing_e.xcrossing.detail = NotifyNonlinear;
25 OtkEventDispatcher::~OtkEventDispatcher()
29 void OtkEventDispatcher::clearAllHandlers(void)
34 void OtkEventDispatcher::registerHandler(Window id, OtkEventHandler *handler)
36 _map.insert(std::pair<Window, OtkEventHandler*>(id, handler));
39 void OtkEventDispatcher::clearHandler(Window id)
44 void OtkEventDispatcher::dispatchEvents(void)
46 OtkEventMap::iterator it;
48 Window focus = None, unfocus = None;
49 Window enter = None, leave = None;
50 Window enter_root = None, leave_root = None;
52 while (XPending(OBDisplay::display)) {
53 XNextEvent(OBDisplay::display, &e);
56 printf("Event %d window %lx\n", e.type, e.xany.window);
59 printf("num=%u scroll=%u\n", OBDisplay::numLockMask(), OBDisplay::scrollLockMask());
61 // grab the lasttime and hack up the modifiers
65 _lasttime = e.xbutton.time;
66 e.xbutton.state &= ~(LockMask | OBDisplay::numLockMask() |
67 OBDisplay::scrollLockMask());
70 e.xkey.state &= ~(LockMask | OBDisplay::numLockMask() |
71 OBDisplay::scrollLockMask());
74 _lasttime = e.xmotion.time;
75 e.xmotion.state &= ~(LockMask | OBDisplay::numLockMask() |
76 OBDisplay::scrollLockMask());
79 _lasttime = e.xproperty.time; break;
82 _lasttime = e.xcrossing.time; break;
85 // these ConfigureRequests require some special attention
86 if (e.type == ConfigureRequest) {
87 // find the actual window! e.xany.window is the parent window
88 it = _map.find(e.xconfigurerequest.window);
91 it->second->handle(e);
93 // unhandled configure requests must be used to configure the window
97 xwc.x = e.xconfigurerequest.x;
98 xwc.y = e.xconfigurerequest.y;
99 xwc.width = e.xconfigurerequest.width;
100 xwc.height = e.xconfigurerequest.height;
101 xwc.border_width = e.xconfigurerequest.border_width;
102 xwc.sibling = e.xconfigurerequest.above;
103 xwc.stack_mode = e.xconfigurerequest.detail;
105 XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
106 e.xconfigurerequest.value_mask, &xwc);
108 // madly compress all focus events
109 } else if (e.type == FocusIn) {
110 // any other types are not ones we're interested in
111 if (e.xfocus.detail == NotifyNonlinear) {
112 focus = e.xfocus.window;
114 //printf("FocusIn focus=%lx unfocus=%lx\n", focus, unfocus);
116 } else if (e.type == FocusOut) {
117 // any other types are not ones we're interested in
118 if (e.xfocus.detail == NotifyNonlinear) {
119 unfocus = e.xfocus.window;
121 //printf("FocusOut focus=%lx unfocus=%lx\n", focus, unfocus);
123 // madly compress all crossing events
124 } else if (e.type == EnterNotify) {
125 // any other types are not ones we're interested in
126 if (e.xcrossing.mode == NotifyNormal) {
127 // any other types are not ones we're interested in
128 enter = e.xcrossing.window;
129 enter_root = e.xcrossing.root;
130 //printf("Enter enter=%lx leave=%lx\n", enter, leave);
132 } else if (e.type == LeaveNotify) {
133 // any other types are not ones we're interested in
134 if (e.xcrossing.mode == NotifyNormal) {
135 leave = e.xcrossing.window;
136 leave_root = e.xcrossing.root;
137 //printf("Leave enter=%lx leave=%lx\n", enter, leave);
145 if (unfocus != None) {
146 // the last focus event was an FocusOut, so where the hell is the focus at?
147 //printf("UNFOCUSING: %lx\n", unfocus);
148 _focus_e.xfocus.type = FocusOut;
149 _focus_e.xfocus.window = unfocus;
153 } else if (focus != None && focus != _focus) {
154 // the last focus event was a FocusIn, so unfocus what used to be focus and
155 // focus this new target
156 //printf("FOCUSING: %lx\n", focus);
157 _focus_e.xfocus.type = FocusIn;
158 _focus_e.xfocus.window = focus;
161 if (_focus != None) {
162 //printf("UNFOCUSING: %lx\n", _focus);
163 _focus_e.xfocus.type = FocusOut;
164 _focus_e.xfocus.window = _focus;
172 _crossing_e.xcrossing.type = LeaveNotify;
173 _crossing_e.xcrossing.window = leave;
174 _crossing_e.xcrossing.root = leave_root;
175 dispatch(_crossing_e);
178 _crossing_e.xcrossing.type = EnterNotify;
179 _crossing_e.xcrossing.window = enter;
180 _crossing_e.xcrossing.root = enter_root;
181 dispatch(_crossing_e);
185 void OtkEventDispatcher::dispatch(const XEvent &e) {
186 OtkEventHandler *handler;
187 OtkEventMap::iterator it;
192 it = _map.find(e.xany.window);
194 if (it != _map.end())
195 handler = it->second;
203 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
205 OtkEventMap::iterator it = _map.find(win);
206 if (it != _map.end())