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