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)
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
29 #include "blackbox.hh"
32 BInput::BInput(Blackbox *b) {
34 _display = b->getXDisplay();
36 // hardcode blackbox's oldschool mouse bindings
39 add(Button1, 0, IconifyButtonClick, Iconify);
40 add(Button1, 0, CloseButtonClick, Close);
42 add(Button1, 0, MaximizeButtonClick, ToggleMaximize);
43 add(Button1, 0, MaximizeButtonClick, Raise);
45 add(Button2, 0, MaximizeButtonClick, ToggleMaximizeVert);
46 add(Button2, 0, MaximizeButtonClick, Raise);
48 add(Button3, 0, MaximizeButtonClick, ToggleMaximizeHoriz);
49 add(Button3, 0, MaximizeButtonClick, Raise);
53 add(Button1, ControlMask, WindowTitlePress, ToggleShade);
54 add(Button2, 0, WindowTitlePress, Lower);
55 add(Button1, 0, WindowTitleDoublePress, ToggleShade);
58 add(Button4, 0, WindowTitlePress, Shade);
59 add(Button5, 0, WindowTitlePress, Unshade);
62 add(Button1, 0, WindowHandleDrag, BeginMove);
63 add(Button1, 0, WindowTitleDrag, BeginMove);
64 add(Button1, Mod1Mask, WindowDrag, BeginMove);
67 add(Button3, Mod1Mask, WindowDrag, BeginResizeRelative);
68 add(Button1, 0, WindowLeftGripDrag, BeginResizeLL);
69 add(Button1, 0, WindowRightGripDrag, BeginResizeLR);
72 add(Button3, 0, WindowTitlePress, ShowWindowMenu);
73 add(Button3, 0, WindowFramePress, ShowWindowMenu);
76 add(Button1, AnyModifier, WindowTitlePress, Raise);
77 add(Button1, AnyModifier, WindowTitlePress, Focus);
78 add(Button1, AnyModifier, WindowFramePress, Raise);
79 add(Button1, AnyModifier, WindowFramePress, Focus);
87 void BInput::add(unsigned int button, unsigned int state, MouseEvent event,
89 _mousebind.push_back(MouseBinding(button, state, event, action));
93 void BInput::add(unsigned int button, unsigned int state, Action action) {
94 _keybind.push_back(KeyBinding(button, state, action));
98 void BInput::remove(unsigned int button, unsigned int state, MouseEvent event,
100 MouseBindingList::iterator it = _mousebind.begin();
101 const MouseBindingList::iterator end = _mousebind.end();
103 if (it->button == button && it->state == state && it->action == action &&
104 it->event == event) {
105 MouseBindingList::iterator tmp = it;
107 _mousebind.erase(tmp);
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();
119 if (it->button == button && it->state == state && it->action == action) {
129 // execute a keyboard binding
130 bool BInput::doAction(BlackboxWindow *window, unsigned int keycode,
131 unsigned int state) const {
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);
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)
158 // execute a mouse binding
159 bool BInput::doAction(BlackboxWindow *window, unsigned int button,
160 unsigned int state, MouseEvent eventtype) const {
163 assert(button == Button1 || button == Button2 || button == Button3 ||
164 button == Button4 || button == Button5);
165 assert(eventtype >= 0 && eventtype < NUM_MOUSEEVENTS);
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);
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);
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)
198 void BInput::doAction(BlackboxWindow *window, Action action) const {
201 if (window) window->raise();
205 if (window) window->lower();
209 if (window && ! window->isStuck()) window->stick();
213 if (window && window->isStuck()) window->stick();
217 if (window) window->stick();
221 if (window && ! window->isShaded()) window->shade();
225 if (window && window->isShaded()) window->shade();
229 if (window) window->shade();
233 if (window && ! window->isFocused()) window->setInputFocus();
237 if (window && ! window->isIconic()) window->iconify();
240 case ToggleMaximizeVert:
241 if (window) window->maximize(2);
244 case ToggleMaximizeHoriz:
245 if (window) window->maximize(3);
249 if (window) window->maximize(1);
253 if (window && window->isClosable()) window->close();
256 case NextWorkspace: {
259 s = _blackbox->getFocusedScreen();
261 w = s->getCurrentWorkspaceID();
262 if (++w >= s->getWorkspaceCount())
264 s->changeWorkspaceID(w);
269 case PrevWorkspace: {
272 s = _blackbox->getFocusedScreen();
274 w = s->getCurrentWorkspaceID();
276 w = s->getWorkspaceCount() - 1;
277 s->changeWorkspaceID(w);
283 if (window && window->isMovable()) {
284 Window root_return, child_return;
285 int root_x_return, root_y_return;
286 int win_x_return, win_y_return;
287 unsigned int mask_return;
289 if (! XQueryPointer(_display, window->getClientWindow(),
290 &root_return, &child_return,
291 &root_x_return, &root_y_return,
292 &win_x_return, &win_y_return,
295 window->beginMove(root_x_return, root_y_return);
303 case BeginResizeRelative:
304 if (window && window->isResizable()) {
305 Window root_return, child_return;
306 int root_x_return, root_y_return;
307 int win_x_return, win_y_return;
308 unsigned int mask_return;
310 if (! XQueryPointer(_display, window->getClientWindow(),
311 &root_return, &child_return,
312 &root_x_return, &root_y_return,
313 &win_x_return, &win_y_return,
317 BlackboxWindow::Corner corner;
319 case BeginResizeUL: corner = BlackboxWindow::TopLeft; break;
320 case BeginResizeUR: corner = BlackboxWindow::TopRight; break;
321 case BeginResizeLL: corner = BlackboxWindow::BottomLeft; break;
322 case BeginResizeLR: corner = BlackboxWindow::BottomRight; break;
323 case BeginResizeRelative: {
324 const Rect &r = window->frameRect();
325 if (! r.contains(root_x_return, root_y_return))
328 bool left = root_x_return < r.x() + (signed)(r.width() / 2);
329 bool top = root_y_return < r.y() + (signed)(r.height() / 2);
331 if (top) corner = BlackboxWindow::TopLeft;
332 else corner = BlackboxWindow::BottomLeft;
334 if (top) corner = BlackboxWindow::TopRight;
335 else corner = BlackboxWindow::BottomRight;
339 default: assert(false); // unhandled action
341 window->beginResize(root_x_return, root_y_return, corner);
347 Window root_return, child_return;
348 int root_x_return, root_y_return;
349 int win_x_return, win_y_return;
350 unsigned int mask_return;
352 if (! XQueryPointer(_display, window->getClientWindow(),
353 &root_return, &child_return,
354 &root_x_return, &root_y_return,
355 &win_x_return, &win_y_return,
358 window->showWindowMenu(root_x_return, root_y_return);
363 assert(false); // unhandled Action