]> icculus.org git repositories - dana/openbox.git/blob - src/actions.cc
segfault no more on root clicks
[dana/openbox.git] / src / actions.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
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 "otk/display.hh"
12
13 #include <stdio.h>
14
15 namespace ob {
16
17 const unsigned int OBActions::DOUBLECLICKDELAY = 300;
18 const int OBActions::BUTTONS;
19
20 OBActions::OBActions()
21   : _button(0)
22 {
23   for (int i=0; i<BUTTONS; ++i)
24     _posqueue[i] = new ButtonPressAction();
25
26   // XXX: load a configuration out of somewhere
27
28 }
29
30
31 OBActions::~OBActions()
32 {
33   for (int i=0; i<BUTTONS; ++i)
34     delete _posqueue[i];
35 }
36
37
38 void OBActions::insertPress(const XButtonEvent &e)
39 {
40   ButtonPressAction *a = _posqueue[BUTTONS - 1];
41   for (int i=BUTTONS-1; i>0;)
42     _posqueue[i] = _posqueue[--i];
43   _posqueue[0] = a;
44   a->button = e.button;
45   a->pos.setPoint(e.x_root, e.y_root);
46
47   OBClient *c = Openbox::instance->findClient(e.window);
48   // if it's not defined, they should have clicked on the root window, so this
49   // area would be meaningless anyways
50   if (c) a->clientarea = c->area();
51 }
52
53 void OBActions::removePress(const XButtonEvent &e)
54 {
55   ButtonPressAction *a = 0;
56   for (int i=0; i<BUTTONS; ++i) {
57     if (_posqueue[i]->button == e.button)
58       a = _posqueue[i];
59     if (a) // found one and removed it
60       _posqueue[i] = _posqueue[i+1];
61   }
62   if (a) { // found one
63     _posqueue[BUTTONS-1] = a;
64     a->button = 0;
65   }
66 }
67
68 void OBActions::buttonPressHandler(const XButtonEvent &e)
69 {
70   OtkEventHandler::buttonPressHandler(e);
71   insertPress(e);
72   
73   // XXX: run the PRESS guile hook
74   OBWidget *w = dynamic_cast<OBWidget*>
75     (Openbox::instance->findHandler(e.window));
76
77   printf("GUILE: PRESS: win %lx type %d modifiers %u button %u time %lx\n",
78          (long)e.window, (w ? w->type():-1), e.state, e.button, e.time);
79     
80   if (_button) return; // won't count toward CLICK events
81
82   _button = e.button;
83 }
84   
85
86 void OBActions::buttonReleaseHandler(const XButtonEvent &e)
87 {
88   OtkEventHandler::buttonReleaseHandler(e);
89   removePress(e);
90   
91   // XXX: run the RELEASE guile hook
92   OBWidget *w = dynamic_cast<OBWidget*>
93     (Openbox::instance->findHandler(e.window));
94
95   printf("GUILE: RELEASE: win %lx type %d, modifiers %u button %u time %lx\n",
96          (long)e.window, (w ? w->type():-1), e.state, e.button, e.time);
97
98   // not for the button we're watching?
99   if (_button != e.button) return;
100
101   _button = 0;
102
103   // find the area of the window
104   XWindowAttributes attr;
105   if (!XGetWindowAttributes(otk::OBDisplay::display, e.window, &attr)) return;
106
107   // if not on the window any more, it isnt a CLICK
108   if (!(e.same_screen && e.x >= 0 && e.y >= 0 &&
109         e.x < attr.width && e.y < attr.height))
110     return;
111
112   // XXX: run the CLICK guile hook
113   printf("GUILE: CLICK: win %lx type %d modifiers %u button %u time %lx\n",
114          (long)e.window, (w ? w->type():-1), e.state, e.button, e.time);
115
116   if (e.time - _release.time < DOUBLECLICKDELAY &&
117       _release.win == e.window && _release.button == e.button) {
118
119     // XXX: run the DOUBLECLICK guile hook
120     printf("GUILE: DOUBLECLICK: win %lx type %d modifiers %u button %u time %lx\n",
121            (long)e.window, (w ? w->type():-1), e.state, e.button, e.time);
122
123     // reset so you cant triple click for 2 doubleclicks
124     _release.win = 0;
125     _release.button = 0;
126     _release.time = 0;
127   } else {
128     // save the button release, might be part of a double click
129     _release.win = e.window;
130     _release.button = e.button;
131     _release.time = e.time;
132   }
133 }
134
135
136 void OBActions::enterHandler(const XCrossingEvent &e)
137 {
138   OtkEventHandler::enterHandler(e);
139   
140   // XXX: run the ENTER guile hook
141   OBWidget *w = dynamic_cast<OBWidget*>
142     (Openbox::instance->findHandler(e.window));
143
144   printf("GUILE: ENTER: win %lx type %d modifiers %u\n",
145          (long)e.window, (w ? w->type():-1), e.state);
146 }
147
148
149 void OBActions::leaveHandler(const XCrossingEvent &e)
150 {
151   OtkEventHandler::leaveHandler(e);
152
153   // XXX: run the LEAVE guile hook
154   OBWidget *w = dynamic_cast<OBWidget*>
155     (Openbox::instance->findHandler(e.window));
156
157   printf("GUILE: LEAVE: win %lx type %d modifiers %u\n",
158          (long)e.window, (w ? w->type():-1), e.state);
159 }
160
161
162 void OBActions::keyPressHandler(const XKeyEvent &e)
163 {
164   // XXX: run the KEY guile hook
165   OBWidget *w = dynamic_cast<OBWidget*>
166     (Openbox::instance->findHandler(e.window));
167
168   printf("GUILE: KEY: win %lx type %d modifiers %u keycode %u\n",
169          (long)e.window, (w ? w->type():-1), e.state, e.keycode);
170 }
171
172
173 void OBActions::motionHandler(const XMotionEvent &e)
174 {
175   if (!e.same_screen) return; // this just gets stupid
176
177   int x_root = e.x_root, y_root = e.y_root;
178   
179   // compress changes to a window into a single change
180   XEvent ce;
181   while (XCheckTypedEvent(otk::OBDisplay::display, e.type, &ce)) {
182     if (ce.xmotion.window != e.window) {
183       XPutBackEvent(otk::OBDisplay::display, &ce);
184       break;
185     } else {
186       x_root = e.x_root;
187       y_root = e.y_root;
188     }
189   }
190
191
192   OBWidget *w = dynamic_cast<OBWidget*>
193     (Openbox::instance->findHandler(e.window));
194
195   _dx = x_root - _posqueue[0]->pos.x();
196   _dy = y_root - _posqueue[0]->pos.y();
197   
198   // XXX: i can envision all sorts of crazy shit with this.. gestures, etc
199   printf("GUILE: MOTION: win %lx type %d  modifiers %u x %d y %d\n",
200          (long)e.window, (w ? w->type():-1), e.state, _dx, _dy);
201
202   OBClient *c = Openbox::instance->findClient(e.window);
203   if (w && c) {
204     switch (w->type()) {
205     case OBWidget::Type_Titlebar:
206     case OBWidget::Type_Label:
207       c->move(_posqueue[0]->clientarea.x() + _dx,
208               _posqueue[0]->clientarea.y() + _dy);
209       break;
210     case OBWidget::Type_LeftGrip:
211       c->resize(OBClient::TopRight,
212                 _posqueue[0]->clientarea.width() - _dx,
213                 _posqueue[0]->clientarea.height() + _dy);
214       break;
215     case OBWidget::Type_RightGrip:
216       c->resize(OBClient::TopLeft,
217                 _posqueue[0]->clientarea.width() + _dx,
218                 _posqueue[0]->clientarea.height() + _dy);
219       break;
220     default:
221       break;
222     }
223   }
224 }
225
226
227 }