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.
24 # include "../../config.h"
25 #endif // HAVE_CONFIG_H
30 #endif // HAVE_STDIO_H
33 # include <sys/types.h>
35 #endif // HAVE_UNISTD_H
47 #include "../../src/BaseDisplay.hh"
48 #include "../../src/XAtom.hh"
53 screen::screen(epist
*epist
, int number
)
54 : _clients(epist
->clientsList()),
55 _active(epist
->activeWindow()) {
57 _xatom
= _epist
->xatom();
58 _last_active
= _clients
.end();
60 _info
= _epist
->getScreenInfo(_number
);
61 _root
= _info
->getRootWindow();
64 // find a window manager supporting NETWM, waiting for it to load if we must
65 int count
= 20; // try for 20 seconds
67 while (! (_epist
->doShutdown() || _managed
|| count
<= 0)) {
68 if (! (_managed
= findSupportingWM()))
73 cout
<< "Found compatible window manager '" << _wm_name
<< "' for screen "
76 cout
<< "Unable to find a compatible window manager for screen " <<
81 XSelectInput(_epist
->getXDisplay(), _root
, PropertyChangeMask
);
86 XSelectInput(_epist
->getXDisplay(), _root
, None
);
90 bool screen::findSupportingWM() {
92 if (! _xatom
->getValue(_root
, XAtom::net_supporting_wm_check
, XAtom::window
,
93 support_win
) || support_win
== None
)
97 _xatom
->getValue(support_win
, XAtom::net_wm_name
, XAtom::utf8
, title
);
103 XWindow
*screen::findWindow(const XEvent
&e
) const {
106 WindowList::const_iterator it
, end
= _clients
.end();
107 for (it
= _clients
.begin(); it
!= end
; ++it
)
108 if (**it
== e
.xany
.window
)
116 void screen::processEvent(const XEvent
&e
) {
118 assert(e
.xany
.window
== _root
);
123 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_number_of_desktops
))
125 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_current_desktop
))
126 updateActiveDesktop();
127 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_active_window
))
128 updateActiveWindow();
129 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_client_list
)) {
130 // catch any window unmaps first
132 if (XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
133 DestroyNotify
, &ev
) ||
134 XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
148 void screen::handleKeypress(const XEvent
&e
) {
149 int scrolllockMask
, numlockMask
;
150 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
152 // Mask out the lock modifiers. We want our keys to always work
153 // This should be made an option
154 unsigned int state
= e
.xkey
.state
& ~(LockMask
|scrolllockMask
|numlockMask
);
155 keytree
&ktree
= _epist
->getKeyTree();
156 const Action
*it
= ktree
.getAction(e
, state
, this);
161 switch (it
->type()) {
162 case Action::nextScreen
:
163 _epist
->cycleScreen(_number
, true);
166 case Action::prevScreen
:
167 _epist
->cycleScreen(_number
, false);
170 case Action::nextWorkspace
:
171 cycleWorkspace(true, it
->number() != 0 ? it
->number(): 1);
174 case Action::prevWorkspace
:
175 cycleWorkspace(false, it
->number() != 0 ? it
->number(): 1);
178 case Action::nextWindow
:
180 cycleWindow(true, it
->number() != 0 ? it
->number(): 1);
183 case Action::prevWindow
:
184 cycleWindow(false, it
->number() != 0 ? it
->number(): 1);
187 case Action::nextWindowOnAllWorkspaces
:
188 cycleWindow(true, it
->number() != 0 ? it
->number(): 1, false, true);
191 case Action::prevWindowOnAllWorkspaces
:
192 cycleWindow(false, it
->number() != 0 ? it
->number(): 1, false, true);
195 case Action::nextWindowOnAllScreens
:
196 cycleWindow(true, it
->number() != 0 ? it
->number(): 1, true);
199 case Action::prevWindowOnAllScreens
:
200 cycleWindow(false, it
->number() != 0 ? it
->number(): 1, true);
203 case Action::nextWindowOfClass
:
204 cycleWindow(true, it
->number() != 0 ? it
->number(): 1,
205 false, false, true, it
->string());
208 case Action::prevWindowOfClass
:
209 cycleWindow(false, it
->number() != 0 ? it
->number(): 1,
210 false, false, true, it
->string());
213 case Action::nextWindowOfClassOnAllWorkspaces
:
214 cycleWindow(true, it
->number() != 0 ? it
->number(): 1,
215 false, true, true, it
->string());
218 case Action::prevWindowOfClassOnAllWorkspaces
:
219 cycleWindow(false, it
->number() != 0 ? it
->number(): 1,
220 false, true, true, it
->string());
223 case Action::changeWorkspace
:
224 changeWorkspace(it
->number());
227 case Action::upWorkspace
:
228 changeWorkspaceVert(-1);
231 case Action::downWorkspace
:
232 changeWorkspaceVert(1);
235 case Action::leftWorkspace
:
236 changeWorkspaceHorz(-1);
239 case Action::rightWorkspace
:
240 changeWorkspaceHorz(1);
243 case Action::execute
:
244 execCommand(it
->string());
247 case Action::showRootMenu
:
248 _xatom
->sendClientMessage(rootWindow(), XAtom::openbox_show_root_menu
,
252 case Action::showWorkspaceMenu
:
253 _xatom
->sendClientMessage(rootWindow(), XAtom::openbox_show_workspace_menu
,
257 case Action::toggleGrabs
: {
259 ktree
.ungrabDefaults(this);
262 ktree
.grabDefaults(this);
272 // these actions require an active window
273 if (_active
!= _clients
.end()) {
274 XWindow
*window
= *_active
;
276 switch (it
->type()) {
277 case Action::iconify
:
293 case Action::sendToWorkspace
:
294 window
->sendTo(it
->number());
297 case Action::toggleomnipresent
:
298 if (window
->desktop() == 0xffffffff)
299 window
->sendTo(_active_desktop
);
301 window
->sendTo(0xffffffff);
304 case Action::moveWindowUp
:
305 window
->move(window
->x(), window
->y() -
306 (it
->number() != 0 ? it
->number(): 1));
309 case Action::moveWindowDown
:
310 window
->move(window
->x(), window
->y() +
311 (it
->number() != 0 ? it
->number(): 1));
314 case Action::moveWindowLeft
:
315 window
->move(window
->x() - (it
->number() != 0 ? it
->number(): 1),
319 case Action::moveWindowRight
:
320 window
->move(window
->x() + (it
->number() != 0 ? it
->number(): 1),
324 case Action::resizeWindowWidth
:
325 window
->resizeRel(it
->number(), 0);
328 case Action::resizeWindowHeight
:
329 window
->resizeRel(0, it
->number());
332 case Action::toggleshade
:
333 window
->shade(! window
->shaded());
336 case Action::toggleMaximizeHorizontal
:
337 window
->toggleMaximize(XWindow::Max_Horz
);
340 case Action::toggleMaximizeVertical
:
341 window
->toggleMaximize(XWindow::Max_Vert
);
344 case Action::toggleMaximizeFull
:
345 window
->toggleMaximize(XWindow::Max_Full
);
348 case Action::toggleDecorations
:
349 window
->decorate(! window
->decorated());
353 assert(false); // unhandled action type!
359 // do we want to add this window to our list?
360 bool screen::doAddWindow(Window window
) const {
364 if (! _xatom
->getValue(window
, XAtom::net_wm_window_type
, XAtom::atom
,
368 if (type
== _xatom
->getAtom(XAtom::net_wm_window_type_dock
) ||
369 type
== _xatom
->getAtom(XAtom::net_wm_window_type_menu
))
376 void screen::updateEverything() {
378 updateActiveDesktop();
380 updateActiveWindow();
384 void screen::updateNumDesktops() {
387 if (! _xatom
->getValue(_root
, XAtom::net_number_of_desktops
, XAtom::cardinal
,
388 (unsigned long)_num_desktops
))
389 _num_desktops
= 1; // assume that there is at least 1 desktop!
393 void screen::updateActiveDesktop() {
396 if (! _xatom
->getValue(_root
, XAtom::net_current_desktop
, XAtom::cardinal
,
397 (unsigned long)_active_desktop
))
398 _active_desktop
= 0; // there must be at least one desktop, and it must
399 // be the current one
403 void screen::updateClientList() {
406 WindowList::iterator insert_point
= _active
;
407 if (insert_point
!= _clients
.end())
408 ++insert_point
; // get to the item client the focused client
410 // get the client list from the root window
411 Window
*rootclients
= 0;
412 unsigned long num
= (unsigned) -1;
413 if (! _xatom
->getValue(_root
, XAtom::net_client_list
, XAtom::window
, num
,
417 WindowList::iterator it
;
418 const WindowList::iterator end
= _clients
.end();
421 // insert new clients after the active window
422 for (i
= 0; i
< num
; ++i
) {
423 for (it
= _clients
.begin(); it
!= end
; ++it
)
424 if (**it
== rootclients
[i
])
426 if (it
== end
) { // didn't already exist
427 if (doAddWindow(rootclients
[i
])) {
428 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
429 _clients
.insert(insert_point
, new XWindow(_epist
, this,
435 // remove clients that no longer exist (that belong to this screen)
436 for (it
= _clients
.begin(); it
!= end
;) {
437 WindowList::iterator it2
= it
;
440 // is on another screen?
441 if ((*it2
)->getScreen() != this)
444 for (i
= 0; i
< num
; ++i
)
445 if (**it2
== rootclients
[i
])
447 if (i
== num
) { // no longer exists
448 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
449 // watch for the active and last-active window
451 _active
= _clients
.end();
452 if (it2
== _last_active
)
453 _last_active
= _clients
.end();
459 if (rootclients
) delete [] rootclients
;
463 const XWindow
*screen::lastActiveWindow() const {
464 if (_last_active
!= _clients
.end())
465 return *_last_active
;
467 // find a window if one exists
468 WindowList::const_iterator it
, end
= _clients
.end();
469 for (it
= _clients
.begin(); it
!= end
; ++it
)
470 if ((*it
)->getScreen() == this && ! (*it
)->iconic() &&
471 ((*it
)->desktop() == 0xffffffff || (*it
)->desktop() == _active_desktop
))
474 // no windows on this screen
479 void screen::updateActiveWindow() {
483 _xatom
->getValue(_root
, XAtom::net_active_window
, XAtom::window
, a
);
485 WindowList::iterator it
, end
= _clients
.end();
486 for (it
= _clients
.begin(); it
!= end
; ++it
) {
488 if ((*it
)->getScreen() != this)
497 /* cout << "Active window is now: ";
498 if (_active == _clients.end()) cout << "None\n";
499 else cout << "0x" << hex << (*_active)->window() << dec << endl;
504 void screen::execCommand(const string
&cmd
) const {
506 if ((pid
= fork()) == 0) {
507 // make the command run on the correct screen
508 if (putenv(const_cast<char*>(_info
->displayString().c_str()))) {
509 cout
<< "warning: couldn't set environment variable 'DISPLAY'\n";
512 execl("/bin/sh", "sh", "-c", cmd
.c_str(), NULL
);
514 } else if (pid
== -1) {
515 cout
<< _epist
->getApplicationName() <<
516 ": Could not fork a process for executing a command\n";
521 void screen::cycleWindow(const bool forward
, const int increment
,
522 const bool allscreens
, const bool alldesktops
,
523 const bool sameclass
, const string
&cn
) const {
525 assert(increment
> 0);
527 if (_clients
.empty()) return;
529 string
classname(cn
);
530 if (sameclass
&& classname
.empty() && _active
!= _clients
.end())
531 classname
= (*_active
)->appClass();
533 WindowList::const_iterator target
= _active
,
534 begin
= _clients
.begin(),
535 end
= _clients
.end();
537 const XWindow
*t
= 0;
539 for (int x
= 0; x
< increment
; ++x
) {
553 // must be no window to focus
554 if (target
== _active
)
557 // start back at the beginning of the loop
561 // determine if this window is invalid for cycling to
563 if (t
->iconic()) continue;
564 if (! allscreens
&& t
->getScreen() != this) continue;
565 if (! alldesktops
&& ! (t
->desktop() == _active_desktop
||
566 t
->desktop() == 0xffffffff)) continue;
567 if (sameclass
&& ! classname
.empty() &&
568 t
->appClass() != classname
) continue;
569 if (! t
->canFocus()) continue;
571 // found a good window so break out of the while, and perhaps continue
577 // phew. we found the window, so focus it.
582 void screen::cycleWorkspace(const bool forward
, const int increment
,
583 const bool loop
) const {
585 assert(increment
> 0);
587 unsigned int destination
= _active_desktop
;
589 for (int x
= 0; x
< increment
; ++x
) {
591 if (destination
< _num_desktops
- 1)
599 destination
= _num_desktops
- 1;
603 if (destination
!= _active_desktop
)
604 changeWorkspace(destination
);
608 void screen::changeWorkspace(const int num
) const {
611 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);
614 void screen::changeWorkspaceVert(const int num
) const {
616 const Config
*conf
= _epist
->getConfig();
617 int width
= conf
->getNumberValue(Config::workspaceColumns
);
618 int num_desktops
= (signed)_num_desktops
;
619 int active_desktop
= (signed)_active_desktop
;
622 if (width
> num_desktops
|| width
<= 0)
625 // a cookie to the person that makes this pretty
627 wnum
= active_desktop
- width
;
629 wnum
= num_desktops
/width
* width
+ active_desktop
;
630 if (wnum
>= num_desktops
)
631 wnum
= num_desktops
- 1;
635 wnum
= active_desktop
+ width
;
636 if (wnum
>= num_desktops
) {
637 wnum
= (active_desktop
+ width
) % num_desktops
- 1;
642 changeWorkspace(wnum
);
645 void screen::changeWorkspaceHorz(const int num
) const {
647 const Config
*conf
= _epist
->getConfig();
648 int width
= conf
->getNumberValue(Config::workspaceColumns
);
649 int num_desktops
= (signed)_num_desktops
;
650 int active_desktop
= (signed)_active_desktop
;
653 if (width
> num_desktops
|| width
<= 0)
657 if (active_desktop
% width
!= 0)
658 changeWorkspace(active_desktop
- 1);
660 wnum
= active_desktop
+ width
- 1;
661 if (wnum
>= num_desktops
)
662 wnum
= num_desktops
- 1;
666 if (active_desktop
% width
!= width
- 1) {
667 wnum
= active_desktop
+ 1;
668 if (wnum
>= num_desktops
)
669 wnum
= num_desktops
/ width
* width
;
672 wnum
= active_desktop
- width
+ 1;
674 changeWorkspace(wnum
);
677 void screen::grabKey(const KeyCode keyCode
, const int modifierMask
) const {
679 Display
*display
= _epist
->getXDisplay();
680 int numlockMask
, scrolllockMask
;
682 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
684 XGrabKey(display
, keyCode
, modifierMask
,
685 _root
, True
, GrabModeAsync
, GrabModeAsync
);
686 XGrabKey(display
, keyCode
,
687 modifierMask
|LockMask
,
688 _root
, True
, GrabModeAsync
, GrabModeAsync
);
689 XGrabKey(display
, keyCode
,
690 modifierMask
|scrolllockMask
,
691 _root
, True
, GrabModeAsync
, GrabModeAsync
);
692 XGrabKey(display
, keyCode
,
693 modifierMask
|numlockMask
,
694 _root
, True
, GrabModeAsync
, GrabModeAsync
);
696 XGrabKey(display
, keyCode
,
697 modifierMask
|LockMask
|scrolllockMask
,
698 _root
, True
, GrabModeAsync
, GrabModeAsync
);
699 XGrabKey(display
, keyCode
,
700 modifierMask
|scrolllockMask
|numlockMask
,
701 _root
, True
, GrabModeAsync
, GrabModeAsync
);
702 XGrabKey(display
, keyCode
,
703 modifierMask
|numlockMask
|LockMask
,
704 _root
, True
, GrabModeAsync
, GrabModeAsync
);
706 XGrabKey(display
, keyCode
,
707 modifierMask
|numlockMask
|LockMask
|scrolllockMask
,
708 _root
, True
, GrabModeAsync
, GrabModeAsync
);
711 void screen::ungrabKey(const KeyCode keyCode
, const int modifierMask
) const {
713 Display
*display
= _epist
->getXDisplay();
714 int numlockMask
, scrolllockMask
;
716 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
718 XUngrabKey(display
, keyCode
, modifierMask
, _root
);
719 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
, _root
);
720 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
, _root
);
721 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
, _root
);
722 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
|scrolllockMask
, _root
);
723 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
|numlockMask
, _root
);
724 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
, _root
);
725 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
|
726 scrolllockMask
, _root
);