]> icculus.org git repositories - mikachu/openbox.git/blob - otk/eventdispatcher.cc
fire the master first
[mikachu/openbox.git] / 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 //    printf("FOCUSING: %lx\n", focus);
131     _focus_e.xfocus.type = FocusIn;
132     _focus_e.xfocus.window = focus;
133     dispatch(_focus_e);
134
135     if (_focus != None) {
136 //      printf("UNFOCUSING: %lx\n", _focus);
137       _focus_e.xfocus.type = FocusOut;
138       _focus_e.xfocus.window = _focus;
139       dispatch(_focus_e);
140     }
141     
142     _focus = focus;
143   }
144   
145   if (leave != None) {
146     _crossing_e.xcrossing.type = LeaveNotify;
147     _crossing_e.xcrossing.window = leave;
148     _crossing_e.xcrossing.root = leave_root;
149     dispatch(_crossing_e);
150   }
151   if (enter != None) {
152     _crossing_e.xcrossing.type = EnterNotify;
153     _crossing_e.xcrossing.window = enter;
154     _crossing_e.xcrossing.root = enter_root;
155     dispatch(_crossing_e);
156   }
157 }
158
159 void OtkEventDispatcher::dispatch(const XEvent &e) {
160   OtkEventHandler *handler;
161   OtkEventMap::iterator it;
162
163   if (_master)
164     _master->handle(e);
165
166   it = _map.find(e.xany.window);
167   
168   if (it != _map.end())
169     handler = it->second;
170   else
171     handler = _fallback;
172
173   if (handler)
174     handler->handle(e);
175 }
176
177 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
178 {
179   OtkEventMap::iterator it = _map.find(win);
180   if (it != _map.end())
181     return it->second;
182   return 0;
183 }
184
185 }