]> Dogcows Code - chaz/openbox/blob - src/Input.cc
Configureable button mappings!
[chaz/openbox] / src / Input.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // Input.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef HAVE_CONFIG_H
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 #include "Input.hh"
29 #include "blackbox.hh"
30 #include "Window.hh"
31
32 BInput::BInput(Blackbox *b) {
33 _blackbox = b;
34 _display = b->getXDisplay();
35
36 // hardcode blackbox's oldschool mouse bindings
37
38 // buttons
39 add(Button1, 0, IconifyButtonClick, Iconify);
40 add(Button1, 0, CloseButtonClick, Close);
41
42 add(Button1, 0, MaximizeButtonClick, ToggleMaximize);
43 add(Button1, 0, MaximizeButtonClick, Raise);
44
45 add(Button2, 0, MaximizeButtonClick, ToggleMaximizeVert);
46 add(Button2, 0, MaximizeButtonClick, Raise);
47
48 add(Button3, 0, MaximizeButtonClick, ToggleMaximizeHoriz);
49 add(Button3, 0, MaximizeButtonClick, Raise);
50
51 // title-bar
52
53 add(Button1, ControlMask, WindowTitlePress, ToggleShade);
54 add(Button2, 0, WindowTitlePress, Lower);
55 add(Button1, 0, WindowTitleDoublePress, ToggleShade);
56
57 // mouse wheel
58 add(Button4, 0, WindowTitlePress, Shade);
59 add(Button5, 0, WindowTitlePress, Unshade);
60
61 // drag moving
62 add(Button1, 0, WindowHandleDrag, BeginMove);
63 add(Button1, 0, WindowTitleDrag, BeginMove);
64 add(Button1, Mod1Mask, WindowDrag, BeginMove);
65
66 // drag resizing
67 add(Button3, Mod1Mask, WindowDrag, BeginResizeRelative);
68 add(Button1, 0, WindowLeftGripDrag, BeginResizeLL);
69 add(Button1, 0, WindowRightGripDrag, BeginResizeLR);
70
71 // window menu
72 add(Button3, 0, WindowTitlePress, ShowWindowMenu);
73 add(Button3, 0, WindowFramePress, ShowWindowMenu);
74
75 // focus/raising
76 add(Button1, AnyModifier, WindowTitlePress, Raise);
77 add(Button1, AnyModifier, WindowTitlePress, Focus);
78 add(Button1, AnyModifier, WindowFramePress, Raise);
79 add(Button1, AnyModifier, WindowFramePress, Focus);
80 }
81
82
83 BInput::~BInput() {
84 }
85
86
87 void BInput::add(unsigned int button, unsigned int state, MouseEvent event,
88 Action action) {
89 _mousebind.push_back(MouseBinding(button, state, event, action));
90 }
91
92
93 void BInput::add(unsigned int button, unsigned int state, Action action) {
94 _keybind.push_back(KeyBinding(button, state, action));
95 }
96
97
98 void BInput::remove(unsigned int button, unsigned int state, MouseEvent event,
99 Action action) {
100 MouseBindingList::iterator it = _mousebind.begin();
101 const MouseBindingList::iterator end = _mousebind.end();
102 while (it != end) {
103 if (it->button == button && it->state == state && it->action == action &&
104 it->event == event) {
105 MouseBindingList::iterator tmp = it;
106 ++it;
107 _mousebind.erase(tmp);
108 } else {
109 ++it;
110 }
111 }
112 }
113
114
115 void BInput::remove(unsigned int button, unsigned int state, Action action) {
116 KeyBindingList::iterator it = _keybind.begin();
117 const KeyBindingList::iterator end = _keybind.end();
118 while (it != end) {
119 if (it->button == button && it->state == state && it->action == action) {
120 ++it;
121 _keybind.erase(it);
122 } else {
123 ++it;
124 }
125 }
126 }
127
128
129 // execute a keyboard binding
130 bool BInput::doAction(BlackboxWindow *window, unsigned int keycode,
131 unsigned int state) const {
132 bool ret = False;
133
134 KeyBindingList::const_iterator it = _keybind.begin();
135 const KeyBindingList::const_iterator end = _keybind.end();
136 for (; it != end; ++it)
137 if ((it->state == state || it->state == AnyModifier) &&
138 it->button == keycode && it->action != NoAction) {
139 doAction(window, it->action);
140 ret = True;
141 }
142 return ret;
143 }
144
145
146 // determine if a keyboard binding exists
147 bool BInput::hasAction(unsigned int keycode, unsigned int state) const {
148 KeyBindingList::const_iterator it = _keybind.begin();
149 const KeyBindingList::const_iterator end = _keybind.end();
150 for (; it != end; ++it)
151 if ((it->state == state || it->state == AnyModifier) &&
152 it->button == keycode && it->action != NoAction)
153 return True;
154 return False;
155 }
156
157
158 // execute a mouse binding
159 bool BInput::doAction(BlackboxWindow *window, unsigned int button,
160 unsigned int state, MouseEvent eventtype) const {
161 bool ret = False;
162
163 assert(button == Button1 || button == Button2 || button == Button3 ||
164 button == Button4 || button == Button5);
165 assert(eventtype >= 0 && eventtype < NUM_MOUSEEVENTS);
166
167 MouseBindingList::const_iterator it = _mousebind.begin();
168 const MouseBindingList::const_iterator end = _mousebind.end();
169 for (; it != end; ++it)
170 if ((it->state == state || it->state == AnyModifier) &&
171 it->button == button && it->event == eventtype &&
172 it->action != NoAction) {
173 doAction(window, it->action);
174 ret = True;
175 }
176 return ret;
177 }
178
179
180 // determine if a mouse binding exists
181 bool BInput::hasAction(unsigned int button, unsigned int state,
182 MouseEvent eventtype) const {
183 assert(button == Button1 || button == Button2 || button == Button3 ||
184 button == Button4 || button == Button5);
185 assert(eventtype >= 0 && eventtype < NUM_MOUSEEVENTS);
186
187 MouseBindingList::const_iterator it = _mousebind.begin();
188 const MouseBindingList::const_iterator end = _mousebind.end();
189 for (; it != end; ++it)
190 if ((it->state == state || it->state == AnyModifier) &&
191 it->button == button && it->event == eventtype &&
192 it->action != NoAction)
193 return True;
194 return False;
195 }
196
197
198 void BInput::doAction(BlackboxWindow *window, Action action) const {
199 switch (action) {
200 case Raise:
201 if (window) window->raise();
202 return;
203
204 case Lower:
205 if (window) window->lower();
206 return;
207
208 case Shade:
209 if (window && ! window->isShaded()) window->shade();
210 return;
211
212 case Unshade:
213 if (window && window->isShaded()) window->shade();
214 return;
215
216 case ToggleShade:
217 if (window) window->shade();
218 return;
219
220 case Focus:
221 if (window && ! window->isFocused()) window->setInputFocus();
222 return;
223
224 case Iconify:
225 if (window && ! window->isIconic()) window->iconify();
226 return;
227
228 case ToggleMaximizeVert:
229 if (window) window->maximize(2);
230 return;
231
232 case ToggleMaximizeHoriz:
233 if (window) window->maximize(3);
234 return;
235
236 case ToggleMaximize:
237 if (window) window->maximize(1);
238 return;
239
240 case Close:
241 if (window && window->isClosable()) window->close();
242 return;
243
244 case BeginMove:
245 if (window && window->isMovable()) {
246 Window root_return, child_return;
247 int root_x_return, root_y_return;
248 int win_x_return, win_y_return;
249 unsigned int mask_return;
250
251 if (! XQueryPointer(_display, window->getClientWindow(),
252 &root_return, &child_return,
253 &root_x_return, &root_y_return,
254 &win_x_return, &win_y_return,
255 &mask_return))
256 return;
257 window->beginMove(root_x_return, root_y_return);
258 }
259 return;
260
261 case BeginResizeUL:
262 case BeginResizeUR:
263 case BeginResizeLL:
264 case BeginResizeLR:
265 case BeginResizeRelative:
266 if (window && window->isResizable()) {
267 Window root_return, child_return;
268 int root_x_return, root_y_return;
269 int win_x_return, win_y_return;
270 unsigned int mask_return;
271
272 if (! XQueryPointer(_display, window->getClientWindow(),
273 &root_return, &child_return,
274 &root_x_return, &root_y_return,
275 &win_x_return, &win_y_return,
276 &mask_return))
277 return;
278
279 BlackboxWindow::Corner corner;
280 switch (action) {
281 case BeginResizeUL: corner = BlackboxWindow::TopLeft; break;
282 case BeginResizeUR: corner = BlackboxWindow::TopRight; break;
283 case BeginResizeLL: corner = BlackboxWindow::BottomLeft; break;
284 case BeginResizeLR: corner = BlackboxWindow::BottomRight; break;
285 case BeginResizeRelative: {
286 const Rect &r = window->frameRect();
287 if (! r.contains(root_x_return, root_y_return))
288 return;
289
290 bool left = root_x_return < r.x() + (signed)(r.width() / 2);
291 bool top = root_y_return < r.y() + (signed)(r.height() / 2);
292 if (left) {
293 if (top) corner = BlackboxWindow::TopLeft;
294 else corner = BlackboxWindow::BottomLeft;
295 } else {
296 if (top) corner = BlackboxWindow::TopRight;
297 else corner = BlackboxWindow::BottomRight;
298 }
299 break;
300 }
301 default: assert(false); // unhandled action
302 }
303 window->beginResize(root_x_return, root_y_return, corner);
304 }
305 return;
306
307 case ShowWindowMenu:
308 if (window) {
309 Window root_return, child_return;
310 int root_x_return, root_y_return;
311 int win_x_return, win_y_return;
312 unsigned int mask_return;
313
314 if (! XQueryPointer(_display, window->getClientWindow(),
315 &root_return, &child_return,
316 &root_x_return, &root_y_return,
317 &win_x_return, &win_y_return,
318 &mask_return))
319 return;
320 window->showWindowMenu(root_x_return, root_y_return);
321 }
322 return;
323
324 default:
325 assert(false); // unhandled Action
326 }
327 }
This page took 0.050727 seconds and 4 git commands to generate.