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