1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
12 #include "bindings.hh"
13 #include "otk/display.hh"
19 const unsigned int OBActions::DOUBLECLICKDELAY = 300;
21 OBActions::OBActions()
24 // XXX: load a configuration out of somewhere
29 OBActions::~OBActions()
34 void OBActions::buttonPressHandler(const XButtonEvent &e)
36 OtkEventHandler::buttonPressHandler(e);
38 // run the PRESS python hook
39 OBWidget *w = dynamic_cast<OBWidget*>
40 (Openbox::instance->findHandler(e.window));
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);
46 if (_button) return; // won't count toward CLICK events
52 void OBActions::buttonReleaseHandler(const XButtonEvent &e)
54 OtkEventHandler::buttonReleaseHandler(e);
56 OBWidget *w = dynamic_cast<OBWidget*>
57 (Openbox::instance->findHandler(e.window));
59 // run the RELEASE python hook
60 doCallback(Action_ButtonRelease, e.window,
61 (OBWidget::WidgetType)(w ? w->type():-1),
62 e.state, e.button, e.x_root, e.y_root, e.time);
64 // not for the button we're watching?
65 if (_button != e.button) return;
69 // find the area of the window
70 XWindowAttributes attr;
71 if (!XGetWindowAttributes(otk::OBDisplay::display, e.window, &attr)) return;
73 // if not on the window any more, it isnt a CLICK
74 if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
75 e.x < attr.width && e.y < attr.height))
78 // run the CLICK python hook
79 doCallback(Action_Click, e.window,
80 (OBWidget::WidgetType)(w ? w->type():-1),
81 e.state, e.button, e.x_root, e.y_root, e.time);
83 if (e.time - _release.time < DOUBLECLICKDELAY &&
84 _release.win == e.window && _release.button == e.button) {
86 // run the DOUBLECLICK python hook
87 doCallback(Action_DoubleClick, e.window,
88 (OBWidget::WidgetType)(w ? w->type():-1),
89 e.state, e.button, e.x_root, e.y_root, e.time);
91 // reset so you cant triple click for 2 doubleclicks
96 // save the button release, might be part of a double click
97 _release.win = e.window;
98 _release.button = e.button;
99 _release.time = e.time;
104 void OBActions::enterHandler(const XCrossingEvent &e)
106 OtkEventHandler::enterHandler(e);
108 OBWidget *w = dynamic_cast<OBWidget*>
109 (Openbox::instance->findHandler(e.window));
111 // run the ENTER python hook
112 doCallback(Action_EnterWindow, e.window,
113 (OBWidget::WidgetType)(w ? w->type():-1), e.state, 0, 0, 0, 0);
117 void OBActions::leaveHandler(const XCrossingEvent &e)
119 OtkEventHandler::leaveHandler(e);
121 OBWidget *w = dynamic_cast<OBWidget*>
122 (Openbox::instance->findHandler(e.window));
124 // run the LEAVE python hook
125 doCallback(Action_LeaveWindow, e.window,
126 (OBWidget::WidgetType)(w ? w->type():-1), e.state, 0, 0, 0, 0);
130 void OBActions::keyPressHandler(const XKeyEvent &e)
132 // OBWidget *w = dynamic_cast<OBWidget*>
133 // (Openbox::instance->findHandler(e.window));
135 Openbox::instance->bindings()->fire(e.state, e.keycode, e.time);
139 void OBActions::motionHandler(const XMotionEvent &e)
141 if (!e.same_screen) return; // this just gets stupid
143 int x_root = e.x_root, y_root = e.y_root;
145 // compress changes to a window into a single change
147 while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
148 if (ce.xmotion.window != e.window) {
149 XPutBackEvent(otk::OBDisplay::display, &ce);
158 OBWidget *w = dynamic_cast<OBWidget*>
159 (Openbox::instance->findHandler(e.window));
161 // XXX: i can envision all sorts of crazy shit with this.. gestures, etc
162 // maybe that should all be done via python tho.. (or radial menus!)
163 // run the simple MOTION python hook for now...
164 doCallback(Action_MouseMotion, e.window,
165 (OBWidget::WidgetType)(w ? w->type():-1),
166 e.state, (unsigned)-1, e.x_root, e.y_root, e.time);
169 void OBActions::mapRequestHandler(const XMapRequestEvent &e)
171 doCallback(Action_NewWindow, e.window, (OBWidget::WidgetType)-1,
175 void OBActions::unmapHandler(const XUnmapEvent &e)
178 doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
182 void OBActions::destroyHandler(const XDestroyWindowEvent &e)
185 doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
189 void OBActions::doCallback(ActionType action, Window window,
190 OBWidget::WidgetType type, unsigned int state,
191 unsigned int button, int xroot, int yroot,
194 std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
195 _callbacks.equal_range(action);
197 CallbackMap::iterator it;
198 for (it = it_pair.first; it != it_pair.second; ++it)
199 python_callback(it->second, action, window, type, state,
200 button, xroot, yroot, time);
203 bool OBActions::registerCallback(ActionType action, PyObject *func,
206 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
207 action == OBActions::Action_KeyPress) {
213 std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
214 _callbacks.equal_range(action);
216 CallbackMap::iterator it;
217 for (it = it_pair.first; it != it_pair.second; ++it)
218 if (it->second == func)
220 if (it == it_pair.second) // not already in there
222 _callbacks.insert(_callbacks.begin(), CallbackMapPair(action, func));
224 _callbacks.insert(CallbackMapPair(action, func));
229 bool OBActions::unregisterCallback(ActionType action, PyObject *func)
231 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
232 action == OBActions::Action_KeyPress) {
238 std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
239 _callbacks.equal_range(action);
241 CallbackMap::iterator it;
242 for (it = it_pair.first; it != it_pair.second; ++it)
243 if (it->second == func)
245 if (it != it_pair.second) { // its been registered before
247 _callbacks.erase(it);
252 bool OBActions::unregisterAllCallbacks(ActionType action)
254 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
255 action == OBActions::Action_KeyPress) {
259 while (!_callbacks.empty()) {
260 CallbackMap::iterator it = _callbacks.begin();
261 Py_DECREF(it->second);
262 _callbacks.erase(it);