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