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 // 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);
65 it->second->handle(e);
67 // unhandled configure requests must be used to configure the window
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;
79 XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
80 e.xconfigurerequest.value_mask, &xwc);
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;
88 printf("FocusIn focus=%lx unfocus=%lx\n", focus, unfocus);
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;
95 printf("FocusOut focus=%lx unfocus=%lx\n", focus, unfocus);
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);
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);
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;
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;
136 // printf("FOCUSING: %lx\n", focus);
137 _focus_e.xfocus.type = FocusIn;
138 _focus_e.xfocus.window = focus;
145 _crossing_e.xcrossing.type = LeaveNotify;
146 _crossing_e.xcrossing.window = leave;
147 _crossing_e.xcrossing.root = leave_root;
148 dispatch(_crossing_e);
151 _crossing_e.xcrossing.type = EnterNotify;
152 _crossing_e.xcrossing.window = enter;
153 _crossing_e.xcrossing.root = enter_root;
154 dispatch(_crossing_e);
158 void OtkEventDispatcher::dispatch(const XEvent &e) {
159 OtkEventHandler *handler;
160 OtkEventMap::iterator it;
162 it = _map.find(e.xany.window);
164 if (it != _map.end())
165 handler = it->second;
176 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
178 OtkEventMap::iterator it = _map.find(win);
179 if (it != _map.end())