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 // grab the lasttime and hack up the modifiers
63 _lasttime = e.xbutton.time;
64 e.xbutton.state &= ~(LockMask | OBDisplay::numLockMask() |
65 OBDisplay::scrollLockMask());
68 e.xkey.state &= ~(LockMask | OBDisplay::numLockMask() |
69 OBDisplay::scrollLockMask());
72 _lasttime = e.xmotion.time;
73 e.xmotion.state &= ~(LockMask | OBDisplay::numLockMask() |
74 OBDisplay::scrollLockMask());
77 _lasttime = e.xproperty.time; break;
80 _lasttime = e.xcrossing.time; break;
83 // these ConfigureRequests require some special attention
84 if (e.type == ConfigureRequest) {
85 // find the actual window! e.xany.window is the parent window
86 it = _map.find(e.xconfigurerequest.window);
92 it->second->handle(e);
94 // unhandled configure requests must be used to configure the window
98 xwc.x = e.xconfigurerequest.x;
99 xwc.y = e.xconfigurerequest.y;
100 xwc.width = e.xconfigurerequest.width;
101 xwc.height = e.xconfigurerequest.height;
102 xwc.border_width = e.xconfigurerequest.border_width;
103 xwc.sibling = e.xconfigurerequest.above;
104 xwc.stack_mode = e.xconfigurerequest.detail;
106 XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
107 e.xconfigurerequest.value_mask, &xwc);
109 // madly compress all focus events
110 } else if (e.type == FocusIn) {
111 // any other types are not ones we're interested in
112 if (e.xfocus.detail == NotifyNonlinear) {
113 focus = e.xfocus.window;
115 //printf("FocusIn focus=%lx unfocus=%lx\n", focus, unfocus);
117 } else if (e.type == FocusOut) {
118 // any other types are not ones we're interested in
119 if (e.xfocus.detail == NotifyNonlinear) {
120 unfocus = e.xfocus.window;
122 //printf("FocusOut focus=%lx unfocus=%lx\n", focus, unfocus);
124 // madly compress all crossing events
125 } else if (e.type == EnterNotify) {
126 // any other types are not ones we're interested in
127 if (e.xcrossing.mode == NotifyNormal) {
128 // any other types are not ones we're interested in
129 enter = e.xcrossing.window;
130 enter_root = e.xcrossing.root;
131 //printf("Enter enter=%lx leave=%lx\n", enter, leave);
133 } else if (e.type == LeaveNotify) {
134 // any other types are not ones we're interested in
135 if (e.xcrossing.mode == NotifyNormal) {
136 leave = e.xcrossing.window;
137 leave_root = e.xcrossing.root;
138 //printf("Leave enter=%lx leave=%lx\n", enter, leave);
146 if (unfocus != None) {
147 // the last focus event was an FocusOut, so where the hell is the focus at?
148 //printf("UNFOCUSING: %lx\n", unfocus);
149 _focus_e.xfocus.type = FocusOut;
150 _focus_e.xfocus.window = unfocus;
154 } else if (focus != None && focus != _focus) {
155 // the last focus event was a FocusIn, so unfocus what used to be focus and
156 // focus this new target
157 //printf("FOCUSING: %lx\n", focus);
158 _focus_e.xfocus.type = FocusIn;
159 _focus_e.xfocus.window = focus;
162 if (_focus != None) {
163 //printf("UNFOCUSING: %lx\n", _focus);
164 _focus_e.xfocus.type = FocusOut;
165 _focus_e.xfocus.window = _focus;
173 _crossing_e.xcrossing.type = LeaveNotify;
174 _crossing_e.xcrossing.window = leave;
175 _crossing_e.xcrossing.root = leave_root;
176 dispatch(_crossing_e);
179 _crossing_e.xcrossing.type = EnterNotify;
180 _crossing_e.xcrossing.window = enter;
181 _crossing_e.xcrossing.root = enter_root;
182 dispatch(_crossing_e);
186 void OtkEventDispatcher::dispatch(const XEvent &e) {
187 OtkEventHandler *handler;
188 OtkEventMap::iterator it;
193 it = _map.find(e.xany.window);
195 if (it != _map.end())
196 handler = it->second;
204 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
206 OtkEventMap::iterator it = _map.find(win);
207 if (it != _map.end())