]> icculus.org git repositories - mikachu/openbox.git/blob - otk/eventdispatcher.cc
build fixes for evetdispatcher
[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.mode = NotifyNormal;
17   _focus_e.xfocus.detail = NotifyNonlinear;
18 }
19
20 OtkEventDispatcher::~OtkEventDispatcher()
21 {
22 }
23
24 void OtkEventDispatcher::clearAllHandlers(void)
25 {
26   _map.clear();
27 }
28
29 void OtkEventDispatcher::registerHandler(Window id, OtkEventHandler *handler)
30 {
31   _map.insert(std::pair<Window, OtkEventHandler*>(id, handler));
32 }
33
34 void OtkEventDispatcher::clearHandler(Window id)
35 {
36   _map.erase(id);
37 }
38
39 void OtkEventDispatcher::dispatchEvents(void)
40 {
41   OtkEventMap::iterator it;
42   XEvent e;
43   Window focus = _focus;
44   Window unfocus = None;
45
46   while (XPending(OBDisplay::display)) {
47     XNextEvent(OBDisplay::display, &e);
48
49 #if 0
50     printf("Event %d window %lx\n", e.type, e.xany.window);
51 #endif
52
53     // these ConfigureRequests require some special attention
54     if (e.type == ConfigureRequest) {
55       // find the actual window! e.xany.window is the parent window
56       it = _map.find(e.xconfigurerequest.window);
57
58       if (it != _map.end())
59         it->second->handle(e);
60       else {
61         // unhandled configure requests must be used to configure the window
62         // directly
63         XWindowChanges xwc;
64
65         xwc.x = e.xconfigurerequest.x;
66         xwc.y = e.xconfigurerequest.y;
67         xwc.width = e.xconfigurerequest.width;
68         xwc.height = e.xconfigurerequest.height;
69         xwc.border_width = e.xconfigurerequest.border_width;
70         xwc.sibling = e.xconfigurerequest.above;
71         xwc.stack_mode = e.xconfigurerequest.detail;
72
73         XConfigureWindow(otk::OBDisplay::display, e.xconfigurerequest.window,
74                          e.xconfigurerequest.value_mask, &xwc);
75       }
76     // madly compress all focus events
77     } else if (e.type == FocusIn) {
78       // any other types are not ones we're interested in
79       if (e.xfocus.detail == NotifyNonlinear) {
80         if (e.xfocus.window != focus) {
81           unfocus = focus;
82           focus = e.xfocus.window;
83         }
84       }
85     } else if (e.type == FocusOut) {
86       // any other types are not ones we're interested in
87       if (e.xfocus.detail == NotifyNonlinear) {
88         if (e.xfocus.window == focus) {
89           unfocus = focus;
90           focus = None;
91         }
92       }
93     } else {
94       // normal events
95       dispatch(e);
96     }
97   }
98
99   if (focus != _focus) {
100     _focus_e.xfocus.type = FocusIn;
101     _focus_e.xfocus.window = focus;
102     dispatch(_focus_e);
103     _focus = focus;
104   }
105   if (unfocus != None) {
106     _focus_e.xfocus.type = FocusOut;
107     _focus_e.xfocus.window = unfocus;
108     dispatch(_focus_e);
109   }
110 }
111
112 void OtkEventDispatcher::dispatch(const XEvent &e) {
113   OtkEventHandler *handler;
114   OtkEventMap::iterator it;
115
116   it = _map.find(e.xany.window);
117   
118   if (it != _map.end())
119     handler = it->second;
120   else
121     handler = _fallback;
122
123   if (handler)
124     handler->handle(e);
125
126   if (_master)
127     _master->handle(e);
128 }
129
130 OtkEventHandler *OtkEventDispatcher::findHandler(Window win)
131 {
132   OtkEventMap::iterator it = _map.find(win);
133   if (it != _map.end())
134     return it->second;
135   return 0;
136 }
137
138 }