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