1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // screen.cc for Epistrophy - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
23 /* A few comments about stacked cycling:
24 * When stacked cycling is turned on, the focused window is always at the top
25 * (front) of the list (_clients), EXCEPT for when we are in cycling mode.
26 * (_cycling is true) If we were to add the focused window to the top of the
27 * stack while we were cycling, we would end in a deadlock between 2 windows.
28 * When the modifiers are released, the window that has focus (but it's not
29 * at the top of the stack, since we are cycling) is placed at the top of the
31 * Hooray and Bummy. - Marius
35 # include "../../config.h"
36 #endif // HAVE_CONFIG_H
41 #endif // HAVE_STDIO_H
44 # include <sys/types.h>
46 #endif // HAVE_UNISTD_H
48 #include <X11/keysym.h>
60 #include "../../src/BaseDisplay.hh"
61 #include "../../src/XAtom.hh"
66 screen::screen(epist
*epist
, int number
)
67 : _clients(epist
->clientsList()), _active(epist
->activeWindow()),
68 _config(epist
->getConfig()), _grabbed(true), _cycling(false),
69 _stacked_cycling(false)
72 _xatom
= _epist
->xatom();
73 _last_active
= _clients
.end();
75 _info
= _epist
->getScreenInfo(_number
);
76 _root
= _info
->getRootWindow();
78 _config
->getValue(Config::stackedCycling
, _stacked_cycling
);
80 // find a window manager supporting NETWM, waiting for it to load if we must
81 int count
= 20; // try for 20 seconds
83 while (! (_epist
->doShutdown() || _managed
|| count
<= 0)) {
84 if (! (_managed
= findSupportingWM()))
89 cout
<< "Found compatible window manager '" << _wm_name
<< "' for screen "
92 cout
<< "Unable to find a compatible window manager for screen " <<
97 XSelectInput(_epist
->getXDisplay(), _root
, PropertyChangeMask
);
102 XSelectInput(_epist
->getXDisplay(), _root
, None
);
106 bool screen::findSupportingWM() {
108 if (! _xatom
->getValue(_root
, XAtom::net_supporting_wm_check
, XAtom::window
,
109 support_win
) || support_win
== None
)
113 _xatom
->getValue(support_win
, XAtom::net_wm_name
, XAtom::utf8
, title
);
119 XWindow
*screen::findWindow(const XEvent
&e
) const {
122 WindowList::const_iterator it
, end
= _clients
.end();
123 for (it
= _clients
.begin(); it
!= end
; ++it
)
124 if (**it
== e
.xany
.window
)
132 void screen::processEvent(const XEvent
&e
) {
134 assert(e
.xany
.window
== _root
);
139 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_number_of_desktops
))
141 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_current_desktop
))
142 updateActiveDesktop();
143 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_active_window
))
144 updateActiveWindow();
145 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_client_list
)) {
146 // catch any window unmaps first
148 if (XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
149 DestroyNotify
, &ev
) ||
150 XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
171 void screen::handleKeypress(const XEvent
&e
) {
172 int scrolllockMask
, numlockMask
;
173 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
175 // Mask out the lock modifiers. We want our keys to always work
176 // This should be made an option
177 unsigned int state
= e
.xkey
.state
& ~(LockMask
|scrolllockMask
|numlockMask
);
178 keytree
&ktree
= _epist
->getKeyTree();
179 const Action
*it
= ktree
.getAction(e
, state
, this);
184 switch (it
->type()) {
185 case Action::nextScreen
:
186 _epist
->cycleScreen(_number
, true);
189 case Action::prevScreen
:
190 _epist
->cycleScreen(_number
, false);
193 case Action::nextWorkspace
:
194 cycleWorkspace(true, it
->number() != 0 ? it
->number(): 1);
197 case Action::prevWorkspace
:
198 cycleWorkspace(false, it
->number() != 0 ? it
->number(): 1);
201 case Action::nextWindow
:
203 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1);
206 case Action::prevWindow
:
207 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1);
210 case Action::nextWindowOnAllWorkspaces
:
211 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1, false, true);
214 case Action::prevWindowOnAllWorkspaces
:
215 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1, false, true);
218 case Action::nextWindowOnAllScreens
:
219 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1, true);
222 case Action::prevWindowOnAllScreens
:
223 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1, true);
226 case Action::nextWindowOfClass
:
227 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1,
228 false, false, true, it
->string());
231 case Action::prevWindowOfClass
:
232 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1,
233 false, false, true, it
->string());
236 case Action::nextWindowOfClassOnAllWorkspaces
:
237 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1,
238 false, true, true, it
->string());
241 case Action::prevWindowOfClassOnAllWorkspaces
:
242 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1,
243 false, true, true, it
->string());
246 case Action::changeWorkspace
:
247 changeWorkspace(it
->number());
250 case Action::upWorkspace
:
251 changeWorkspaceVert(-1);
254 case Action::downWorkspace
:
255 changeWorkspaceVert(1);
258 case Action::leftWorkspace
:
259 changeWorkspaceHorz(-1);
262 case Action::rightWorkspace
:
263 changeWorkspaceHorz(1);
266 case Action::execute
:
267 execCommand(it
->string());
270 case Action::showRootMenu
:
271 _xatom
->sendClientMessage(rootWindow(), XAtom::openbox_show_root_menu
,
275 case Action::showWorkspaceMenu
:
276 _xatom
->sendClientMessage(rootWindow(), XAtom::openbox_show_workspace_menu
,
280 case Action::toggleGrabs
: {
282 ktree
.ungrabDefaults(this);
285 ktree
.grabDefaults(this);
295 // these actions require an active window
296 if (_active
!= _clients
.end()) {
297 XWindow
*window
= *_active
;
299 switch (it
->type()) {
300 case Action::iconify
:
316 case Action::sendToWorkspace
:
317 window
->sendTo(it
->number());
320 case Action::toggleOmnipresent
:
321 if (window
->desktop() == 0xffffffff)
322 window
->sendTo(_active_desktop
);
324 window
->sendTo(0xffffffff);
327 case Action::moveWindowUp
:
328 window
->move(window
->x(), window
->y() -
329 (it
->number() != 0 ? it
->number(): 1));
332 case Action::moveWindowDown
:
333 window
->move(window
->x(), window
->y() +
334 (it
->number() != 0 ? it
->number(): 1));
337 case Action::moveWindowLeft
:
338 window
->move(window
->x() - (it
->number() != 0 ? it
->number(): 1),
342 case Action::moveWindowRight
:
343 window
->move(window
->x() + (it
->number() != 0 ? it
->number(): 1),
347 case Action::resizeWindowWidth
:
348 window
->resizeRel(it
->number(), 0);
351 case Action::resizeWindowHeight
:
352 window
->resizeRel(0, it
->number());
355 case Action::toggleShade
:
356 window
->shade(! window
->shaded());
359 case Action::toggleMaximizeHorizontal
:
360 window
->toggleMaximize(XWindow::Max_Horz
);
363 case Action::toggleMaximizeVertical
:
364 window
->toggleMaximize(XWindow::Max_Vert
);
367 case Action::toggleMaximizeFull
:
368 window
->toggleMaximize(XWindow::Max_Full
);
371 case Action::toggleDecorations
:
372 window
->decorate(! window
->decorated());
376 assert(false); // unhandled action type!
383 void screen::handleKeyrelease(const XEvent
&) {
384 // the only keyrelease event we care about (for now) is when we do stacked
385 // cycling and the modifier is released
386 if (_stacked_cycling
&& _cycling
&& nothingIsPressed()) {
387 // all modifiers have been released. ungrab the keyboard, move the
388 // focused window to the top of the Z-order and raise it
391 if (_active
!= _clients
.end()) {
392 XWindow
*w
= *_active
;
393 bool e
= _last_active
== _active
;
395 _clients
.push_front(w
);
396 _active
= _clients
.begin();
397 if (e
) _last_active
= _active
;
406 // do we want to add this window to our list?
407 bool screen::doAddWindow(Window window
) const {
411 if (! _xatom
->getValue(window
, XAtom::net_wm_window_type
, XAtom::atom
,
415 if (type
== _xatom
->getAtom(XAtom::net_wm_window_type_dock
) ||
416 type
== _xatom
->getAtom(XAtom::net_wm_window_type_menu
))
423 void screen::updateEverything() {
425 updateActiveDesktop();
427 updateActiveWindow();
431 void screen::updateNumDesktops() {
434 if (! _xatom
->getValue(_root
, XAtom::net_number_of_desktops
, XAtom::cardinal
,
435 (unsigned long)_num_desktops
))
436 _num_desktops
= 1; // assume that there is at least 1 desktop!
440 void screen::updateActiveDesktop() {
443 if (! _xatom
->getValue(_root
, XAtom::net_current_desktop
, XAtom::cardinal
,
444 (unsigned long)_active_desktop
))
445 _active_desktop
= 0; // there must be at least one desktop, and it must
446 // be the current one
450 void screen::updateClientList() {
453 WindowList::iterator insert_point
= _active
;
454 if (insert_point
!= _clients
.end())
455 ++insert_point
; // get to the item client the focused client
457 // get the client list from the root window
458 Window
*rootclients
= 0;
459 unsigned long num
= (unsigned) -1;
460 if (! _xatom
->getValue(_root
, XAtom::net_client_list
, XAtom::window
, num
,
464 WindowList::iterator it
;
465 const WindowList::iterator end
= _clients
.end();
468 for (i
= 0; i
< num
; ++i
) {
469 for (it
= _clients
.begin(); it
!= end
; ++it
)
470 if (**it
== rootclients
[i
])
472 if (it
== end
) { // didn't already exist
473 if (doAddWindow(rootclients
[i
])) {
474 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
475 if (_stacked_cycling
) {
476 // insert new clients after the active window
477 _clients
.insert(insert_point
, new XWindow(_epist
, this,
480 // insert new clients at the front of the list
481 _clients
.push_front(new XWindow(_epist
, this, rootclients
[i
]));
487 // remove clients that no longer exist (that belong to this screen)
488 for (it
= _clients
.begin(); it
!= end
;) {
489 WindowList::iterator it2
= it
;
492 // is on another screen?
493 if ((*it2
)->getScreen() != this)
496 for (i
= 0; i
< num
; ++i
)
497 if (**it2
== rootclients
[i
])
499 if (i
== num
) { // no longer exists
500 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
501 // watch for the active and last-active window
503 _active
= _clients
.end();
504 if (it2
== _last_active
)
505 _last_active
= _clients
.end();
511 if (rootclients
) delete [] rootclients
;
515 const XWindow
*screen::lastActiveWindow() const {
516 if (_last_active
!= _clients
.end())
517 return *_last_active
;
519 // find a window if one exists
520 WindowList::const_iterator it
, end
= _clients
.end();
521 for (it
= _clients
.begin(); it
!= end
; ++it
)
522 if ((*it
)->getScreen() == this && ! (*it
)->iconic() &&
523 ((*it
)->desktop() == 0xffffffff || (*it
)->desktop() == _active_desktop
))
526 // no windows on this screen
531 void screen::updateActiveWindow() {
535 _xatom
->getValue(_root
, XAtom::net_active_window
, XAtom::window
, a
);
537 WindowList::iterator it
, end
= _clients
.end();
538 for (it
= _clients
.begin(); it
!= end
; ++it
) {
540 if ((*it
)->getScreen() != this)
548 if (_active
!= end
) {
549 /* if we're not cycling and a window gets focus, add it to the top of the
552 if (_stacked_cycling
&& !_cycling
) {
553 XWindow
*win
= *_active
;
554 _clients
.remove(win
);
555 _clients
.push_front(win
);
556 _active
= _clients
.begin();
558 _last_active
= _active
;
562 /* cout << "Active window is now: ";
563 if (_active == _clients.end()) cout << "None\n";
564 else cout << "0x" << hex << (*_active)->window() << dec << endl;
569 void screen::execCommand(const string
&cmd
) const {
571 if ((pid
= fork()) == 0) {
572 // make the command run on the correct screen
573 if (putenv(const_cast<char*>(_info
->displayString().c_str()))) {
574 cout
<< "warning: couldn't set environment variable 'DISPLAY'\n";
577 execl("/bin/sh", "sh", "-c", cmd
.c_str(), NULL
);
579 } else if (pid
== -1) {
580 cout
<< _epist
->getApplicationName() <<
581 ": Could not fork a process for executing a command\n";
586 void screen::cycleWindow(unsigned int state
, const bool forward
,
587 const int increment
, const bool allscreens
,
588 const bool alldesktops
, const bool sameclass
,
592 assert(increment
> 0);
594 if (_clients
.empty()) return;
596 string
classname(cn
);
597 if (sameclass
&& classname
.empty() && _active
!= _clients
.end())
598 classname
= (*_active
)->appClass();
600 WindowList::const_iterator target
= _active
,
601 begin
= _clients
.begin(),
602 end
= _clients
.end();
606 for (int x
= 0; x
< increment
; ++x
) {
620 // must be no window to focus
621 if (target
== _active
)
624 // start back at the beginning of the loop
628 // determine if this window is invalid for cycling to
630 if (t
->iconic()) continue;
631 if (! allscreens
&& t
->getScreen() != this) continue;
632 if (! alldesktops
&& ! (t
->desktop() == _active_desktop
||
633 t
->desktop() == 0xffffffff)) continue;
634 if (sameclass
&& ! classname
.empty() &&
635 t
->appClass() != classname
) continue;
636 if (! t
->canFocus()) continue;
638 // found a good window so break out of the while, and perhaps continue
644 // phew. we found the window, so focus it.
645 if (_stacked_cycling
&& state
) {
647 // grab modifiers so we can intercept KeyReleases from them
652 // if the window is on another desktop, we can't use XSetInputFocus, since
653 // it doesn't imply a workspace change.
654 if (t
->desktop() == _active_desktop
|| t
->desktop() == 0xffffffff)
655 t
->focus(false); // focus, but don't raise
657 t
->focus(); // change workspace and focus
665 void screen::cycleWorkspace(const bool forward
, const int increment
,
666 const bool loop
) const {
668 assert(increment
> 0);
670 unsigned int destination
= _active_desktop
;
672 for (int x
= 0; x
< increment
; ++x
) {
674 if (destination
< _num_desktops
- 1)
682 destination
= _num_desktops
- 1;
686 if (destination
!= _active_desktop
)
687 changeWorkspace(destination
);
691 void screen::changeWorkspace(const int num
) const {
694 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);
697 void screen::changeWorkspaceVert(const int num
) const {
700 int num_desktops
= (signed)_num_desktops
;
701 int active_desktop
= (signed)_active_desktop
;
704 _config
->getValue(Config::workspaceColumns
, width
);
706 if (width
> num_desktops
|| width
<= 0)
709 // a cookie to the person that makes this pretty
711 wnum
= active_desktop
- width
;
713 wnum
= num_desktops
/width
* width
+ active_desktop
;
714 if (wnum
>= num_desktops
)
715 wnum
= num_desktops
- 1;
719 wnum
= active_desktop
+ width
;
720 if (wnum
>= num_desktops
) {
721 wnum
= (active_desktop
+ width
) % num_desktops
- 1;
726 changeWorkspace(wnum
);
729 void screen::changeWorkspaceHorz(const int num
) const {
732 int num_desktops
= (signed)_num_desktops
;
733 int active_desktop
= (signed)_active_desktop
;
736 _config
->getValue(Config::workspaceColumns
, width
);
738 if (width
> num_desktops
|| width
<= 0)
742 if (active_desktop
% width
!= 0)
743 changeWorkspace(active_desktop
- 1);
745 wnum
= active_desktop
+ width
- 1;
746 if (wnum
>= num_desktops
)
747 wnum
= num_desktops
- 1;
751 if (active_desktop
% width
!= width
- 1) {
752 wnum
= active_desktop
+ 1;
753 if (wnum
>= num_desktops
)
754 wnum
= num_desktops
/ width
* width
;
757 wnum
= active_desktop
- width
+ 1;
759 changeWorkspace(wnum
);
762 void screen::grabKey(const KeyCode keyCode
, const int modifierMask
) const {
764 Display
*display
= _epist
->getXDisplay();
765 int numlockMask
, scrolllockMask
;
767 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
769 XGrabKey(display
, keyCode
, modifierMask
,
770 _root
, True
, GrabModeAsync
, GrabModeAsync
);
771 XGrabKey(display
, keyCode
,
772 modifierMask
|LockMask
,
773 _root
, True
, GrabModeAsync
, GrabModeAsync
);
774 XGrabKey(display
, keyCode
,
775 modifierMask
|scrolllockMask
,
776 _root
, True
, GrabModeAsync
, GrabModeAsync
);
777 XGrabKey(display
, keyCode
,
778 modifierMask
|numlockMask
,
779 _root
, True
, GrabModeAsync
, GrabModeAsync
);
781 XGrabKey(display
, keyCode
,
782 modifierMask
|LockMask
|scrolllockMask
,
783 _root
, True
, GrabModeAsync
, GrabModeAsync
);
784 XGrabKey(display
, keyCode
,
785 modifierMask
|scrolllockMask
|numlockMask
,
786 _root
, True
, GrabModeAsync
, GrabModeAsync
);
787 XGrabKey(display
, keyCode
,
788 modifierMask
|numlockMask
|LockMask
,
789 _root
, True
, GrabModeAsync
, GrabModeAsync
);
791 XGrabKey(display
, keyCode
,
792 modifierMask
|numlockMask
|LockMask
|scrolllockMask
,
793 _root
, True
, GrabModeAsync
, GrabModeAsync
);
796 void screen::ungrabKey(const KeyCode keyCode
, const int modifierMask
) const {
798 Display
*display
= _epist
->getXDisplay();
799 int numlockMask
, scrolllockMask
;
801 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
803 XUngrabKey(display
, keyCode
, modifierMask
, _root
);
804 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
, _root
);
805 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
, _root
);
806 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
, _root
);
807 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
|scrolllockMask
, _root
);
808 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
|numlockMask
, _root
);
809 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
, _root
);
810 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
|
811 scrolllockMask
, _root
);
815 void screen::grabModifiers() const {
816 Display
*display
= _epist
->getXDisplay();
818 XGrabKeyboard(display
, rootWindow(), True
, GrabModeAsync
,
819 GrabModeAsync
, CurrentTime
);
823 void screen::ungrabModifiers() const {
824 Display
*display
= _epist
->getXDisplay();
826 XUngrabKeyboard(display
, CurrentTime
);
830 bool screen::nothingIsPressed(void) const
833 XQueryKeymap(_epist
->getXDisplay(), keys
);
835 for (int i
= 0; i
< 32; ++i
) {