]> icculus.org git repositories - dana/openbox.git/blob - src/actions.cc
merge the C branch into HEAD
[dana/openbox.git] / src / actions.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "actions.hh"
6 #include "openbox.hh"
7 #include "client.hh"
8 #include "frame.hh"
9 #include "screen.hh"
10 #include "python.hh"
11 #include "bindings.hh"
12 #include "otk/display.hh"
13
14 #include <cstdio>
15 #include <algorithm>
16
17 namespace ob {
18
19 Actions::Actions()
20   : _dragging(false)
21 {
22 }
23
24
25 Actions::~Actions()
26 {
27 }
28
29
30 void Actions::buttonPressHandler(const XButtonEvent &e)
31 {
32   otk::EventHandler::buttonPressHandler(e);
33
34   MouseContext::MC context;
35   EventHandler *h = openbox->findHandler(e.window);
36   Frame *f = dynamic_cast<Frame*>(h);
37   if (f)
38     context= f->mouseContext(e.window);
39   else if (dynamic_cast<Client*>(h))
40     context = MouseContext::Window;
41   else if (dynamic_cast<Screen*>(h))
42     context = MouseContext::Root;
43   else
44     return; // not a valid mouse context
45
46   if (_press.button) {
47     unsigned int mask;
48     switch(_press.button) {
49     case Button1: mask = Button1Mask; break;
50     case Button2: mask = Button2Mask; break;
51     case Button3: mask = Button3Mask; break;
52     case Button4: mask = Button4Mask; break;
53     case Button5: mask = Button5Mask; break;
54     default: mask = 0; // on other buttons we have to assume its not pressed...
55     }
56     // was the button released but we didnt get the event? (pointergrabs cause
57     // this)
58     if (!(e.state & mask))
59       _press.button = 0;
60   }
61   
62   // run the PRESS python hook
63   // kill off the Button1Mask etc, only want the modifiers
64   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
65                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
66   int screen;
67   Client *c = openbox->findClient(e.window);
68   if (c)
69     screen = c->screen();
70   else
71     screen = otk::display->findScreen(e.root)->screen();
72   MouseData data(screen, c, e.time, state, e.button, context,
73                  MouseAction::Press);
74   openbox->bindings()->fireButton(&data);
75     
76   if (_press.button) return; // won't count toward CLICK events
77
78   _press.win = e.window;
79   _press.button = e.button;
80   _press.pos = otk::Point(e.x_root, e.y_root);
81   if (c)
82     _press.clientarea = c->area();
83
84   printf("press queue %u pressed %u\n", _press.button, e.button);
85
86   if (context == MouseContext::Window) {
87     /*
88       Because of how events are grabbed on the client window, we can't get
89       ButtonRelease events, so instead we simply manufacture them here, so that
90       clicks/doubleclicks etc still work.
91     */
92     //XButtonEvent ev = e;
93     //ev.type = ButtonRelease;
94     buttonReleaseHandler(e);
95   }
96 }
97   
98
99 void Actions::buttonReleaseHandler(const XButtonEvent &e)
100 {
101   otk::EventHandler::buttonReleaseHandler(e);
102   
103   MouseContext::MC context;
104   EventHandler *h = openbox->findHandler(e.window);
105   Frame *f = dynamic_cast<Frame*>(h);
106   if (f)
107     context= f->mouseContext(e.window);
108   else if (dynamic_cast<Client*>(h))
109     context = MouseContext::Window;
110   else if (dynamic_cast<Screen*>(h))
111     context = MouseContext::Root;
112   else
113     return; // not a valid mouse context
114
115   // run the RELEASE python hook
116   // kill off the Button1Mask etc, only want the modifiers
117   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
118                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
119   int screen;
120   Client *c = openbox->findClient(e.window);
121   if (c)
122     screen = c->screen();
123   else
124     screen = otk::display->findScreen(e.root)->screen();
125   MouseData data(screen, c, e.time, state, e.button, context,
126                  MouseAction::Release);
127   openbox->bindings()->fireButton(&data);
128
129   // not for the button we're watching?
130   if (_press.button != e.button) return;
131
132   _press.button = 0;
133   _dragging = false;
134
135   // find the area of the window
136   XWindowAttributes attr;
137   if (!XGetWindowAttributes(**otk::display, e.window, &attr)) return;
138
139   // if not on the window any more, it isnt a CLICK
140   if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
141         e.x < attr.width && e.y < attr.height))
142     return;
143
144   // run the CLICK python hook
145   data.action = MouseAction::Click;
146   openbox->bindings()->fireButton(&data);
147     
148   long dblclick = 0;
149   python_get_long("double_click_delay", &dblclick);
150   if (e.time - _release.time < (unsigned)dblclick &&
151       _release.win == e.window && _release.button == e.button) {
152
153     // run the DOUBLECLICK python hook
154     data.action = MouseAction::DoubleClick;
155     openbox->bindings()->fireButton(&data);
156     
157     // reset so you cant triple click for 2 doubleclicks
158     _release.win = 0;
159     _release.button = 0;
160     _release.time = 0;
161   } else {
162     // save the button release, might be part of a double click
163     _release.win = e.window;
164     _release.button = e.button;
165     _release.time = e.time;
166   }
167 }
168
169
170 void Actions::enterHandler(const XCrossingEvent &e)
171 {
172   otk::EventHandler::enterHandler(e);
173   
174   // run the ENTER python hook
175   int screen;
176   Client *c = openbox->findClient(e.window);
177   if (c)
178     screen = c->screen();
179   else
180     screen = otk::display->findScreen(e.root)->screen();
181   EventData data(screen, c, EventAction::EnterWindow, e.state);
182   openbox->bindings()->fireEvent(&data);
183 }
184
185
186 void Actions::leaveHandler(const XCrossingEvent &e)
187 {
188   otk::EventHandler::leaveHandler(e);
189
190   // run the LEAVE python hook
191   int screen;
192   Client *c = openbox->findClient(e.window);
193   if (c)
194     screen = c->screen();
195   else
196     screen = otk::display->findScreen(e.root)->screen();
197   EventData data(screen, c, EventAction::LeaveWindow, e.state);
198   openbox->bindings()->fireEvent(&data);
199 }
200
201
202 void Actions::keyPressHandler(const XKeyEvent &e)
203 {
204   otk::EventHandler::keyPressHandler(e);
205
206   // kill off the Button1Mask etc, only want the modifiers
207   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
208                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
209
210   // add to the state the mask of the modifier being pressed, if it is
211   // a modifier key being pressed (this is a little ugly..)
212   const XModifierKeymap *map = otk::display->modifierMap();
213   const int mask_table[] = {
214     ShiftMask, LockMask, ControlMask, Mod1Mask,
215     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
216   };
217   KeyCode *kp = map->modifiermap;
218   for (int i = 0, n = sizeof(mask_table)/sizeof(mask_table[0]); i < n; ++i) {
219     for (int k = 0; k < map->max_keypermod; ++k) {
220       if (*kp == e.keycode) { // found the keycode
221         state |= mask_table[i]; // add the mask for it
222         i = n; // cause the first loop to break;
223         break; // get outta here!
224       }
225       ++kp;
226     }
227   }
228   
229   openbox->bindings()->
230     fireKey(otk::display->findScreen(e.root)->screen(),
231             state, e.keycode, e.time, KeyAction::Press);
232 }
233
234
235 void Actions::keyReleaseHandler(const XKeyEvent &e)
236 {
237   otk::EventHandler::keyReleaseHandler(e);
238
239   // kill off the Button1Mask etc, only want the modifiers
240   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
241                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
242
243   // remove from the state the mask of the modifier being released, if it is
244   // a modifier key being released (this is a little ugly..)
245   const XModifierKeymap *map = otk::display->modifierMap();
246   const int mask_table[] = {
247     ShiftMask, LockMask, ControlMask, Mod1Mask,
248     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
249   };
250   KeyCode *kp = map->modifiermap;
251   for (int i = 0, n = sizeof(mask_table)/sizeof(mask_table[0]); i < n; ++i) {
252     for (int k = 0; k < map->max_keypermod; ++k) {
253       if (*kp == e.keycode) { // found the keycode
254         state &= ~mask_table[i]; // remove the mask for it
255         i = n; // cause the first loop to break;
256         break; // get outta here!
257       }
258       ++kp;
259     }
260   }
261   
262   openbox->bindings()->
263     fireKey(otk::display->findScreen(e.root)->screen(),
264             state, e.keycode, e.time, KeyAction::Release);
265 }
266
267
268 void Actions::motionHandler(const XMotionEvent &e)
269 {
270   otk::EventHandler::motionHandler(e);
271
272   if (!e.same_screen) return; // this just gets stupid
273
274   if (e.window != _press.win) return;
275   
276   MouseContext::MC context;
277   EventHandler *h = openbox->findHandler(e.window);
278   Frame *f = dynamic_cast<Frame*>(h);
279   if (f)
280     context= f->mouseContext(e.window);
281   else if (dynamic_cast<Client*>(h))
282     context = MouseContext::Window;
283   else if (dynamic_cast<Screen*>(h))
284     context = MouseContext::Root;
285   else
286     return; // not a valid mouse context
287
288   int x_root = e.x_root, y_root = e.y_root;
289
290   // compress changes to a window into a single change
291   XEvent ce;
292   while (XCheckTypedWindowEvent(**otk::display, e.window, e.type, &ce)) {
293     x_root = ce.x_root;
294     y_root = ce.y_root;
295   }
296
297   int screen;
298   Client *c = openbox->findClient(e.window);
299   if (c)
300     screen = c->screen();
301   else
302     screen = otk::display->findScreen(e.root)->screen();
303
304   if (!_dragging) {
305     int dx = x_root - _press.pos.x();
306     int dy = y_root - _press.pos.y();
307     long threshold = 0;
308     python_get_long("drag_threshold", &threshold);
309     if (!(std::abs(dx) >= threshold || std::abs(dy) >= threshold))
310       return; // not at the threshold yet
311   }
312   _dragging = true; // in a drag now
313   
314   // check if the movement is more than the threshold
315
316   // run the MOTION python hook
317   // kill off the Button1Mask etc, only want the modifiers
318   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
319                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
320   unsigned int button = _press.button;
321   MouseData data(screen, c, e.time, state, button, context,
322                  MouseAction::Motion, x_root, y_root,
323                  _press.pos, _press.clientarea);
324   openbox->bindings()->fireButton(&data);
325 }
326
327 #ifdef    XKB
328 void Actions::xkbHandler(const XkbEvent &e)
329 {
330   Window w;
331   int screen;
332   
333   otk::EventHandler::xkbHandler(e);
334
335   switch (((XkbAnyEvent*)&e)->xkb_type) {
336   case XkbBellNotify:
337     w = ((XkbBellNotifyEvent*)&e)->window;
338     Client *c = openbox->findClient(w);
339     if (c)
340       screen = c->screen();
341     else
342       screen = openbox->focusedScreen()->number();
343     EventData data(screen, c, EventAction::Bell, 0);
344     openbox->bindings()->fireEvent(&data);
345     break;
346   }
347 }
348 #endif // XKB
349
350 }
351