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), _stacked_raise(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 _config
->getValue(Config::stackedCyclingRaise
, _stacked_raise
);
82 // find a window manager supporting NETWM, waiting for it to load if we must
83 int count
= 20; // try for 20 seconds
85 while (! (_epist
->doShutdown() || _managed
|| count
<= 0)) {
86 if (! (_managed
= findSupportingWM()))
91 cout
<< "Found compatible window manager '" << _wm_name
<< "' for screen "
94 cout
<< "Unable to find a compatible window manager for screen " <<
99 XSelectInput(_epist
->getXDisplay(), _root
, PropertyChangeMask
);
104 XSelectInput(_epist
->getXDisplay(), _root
, None
);
108 bool screen::findSupportingWM() {
110 if (! _xatom
->getValue(_root
, XAtom::net_supporting_wm_check
, XAtom::window
,
111 support_win
) || support_win
== None
)
115 _xatom
->getValue(support_win
, XAtom::net_wm_name
, XAtom::utf8
, title
);
121 XWindow
*screen::findWindow(const XEvent
&e
) const {
124 WindowList::const_iterator it
, end
= _clients
.end();
125 for (it
= _clients
.begin(); it
!= end
; ++it
)
126 if (**it
== e
.xany
.window
)
134 void screen::processEvent(const XEvent
&e
) {
136 assert(e
.xany
.window
== _root
);
141 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_number_of_desktops
))
143 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_current_desktop
))
144 updateActiveDesktop();
145 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_active_window
))
146 updateActiveWindow();
147 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_client_list
)) {
148 // catch any window unmaps first
150 if (XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
151 DestroyNotify
, &ev
) ||
152 XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
155 XWindow
*win
= _epist
->findWindow(e
.xany
.window
);
156 if (win
) win
->processEvent(ev
);
175 void screen::handleKeypress(const XEvent
&e
) {
176 int scrolllockMask
, numlockMask
;
177 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
179 // Mask out the lock modifiers. We want our keys to always work
180 // This should be made an option
181 unsigned int state
= e
.xkey
.state
& ~(LockMask
|scrolllockMask
|numlockMask
);
182 keytree
&ktree
= _epist
->getKeyTree();
183 const Action
*it
= ktree
.getAction(e
, state
, this);
188 switch (it
->type()) {
189 case Action::nextScreen
:
190 _epist
->cycleScreen(_number
, true);
193 case Action::prevScreen
:
194 _epist
->cycleScreen(_number
, false);
197 case Action::nextWorkspace
:
198 cycleWorkspace(true, it
->number() != 0 ? it
->number(): 1);
201 case Action::prevWorkspace
:
202 cycleWorkspace(false, it
->number() != 0 ? it
->number(): 1);
205 case Action::nextWindow
:
207 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1);
210 case Action::prevWindow
:
211 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1);
214 case Action::nextWindowOnAllWorkspaces
:
215 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1, false, true);
218 case Action::prevWindowOnAllWorkspaces
:
219 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1, false, true);
222 case Action::nextWindowOnAllScreens
:
223 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1, true);
226 case Action::prevWindowOnAllScreens
:
227 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1, true);
230 case Action::nextWindowOfClass
:
231 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1,
232 false, false, true, it
->string());
235 case Action::prevWindowOfClass
:
236 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1,
237 false, false, true, it
->string());
240 case Action::nextWindowOfClassOnAllWorkspaces
:
241 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1,
242 false, true, true, it
->string());
245 case Action::prevWindowOfClassOnAllWorkspaces
:
246 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1,
247 false, true, true, it
->string());
250 case Action::changeWorkspace
:
251 changeWorkspace(it
->number());
254 case Action::upWorkspace
:
255 changeWorkspaceVert(-1);
258 case Action::downWorkspace
:
259 changeWorkspaceVert(1);
262 case Action::leftWorkspace
:
263 changeWorkspaceHorz(-1);
266 case Action::rightWorkspace
:
267 changeWorkspaceHorz(1);
270 case Action::execute
:
271 execCommand(it
->string());
274 case Action::showRootMenu
:
275 _xatom
->sendClientMessage(rootWindow(), XAtom::openbox_show_root_menu
,
279 case Action::showWorkspaceMenu
:
280 _xatom
->sendClientMessage(rootWindow(), XAtom::openbox_show_workspace_menu
,
284 case Action::toggleGrabs
: {
286 ktree
.ungrabDefaults(this);
289 ktree
.grabDefaults(this);
299 // these actions require an active window
300 if (_active
!= _clients
.end()) {
301 XWindow
*window
= *_active
;
303 switch (it
->type()) {
304 case Action::iconify
:
320 case Action::sendToWorkspace
:
321 window
->sendTo(it
->number());
324 case Action::toggleOmnipresent
:
325 if (window
->desktop() == 0xffffffff)
326 window
->sendTo(_active_desktop
);
328 window
->sendTo(0xffffffff);
331 case Action::moveWindowUp
:
332 window
->move(window
->x(), window
->y() -
333 (it
->number() != 0 ? it
->number(): 1));
336 case Action::moveWindowDown
:
337 window
->move(window
->x(), window
->y() +
338 (it
->number() != 0 ? it
->number(): 1));
341 case Action::moveWindowLeft
:
342 window
->move(window
->x() - (it
->number() != 0 ? it
->number(): 1),
346 case Action::moveWindowRight
:
347 window
->move(window
->x() + (it
->number() != 0 ? it
->number(): 1),
351 case Action::resizeWindowWidth
:
352 window
->resizeRel(it
->number(), 0);
355 case Action::resizeWindowHeight
:
356 window
->resizeRel(0, it
->number());
359 case Action::toggleShade
:
360 window
->shade(! window
->shaded());
363 case Action::toggleMaximizeHorizontal
:
364 window
->toggleMaximize(XWindow::Max_Horz
);
367 case Action::toggleMaximizeVertical
:
368 window
->toggleMaximize(XWindow::Max_Vert
);
371 case Action::toggleMaximizeFull
:
372 window
->toggleMaximize(XWindow::Max_Full
);
375 case Action::toggleDecorations
:
376 window
->decorate(! window
->decorated());
380 assert(false); // unhandled action type!
387 void screen::handleKeyrelease(const XEvent
&) {
388 // the only keyrelease event we care about (for now) is when we do stacked
389 // cycling and the modifier is released
390 if (_stacked_cycling
&& _cycling
&& nothingIsPressed()) {
391 // all modifiers have been released. ungrab the keyboard, move the
392 // focused window to the top of the Z-order and raise it
395 if (_active
!= _clients
.end()) {
396 XWindow
*w
= *_active
;
397 bool e
= _last_active
== _active
;
399 _clients
.push_front(w
);
400 _active
= _clients
.begin();
401 if (e
) _last_active
= _active
;
410 // do we want to add this window to our list?
411 bool screen::doAddWindow(Window window
) const {
415 if (! _xatom
->getValue(window
, XAtom::net_wm_window_type
, XAtom::atom
,
419 if (type
== _xatom
->getAtom(XAtom::net_wm_window_type_dock
) ||
420 type
== _xatom
->getAtom(XAtom::net_wm_window_type_menu
))
427 void screen::updateEverything() {
429 updateActiveDesktop();
431 updateActiveWindow();
435 void screen::updateNumDesktops() {
438 if (! _xatom
->getValue(_root
, XAtom::net_number_of_desktops
, XAtom::cardinal
,
439 (unsigned long)_num_desktops
))
440 _num_desktops
= 1; // assume that there is at least 1 desktop!
444 void screen::updateActiveDesktop() {
447 if (! _xatom
->getValue(_root
, XAtom::net_current_desktop
, XAtom::cardinal
,
448 (unsigned long)_active_desktop
))
449 _active_desktop
= 0; // there must be at least one desktop, and it must
450 // be the current one
454 void screen::updateClientList() {
457 WindowList::iterator insert_point
= _active
;
458 if (insert_point
!= _clients
.end())
459 ++insert_point
; // get to the item client the focused client
461 // get the client list from the root window
462 Window
*rootclients
= 0;
463 unsigned long num
= (unsigned) -1;
464 if (! _xatom
->getValue(_root
, XAtom::net_client_list
, XAtom::window
, num
,
468 WindowList::iterator it
;
469 const WindowList::iterator end
= _clients
.end();
472 for (i
= 0; i
< num
; ++i
) {
473 for (it
= _clients
.begin(); it
!= end
; ++it
)
474 if (**it
== rootclients
[i
])
476 if (it
== end
) { // didn't already exist
477 if (doAddWindow(rootclients
[i
])) {
478 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
479 // insert new clients after the active window
480 _clients
.insert(insert_point
, new XWindow(_epist
, this,
486 // remove clients that no longer exist (that belong to this screen)
487 for (it
= _clients
.begin(); it
!= end
;) {
488 WindowList::iterator it2
= it
;
491 // is on another screen?
492 if ((*it2
)->getScreen() != this)
495 for (i
= 0; i
< num
; ++i
)
496 if (**it2
== rootclients
[i
])
498 if (i
== num
) { // no longer exists
499 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
500 // watch for the active and last-active window
502 _active
= _clients
.end();
503 if (it2
== _last_active
)
504 _last_active
= _clients
.end();
510 if (rootclients
) delete [] rootclients
;
514 const XWindow
*screen::lastActiveWindow() const {
515 if (_last_active
!= _clients
.end())
516 return *_last_active
;
518 // find a window if one exists
519 WindowList::const_iterator it
, end
= _clients
.end();
520 for (it
= _clients
.begin(); it
!= end
; ++it
)
521 if ((*it
)->getScreen() == this && ! (*it
)->iconic() &&
523 ((*it
)->desktop() == 0xffffffff ||
524 (*it
)->desktop() == _active_desktop
))
527 // no windows on this screen
532 void screen::updateActiveWindow() {
536 _xatom
->getValue(_root
, XAtom::net_active_window
, XAtom::window
, a
);
538 WindowList::iterator it
, end
= _clients
.end();
539 for (it
= _clients
.begin(); it
!= end
; ++it
) {
541 if ((*it
)->getScreen() != this)
549 if (_active
!= end
) {
550 /* if we're not cycling and a window gets focus, add it to the top of the
553 if (_stacked_cycling
&& !_cycling
) {
554 XWindow
*win
= *_active
;
555 _clients
.remove(win
);
556 _clients
.push_front(win
);
557 _active
= _clients
.begin();
559 _last_active
= _active
;
563 /* cout << "Active window is now: ";
564 if (_active == _clients.end()) cout << "None\n";
565 else cout << "0x" << hex << (*_active)->window() << dec << endl;
570 void screen::execCommand(const string
&cmd
) const {
572 if ((pid
= fork()) == 0) {
573 // disconnect the child from epist's session and the tty
574 if (setsid() == -1) {
575 cout
<< "warning: could not start a new process group\n";
579 // make the command run on the correct screen
580 if (putenv(const_cast<char*>(_info
->displayString().c_str()))) {
581 cout
<< "warning: couldn't set environment variable 'DISPLAY'\n";
584 execl("/bin/sh", "sh", "-c", cmd
.c_str(), NULL
);
586 } else if (pid
== -1) {
587 cout
<< _epist
->getApplicationName() <<
588 ": Could not fork a process for executing a command\n";
593 void screen::cycleWindow(unsigned int state
, const bool forward
,
594 const int increment
, const bool allscreens
,
595 const bool alldesktops
, const bool sameclass
,
599 assert(increment
> 0);
601 if (_clients
.empty()) return;
603 string
classname(cn
);
604 if (sameclass
&& classname
.empty() && _active
!= _clients
.end())
605 classname
= (*_active
)->appClass();
607 WindowList::const_iterator target
= _active
,
608 begin
= _clients
.begin(),
609 end
= _clients
.end();
613 for (int x
= 0; x
< increment
; ++x
) {
627 // must be no window to focus
628 if (target
== _active
)
631 // start back at the beginning of the loop
635 // determine if this window is invalid for cycling to
637 if (t
->iconic()) continue;
638 if (! allscreens
&& t
->getScreen() != this) continue;
639 if (! alldesktops
&& ! (t
->desktop() == _active_desktop
||
640 t
->desktop() == 0xffffffff)) continue;
641 if (sameclass
&& ! classname
.empty() &&
642 t
->appClass() != classname
) continue;
643 if (! t
->canFocus()) continue;
645 // found a good window so break out of the while, and perhaps continue
651 // phew. we found the window, so focus it.
652 if (_stacked_cycling
&& state
) {
654 // grab modifiers so we can intercept KeyReleases from them
659 // if the window is on another desktop, we can't use XSetInputFocus, since
660 // it doesn't imply a workspace change.
661 if (_stacked_raise
|| (t
->desktop() != _active_desktop
&&
662 t
->desktop() != 0xffffffff))
665 t
->focus(false); // don't raise
673 void screen::cycleWorkspace(const bool forward
, const int increment
,
674 const bool loop
) const {
676 assert(increment
> 0);
678 unsigned int destination
= _active_desktop
;
680 for (int x
= 0; x
< increment
; ++x
) {
682 if (destination
< _num_desktops
- 1)
690 destination
= _num_desktops
- 1;
694 if (destination
!= _active_desktop
)
695 changeWorkspace(destination
);
699 void screen::changeWorkspace(const int num
) const {
702 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);
705 void screen::changeWorkspaceVert(const int num
) const {
708 int num_desktops
= (signed)_num_desktops
;
709 int active_desktop
= (signed)_active_desktop
;
712 _config
->getValue(Config::workspaceColumns
, width
);
714 if (width
> num_desktops
|| width
<= 0)
717 // a cookie to the person that makes this pretty
719 wnum
= active_desktop
- width
;
721 wnum
= num_desktops
/width
* width
+ active_desktop
;
722 if (wnum
>= num_desktops
)
723 wnum
= num_desktops
- 1;
727 wnum
= active_desktop
+ width
;
728 if (wnum
>= num_desktops
) {
729 wnum
= (active_desktop
+ width
) % num_desktops
- 1;
734 changeWorkspace(wnum
);
737 void screen::changeWorkspaceHorz(const int num
) const {
740 int num_desktops
= (signed)_num_desktops
;
741 int active_desktop
= (signed)_active_desktop
;
744 _config
->getValue(Config::workspaceColumns
, width
);
746 if (width
> num_desktops
|| width
<= 0)
750 if (active_desktop
% width
!= 0)
751 changeWorkspace(active_desktop
- 1);
753 wnum
= active_desktop
+ width
- 1;
754 if (wnum
>= num_desktops
)
755 wnum
= num_desktops
- 1;
759 if (active_desktop
% width
!= width
- 1) {
760 wnum
= active_desktop
+ 1;
761 if (wnum
>= num_desktops
)
762 wnum
= num_desktops
/ width
* width
;
765 wnum
= active_desktop
- width
+ 1;
767 changeWorkspace(wnum
);
770 void screen::grabKey(const KeyCode keyCode
, const int modifierMask
) const {
772 Display
*display
= _epist
->getXDisplay();
773 int numlockMask
, scrolllockMask
;
775 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
777 XGrabKey(display
, keyCode
, modifierMask
,
778 _root
, True
, GrabModeAsync
, GrabModeAsync
);
779 XGrabKey(display
, keyCode
,
780 modifierMask
|LockMask
,
781 _root
, True
, GrabModeAsync
, GrabModeAsync
);
782 XGrabKey(display
, keyCode
,
783 modifierMask
|scrolllockMask
,
784 _root
, True
, GrabModeAsync
, GrabModeAsync
);
785 XGrabKey(display
, keyCode
,
786 modifierMask
|numlockMask
,
787 _root
, True
, GrabModeAsync
, GrabModeAsync
);
789 XGrabKey(display
, keyCode
,
790 modifierMask
|LockMask
|scrolllockMask
,
791 _root
, True
, GrabModeAsync
, GrabModeAsync
);
792 XGrabKey(display
, keyCode
,
793 modifierMask
|scrolllockMask
|numlockMask
,
794 _root
, True
, GrabModeAsync
, GrabModeAsync
);
795 XGrabKey(display
, keyCode
,
796 modifierMask
|numlockMask
|LockMask
,
797 _root
, True
, GrabModeAsync
, GrabModeAsync
);
799 XGrabKey(display
, keyCode
,
800 modifierMask
|numlockMask
|LockMask
|scrolllockMask
,
801 _root
, True
, GrabModeAsync
, GrabModeAsync
);
804 void screen::ungrabKey(const KeyCode keyCode
, const int modifierMask
) const {
806 Display
*display
= _epist
->getXDisplay();
807 int numlockMask
, scrolllockMask
;
809 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
811 XUngrabKey(display
, keyCode
, modifierMask
, _root
);
812 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
, _root
);
813 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
, _root
);
814 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
, _root
);
815 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
|scrolllockMask
, _root
);
816 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
|numlockMask
, _root
);
817 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
, _root
);
818 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
|
819 scrolllockMask
, _root
);
823 void screen::grabModifiers() const {
824 Display
*display
= _epist
->getXDisplay();
826 XGrabKeyboard(display
, rootWindow(), True
, GrabModeAsync
,
827 GrabModeAsync
, CurrentTime
);
831 void screen::ungrabModifiers() const {
832 Display
*display
= _epist
->getXDisplay();
834 XUngrabKeyboard(display
, CurrentTime
);
838 bool screen::nothingIsPressed(void) const
841 XQueryKeymap(_epist
->getXDisplay(), keys
);
843 for (int i
= 0; i
< 32; ++i
) {