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