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