]> icculus.org git repositories - mikachu/openbox.git/blob - src/actions.cc
labels are not the size of buttons
[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 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 = openbox->screen(screen)->config().double_click_delay;
149   if (e.time - _release.time < (unsigned)dblclick &&
150       _release.win == e.window && _release.button == e.button) {
151
152     // run the DOUBLECLICK python hook
153     data.action = MouseAction::DoubleClick;
154     openbox->bindings()->fireButton(&data);
155     
156     // reset so you cant triple click for 2 doubleclicks
157     _release.win = 0;
158     _release.button = 0;
159     _release.time = 0;
160   } else {
161     // save the button release, might be part of a double click
162     _release.win = e.window;
163     _release.button = e.button;
164     _release.time = e.time;
165   }
166 }
167
168
169 void Actions::enterHandler(const XCrossingEvent &e)
170 {
171   otk::EventHandler::enterHandler(e);
172   
173   // run the ENTER python hook
174   int screen;
175   Client *c = openbox->findClient(e.window);
176   if (c)
177     screen = c->screen();
178   else
179     screen = otk::display->findScreen(e.root)->screen();
180   EventData data(screen, c, EventAction::EnterWindow, e.state);
181   openbox->bindings()->fireEvent(&data);
182 }
183
184
185 void Actions::leaveHandler(const XCrossingEvent &e)
186 {
187   otk::EventHandler::leaveHandler(e);
188
189   // run the LEAVE python hook
190   int screen;
191   Client *c = openbox->findClient(e.window);
192   if (c)
193     screen = c->screen();
194   else
195     screen = otk::display->findScreen(e.root)->screen();
196   EventData data(screen, c, EventAction::LeaveWindow, e.state);
197   openbox->bindings()->fireEvent(&data);
198 }
199
200
201 void Actions::keyPressHandler(const XKeyEvent &e)
202 {
203   otk::EventHandler::keyPressHandler(e);
204
205   // kill off the Button1Mask etc, only want the modifiers
206   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
207                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
208
209   // add to the state the mask of the modifier being pressed, if it is
210   // a modifier key being pressed (this is a little ugly..)
211   const XModifierKeymap *map = otk::display->modifierMap();
212   const int mask_table[] = {
213     ShiftMask, LockMask, ControlMask, Mod1Mask,
214     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
215   };
216   KeyCode *kp = map->modifiermap;
217   for (int i = 0, n = sizeof(mask_table)/sizeof(mask_table[0]); i < n; ++i) {
218     for (int k = 0; k < map->max_keypermod; ++k) {
219       if (*kp == e.keycode) { // found the keycode
220         state |= mask_table[i]; // add the mask for it
221         i = n; // cause the first loop to break;
222         break; // get outta here!
223       }
224       ++kp;
225     }
226   }
227   
228   openbox->bindings()->
229     fireKey(otk::display->findScreen(e.root)->screen(),
230             state, e.keycode, e.time, KeyAction::Press);
231 }
232
233
234 void Actions::keyReleaseHandler(const XKeyEvent &e)
235 {
236   otk::EventHandler::keyReleaseHandler(e);
237
238   // kill off the Button1Mask etc, only want the modifiers
239   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
240                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
241
242   // remove from the state the mask of the modifier being released, if it is
243   // a modifier key being released (this is a little ugly..)
244   const XModifierKeymap *map = otk::display->modifierMap();
245   const int mask_table[] = {
246     ShiftMask, LockMask, ControlMask, Mod1Mask,
247     Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
248   };
249   KeyCode *kp = map->modifiermap;
250   for (int i = 0, n = sizeof(mask_table)/sizeof(mask_table[0]); i < n; ++i) {
251     for (int k = 0; k < map->max_keypermod; ++k) {
252       if (*kp == e.keycode) { // found the keycode
253         state &= ~mask_table[i]; // remove the mask for it
254         i = n; // cause the first loop to break;
255         break; // get outta here!
256       }
257       ++kp;
258     }
259   }
260   
261   openbox->bindings()->
262     fireKey(otk::display->findScreen(e.root)->screen(),
263             state, e.keycode, e.time, KeyAction::Release);
264 }
265
266
267 void Actions::motionHandler(const XMotionEvent &e)
268 {
269   otk::EventHandler::motionHandler(e);
270
271   if (!e.same_screen) return; // this just gets stupid
272
273   if (e.window != _press.win) return;
274   
275   MouseContext::MC context;
276   EventHandler *h = openbox->findHandler(e.window);
277   Frame *f = dynamic_cast<Frame*>(h);
278   if (f)
279     context= f->mouseContext(e.window);
280   else if (dynamic_cast<Client*>(h))
281     context = MouseContext::Window;
282   else if (dynamic_cast<Screen*>(h))
283     context = MouseContext::Root;
284   else
285     return; // not a valid mouse context
286
287   int x_root = e.x_root, y_root = e.y_root;
288
289   // compress changes to a window into a single change
290   XEvent ce;
291   while (XCheckTypedWindowEvent(**otk::display, e.window, e.type, &ce)) {
292     x_root = e.x_root;
293     y_root = e.y_root;
294   }
295
296   int screen;
297   Client *c = openbox->findClient(e.window);
298   if (c)
299     screen = c->screen();
300   else
301     screen = otk::display->findScreen(e.root)->screen();
302
303   if (!_dragging) {
304     int dx = x_root - _press.pos.x();
305     int dy = y_root - _press.pos.y();
306     long threshold = openbox->screen(screen)->config().drag_threshold;
307     if (!(std::abs(dx) >= threshold || std::abs(dy) >= threshold))
308       return; // not at the threshold yet
309   }
310   _dragging = true; // in a drag now
311   
312   // check if the movement is more than the threshold
313
314   // run the MOTION python hook
315   // kill off the Button1Mask etc, only want the modifiers
316   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
317                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
318   unsigned int button = _press.button;
319   MouseData data(screen, c, e.time, state, button, context,
320                  MouseAction::Motion, x_root, y_root,
321                  _press.pos, _press.clientarea);
322   openbox->bindings()->fireButton(&data);
323 }
324
325 #ifdef    XKB
326 void Actions::xkbHandler(const XkbEvent &e)
327 {
328   Window w;
329   int screen;
330   
331   otk::EventHandler::xkbHandler(e);
332
333   switch (((XkbAnyEvent*)&e)->xkb_type) {
334   case XkbBellNotify:
335     w = ((XkbBellNotifyEvent*)&e)->window;
336     Client *c = openbox->findClient(w);
337     if (c)
338       screen = c->screen();
339     else
340       screen = openbox->focusedScreen()->number();
341     EventData data(screen, c, EventAction::Bell, 0);
342     openbox->bindings()->fireEvent(&data);
343     break;
344   }
345 }
346 #endif // XKB
347
348 }
349