]> icculus.org git repositories - mikachu/openbox.git/blob - otk/eventdispatcher.cc
ignore errors when proxying configurerequests
[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 EventDispatcher::EventDispatcher()
14   : _fallback(0), _master(0)
15 {
16 }
17
18 EventDispatcher::~EventDispatcher()
19 {
20 }
21
22 void EventDispatcher::clearAllHandlers(void)
23 {
24   _map.clear();
25 }
26
27 void EventDispatcher::registerHandler(Window id, EventHandler *handler)
28 {
29   _map.insert(std::pair<Window, EventHandler*>(id, handler));
30 }
31
32 void EventDispatcher::clearHandler(Window id)
33 {
34   _map.erase(id);
35 }
36
37 void EventDispatcher::dispatchEvents(void)
38 {
39   XEvent e;
40
41   while (XPending(**display)) {
42     XNextEvent(**display, &e);
43
44 #if 0//defined(DEBUG)
45     printf("Event %d window %lx\n", e.type, e.xany.window);
46 #endif
47
48     if (e.type == FocusIn || e.type == FocusOut) {
49       // focus events are a beast all their own.. yuk, hate, etc.
50       dispatchFocus(e);
51     } else {    
52       Window win;
53
54       // pick a window
55       switch (e.type) {
56       case UnmapNotify:
57         win = e.xunmap.window;
58         break;
59       case DestroyNotify:
60         win = e.xdestroywindow.window;
61         break;
62       case ConfigureRequest:
63         win = e.xconfigurerequest.window;
64         break;
65       default:
66         win = e.xany.window;
67       }
68     
69       // grab the lasttime and hack up the modifiers
70       switch (e.type) {
71       case ButtonPress:
72       case ButtonRelease:
73         _lasttime = e.xbutton.time;
74         e.xbutton.state &= ~(LockMask | display->numLockMask() |
75                              display->scrollLockMask());
76         break;
77       case KeyPress:
78         e.xkey.state &= ~(LockMask | display->numLockMask() |
79                           display->scrollLockMask());
80         break;
81       case MotionNotify:
82         _lasttime = e.xmotion.time;
83         e.xmotion.state &= ~(LockMask | display->numLockMask() |
84                              display->scrollLockMask());
85         break;
86       case PropertyNotify:
87         _lasttime = e.xproperty.time;
88         break;
89       case EnterNotify:
90       case LeaveNotify:
91         _lasttime = e.xcrossing.time;
92         if (e.xcrossing.mode != NotifyNormal)
93           continue; // skip me!
94         break;
95       }
96
97       dispatch(win, e);
98     }
99   }
100 }
101
102 void EventDispatcher::dispatchFocus(const XEvent &e)
103 {
104 //  printf("focus %s detail %d -> 0x%lx\n",
105 //         (e.xfocus.type == FocusIn ? "IN" : "OUT"),
106 //         e.xfocus.detail, e.xfocus.window);
107   // ignore focus changes from grabs
108   if (e.xfocus.mode == NotifyGrab) //|| e.xfocus.mode == NotifyUngrab ||
109       // From Metacity, from WindowMaker, ignore all funky pointer root events
110       // its commented out cuz I don't think we need this at all. If problems
111       // arise we can look into it
112       //e.xfocus.detail > NotifyNonlinearVirtual)
113     return;
114   
115   if (e.type == FocusIn) {
116     //printf("Got FocusIn!\n");
117
118     // send a FocusIn to whatever was just focused
119     dispatch(e.xfocus.window, e);
120     //printf("Sent FocusIn 0x%lx\n", e.xfocus.window);
121
122   } else if (e.type == FocusOut) {
123     //printf("Got FocusOut!\n");
124
125     // FocusOut events just make us look for FocusIn events. They are ignored
126     // otherwise.
127     XEvent fi;
128     if (XCheckTypedEvent(**display, FocusIn, &fi)) {
129       //printf("Found FocusIn\n");
130       dispatchFocus(fi);
131       // dont unfocus the window we just focused!
132       if (fi.xfocus.window == e.xfocus.window)
133         return;
134     }
135
136     dispatch(e.xfocus.window, e);
137     //printf("Sent FocusOut 0x%lx\n", e.xfocus.window);
138   }
139 }
140
141 void EventDispatcher::dispatch(Window win, const XEvent &e)
142 {
143   EventHandler *handler = 0;
144   EventMap::iterator it;
145
146   // master gets everything first
147   if (_master)
148     _master->handle(e);
149
150   // find handler for the chosen window
151   it = _map.find(win);
152
153   if (it != _map.end()) {
154     // if we found a handler
155     handler = it->second;
156   } else if (e.type == ConfigureRequest) {
157     // unhandled configure requests must be used to configure the window
158     // directly
159     XWindowChanges xwc;
160       
161     xwc.x = e.xconfigurerequest.x;
162     xwc.y = e.xconfigurerequest.y;
163     xwc.width = e.xconfigurerequest.width;
164     xwc.height = e.xconfigurerequest.height;
165     xwc.border_width = e.xconfigurerequest.border_width;
166     xwc.sibling = e.xconfigurerequest.above;
167     xwc.stack_mode = e.xconfigurerequest.detail;
168
169     // we are not to be held responsible if someone sends us an invalid
170     // request!
171     display->setIgnoreErrors(true); 
172     XConfigureWindow(**display, e.xconfigurerequest.window,
173                      e.xconfigurerequest.value_mask, &xwc);
174     display->setIgnoreErrors(false);
175   } else {
176     // grab a falback if it exists
177     handler = _fallback;
178   }
179
180   if (handler)
181     handler->handle(e);
182 }
183
184 EventHandler *EventDispatcher::findHandler(Window win)
185 {
186   EventMap::iterator it = _map.find(win);
187   if (it != _map.end())
188     return it->second;
189   return 0;
190 }
191
192 }