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 // make the command run on the correct screen
572 if (putenv(const_cast<char*>(_info
->displayString().c_str()))) {
573 cout
<< "warning: couldn't set environment variable 'DISPLAY'\n";
576 execl("/bin/sh", "sh", "-c", cmd
.c_str(), NULL
);
578 } else if (pid
== -1) {
579 cout
<< _epist
->getApplicationName() <<
580 ": Could not fork a process for executing a command\n";
585 void screen::cycleWindow(unsigned int state
, const bool forward
,
586 const int increment
, const bool allscreens
,
587 const bool alldesktops
, const bool sameclass
,
591 assert(increment
> 0);
593 if (_clients
.empty()) return;
595 string
classname(cn
);
596 if (sameclass
&& classname
.empty() && _active
!= _clients
.end())
597 classname
= (*_active
)->appClass();
599 WindowList::const_iterator target
= _active
,
600 begin
= _clients
.begin(),
601 end
= _clients
.end();
605 for (int x
= 0; x
< increment
; ++x
) {
619 // must be no window to focus
620 if (target
== _active
)
623 // start back at the beginning of the loop
627 // determine if this window is invalid for cycling to
629 if (t
->iconic()) continue;
630 if (! allscreens
&& t
->getScreen() != this) continue;
631 if (! alldesktops
&& ! (t
->desktop() == _active_desktop
||
632 t
->desktop() == 0xffffffff)) continue;
633 if (sameclass
&& ! classname
.empty() &&
634 t
->appClass() != classname
) continue;
635 if (! t
->canFocus()) continue;
637 // found a good window so break out of the while, and perhaps continue
643 // phew. we found the window, so focus it.
644 if (_stacked_cycling
&& state
) {
646 // grab modifiers so we can intercept KeyReleases from them
651 // if the window is on another desktop, we can't use XSetInputFocus, since
652 // it doesn't imply a workspace change.
653 if (t
->desktop() == _active_desktop
|| t
->desktop() == 0xffffffff)
654 t
->focus(false); // focus, but don't raise
656 t
->focus(); // change workspace and focus
664 void screen::cycleWorkspace(const bool forward
, const int increment
,
665 const bool loop
) const {
667 assert(increment
> 0);
669 unsigned int destination
= _active_desktop
;
671 for (int x
= 0; x
< increment
; ++x
) {
673 if (destination
< _num_desktops
- 1)
681 destination
= _num_desktops
- 1;
685 if (destination
!= _active_desktop
)
686 changeWorkspace(destination
);
690 void screen::changeWorkspace(const int num
) const {
693 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);
696 void screen::changeWorkspaceVert(const int num
) const {
699 int num_desktops
= (signed)_num_desktops
;
700 int active_desktop
= (signed)_active_desktop
;
703 _config
->getValue(Config::workspaceColumns
, width
);
705 if (width
> num_desktops
|| width
<= 0)
708 // a cookie to the person that makes this pretty
710 wnum
= active_desktop
- width
;
712 wnum
= num_desktops
/width
* width
+ active_desktop
;
713 if (wnum
>= num_desktops
)
714 wnum
= num_desktops
- 1;
718 wnum
= active_desktop
+ width
;
719 if (wnum
>= num_desktops
) {
720 wnum
= (active_desktop
+ width
) % num_desktops
- 1;
725 changeWorkspace(wnum
);
728 void screen::changeWorkspaceHorz(const int num
) const {
731 int num_desktops
= (signed)_num_desktops
;
732 int active_desktop
= (signed)_active_desktop
;
735 _config
->getValue(Config::workspaceColumns
, width
);
737 if (width
> num_desktops
|| width
<= 0)
741 if (active_desktop
% width
!= 0)
742 changeWorkspace(active_desktop
- 1);
744 wnum
= active_desktop
+ width
- 1;
745 if (wnum
>= num_desktops
)
746 wnum
= num_desktops
- 1;
750 if (active_desktop
% width
!= width
- 1) {
751 wnum
= active_desktop
+ 1;
752 if (wnum
>= num_desktops
)
753 wnum
= num_desktops
/ width
* width
;
756 wnum
= active_desktop
- width
+ 1;
758 changeWorkspace(wnum
);
761 void screen::grabKey(const KeyCode keyCode
, const int modifierMask
) const {
763 Display
*display
= _epist
->getXDisplay();
764 int numlockMask
, scrolllockMask
;
766 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
768 XGrabKey(display
, keyCode
, modifierMask
,
769 _root
, True
, GrabModeAsync
, GrabModeAsync
);
770 XGrabKey(display
, keyCode
,
771 modifierMask
|LockMask
,
772 _root
, True
, GrabModeAsync
, GrabModeAsync
);
773 XGrabKey(display
, keyCode
,
774 modifierMask
|scrolllockMask
,
775 _root
, True
, GrabModeAsync
, GrabModeAsync
);
776 XGrabKey(display
, keyCode
,
777 modifierMask
|numlockMask
,
778 _root
, True
, GrabModeAsync
, GrabModeAsync
);
780 XGrabKey(display
, keyCode
,
781 modifierMask
|LockMask
|scrolllockMask
,
782 _root
, True
, GrabModeAsync
, GrabModeAsync
);
783 XGrabKey(display
, keyCode
,
784 modifierMask
|scrolllockMask
|numlockMask
,
785 _root
, True
, GrabModeAsync
, GrabModeAsync
);
786 XGrabKey(display
, keyCode
,
787 modifierMask
|numlockMask
|LockMask
,
788 _root
, True
, GrabModeAsync
, GrabModeAsync
);
790 XGrabKey(display
, keyCode
,
791 modifierMask
|numlockMask
|LockMask
|scrolllockMask
,
792 _root
, True
, GrabModeAsync
, GrabModeAsync
);
795 void screen::ungrabKey(const KeyCode keyCode
, const int modifierMask
) const {
797 Display
*display
= _epist
->getXDisplay();
798 int numlockMask
, scrolllockMask
;
800 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
802 XUngrabKey(display
, keyCode
, modifierMask
, _root
);
803 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
, _root
);
804 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
, _root
);
805 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
, _root
);
806 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
|scrolllockMask
, _root
);
807 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
|numlockMask
, _root
);
808 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
, _root
);
809 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
|
810 scrolllockMask
, _root
);
814 void screen::grabModifiers() const {
815 Display
*display
= _epist
->getXDisplay();
817 XGrabKeyboard(display
, rootWindow(), True
, GrabModeAsync
,
818 GrabModeAsync
, CurrentTime
);
822 void screen::ungrabModifiers() const {
823 Display
*display
= _epist
->getXDisplay();
825 XUngrabKeyboard(display
, CurrentTime
);
829 bool screen::nothingIsPressed(void) const
832 XQueryKeymap(_epist
->getXDisplay(), keys
);
834 for (int i
= 0; i
< 32; ++i
) {