]> icculus.org git repositories - mikachu/openbox.git/blob - src/actions.cc
trasitioning for new button event handling
[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
21 OBActions::OBActions()
22   : _button(0)
23 {
24   // XXX: load a configuration out of somewhere
25
26 }
27
28
29 OBActions::~OBActions()
30 {
31 }
32
33
34 void OBActions::buttonPressHandler(const XButtonEvent &e)
35 {
36   OtkEventHandler::buttonPressHandler(e);
37   
38   // run the PRESS python hook
39   OBWidget *w = dynamic_cast<OBWidget*>
40     (Openbox::instance->findHandler(e.window));
41
42   doCallback(Action_ButtonPress, e.window,
43              (OBWidget::WidgetType)(w ? w->type():-1),
44              e.state, e.button, e.x_root, e.y_root, e.time);
45     
46   if (_button) return; // won't count toward CLICK events
47
48   _button = e.button;
49 }
50   
51
52 void OBActions::buttonReleaseHandler(const XButtonEvent &e)
53 {
54   OtkEventHandler::buttonReleaseHandler(e);
55   
56   OBWidget *w = dynamic_cast<OBWidget*>
57     (Openbox::instance->findHandler(e.window));
58
59   // not for the button we're watching?
60   if (_button != e.button) return;
61
62   _button = 0;
63
64   // find the area of the window
65   XWindowAttributes attr;
66   if (!XGetWindowAttributes(otk::OBDisplay::display, e.window, &attr)) return;
67
68   // if not on the window any more, it isnt a CLICK
69   if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
70         e.x < attr.width && e.y < attr.height))
71     return;
72
73   // run the CLICK python hook
74   doCallback(Action_Click, e.window,
75              (OBWidget::WidgetType)(w ? w->type():-1),
76              e.state, e.button, e.x_root, e.y_root, e.time);
77
78   if (e.time - _release.time < DOUBLECLICKDELAY &&
79       _release.win == e.window && _release.button == e.button) {
80
81     // run the DOUBLECLICK python hook
82     doCallback(Action_DoubleClick, e.window,
83                (OBWidget::WidgetType)(w ? w->type():-1),
84                e.state, e.button, e.x_root, e.y_root, e.time);
85     
86     // reset so you cant triple click for 2 doubleclicks
87     _release.win = 0;
88     _release.button = 0;
89     _release.time = 0;
90   } else {
91     // save the button release, might be part of a double click
92     _release.win = e.window;
93     _release.button = e.button;
94     _release.time = e.time;
95   }
96 }
97
98
99 void OBActions::enterHandler(const XCrossingEvent &e)
100 {
101   OtkEventHandler::enterHandler(e);
102   
103   OBWidget *w = dynamic_cast<OBWidget*>
104     (Openbox::instance->findHandler(e.window));
105
106   // run the ENTER python hook
107   doCallback(Action_EnterWindow, e.window,
108              (OBWidget::WidgetType)(w ? w->type():-1), e.state, 0, 0, 0, 0);
109 }
110
111
112 void OBActions::leaveHandler(const XCrossingEvent &e)
113 {
114   OtkEventHandler::leaveHandler(e);
115
116   OBWidget *w = dynamic_cast<OBWidget*>
117     (Openbox::instance->findHandler(e.window));
118
119   // run the LEAVE python hook
120   doCallback(Action_LeaveWindow, e.window,
121              (OBWidget::WidgetType)(w ? w->type():-1), e.state, 0, 0, 0, 0);
122 }
123
124
125 void OBActions::keyPressHandler(const XKeyEvent &e)
126 {
127 //  OBWidget *w = dynamic_cast<OBWidget*>
128 //    (Openbox::instance->findHandler(e.window));
129
130   Openbox::instance->bindings()->fire(e.state, e.keycode, e.time);
131 }
132
133
134 void OBActions::motionHandler(const XMotionEvent &e)
135 {
136   if (!e.same_screen) return; // this just gets stupid
137
138   int x_root = e.x_root, y_root = e.y_root;
139   
140   // compress changes to a window into a single change
141   XEvent ce;
142   while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
143     if (ce.xmotion.window != e.window) {
144       XPutBackEvent(otk::OBDisplay::display, &ce);
145       break;
146     } else {
147       x_root = e.x_root;
148       y_root = e.y_root;
149     }
150   }
151
152
153   OBWidget *w = dynamic_cast<OBWidget*>
154     (Openbox::instance->findHandler(e.window));
155
156   // XXX: i can envision all sorts of crazy shit with this.. gestures, etc
157   //      maybe that should all be done via python tho.. (or radial menus!)
158   // run the simple MOTION python hook for now...
159   doCallback(Action_MouseMotion, e.window,
160              (OBWidget::WidgetType)(w ? w->type():-1),
161              e.state, (unsigned)-1, x_root, y_root, e.time);
162 }
163
164 void OBActions::mapRequestHandler(const XMapRequestEvent &e)
165 {
166   doCallback(Action_NewWindow, e.window, (OBWidget::WidgetType)-1,
167              0, 0, 0, 0, 0);
168 }
169
170 void OBActions::unmapHandler(const XUnmapEvent &e)
171 {
172   (void)e;
173   doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
174              0, 0, 0, 0, 0);
175 }
176
177 void OBActions::destroyHandler(const XDestroyWindowEvent &e)
178 {
179   (void)e;
180   doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
181              0, 0, 0, 0, 0);
182 }
183
184 void OBActions::doCallback(ActionType action, Window window,
185                            OBWidget::WidgetType type, unsigned int state,
186                            unsigned int button, int xroot, int yroot,
187                            Time time)
188 {
189   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
190     _callbacks.equal_range(action);
191
192   CallbackMap::iterator it;
193   for (it = it_pair.first; it != it_pair.second; ++it)
194     python_callback(it->second, action, window, type, state,
195                     button, xroot, yroot, time);
196 }
197
198 bool OBActions::registerCallback(ActionType action, PyObject *func,
199                                  bool atfront)
200 {
201   if (action < 0 || action >= OBActions::NUM_ACTIONS ||
202       action == OBActions::Action_KeyPress) {
203     return false;
204   }
205   if (!func)
206     return false;
207
208   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
209     _callbacks.equal_range(action);
210
211   CallbackMap::iterator it;
212   for (it = it_pair.first; it != it_pair.second; ++it)
213     if (it->second == func)
214       return true; // already in there
215   if (atfront)
216     _callbacks.insert(_callbacks.begin(), CallbackMapPair(action, func));
217   else
218     _callbacks.insert(CallbackMapPair(action, func));
219   Py_INCREF(func);
220   return true;
221 }
222
223 bool OBActions::unregisterCallback(ActionType action, PyObject *func)
224 {
225   if (action < 0 || action >= OBActions::NUM_ACTIONS ||
226       action == OBActions::Action_KeyPress) {
227     return false;
228   }
229   if (!func)
230     return false;
231   
232   std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
233     _callbacks.equal_range(action);
234   
235   CallbackMap::iterator it;
236   for (it = it_pair.first; it != it_pair.second; ++it)
237     if (it->second == func)
238       break;
239   if (it != it_pair.second) { // its been registered before
240     Py_DECREF(func);
241     _callbacks.erase(it);
242   }
243   return true;
244 }
245
246 bool OBActions::unregisterAllCallbacks(ActionType action)
247 {
248   if (action < 0 || action >= OBActions::NUM_ACTIONS ||
249       action == OBActions::Action_KeyPress) {
250     return false;
251   }
252
253   while (!_callbacks.empty()) {
254     CallbackMap::iterator it = _callbacks.begin();
255     Py_DECREF(it->second);
256     _callbacks.erase(it);
257   }
258   return true;
259 }
260
261 }