]> icculus.org git repositories - mikachu/openbox.git/blob - src/actions.cc
moving a window is possible once again
[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 unsigned int OBActions::DOUBLECLICKDELAY = 300;
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   printf("press %d x:%d y:%d winx:%d winy:%d\n", e.button, e.x_root, e.y_root, c->area().x(), c->area().y());
49 }
50
51 void OBActions::removePress(const XButtonEvent &e)
52 {
53   ButtonPressAction *a = 0;
54   for (int i=0; i<BUTTONS; ++i) {
55     if (_posqueue[i]->button == e.button)
56       a = _posqueue[i];
57     if (a) // found one and removed it
58       _posqueue[i] = _posqueue[i+1];
59   }
60   if (a) { // found one
61     _posqueue[BUTTONS-1] = a;
62     a->button = 0;
63   }
64 }
65
66 void OBActions::buttonPressHandler(const XButtonEvent &e)
67 {
68   OtkEventHandler::buttonPressHandler(e);
69   insertPress(e);
70   
71   // run the PRESS python hook
72   OBWidget *w = dynamic_cast<OBWidget*>
73     (Openbox::instance->findHandler(e.window));
74   assert(w); // everything should be a widget
75
76   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
77                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
78   ButtonData *data = new_button_data(e.window, e.time, state, e.button,
79                                      w->mcontext(), MousePress);
80   Openbox::instance->bindings()->fire(data);
81   Py_DECREF((PyObject*)data);
82     
83   if (_button) return; // won't count toward CLICK events
84
85   _button = e.button;
86 }
87   
88
89 void OBActions::buttonReleaseHandler(const XButtonEvent &e)
90 {
91   OtkEventHandler::buttonReleaseHandler(e);
92   removePress(e);
93   
94   OBWidget *w = dynamic_cast<OBWidget*>
95     (Openbox::instance->findHandler(e.window));
96   assert(w); // everything should be a widget
97
98   // not for the button we're watching?
99   if (_button != e.button) return;
100
101   _button = 0;
102
103   // find the area of the window
104   XWindowAttributes attr;
105   if (!XGetWindowAttributes(otk::OBDisplay::display, e.window, &attr)) return;
106
107   // if not on the window any more, it isnt a CLICK
108   if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
109         e.x < attr.width && e.y < attr.height))
110     return;
111
112   // run the CLICK python hook
113   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
114                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
115   ButtonData *data = new_button_data(e.window, e.time, state, e.button,
116                                      w->mcontext(), MouseClick);
117   Openbox::instance->bindings()->fire(data);
118     
119
120   if (e.time - _release.time < DOUBLECLICKDELAY &&
121       _release.win == e.window && _release.button == e.button) {
122
123     // run the DOUBLECLICK python hook
124     data->action = MouseDoubleClick;
125     Openbox::instance->bindings()->fire(data);
126     
127     // reset so you cant triple click for 2 doubleclicks
128     _release.win = 0;
129     _release.button = 0;
130     _release.time = 0;
131   } else {
132     // save the button release, might be part of a double click
133     _release.win = e.window;
134     _release.button = e.button;
135     _release.time = e.time;
136   }
137
138   Py_DECREF((PyObject*)data);
139 }
140
141
142 void OBActions::enterHandler(const XCrossingEvent &e)
143 {
144   OtkEventHandler::enterHandler(e);
145   
146   OBWidget *w = dynamic_cast<OBWidget*>
147     (Openbox::instance->findHandler(e.window));
148
149   // run the ENTER python hook
150   doCallback(Action_EnterWindow, e.window,
151              (OBWidget::WidgetType)(w ? w->type():-1), e.state, 0, 0, 0, 0);
152 }
153
154
155 void OBActions::leaveHandler(const XCrossingEvent &e)
156 {
157   OtkEventHandler::leaveHandler(e);
158
159   OBWidget *w = dynamic_cast<OBWidget*>
160     (Openbox::instance->findHandler(e.window));
161
162   // run the LEAVE python hook
163   doCallback(Action_LeaveWindow, e.window,
164              (OBWidget::WidgetType)(w ? w->type():-1), e.state, 0, 0, 0, 0);
165 }
166
167
168 void OBActions::keyPressHandler(const XKeyEvent &e)
169 {
170 //  OBWidget *w = dynamic_cast<OBWidget*>
171 //    (Openbox::instance->findHandler(e.window));
172
173   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
174                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
175   Openbox::instance->bindings()->fire(state, e.keycode, e.time);
176 }
177
178
179 void OBActions::motionHandler(const XMotionEvent &e)
180 {
181   if (!e.same_screen) return; // this just gets stupid
182
183   int x_root = e.x_root, y_root = e.y_root;
184   
185   // compress changes to a window into a single change
186   XEvent ce;
187   while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
188     if (ce.xmotion.window != e.window) {
189       XPutBackEvent(otk::OBDisplay::display, &ce);
190       break;
191     } else {
192       x_root = e.x_root;
193       y_root = e.y_root;
194     }
195   }
196
197   OBWidget *w = dynamic_cast<OBWidget*>
198     (Openbox::instance->findHandler(e.window));
199   assert(w); // everything should be a widget
200
201   // run the MOTION python hook
202   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
203                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
204   unsigned int button = _posqueue[0]->button;
205   printf("motion %d x:%d y:%d winx:%d winy:%d\n", button, x_root, y_root,
206          _posqueue[0]->clientarea.x(), _posqueue[0]->clientarea.y());
207   MotionData *data = new_motion_data(e.window, e.time, state, button,
208                                      w->mcontext(), MouseMotion,
209                                      x_root, y_root, _posqueue[0]->pos,
210                                      _posqueue[0]->clientarea);
211   Openbox::instance->bindings()->fire((ButtonData*)data);
212   Py_DECREF((PyObject*)data);
213 }
214
215 void OBActions::mapRequestHandler(const XMapRequestEvent &e)
216 {
217   doCallback(Action_NewWindow, e.window, (OBWidget::WidgetType)-1,
218              0, 0, 0, 0, 0);
219 }
220
221 void OBActions::unmapHandler(const XUnmapEvent &e)
222 {
223   (void)e;
224   doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
225              0, 0, 0, 0, 0);
226 }
227
228 void OBActions::destroyHandler(const XDestroyWindowEvent &e)
229 {
230   (void)e;
231   doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
232              0, 0, 0, 0, 0);
233 }
234
235 void OBActions::doCallback(ActionType action, Window window,
236                            OBWidget::WidgetType type, unsigned int state,
237                            unsigned int button, int xroot, int yroot,
238                            Time time)
239 {
240   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
241     _callbacks.equal_range(action);
242
243   CallbackMap::iterator it;
244 //  for (it = it_pair.first; it != it_pair.second; ++it)
245 //    python_callback(it->second, action, window, type, state,
246 //                    button, xroot, yroot, time);
247   // XXX do a callback
248 }
249
250 bool OBActions::registerCallback(ActionType action, PyObject *func,
251                                  bool atfront)
252 {
253   if (action < 0 || action >= OBActions::NUM_ACTIONS) {
254     return false;
255   }
256   if (!func)
257     return false;
258
259   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
260     _callbacks.equal_range(action);
261
262   CallbackMap::iterator it;
263   for (it = it_pair.first; it != it_pair.second; ++it)
264     if (it->second == func)
265       return true; // already in there
266   if (atfront)
267     _callbacks.insert(_callbacks.begin(), CallbackMapPair(action, func));
268   else
269     _callbacks.insert(CallbackMapPair(action, func));
270   Py_INCREF(func);
271   return true;
272 }
273
274 bool OBActions::unregisterCallback(ActionType action, PyObject *func)
275 {
276   if (action < 0 || action >= OBActions::NUM_ACTIONS) {
277     return false;
278   }
279   if (!func)
280     return false;
281   
282   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
283     _callbacks.equal_range(action);
284   
285   CallbackMap::iterator it;
286   for (it = it_pair.first; it != it_pair.second; ++it)
287     if (it->second == func)
288       break;
289   if (it != it_pair.second) { // its been registered before
290     Py_DECREF(func);
291     _callbacks.erase(it);
292   }
293   return true;
294 }
295
296 bool OBActions::unregisterAllCallbacks(ActionType action)
297 {
298   if (action < 0 || action >= OBActions::NUM_ACTIONS) {
299     return false;
300   }
301
302   while (!_callbacks.empty()) {
303     CallbackMap::iterator it = _callbacks.begin();
304     Py_DECREF(it->second);
305     _callbacks.erase(it);
306   }
307   return true;
308 }
309
310 }