]> icculus.org git repositories - mikachu/openbox.git/blob - src/actions.cc
when python_exec fails on user.py:
[mikachu/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   // run the PRESS python hook
90   // kill off the Button1Mask etc, only want the modifiers
91   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
92                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
93   int screen;
94   Client *c = openbox->findClient(e.window);
95   if (c)
96     screen = c->screen();
97   else
98     screen = otk::display->findScreen(e.root)->screen();
99   MouseData data(screen, c, e.time, state, e.button, context,
100                  MouseAction::Press);
101   openbox->bindings()->fireButton(&data);
102     
103   if (_button) return; // won't count toward CLICK events
104
105   _button = e.button;
106
107   if (context == MouseContext::Window) {
108     /*
109       Because of how events are grabbed on the client window, we can't get
110       ButtonRelease events, so instead we simply manufacture them here, so that
111       clicks/doubleclicks etc still work.
112     */
113     //XButtonEvent ev = e;
114     //ev.type = ButtonRelease;
115     buttonReleaseHandler(e);
116   }
117 }
118   
119
120 void Actions::buttonReleaseHandler(const XButtonEvent &e)
121 {
122   otk::EventHandler::buttonReleaseHandler(e);
123   removePress(e);
124   
125   MouseContext::MC context;
126   EventHandler *h = openbox->findHandler(e.window);
127   Frame *f = dynamic_cast<Frame*>(h);
128   if (f)
129     context= f->mouseContext(e.window);
130   else if (dynamic_cast<Client*>(h))
131     context = MouseContext::Window;
132   else if (dynamic_cast<Screen*>(h))
133     context = MouseContext::Root;
134   else
135     return; // not a valid mouse context
136
137   // run the RELEASE python hook
138   // kill off the Button1Mask etc, only want the modifiers
139   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
140                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
141   int screen;
142   Client *c = openbox->findClient(e.window);
143   if (c)
144     screen = c->screen();
145   else
146     screen = otk::display->findScreen(e.root)->screen();
147   MouseData data(screen, c, e.time, state, e.button, context,
148                  MouseAction::Release);
149   openbox->bindings()->fireButton(&data);
150
151   // not for the button we're watching?
152   if (_button != e.button) return;
153
154   _button = 0;
155   _dragging = false;
156
157   // find the area of the window
158   XWindowAttributes attr;
159   if (!XGetWindowAttributes(**otk::display, e.window, &attr)) return;
160
161   // if not on the window any more, it isnt a CLICK
162   if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
163         e.x < attr.width && e.y < attr.height))
164     return;
165
166   // run the CLICK python hook
167   data.action = MouseAction::Click;
168   openbox->bindings()->fireButton(&data);
169     
170   long dblclick = openbox->screen(screen)->config().double_click_delay;
171   if (e.time - _release.time < (unsigned)dblclick &&
172       _release.win == e.window && _release.button == e.button) {
173
174     // run the DOUBLECLICK python hook
175     data.action = MouseAction::DoubleClick;
176     openbox->bindings()->fireButton(&data);
177     
178     // reset so you cant triple click for 2 doubleclicks
179     _release.win = 0;
180     _release.button = 0;
181     _release.time = 0;
182   } else {
183     // save the button release, might be part of a double click
184     _release.win = e.window;
185     _release.button = e.button;
186     _release.time = e.time;
187   }
188 }
189
190
191 void Actions::enterHandler(const XCrossingEvent &e)
192 {
193   otk::EventHandler::enterHandler(e);
194   
195   // run the ENTER python hook
196   int screen;
197   Client *c = openbox->findClient(e.window);
198   if (c)
199     screen = c->screen();
200   else
201     screen = otk::display->findScreen(e.root)->screen();
202   EventData data(screen, c, EventAction::EnterWindow, e.state);
203   openbox->bindings()->fireEvent(&data);
204 }
205
206
207 void Actions::leaveHandler(const XCrossingEvent &e)
208 {
209   otk::EventHandler::leaveHandler(e);
210
211   // run the LEAVE 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::LeaveWindow, e.state);
219   openbox->bindings()->fireEvent(&data);
220 }
221
222
223 void Actions::keyPressHandler(const XKeyEvent &e)
224 {
225   otk::EventHandler::keyPressHandler(e);
226
227   // kill off the Button1Mask etc, only want the modifiers
228   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
229                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
230
231   // add to the state the mask of the modifier being pressed, if it is
232   // a modifier key being pressed (this is a little ugly..)
233   const XModifierKeymap *map = otk::display->modifierMap();
234   const int mask_table[] = {
235     ShiftMask, LockMask, ControlMask, Mod1Mask,
236     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
237   };
238   KeyCode *kp = map->modifiermap;
239   for (int i = 0, n = sizeof(mask_table)/sizeof(mask_table[0]); i < n; ++i) {
240     for (int k = 0; k < map->max_keypermod; ++k) {
241       if (*kp == e.keycode) { // found the keycode
242         state |= mask_table[i]; // add the mask for it
243         i = n; // cause the first loop to break;
244         break; // get outta here!
245       }
246       ++kp;
247     }
248   }
249   
250   openbox->bindings()->
251     fireKey(otk::display->findScreen(e.root)->screen(),
252             state, e.keycode, e.time, KeyAction::Press);
253 }
254
255
256 void Actions::keyReleaseHandler(const XKeyEvent &e)
257 {
258   otk::EventHandler::keyReleaseHandler(e);
259
260   // kill off the Button1Mask etc, only want the modifiers
261   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
262                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
263
264   // remove from the state the mask of the modifier being released, if it is
265   // a modifier key being released (this is a little ugly..)
266   const XModifierKeymap *map = otk::display->modifierMap();
267   const int mask_table[] = {
268     ShiftMask, LockMask, ControlMask, Mod1Mask,
269     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
270   };
271   KeyCode *kp = map->modifiermap;
272   for (int i = 0, n = sizeof(mask_table)/sizeof(mask_table[0]); i < n; ++i) {
273     for (int k = 0; k < map->max_keypermod; ++k) {
274       if (*kp == e.keycode) { // found the keycode
275         state &= ~mask_table[i]; // remove the mask for it
276         i = n; // cause the first loop to break;
277         break; // get outta here!
278       }
279       ++kp;
280     }
281   }
282   
283   openbox->bindings()->
284     fireKey(otk::display->findScreen(e.root)->screen(),
285             state, e.keycode, e.time, KeyAction::Release);
286 }
287
288
289 void Actions::motionHandler(const XMotionEvent &e)
290 {
291   otk::EventHandler::motionHandler(e);
292
293   if (!e.same_screen) return; // this just gets stupid
294
295   if (e.window != _posqueue[0]->win) return;
296   
297   MouseContext::MC context;
298   EventHandler *h = openbox->findHandler(e.window);
299   Frame *f = dynamic_cast<Frame*>(h);
300   if (f)
301     context= f->mouseContext(e.window);
302   else if (dynamic_cast<Client*>(h))
303     context = MouseContext::Window;
304   else if (dynamic_cast<Screen*>(h))
305     context = MouseContext::Root;
306   else
307     return; // not a valid mouse context
308
309   int x_root = e.x_root, y_root = e.y_root;
310
311   // compress changes to a window into a single change
312   XEvent ce;
313   while (XCheckTypedWindowEvent(**otk::display, e.window, e.type, &ce)) {
314     x_root = e.x_root;
315     y_root = e.y_root;
316   }
317
318   int screen;
319   Client *c = openbox->findClient(e.window);
320   if (c)
321     screen = c->screen();
322   else
323     screen = otk::display->findScreen(e.root)->screen();
324
325   if (!_dragging) {
326     int dx = x_root - _posqueue[0]->pos.x();
327     int dy = y_root - _posqueue[0]->pos.y();
328     long threshold = openbox->screen(screen)->config().drag_threshold;
329     if (!(std::abs(dx) >= threshold || std::abs(dy) >= threshold))
330       return; // not at the threshold yet
331   }
332   _dragging = true; // in a drag now
333   
334   // check if the movement is more than the threshold
335
336   // run the MOTION python hook
337   // kill off the Button1Mask etc, only want the modifiers
338   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
339                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
340   unsigned int button = _posqueue[0]->button;
341   MouseData data(screen, c, e.time, state, button, context,
342                  MouseAction::Motion, x_root, y_root,
343                  _posqueue[0]->pos, _posqueue[0]->clientarea);
344   openbox->bindings()->fireButton(&data);
345 }
346
347 #ifdef    XKB
348 void Actions::xkbHandler(const XkbEvent &e)
349 {
350   Window w;
351   int screen;
352   
353   otk::EventHandler::xkbHandler(e);
354
355   switch (((XkbAnyEvent*)&e)->xkb_type) {
356   case XkbBellNotify:
357     w = ((XkbBellNotifyEvent*)&e)->window;
358     Client *c = openbox->findClient(w);
359     if (c)
360       screen = c->screen();
361     else
362       screen = openbox->focusedScreen()->number();
363     EventData data(screen, c, EventAction::Bell, 0);
364     openbox->bindings()->fireEvent(&data);
365     break;
366   }
367 }
368 #endif // XKB
369
370 }
371