]> icculus.org git repositories - mikachu/openbox.git/blob - src/actions.cc
provide RenderControls to all otk from the display class. initialize them all there...
[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 "widgetbase.hh"
9 #include "openbox.hh"
10 #include "client.hh"
11 #include "screen.hh"
12 #include "python.hh"
13 #include "bindings.hh"
14 #include "otk/display.hh"
15
16 #include <stdio.h>
17 #include <algorithm>
18
19 namespace ob {
20
21 const int Actions::BUTTONS;
22
23 Actions::Actions()
24   : _button(0),
25     _dragging(false)
26 {
27   for (int i=0; i<BUTTONS; ++i)
28     _posqueue[i] = new ButtonPressAction();
29 }
30
31
32 Actions::~Actions()
33 {
34   for (int i=0; i<BUTTONS; ++i)
35     delete _posqueue[i];
36 }
37
38
39 void Actions::insertPress(const XButtonEvent &e)
40 {
41   ButtonPressAction *a = _posqueue[BUTTONS - 1];
42   // rm'd the last one, shift them all down one
43   for (int i = BUTTONS-1; i > 0; --i) {
44     _posqueue[i] = _posqueue[i-1];
45   }
46   _posqueue[0] = a;
47   a->button = e.button;
48   a->pos.setPoint(e.x_root, e.y_root);
49
50   Client *c = openbox->findClient(e.window);
51   if (c) a->clientarea = c->area();
52 }
53
54 void Actions::removePress(const XButtonEvent &e)
55 {
56   int i;
57   ButtonPressAction *a = 0;
58   for (i=0; i<BUTTONS-1; ++i)
59     if (_posqueue[i]->button == e.button) {
60       a = _posqueue[i];
61       break;
62     }
63   if (a) { // found one, remove it and shift the rest up one
64     for (; i < BUTTONS-1; ++i)
65       _posqueue[i] = _posqueue[i+1];
66     _posqueue[BUTTONS-1] = a;
67   }
68   _posqueue[BUTTONS-1]->button = 0;
69 }
70
71 void Actions::buttonPressHandler(const XButtonEvent &e)
72 {
73   otk::EventHandler::buttonPressHandler(e);
74   insertPress(e);
75   
76   // run the PRESS python hook
77   WidgetBase *w = dynamic_cast<WidgetBase*>
78     (openbox->findHandler(e.window));
79   if (!w) return;
80
81   // kill off the Button1Mask etc, only want the modifiers
82   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
83                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
84   int screen;
85   Client *c = openbox->findClient(e.window);
86   if (c)
87     screen = c->screen();
88   else
89     screen = otk::display->findScreen(e.root)->screen();
90   MouseData data(screen, c, e.time, state, e.button, w->mcontext(),
91                  MousePress);
92   openbox->bindings()->fireButton(&data);
93     
94   if (_button) return; // won't count toward CLICK events
95
96   _button = e.button;
97
98   if (w->mcontext() == MC_Window) {
99     /*
100       Because of how events are grabbed on the client window, we can't get
101       ButtonRelease events, so instead we simply manufacture them here, so that
102       clicks/doubleclicks etc still work.
103     */
104     //XButtonEvent ev = e;
105     //ev.type = ButtonRelease;
106     buttonReleaseHandler(e);
107   }
108 }
109   
110
111 void Actions::buttonReleaseHandler(const XButtonEvent &e)
112 {
113   otk::EventHandler::buttonReleaseHandler(e);
114   removePress(e);
115   
116   WidgetBase *w = dynamic_cast<WidgetBase*>
117     (openbox->findHandler(e.window));
118   if (!w) return;
119
120   // not for the button we're watching?
121   if (_button != e.button) return;
122
123   _button = 0;
124   _dragging = false;
125
126   // find the area of the window
127   XWindowAttributes attr;
128   if (!XGetWindowAttributes(**otk::display, e.window, &attr)) return;
129
130   // if not on the window any more, it isnt a CLICK
131   if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
132         e.x < attr.width && e.y < attr.height))
133     return;
134
135   // run the CLICK python hook
136   // kill off the Button1Mask etc, only want the modifiers
137   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
138                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
139   int screen;
140   Client *c = openbox->findClient(e.window);
141   if (c)
142     screen = c->screen();
143   else
144     screen = otk::display->findScreen(e.root)->screen();
145   MouseData data(screen, c, e.time, state, e.button, w->mcontext(),
146                  MouseClick);
147   openbox->bindings()->fireButton(&data);
148     
149
150   // XXX: dont load this every time!!@*
151   long dblclick;
152   if (!python_get_long("double_click_delay", &dblclick))
153     dblclick = 300;
154
155   if (e.time - _release.time < (unsigned)dblclick &&
156       _release.win == e.window && _release.button == e.button) {
157
158     // run the DOUBLECLICK python hook
159     data.action = MouseDoubleClick;
160     openbox->bindings()->fireButton(&data);
161     
162     // reset so you cant triple click for 2 doubleclicks
163     _release.win = 0;
164     _release.button = 0;
165     _release.time = 0;
166   } else {
167     // save the button release, might be part of a double click
168     _release.win = e.window;
169     _release.button = e.button;
170     _release.time = e.time;
171   }
172 }
173
174
175 void Actions::enterHandler(const XCrossingEvent &e)
176 {
177   otk::EventHandler::enterHandler(e);
178   
179   // run the ENTER python hook
180   int screen;
181   Client *c = openbox->findClient(e.window);
182   if (c)
183     screen = c->screen();
184   else
185     screen = otk::display->findScreen(e.root)->screen();
186   EventData data(screen, c, EventEnterWindow, e.state);
187   openbox->bindings()->fireEvent(&data);
188 }
189
190
191 void Actions::leaveHandler(const XCrossingEvent &e)
192 {
193   otk::EventHandler::leaveHandler(e);
194
195   // run the LEAVE python hook
196   int screen;
197   Client *c = openbox->findClient(e.window);
198   if (c)
199     screen = c->screen();
200   else
201     screen = otk::display->findScreen(e.root)->screen();
202   EventData data(screen, c, EventLeaveWindow, e.state);
203   openbox->bindings()->fireEvent(&data);
204 }
205
206
207 void Actions::keyPressHandler(const XKeyEvent &e)
208 {
209   otk::EventHandler::keyPressHandler(e);
210
211   // kill off the Button1Mask etc, only want the modifiers
212   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
213                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
214   openbox->bindings()->
215     fireKey(otk::display->findScreen(e.root)->screen(),
216             state, e.keycode, e.time);
217 }
218
219
220 void Actions::motionHandler(const XMotionEvent &e)
221 {
222   otk::EventHandler::motionHandler(e);
223
224   if (!e.same_screen) return; // this just gets stupid
225
226   int x_root = e.x_root, y_root = e.y_root;
227   
228   // compress changes to a window into a single change
229   XEvent ce;
230   while (XCheckTypedEvent(**otk::display, e.type, &ce)) {
231     if (ce.xmotion.window != e.window) {
232       XPutBackEvent(**otk::display, &ce);
233       break;
234     } else {
235       x_root = e.x_root;
236       y_root = e.y_root;
237     }
238   }
239
240   WidgetBase *w = dynamic_cast<WidgetBase*>
241     (openbox->findHandler(e.window));
242   if (!w) return;
243
244   if (!_dragging) {
245     long threshold;
246     int dx = x_root - _posqueue[0]->pos.x();
247     int dy = y_root - _posqueue[0]->pos.y();
248     // XXX: dont get this from python every time!
249     if (!python_get_long("drag_threshold", &threshold))
250       threshold = 0;
251     if (!(std::abs(dx) >= threshold || std::abs(dy) >= threshold))
252       return; // not at the threshold yet
253   }
254   _dragging = true; // in a drag now
255   
256   // check if the movement is more than the threshold
257
258   // run the MOTION python hook
259   // kill off the Button1Mask etc, only want the modifiers
260   unsigned int state = e.state & (ControlMask | ShiftMask | Mod1Mask |
261                                   Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask);
262   unsigned int button = _posqueue[0]->button;
263   int screen;
264   Client *c = openbox->findClient(e.window);
265   if (c)
266     screen = c->screen();
267   else
268     screen = otk::display->findScreen(e.root)->screen();
269   MouseData data(screen, c, e.time, state, button, w->mcontext(), MouseMotion,
270                  x_root, y_root, _posqueue[0]->pos, _posqueue[0]->clientarea);
271   openbox->bindings()->fireButton(&data);
272 }
273
274 void Actions::mapRequestHandler(const XMapRequestEvent &e)
275 {
276   otk::EventHandler::mapRequestHandler(e);
277   // do this in Screen::manageWindow
278 }
279
280 void Actions::unmapHandler(const XUnmapEvent &e)
281 {
282   otk::EventHandler::unmapHandler(e);
283   // do this in Screen::unmanageWindow
284 }
285
286 void Actions::destroyHandler(const XDestroyWindowEvent &e)
287 {
288   otk::EventHandler::destroyHandler(e);
289   // do this in Screen::unmanageWindow
290 }
291
292 #ifdef    XKB
293 void Actions::xkbHandler(const XkbEvent &e)
294 {
295   Window w;
296   int screen;
297   
298   otk::EventHandler::xkbHandler(e);
299
300   switch (((XkbAnyEvent*)&e)->xkb_type) {
301   case XkbBellNotify:
302     w = ((XkbBellNotifyEvent*)&e)->window;
303     Client *c = openbox->findClient(w);
304     if (c)
305       screen = c->screen();
306     else
307       screen = openbox->focusedScreen()->number();
308     EventData data(screen, c, EventBell, 0);
309     openbox->bindings()->fireEvent(&data);
310     break;
311   }
312 }
313 #endif // XKB
314
315 }
316