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
,
153 XWindow
*win
= _epist
->findWindow(e
.xany
.window
);
154 if (win
) win
->processEvent(ev
);
173 void screen::handleKeypress(const XEvent
&e
) {
174 int scrolllockMask
, numlockMask
;
175 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
177 // Mask out the lock modifiers. We want our keys to always work
178 // This should be made an option
179 unsigned int state
= e
.xkey
.state
& ~(LockMask
|scrolllockMask
|numlockMask
);
180 keytree
&ktree
= _epist
->getKeyTree();
181 const Action
*it
= ktree
.getAction(e
, state
, this);
186 switch (it
->type()) {
187 case Action::nextScreen
:
188 _epist
->cycleScreen(_number
, true);
191 case Action::prevScreen
:
192 _epist
->cycleScreen(_number
, false);
195 case Action::nextWorkspace
:
196 cycleWorkspace(true, it
->number() != 0 ? it
->number(): 1);
199 case Action::prevWorkspace
:
200 cycleWorkspace(false, it
->number() != 0 ? it
->number(): 1);
203 case Action::nextWindow
:
205 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1);
208 case Action::prevWindow
:
209 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1);
212 case Action::nextWindowOnAllWorkspaces
:
213 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1, false, true);
216 case Action::prevWindowOnAllWorkspaces
:
217 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1, false, true);
220 case Action::nextWindowOnAllScreens
:
221 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1, true);
224 case Action::prevWindowOnAllScreens
:
225 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1, true);
228 case Action::nextWindowOfClass
:
229 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1,
230 false, false, true, it
->string());
233 case Action::prevWindowOfClass
:
234 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1,
235 false, false, true, it
->string());
238 case Action::nextWindowOfClassOnAllWorkspaces
:
239 cycleWindow(state
, true, it
->number() != 0 ? it
->number(): 1,
240 false, true, true, it
->string());
243 case Action::prevWindowOfClassOnAllWorkspaces
:
244 cycleWindow(state
, false, it
->number() != 0 ? it
->number(): 1,
245 false, true, true, it
->string());
248 case Action::changeWorkspace
:
249 changeWorkspace(it
->number());
252 case Action::upWorkspace
:
253 changeWorkspaceVert(-1);
256 case Action::downWorkspace
:
257 changeWorkspaceVert(1);
260 case Action::leftWorkspace
:
261 changeWorkspaceHorz(-1);
264 case Action::rightWorkspace
:
265 changeWorkspaceHorz(1);
268 case Action::execute
:
269 execCommand(it
->string());
272 case Action::showRootMenu
:
273 _xatom
->sendClientMessage(rootWindow(), XAtom::openbox_show_root_menu
,
277 case Action::showWorkspaceMenu
:
278 _xatom
->sendClientMessage(rootWindow(), XAtom::openbox_show_workspace_menu
,
282 case Action::toggleGrabs
: {
284 ktree
.ungrabDefaults(this);
287 ktree
.grabDefaults(this);
297 // these actions require an active window
298 if (_active
!= _clients
.end()) {
299 XWindow
*window
= *_active
;
301 switch (it
->type()) {
302 case Action::iconify
:
318 case Action::sendToWorkspace
:
319 window
->sendTo(it
->number());
322 case Action::toggleOmnipresent
:
323 if (window
->desktop() == 0xffffffff)
324 window
->sendTo(_active_desktop
);
326 window
->sendTo(0xffffffff);
329 case Action::moveWindowUp
:
330 window
->move(window
->x(), window
->y() -
331 (it
->number() != 0 ? it
->number(): 1));
334 case Action::moveWindowDown
:
335 window
->move(window
->x(), window
->y() +
336 (it
->number() != 0 ? it
->number(): 1));
339 case Action::moveWindowLeft
:
340 window
->move(window
->x() - (it
->number() != 0 ? it
->number(): 1),
344 case Action::moveWindowRight
:
345 window
->move(window
->x() + (it
->number() != 0 ? it
->number(): 1),
349 case Action::resizeWindowWidth
:
350 window
->resizeRel(it
->number(), 0);
353 case Action::resizeWindowHeight
:
354 window
->resizeRel(0, it
->number());
357 case Action::toggleShade
:
358 window
->shade(! window
->shaded());
361 case Action::toggleMaximizeHorizontal
:
362 window
->toggleMaximize(XWindow::Max_Horz
);
365 case Action::toggleMaximizeVertical
:
366 window
->toggleMaximize(XWindow::Max_Vert
);
369 case Action::toggleMaximizeFull
:
370 window
->toggleMaximize(XWindow::Max_Full
);
373 case Action::toggleDecorations
:
374 window
->decorate(! window
->decorated());
378 assert(false); // unhandled action type!
385 void screen::handleKeyrelease(const XEvent
&) {
386 // the only keyrelease event we care about (for now) is when we do stacked
387 // cycling and the modifier is released
388 if (_stacked_cycling
&& _cycling
&& nothingIsPressed()) {
389 // all modifiers have been released. ungrab the keyboard, move the
390 // focused window to the top of the Z-order and raise it
393 if (_active
!= _clients
.end()) {
394 XWindow
*w
= *_active
;
395 bool e
= _last_active
== _active
;
397 _clients
.push_front(w
);
398 _active
= _clients
.begin();
399 if (e
) _last_active
= _active
;
408 // do we want to add this window to our list?
409 bool screen::doAddWindow(Window window
) const {
413 if (! _xatom
->getValue(window
, XAtom::net_wm_window_type
, XAtom::atom
,
417 if (type
== _xatom
->getAtom(XAtom::net_wm_window_type_dock
) ||
418 type
== _xatom
->getAtom(XAtom::net_wm_window_type_menu
))
425 void screen::updateEverything() {
427 updateActiveDesktop();
429 updateActiveWindow();
433 void screen::updateNumDesktops() {
436 if (! _xatom
->getValue(_root
, XAtom::net_number_of_desktops
, XAtom::cardinal
,
437 (unsigned long)_num_desktops
))
438 _num_desktops
= 1; // assume that there is at least 1 desktop!
442 void screen::updateActiveDesktop() {
445 if (! _xatom
->getValue(_root
, XAtom::net_current_desktop
, XAtom::cardinal
,
446 (unsigned long)_active_desktop
))
447 _active_desktop
= 0; // there must be at least one desktop, and it must
448 // be the current one
452 void screen::updateClientList() {
455 WindowList::iterator insert_point
= _active
;
456 if (insert_point
!= _clients
.end())
457 ++insert_point
; // get to the item client the focused client
459 // get the client list from the root window
460 Window
*rootclients
= 0;
461 unsigned long num
= (unsigned) -1;
462 if (! _xatom
->getValue(_root
, XAtom::net_client_list
, XAtom::window
, num
,
466 WindowList::iterator it
;
467 const WindowList::iterator end
= _clients
.end();
470 for (i
= 0; i
< num
; ++i
) {
471 for (it
= _clients
.begin(); it
!= end
; ++it
)
472 if (**it
== rootclients
[i
])
474 if (it
== end
) { // didn't already exist
475 if (doAddWindow(rootclients
[i
])) {
476 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
477 // insert new clients after the active window
478 _clients
.insert(insert_point
, new XWindow(_epist
, this,
484 // remove clients that no longer exist (that belong to this screen)
485 for (it
= _clients
.begin(); it
!= end
;) {
486 WindowList::iterator it2
= it
;
489 // is on another screen?
490 if ((*it2
)->getScreen() != this)
493 for (i
= 0; i
< num
; ++i
)
494 if (**it2
== rootclients
[i
])
496 if (i
== num
) { // no longer exists
497 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
498 // watch for the active and last-active window
500 _active
= _clients
.end();
501 if (it2
== _last_active
)
502 _last_active
= _clients
.end();
508 if (rootclients
) delete [] rootclients
;
512 const XWindow
*screen::lastActiveWindow() const {
513 if (_last_active
!= _clients
.end())
514 return *_last_active
;
516 // find a window if one exists
517 WindowList::const_iterator it
, end
= _clients
.end();
518 for (it
= _clients
.begin(); it
!= end
; ++it
)
519 if ((*it
)->getScreen() == this && ! (*it
)->iconic() &&
521 ((*it
)->desktop() == 0xffffffff ||
522 (*it
)->desktop() == _active_desktop
))
525 // no windows on this screen
530 void screen::updateActiveWindow() {
534 _xatom
->getValue(_root
, XAtom::net_active_window
, XAtom::window
, a
);
536 WindowList::iterator it
, end
= _clients
.end();
537 for (it
= _clients
.begin(); it
!= end
; ++it
) {
539 if ((*it
)->getScreen() != this)
547 if (_active
!= end
) {
548 /* if we're not cycling and a window gets focus, add it to the top of the
551 if (_stacked_cycling
&& !_cycling
) {
552 XWindow
*win
= *_active
;
553 _clients
.remove(win
);
554 _clients
.push_front(win
);
555 _active
= _clients
.begin();
557 _last_active
= _active
;
561 /* cout << "Active window is now: ";
562 if (_active == _clients.end()) cout << "None\n";
563 else cout << "0x" << hex << (*_active)->window() << dec << endl;
568 void screen::execCommand(const string
&cmd
) const {
570 if ((pid
= fork()) == 0) {
571 // disconnect the child from epist's session and the tty
572 if (setsid() == -1) {
573 cout
<< "warning: could not start a new process group\n";
577 // make the command run on the correct screen
578 if (putenv(const_cast<char*>(_info
->displayString().c_str()))) {
579 cout
<< "warning: couldn't set environment variable 'DISPLAY'\n";
582 execl("/bin/sh", "sh", "-c", cmd
.c_str(), NULL
);
584 } else if (pid
== -1) {
585 cout
<< _epist
->getApplicationName() <<
586 ": Could not fork a process for executing a command\n";
591 void screen::cycleWindow(unsigned int state
, const bool forward
,
592 const int increment
, const bool allscreens
,
593 const bool alldesktops
, const bool sameclass
,
597 assert(increment
> 0);
599 if (_clients
.empty()) return;
601 string
classname(cn
);
602 if (sameclass
&& classname
.empty() && _active
!= _clients
.end())
603 classname
= (*_active
)->appClass();
605 WindowList::const_iterator target
= _active
,
606 begin
= _clients
.begin(),
607 end
= _clients
.end();
611 for (int x
= 0; x
< increment
; ++x
) {
625 // must be no window to focus
626 if (target
== _active
)
629 // start back at the beginning of the loop
633 // determine if this window is invalid for cycling to
635 if (t
->iconic()) continue;
636 if (! allscreens
&& t
->getScreen() != this) continue;
637 if (! alldesktops
&& ! (t
->desktop() == _active_desktop
||
638 t
->desktop() == 0xffffffff)) continue;
639 if (sameclass
&& ! classname
.empty() &&
640 t
->appClass() != classname
) continue;
641 if (! t
->canFocus()) continue;
643 // found a good window so break out of the while, and perhaps continue
649 // phew. we found the window, so focus it.
650 if (_stacked_cycling
&& state
) {
652 // grab modifiers so we can intercept KeyReleases from them
657 // if the window is on another desktop, we can't use XSetInputFocus, since
658 // it doesn't imply a workspace change.
659 if (t
->desktop() == _active_desktop
|| t
->desktop() == 0xffffffff)
660 t
->focus(false); // focus, but don't raise
662 t
->focus(); // change workspace and focus
670 void screen::cycleWorkspace(const bool forward
, const int increment
,
671 const bool loop
) const {
673 assert(increment
> 0);
675 unsigned int destination
= _active_desktop
;
677 for (int x
= 0; x
< increment
; ++x
) {
679 if (destination
< _num_desktops
- 1)
687 destination
= _num_desktops
- 1;
691 if (destination
!= _active_desktop
)
692 changeWorkspace(destination
);
696 void screen::changeWorkspace(const int num
) const {
699 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);
702 void screen::changeWorkspaceVert(const int num
) const {
705 int num_desktops
= (signed)_num_desktops
;
706 int active_desktop
= (signed)_active_desktop
;
709 _config
->getValue(Config::workspaceColumns
, width
);
711 if (width
> num_desktops
|| width
<= 0)
714 // a cookie to the person that makes this pretty
716 wnum
= active_desktop
- width
;
718 wnum
= num_desktops
/width
* width
+ active_desktop
;
719 if (wnum
>= num_desktops
)
720 wnum
= num_desktops
- 1;
724 wnum
= active_desktop
+ width
;
725 if (wnum
>= num_desktops
) {
726 wnum
= (active_desktop
+ width
) % num_desktops
- 1;
731 changeWorkspace(wnum
);
734 void screen::changeWorkspaceHorz(const int num
) const {
737 int num_desktops
= (signed)_num_desktops
;
738 int active_desktop
= (signed)_active_desktop
;
741 _config
->getValue(Config::workspaceColumns
, width
);
743 if (width
> num_desktops
|| width
<= 0)
747 if (active_desktop
% width
!= 0)
748 changeWorkspace(active_desktop
- 1);
750 wnum
= active_desktop
+ width
- 1;
751 if (wnum
>= num_desktops
)
752 wnum
= num_desktops
- 1;
756 if (active_desktop
% width
!= width
- 1) {
757 wnum
= active_desktop
+ 1;
758 if (wnum
>= num_desktops
)
759 wnum
= num_desktops
/ width
* width
;
762 wnum
= active_desktop
- width
+ 1;
764 changeWorkspace(wnum
);
767 void screen::grabKey(const KeyCode keyCode
, const int modifierMask
) const {
769 Display
*display
= _epist
->getXDisplay();
770 int numlockMask
, scrolllockMask
;
772 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
774 XGrabKey(display
, keyCode
, modifierMask
,
775 _root
, True
, GrabModeAsync
, GrabModeAsync
);
776 XGrabKey(display
, keyCode
,
777 modifierMask
|LockMask
,
778 _root
, True
, GrabModeAsync
, GrabModeAsync
);
779 XGrabKey(display
, keyCode
,
780 modifierMask
|scrolllockMask
,
781 _root
, True
, GrabModeAsync
, GrabModeAsync
);
782 XGrabKey(display
, keyCode
,
783 modifierMask
|numlockMask
,
784 _root
, True
, GrabModeAsync
, GrabModeAsync
);
786 XGrabKey(display
, keyCode
,
787 modifierMask
|LockMask
|scrolllockMask
,
788 _root
, True
, GrabModeAsync
, GrabModeAsync
);
789 XGrabKey(display
, keyCode
,
790 modifierMask
|scrolllockMask
|numlockMask
,
791 _root
, True
, GrabModeAsync
, GrabModeAsync
);
792 XGrabKey(display
, keyCode
,
793 modifierMask
|numlockMask
|LockMask
,
794 _root
, True
, GrabModeAsync
, GrabModeAsync
);
796 XGrabKey(display
, keyCode
,
797 modifierMask
|numlockMask
|LockMask
|scrolllockMask
,
798 _root
, True
, GrabModeAsync
, GrabModeAsync
);
801 void screen::ungrabKey(const KeyCode keyCode
, const int modifierMask
) const {
803 Display
*display
= _epist
->getXDisplay();
804 int numlockMask
, scrolllockMask
;
806 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
808 XUngrabKey(display
, keyCode
, modifierMask
, _root
);
809 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
, _root
);
810 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
, _root
);
811 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
, _root
);
812 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
|scrolllockMask
, _root
);
813 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
|numlockMask
, _root
);
814 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
, _root
);
815 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
|
816 scrolllockMask
, _root
);
820 void screen::grabModifiers() const {
821 Display
*display
= _epist
->getXDisplay();
823 XGrabKeyboard(display
, rootWindow(), True
, GrabModeAsync
,
824 GrabModeAsync
, CurrentTime
);
828 void screen::ungrabModifiers() const {
829 Display
*display
= _epist
->getXDisplay();
831 XUngrabKeyboard(display
, CurrentTime
);
835 bool screen::nothingIsPressed(void) const
838 XQueryKeymap(_epist
->getXDisplay(), keys
);
840 for (int i
= 0; i
< 32; ++i
) {