]> Dogcows Code - chaz/openbox/blob - src/actions.cc
new code for bindings/callbacks. much sexier. now passes python classes back to the...
[chaz/openbox] / 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 // 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);
63
64 // not for the button we're watching?
65 if (_button != e.button) return;
66
67 _button = 0;
68
69 // find the area of the window
70 XWindowAttributes attr;
71 if (!XGetWindowAttributes(otk::OBDisplay::display, e.window, &attr)) return;
72
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))
76 return;
77
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);
82
83 if (e.time - _release.time < DOUBLECLICKDELAY &&
84 _release.win == e.window && _release.button == e.button) {
85
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);
90
91 // reset so you cant triple click for 2 doubleclicks
92 _release.win = 0;
93 _release.button = 0;
94 _release.time = 0;
95 } else {
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;
100 }
101 }
102
103
104 void OBActions::enterHandler(const XCrossingEvent &e)
105 {
106 OtkEventHandler::enterHandler(e);
107
108 OBWidget *w = dynamic_cast<OBWidget*>
109 (Openbox::instance->findHandler(e.window));
110
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);
114 }
115
116
117 void OBActions::leaveHandler(const XCrossingEvent &e)
118 {
119 OtkEventHandler::leaveHandler(e);
120
121 OBWidget *w = dynamic_cast<OBWidget*>
122 (Openbox::instance->findHandler(e.window));
123
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);
127 }
128
129
130 void OBActions::keyPressHandler(const XKeyEvent &e)
131 {
132 // OBWidget *w = dynamic_cast<OBWidget*>
133 // (Openbox::instance->findHandler(e.window));
134
135 Openbox::instance->bindings()->fire(e.state, e.keycode, e.time);
136 }
137
138
139 void OBActions::motionHandler(const XMotionEvent &e)
140 {
141 if (!e.same_screen) return; // this just gets stupid
142
143 int x_root = e.x_root, y_root = e.y_root;
144
145 // compress changes to a window into a single change
146 XEvent ce;
147 while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
148 if (ce.xmotion.window != e.window) {
149 XPutBackEvent(otk::OBDisplay::display, &ce);
150 break;
151 } else {
152 x_root = e.x_root;
153 y_root = e.y_root;
154 }
155 }
156
157
158 OBWidget *w = dynamic_cast<OBWidget*>
159 (Openbox::instance->findHandler(e.window));
160
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);
167 }
168
169 void OBActions::mapRequestHandler(const XMapRequestEvent &e)
170 {
171 doCallback(Action_NewWindow, e.window, (OBWidget::WidgetType)-1,
172 0, 0, 0, 0, 0);
173 }
174
175 void OBActions::unmapHandler(const XUnmapEvent &e)
176 {
177 (void)e;
178 doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
179 0, 0, 0, 0, 0);
180 }
181
182 void OBActions::destroyHandler(const XDestroyWindowEvent &e)
183 {
184 (void)e;
185 doCallback(Action_CloseWindow, e.window, (OBWidget::WidgetType)-1,
186 0, 0, 0, 0, 0);
187 }
188
189 void OBActions::doCallback(ActionType action, Window window,
190 OBWidget::WidgetType type, unsigned int state,
191 unsigned int button, int xroot, int yroot,
192 Time time)
193 {
194 std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
195 _callbacks.equal_range(action);
196
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);
201 }
202
203 bool OBActions::registerCallback(ActionType action, PyObject *func,
204 bool atfront)
205 {
206 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
207 action == OBActions::Action_KeyPress) {
208 return false;
209 }
210 if (!func)
211 return false;
212
213 std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
214 _callbacks.equal_range(action);
215
216 CallbackMap::iterator it;
217 for (it = it_pair.first; it != it_pair.second; ++it)
218 if (it->second == func)
219 break;
220 if (it == it_pair.second) // not already in there
221 if (atfront)
222 _callbacks.insert(_callbacks.begin(), CallbackMapPair(action, func));
223 else
224 _callbacks.insert(CallbackMapPair(action, func));
225 Py_INCREF(func);
226 return true;
227 }
228
229 bool OBActions::unregisterCallback(ActionType action, PyObject *func)
230 {
231 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
232 action == OBActions::Action_KeyPress) {
233 return false;
234 }
235 if (!func)
236 return false;
237
238 std::pair<CallbackMap::iterator, CallbackMap::iterator> it_pair =
239 _callbacks.equal_range(action);
240
241 CallbackMap::iterator it;
242 for (it = it_pair.first; it != it_pair.second; ++it)
243 if (it->second == func)
244 break;
245 if (it != it_pair.second) { // its been registered before
246 Py_DECREF(func);
247 _callbacks.erase(it);
248 }
249 return true;
250 }
251
252 bool OBActions::unregisterAllCallbacks(ActionType action)
253 {
254 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
255 action == OBActions::Action_KeyPress) {
256 return false;
257 }
258
259 while (!_callbacks.empty()) {
260 CallbackMap::iterator it = _callbacks.begin();
261 Py_DECREF(it->second);
262 _callbacks.erase(it);
263 }
264 return true;
265 }
266
267 }
This page took 0.044612 seconds and 5 git commands to generate.