]> icculus.org git repositories - mikachu/openbox.git/blob - src/Input.cc
sync with bb. mostly cleanups in Window.cc
[mikachu/openbox.git] / 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 Stick:
209     if (window && ! window->isStuck()) window->stick();
210     return;
211
212   case Unstick:
213     if (window && window->isStuck()) window->stick();
214     return;
215
216   case ToggleStick:
217     if (window) window->stick();
218     return;
219
220   case Shade:
221     if (window && ! window->isShaded()) window->shade();
222     return;
223
224   case Unshade:
225     if (window && window->isShaded()) window->shade();
226     return;
227
228   case ToggleShade:
229     if (window) window->shade();
230     return;
231
232   case Focus:
233     if (window && ! window->isFocused()) window->setInputFocus();
234     return;
235
236   case Iconify:
237     if (window && ! window->isIconic()) window->iconify();
238     return;
239
240   case ToggleMaximizeVert:
241     if (window) window->maximize(2);
242     return;
243
244   case ToggleMaximizeHoriz:
245     if (window) window->maximize(3);
246     return;
247
248   case ToggleMaximize:
249     if (window) window->maximize(1);
250     return;
251
252   case Close:
253     if (window && window->isClosable()) window->close();
254     return;
255
256   case NextWorkspace: {
257     BScreen *s;
258     unsigned int w;
259     s = _blackbox->getFocusedScreen();
260     if (s) {
261       w = s->getCurrentWorkspaceID();
262       if (++w >= s->getWorkspaceCount())
263         w = 0;
264       s->changeWorkspaceID(w);
265     }
266     return;
267   }
268
269   case PrevWorkspace: {
270     BScreen *s;
271     int w;
272     s = _blackbox->getFocusedScreen();
273     if (s) {
274       w = s->getCurrentWorkspaceID();
275       if (w-- == 0)
276         w = s->getWorkspaceCount() - 1;
277       s->changeWorkspaceID(w);
278     }
279     return;
280   }
281
282   case BeginMove:
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;
288
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,
293                           &mask_return))
294         return;
295       window->beginMove(root_x_return, root_y_return);
296     }
297     return;
298
299   case BeginResizeUL:
300   case BeginResizeUR:
301   case BeginResizeLL:
302   case BeginResizeLR:
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;
309
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,
314                           &mask_return))
315         return;
316
317       BlackboxWindow::Corner corner;
318       switch (action) {
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))
326           return;
327
328         bool left = root_x_return < r.x() + (signed)(r.width() / 2);
329         bool top = root_y_return < r.y() + (signed)(r.height() / 2);
330         if (left) {
331           if (top) corner = BlackboxWindow::TopLeft;
332           else corner = BlackboxWindow::BottomLeft;
333         } else {
334           if (top) corner = BlackboxWindow::TopRight;
335           else corner = BlackboxWindow::BottomRight;
336         }
337         break;
338       }
339       default: assert(false); // unhandled action
340       }
341       window->beginResize(root_x_return, root_y_return, corner);
342     }
343     return;
344
345   case ShowWindowMenu:
346     if (window) {
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;
351
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,
356                           &mask_return))
357         return;
358       window->showWindowMenu(root_x_return, root_y_return);
359     }
360     return;
361
362   default:
363     assert(false);  // unhandled Action
364   }
365 }