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