]> icculus.org git repositories - dana/openbox.git/blob - src/actions.cc
new mouse button code is seeming to work. you can move windows
[dana/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 }
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
74 /*  doCallback(Action_ButtonPress, e.window,
75              (OBWidget::WidgetType)(w ? w->type():-1),
76              e.state, e.button, e.x_root, e.y_root, e.time);*/
77   if (w) {
78     Openbox::instance->bindings()->fire(MousePress, w->type(), e.window,
79                                         e.state, e.button,
80                                         e.x_root, e.y_root, e.time);
81   } else
82     assert(false); // why isnt there a type?
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
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 /*  doCallback(Action_Click, e.window,
114              (OBWidget::WidgetType)(w ? w->type():-1),
115              e.state, e.button, e.x_root, e.y_root, e.time);*/
116   if (w) {
117     Openbox::instance->bindings()->fire(MouseClick, w->type(), e.window,
118                                         e.state, e.button,
119                                         e.x_root, e.y_root, e.time);
120   } else
121     assert(false); // why isnt there a type?
122
123   if (e.time - _release.time < DOUBLECLICKDELAY &&
124       _release.win == e.window && _release.button == e.button) {
125
126     // run the DOUBLECLICK python hook
127 /*    doCallback(Action_DoubleClick, e.window,
128                (OBWidget::WidgetType)(w ? w->type():-1),
129                e.state, e.button, e.x_root, e.y_root, e.time);*/
130     if (w) {
131       Openbox::instance->bindings()->fire(MouseDoubleClick, w->type(),
132                                           e.window, e.state, e.button,
133                                           e.x_root, e.y_root, e.time);
134     } else
135       assert(false); // why isnt there a type?
136     
137     // reset so you cant triple click for 2 doubleclicks
138     _release.win = 0;
139     _release.button = 0;
140     _release.time = 0;
141   } else {
142     // save the button release, might be part of a double click
143     _release.win = e.window;
144     _release.button = e.button;
145     _release.time = e.time;
146   }
147 }
148
149
150 void OBActions::enterHandler(const XCrossingEvent &e)
151 {
152   OtkEventHandler::enterHandler(e);
153   
154   OBWidget *w = dynamic_cast<OBWidget*>
155     (Openbox::instance->findHandler(e.window));
156
157   // run the ENTER python hook
158   doCallback(Action_EnterWindow, e.window,
159              (OBWidget::WidgetType)(w ? w->type():-1), e.state, 0, 0, 0, 0);
160 }
161
162
163 void OBActions::leaveHandler(const XCrossingEvent &e)
164 {
165   OtkEventHandler::leaveHandler(e);
166
167   OBWidget *w = dynamic_cast<OBWidget*>
168     (Openbox::instance->findHandler(e.window));
169
170   // run the LEAVE python hook
171   doCallback(Action_LeaveWindow, e.window,
172              (OBWidget::WidgetType)(w ? w->type():-1), e.state, 0, 0, 0, 0);
173 }
174
175
176 void OBActions::keyPressHandler(const XKeyEvent &e)
177 {
178 //  OBWidget *w = dynamic_cast<OBWidget*>
179 //    (Openbox::instance->findHandler(e.window));
180
181   Openbox::instance->bindings()->fire(e.state, e.keycode, e.time);
182 }
183
184
185 void OBActions::motionHandler(const XMotionEvent &e)
186 {
187   if (!e.same_screen) return; // this just gets stupid
188
189   int x_root = e.x_root, y_root = e.y_root;
190   
191   // compress changes to a window into a single change
192   XEvent ce;
193   while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
194     if (ce.xmotion.window != e.window) {
195       XPutBackEvent(otk::OBDisplay::display, &ce);
196       break;
197     } else {
198       x_root = e.x_root;
199       y_root = e.y_root;
200     }
201   }
202
203   _dx = x_root - _posqueue[0]->pos.x(); _posqueue[0]->pos.setX(x_root);
204   _dy = y_root - _posqueue[0]->pos.y(); _posqueue[0]->pos.setY(y_root);
205   
206   OBWidget *w = dynamic_cast<OBWidget*>
207     (Openbox::instance->findHandler(e.window));
208
209   // XXX: i can envision all sorts of crazy shit with this.. gestures, etc
210   //      maybe that should all be done via python tho.. (or radial menus!)
211   // run the simple MOTION python hook for now...
212 /*  doCallback(Action_MouseMotion, e.window,
213              (OBWidget::WidgetType)(w ? w->type():-1),
214              e.state, (unsigned)-1, x_root, y_root, e.time);*/
215   if (w) {
216     Openbox::instance->bindings()->fire(MouseMotion, w->type(), e.window,
217                                         e.state, _posqueue[0]->button,
218                                         _dx, _dy, e.time);
219   } else
220     assert(false); // why isnt there a type?
221 }
222
223 void OBActions::mapRequestHandler(const XMapRequestEvent &e)
224 {
225   doCallback(Action_NewWindow, e.window, (OBWidget::WidgetType)-1,
226              0, 0, 0, 0, 0);
227 }
228
229 void OBActions::unmapHandler(const XUnmapEvent &e)
230 {
231   (void)e;
232   doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
233              0, 0, 0, 0, 0);
234 }
235
236 void OBActions::destroyHandler(const XDestroyWindowEvent &e)
237 {
238   (void)e;
239   doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
240              0, 0, 0, 0, 0);
241 }
242
243 void OBActions::doCallback(ActionType action, Window window,
244                            OBWidget::WidgetType type, unsigned int state,
245                            unsigned int button, int xroot, int yroot,
246                            Time time)
247 {
248   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
249     _callbacks.equal_range(action);
250
251   CallbackMap::iterator it;
252 //  for (it = it_pair.first; it != it_pair.second; ++it)
253 //    python_callback(it->second, action, window, type, state,
254 //                    button, xroot, yroot, time);
255   // XXX do a callback
256 }
257
258 bool OBActions::registerCallback(ActionType action, PyObject *func,
259                                  bool atfront)
260 {
261   if (action < 0 || action >= OBActions::NUM_ACTIONS) {
262     return false;
263   }
264   if (!func)
265     return false;
266
267   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
268     _callbacks.equal_range(action);
269
270   CallbackMap::iterator it;
271   for (it = it_pair.first; it != it_pair.second; ++it)
272     if (it->second == func)
273       return true; // already in there
274   if (atfront)
275     _callbacks.insert(_callbacks.begin(), CallbackMapPair(action, func));
276   else
277     _callbacks.insert(CallbackMapPair(action, func));
278   Py_INCREF(func);
279   return true;
280 }
281
282 bool OBActions::unregisterCallback(ActionType action, PyObject *func)
283 {
284   if (action < 0 || action >= OBActions::NUM_ACTIONS) {
285     return false;
286   }
287   if (!func)
288     return false;
289   
290   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
291     _callbacks.equal_range(action);
292   
293   CallbackMap::iterator it;
294   for (it = it_pair.first; it != it_pair.second; ++it)
295     if (it->second == func)
296       break;
297   if (it != it_pair.second) { // its been registered before
298     Py_DECREF(func);
299     _callbacks.erase(it);
300   }
301   return true;
302 }
303
304 bool OBActions::unregisterAllCallbacks(ActionType action)
305 {
306   if (action < 0 || action >= OBActions::NUM_ACTIONS) {
307     return false;
308   }
309
310   while (!_callbacks.empty()) {
311     CallbackMap::iterator it = _callbacks.begin();
312     Py_DECREF(it->second);
313     _callbacks.erase(it);
314   }
315   return true;
316 }
317
318 }