]> icculus.org git repositories - mikachu/openbox.git/blob - src/actions.cc
bind the exec properly
[mikachu/openbox.git] / src / actions.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "actions.hh"
8 #include "widgetbase.hh"
9 #include "openbox.hh"
10 #include "client.hh"
11 #include "screen.hh"
12 #include "python.hh"
13 #include "bindings.hh"
14 #include "otk/display.hh"
15
16 #include <stdio.h>
17
18 namespace ob {
19
20 const int OBActions::BUTTONS;
21
22 OBActions::OBActions()
23   : _button(0)
24 {
25   for (int i=0; i<BUTTONS; ++i)
26     _posqueue[i] = new ButtonPressAction();
27 }
28
29
30 OBActions::~OBActions()
31 {
32   for (int i=0; i<BUTTONS; ++i)
33     delete _posqueue[i];
34 }
35
36
37 void OBActions::insertPress(const XButtonEvent &e)
38 {
39   ButtonPressAction *a = _posqueue[BUTTONS - 1];
40   for (int i=BUTTONS-1; i>0;)
41     _posqueue[i] = _posqueue[--i];
42   _posqueue[0] = a;
43   a->button = e.button;
44   a->pos.setPoint(e.x_root, e.y_root);
45
46   OBClient *c = Openbox::instance->findClient(e.window);
47   if (c) a->clientarea = c->area();
48 }
49
50 void OBActions::removePress(const XButtonEvent &e)
51 {
52   ButtonPressAction *a = 0;
53   for (int i=0; i<BUTTONS; ++i) {
54     if (_posqueue[i]->button == e.button)
55       a = _posqueue[i];
56     if (a) // found one and removed it
57       _posqueue[i] = _posqueue[i+1];
58   }
59   if (a) { // found one
60     _posqueue[BUTTONS-1] = a;
61     a->button = 0;
62   }
63 }
64
65 void OBActions::buttonPressHandler(const XButtonEvent &e)
66 {
67   OtkEventHandler::buttonPressHandler(e);
68   insertPress(e);
69   
70   // run the PRESS python hook
71   OBWidget *w = dynamic_cast<OBWidget*>
72     (Openbox::instance->findHandler(e.window));
73   assert(w); // everything should be a widget
74
75   // kill off the Button1Mask etc, only want the modifiers
76   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
77                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
78   int screen;
79   OBClient *c = Openbox::instance->findClient(e.window);
80   if (c)
81     screen = c->screen();
82   else
83     screen = otk::OBDisplay::findScreen(e.root)->screen();
84   MouseData data(screen, c, e.time, state, e.button, w->mcontext(),
85                  MousePress);
86   Openbox::instance->bindings()->fireButton(&data);
87     
88   if (_button) return; // won't count toward CLICK events
89
90   _button = e.button;
91
92   if (w->mcontext() == MC_Window) {
93     /*
94       Because of how events are grabbed on the client window, we can't get
95       ButtonRelease events, so instead we simply manufacture them here, so that
96       clicks/doubleclicks etc still work.
97     */
98     //XButtonEvent ev = e;
99     //ev.type = ButtonRelease;
100     buttonReleaseHandler(e);
101   }
102 }
103   
104
105 void OBActions::buttonReleaseHandler(const XButtonEvent &e)
106 {
107   OtkEventHandler::buttonReleaseHandler(e);
108   removePress(e);
109   
110   OBWidget *w = dynamic_cast<OBWidget*>
111     (Openbox::instance->findHandler(e.window));
112   assert(w); // everything should be a widget
113
114   // not for the button we're watching?
115   if (_button != e.button) return;
116
117   _button = 0;
118
119   // find the area of the window
120   XWindowAttributes attr;
121   if (!XGetWindowAttributes(otk::OBDisplay::display, e.window, &attr)) return;
122
123   // if not on the window any more, it isnt a CLICK
124   if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
125         e.x < attr.width && e.y < attr.height))
126     return;
127
128   // run the CLICK python hook
129   // kill off the Button1Mask etc, only want the modifiers
130   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
131                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
132   int screen;
133   OBClient *c = Openbox::instance->findClient(e.window);
134   if (c)
135     screen = c->screen();
136   else
137     screen = otk::OBDisplay::findScreen(e.root)->screen();
138   MouseData data(screen, c, e.time, state, e.button, w->mcontext(),
139                  MouseClick);
140   Openbox::instance->bindings()->fireButton(&data);
141     
142
143   // XXX: dont load this every time!!@*
144   long dblclick;
145   if (!python_get_long("double_click_delay", &dblclick))
146     dblclick = 300;
147
148   if (e.time - _release.time < (unsigned)dblclick &&
149       _release.win == e.window && _release.button == e.button) {
150
151     // run the DOUBLECLICK python hook
152     data.action = MouseDoubleClick;
153     Openbox::instance->bindings()->fireButton(&data);
154     
155     // reset so you cant triple click for 2 doubleclicks
156     _release.win = 0;
157     _release.button = 0;
158     _release.time = 0;
159   } else {
160     // save the button release, might be part of a double click
161     _release.win = e.window;
162     _release.button = e.button;
163     _release.time = e.time;
164   }
165 }
166
167
168 void OBActions::enterHandler(const XCrossingEvent &e)
169 {
170   OtkEventHandler::enterHandler(e);
171   
172   // run the ENTER python hook
173   int screen;
174   OBClient *c = Openbox::instance->findClient(e.window);
175   if (c)
176     screen = c->screen();
177   else
178     screen = otk::OBDisplay::findScreen(e.root)->screen();
179   EventData data(screen, c, EventEnterWindow, e.state);
180   Openbox::instance->bindings()->fireEvent(&data);
181 }
182
183
184 void OBActions::leaveHandler(const XCrossingEvent &e)
185 {
186   OtkEventHandler::leaveHandler(e);
187
188   // run the LEAVE python hook
189   int screen;
190   OBClient *c = Openbox::instance->findClient(e.window);
191   if (c)
192     screen = c->screen();
193   else
194     screen = otk::OBDisplay::findScreen(e.root)->screen();
195   EventData data(screen, c, EventLeaveWindow, e.state);
196   Openbox::instance->bindings()->fireEvent(&data);
197 }
198
199
200 void OBActions::keyPressHandler(const XKeyEvent &e)
201 {
202   OtkEventHandler::keyPressHandler(e);
203
204   // kill off the Button1Mask etc, only want the modifiers
205   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
206                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
207   Openbox::instance->bindings()->
208     fireKey(otk::OBDisplay::findScreen(e.root)->screen(),
209             state, e.keycode, e.time);
210 }
211
212
213 void OBActions::motionHandler(const XMotionEvent &e)
214 {
215   OtkEventHandler::motionHandler(e);
216
217   if (!e.same_screen) return; // this just gets stupid
218
219   int x_root = e.x_root, y_root = e.y_root;
220   
221   // compress changes to a window into a single change
222   XEvent ce;
223   while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
224     if (ce.xmotion.window != e.window) {
225       XPutBackEvent(otk::OBDisplay::display, &ce);
226       break;
227     } else {
228       x_root = e.x_root;
229       y_root = e.y_root;
230     }
231   }
232
233   OBWidget *w = dynamic_cast<OBWidget*>
234     (Openbox::instance->findHandler(e.window));
235   assert(w); // everything should be a widget
236
237   // run the MOTION python hook
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   unsigned int button = _posqueue[0]->button;
242   int screen;
243   OBClient *c = Openbox::instance->findClient(e.window);
244   if (c)
245     screen = c->screen();
246   else
247     screen = otk::OBDisplay::findScreen(e.root)->screen();
248   MouseData data(screen, c, e.time, state, button, w->mcontext(), MouseMotion,
249                  x_root, y_root, _posqueue[0]->pos, _posqueue[0]->clientarea);
250   Openbox::instance->bindings()->fireButton(&data);
251 }
252
253 void OBActions::mapRequestHandler(const XMapRequestEvent &e)
254 {
255   OtkEventHandler::mapRequestHandler(e);
256   // do this in OBScreen::manageWindow
257 }
258
259 void OBActions::unmapHandler(const XUnmapEvent &e)
260 {
261   OtkEventHandler::unmapHandler(e);
262   // do this in OBScreen::unmanageWindow
263 }
264
265 void OBActions::destroyHandler(const XDestroyWindowEvent &e)
266 {
267   OtkEventHandler::destroyHandler(e);
268   // do this in OBScreen::unmanageWindow
269 }
270
271 #ifdef    XKB
272 void OBActions::xkbHandler(const XkbEvent &e)
273 {
274   Window w;
275   int screen;
276   
277   OtkEventHandler::xkbHandler(e);
278
279   switch (((XkbAnyEvent*)&e)->xkb_type) {
280   case XkbBellNotify:
281     w = ((XkbBellNotifyEvent*)&e)->window;
282     OBClient *c = Openbox::instance->findClient(w);
283     if (c)
284       screen = c->screen();
285     else
286       screen = Openbox::instance->focusedScreen()->number();
287     EventData data(screen, c, EventBell, 0);
288     Openbox::instance->bindings()->fireEvent(&data);
289     break;
290   }
291 }
292 #endif // XKB
293
294 }
295