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();
63 // find a window manager supporting NETWM, waiting for it to load if we must
64 int count
= 20; // try for 20 seconds
66 while (! (_epist
->doShutdown() || _managed
|| count
<= 0)) {
67 if (! (_managed
= findSupportingWM()))
72 cout
<< "Found compatible window manager '" << _wm_name
<< "' for screen "
75 cout
<< "Unable to find a compatible window manager for screen " <<
80 XSelectInput(_epist
->getXDisplay(), _root
, PropertyChangeMask
);
85 XSelectInput(_epist
->getXDisplay(), _root
, None
);
89 bool screen::findSupportingWM() {
91 if (! _xatom
->getValue(_root
, XAtom::net_supporting_wm_check
, XAtom::window
,
92 support_win
) || support_win
== None
)
96 _xatom
->getValue(support_win
, XAtom::net_wm_name
, XAtom::utf8
, title
);
102 XWindow
*screen::findWindow(const XEvent
&e
) const {
105 WindowList::const_iterator it
, end
= _clients
.end();
106 for (it
= _clients
.begin(); it
!= end
; ++it
)
107 if (**it
== e
.xany
.window
)
115 void screen::processEvent(const XEvent
&e
) {
117 assert(e
.xany
.window
== _root
);
122 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_number_of_desktops
))
124 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_current_desktop
))
125 updateActiveDesktop();
126 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_active_window
))
127 updateActiveWindow();
128 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_client_list
)) {
129 // catch any window unmaps first
131 if (XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
132 DestroyNotify
, &ev
) ||
133 XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
147 void screen::handleKeypress(const XEvent
&e
) {
148 int scrolllockMask
, numlockMask
;
149 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
151 // Mask out the lock modifiers. We want our keys to always work
152 // This should be made an option
153 unsigned int state
= e
.xkey
.state
& ~(LockMask
|scrolllockMask
|numlockMask
);
154 const Action
*it
= _epist
->getKeyTree().getAction(e
, state
, this);
159 switch (it
->type()) {
160 case Action::nextScreen
:
161 _epist
->cycleScreen(_number
, true);
164 case Action::prevScreen
:
165 _epist
->cycleScreen(_number
, false);
168 case Action::nextWorkspace
:
169 cycleWorkspace(true, it
->number() != 0 ? it
->number(): 1);
172 case Action::prevWorkspace
:
173 cycleWorkspace(false, it
->number() != 0 ? it
->number(): 1);
176 case Action::nextWindow
:
178 cycleWindow(true, it
->number() != 0 ? it
->number(): 1);
181 case Action::prevWindow
:
182 cycleWindow(false, it
->number() != 0 ? it
->number(): 1);
185 case Action::nextWindowOnAllWorkspaces
:
186 cycleWindow(true, it
->number() != 0 ? it
->number(): 1, false, true);
189 case Action::prevWindowOnAllWorkspaces
:
190 cycleWindow(false, it
->number() != 0 ? it
->number(): 1, false, true);
193 case Action::nextWindowOnAllScreens
:
194 cycleWindow(true, it
->number() != 0 ? it
->number(): 1, true);
197 case Action::prevWindowOnAllScreens
:
198 cycleWindow(false, it
->number() != 0 ? it
->number(): 1, true);
201 case Action::nextWindowOfClass
:
202 cycleWindow(true, it
->number() != 0 ? it
->number(): 1,
203 false, false, true, it
->string());
206 case Action::prevWindowOfClass
:
207 cycleWindow(false, it
->number() != 0 ? it
->number(): 1,
208 false, false, true, it
->string());
211 case Action::nextWindowOfClassOnAllWorkspaces
:
212 cycleWindow(true, it
->number() != 0 ? it
->number(): 1,
213 false, true, true, it
->string());
216 case Action::prevWindowOfClassOnAllWorkspaces
:
217 cycleWindow(false, it
->number() != 0 ? it
->number(): 1,
218 false, true, true, it
->string());
221 case Action::changeWorkspace
:
222 changeWorkspace(it
->number());
225 case Action::upWorkspace
:
226 changeWorkspaceVert(-1);
229 case Action::downWorkspace
:
230 changeWorkspaceVert(1);
233 case Action::leftWorkspace
:
234 changeWorkspaceHorz(-1);
237 case Action::rightWorkspace
:
238 changeWorkspaceHorz(1);
241 case Action::execute
:
242 execCommand(it
->string());
249 // these actions require an active window
250 if (_active
!= _clients
.end()) {
251 XWindow
*window
= *_active
;
253 switch (it
->type()) {
254 case Action::iconify
:
270 case Action::sendToWorkspace
:
271 window
->sendTo(it
->number());
274 case Action::toggleomnipresent
:
275 if (window
->desktop() == 0xffffffff)
276 window
->sendTo(_active_desktop
);
278 window
->sendTo(0xffffffff);
281 case Action::moveWindowUp
:
282 window
->move(window
->x(), window
->y() -
283 (it
->number() != 0 ? it
->number(): 1));
286 case Action::moveWindowDown
:
287 window
->move(window
->x(), window
->y() +
288 (it
->number() != 0 ? it
->number(): 1));
291 case Action::moveWindowLeft
:
292 window
->move(window
->x() - (it
->number() != 0 ? it
->number(): 1),
296 case Action::moveWindowRight
:
297 window
->move(window
->x() + (it
->number() != 0 ? it
->number(): 1),
301 case Action::resizeWindowWidth
:
302 window
->resizeRel(it
->number(), 0);
305 case Action::resizeWindowHeight
:
306 window
->resizeRel(0, it
->number());
309 case Action::toggleshade
:
310 window
->shade(! window
->shaded());
313 case Action::toggleMaximizeHorizontal
:
314 window
->toggleMaximize(XWindow::Max_Horz
);
317 case Action::toggleMaximizeVertical
:
318 window
->toggleMaximize(XWindow::Max_Vert
);
321 case Action::toggleMaximizeFull
:
322 window
->toggleMaximize(XWindow::Max_Full
);
326 assert(false); // unhandled action type!
332 // do we want to add this window to our list?
333 bool screen::doAddWindow(Window window
) const {
337 if (! _xatom
->getValue(window
, XAtom::net_wm_window_type
, XAtom::atom
,
341 if (type
== _xatom
->getAtom(XAtom::net_wm_window_type_dock
) ||
342 type
== _xatom
->getAtom(XAtom::net_wm_window_type_menu
))
349 void screen::updateEverything() {
351 updateActiveDesktop();
353 updateActiveWindow();
357 void screen::updateNumDesktops() {
360 if (! _xatom
->getValue(_root
, XAtom::net_number_of_desktops
, XAtom::cardinal
,
361 (unsigned long)_num_desktops
))
362 _num_desktops
= 1; // assume that there is at least 1 desktop!
366 void screen::updateActiveDesktop() {
369 if (! _xatom
->getValue(_root
, XAtom::net_current_desktop
, XAtom::cardinal
,
370 (unsigned long)_active_desktop
))
371 _active_desktop
= 0; // there must be at least one desktop, and it must
372 // be the current one
376 void screen::updateClientList() {
379 WindowList::iterator insert_point
= _active
;
380 if (insert_point
!= _clients
.end())
381 ++insert_point
; // get to the item client the focused client
383 // get the client list from the root window
384 Window
*rootclients
= 0;
385 unsigned long num
= (unsigned) -1;
386 if (! _xatom
->getValue(_root
, XAtom::net_client_list
, XAtom::window
, num
,
390 WindowList::iterator it
;
391 const WindowList::iterator end
= _clients
.end();
394 // insert new clients after the active window
395 for (i
= 0; i
< num
; ++i
) {
396 for (it
= _clients
.begin(); it
!= end
; ++it
)
397 if (**it
== rootclients
[i
])
399 if (it
== end
) { // didn't already exist
400 if (doAddWindow(rootclients
[i
])) {
401 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
402 _clients
.insert(insert_point
, new XWindow(_epist
, this,
408 // remove clients that no longer exist (that belong to this screen)
409 for (it
= _clients
.begin(); it
!= end
;) {
410 WindowList::iterator it2
= it
;
413 // is on another screen?
414 if ((*it2
)->getScreen() != this)
417 for (i
= 0; i
< num
; ++i
)
418 if (**it2
== rootclients
[i
])
420 if (i
== num
) { // no longer exists
421 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
422 // watch for the active and last-active window
424 _active
= _clients
.end();
425 if (it2
== _last_active
)
426 _last_active
= _clients
.end();
432 if (rootclients
) delete [] rootclients
;
436 const XWindow
*screen::lastActiveWindow() const {
437 if (_last_active
!= _clients
.end())
438 return *_last_active
;
440 // find a window if one exists
441 WindowList::const_iterator it
, end
= _clients
.end();
442 for (it
= _clients
.begin(); it
!= end
; ++it
)
443 if ((*it
)->getScreen() == this && ! (*it
)->iconic() &&
444 ((*it
)->desktop() == 0xffffffff || (*it
)->desktop() == _active_desktop
))
447 // no windows on this screen
452 void screen::updateActiveWindow() {
456 _xatom
->getValue(_root
, XAtom::net_active_window
, XAtom::window
, a
);
458 WindowList::iterator it
, end
= _clients
.end();
459 for (it
= _clients
.begin(); it
!= end
; ++it
) {
461 if ((*it
)->getScreen() != this)
470 /* cout << "Active window is now: ";
471 if (_active == _clients.end()) cout << "None\n";
472 else cout << "0x" << hex << (*_active)->window() << dec << endl;
477 void screen::execCommand(const string
&cmd
) const {
479 if ((pid
= fork()) == 0) {
480 // make the command run on the correct screen
481 if (putenv(const_cast<char*>(_info
->displayString().c_str()))) {
482 cout
<< "warning: couldn't set environment variable 'DISPLAY'\n";
485 execl("/bin/sh", "sh", "-c", cmd
.c_str(), NULL
);
487 } else if (pid
== -1) {
488 cout
<< _epist
->getApplicationName() <<
489 ": Could not fork a process for executing a command\n";
494 void screen::cycleWindow(const bool forward
, const int increment
,
495 const bool allscreens
, const bool alldesktops
,
496 const bool sameclass
, const string
&cn
) const {
498 assert(increment
> 0);
500 if (_clients
.empty()) return;
502 string
classname(cn
);
503 if (sameclass
&& classname
.empty() && _active
!= _clients
.end())
504 classname
= (*_active
)->appClass();
506 WindowList::const_iterator target
= _active
,
507 begin
= _clients
.begin(),
508 end
= _clients
.end();
510 const XWindow
*t
= 0;
512 for (int x
= 0; x
< increment
; ++x
) {
526 // must be no window to focus
527 if (target
== _active
)
530 // start back at the beginning of the loop
534 // determine if this window is invalid for cycling to
536 if (t
->iconic()) continue;
537 if (! allscreens
&& t
->getScreen() != this) continue;
538 if (! alldesktops
&& ! (t
->desktop() == _active_desktop
||
539 t
->desktop() == 0xffffffff)) continue;
540 if (sameclass
&& ! classname
.empty() &&
541 t
->appClass() != classname
) continue;
542 if (! t
->canFocus()) continue;
544 // found a good window so break out of the while, and perhaps continue
550 // phew. we found the window, so focus it.
555 void screen::cycleWorkspace(const bool forward
, const int increment
,
556 const bool loop
) const {
558 assert(increment
> 0);
560 unsigned int destination
= _active_desktop
;
562 for (int x
= 0; x
< increment
; ++x
) {
564 if (destination
< _num_desktops
- 1)
572 destination
= _num_desktops
- 1;
576 if (destination
!= _active_desktop
)
577 changeWorkspace(destination
);
581 void screen::changeWorkspace(const int num
) const {
584 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);
587 void screen::changeWorkspaceVert(const int num
) const {
589 const Config
*conf
= _epist
->getConfig();
590 int width
= conf
->getNumberValue(Config::workspaceColumns
);
592 if (width
> _num_desktops
|| width
<= 0)
597 // a cookie to the person that makes this pretty
599 wnum
= _active_desktop
- width
;
601 wnum
= _num_desktops
/width
* width
+ _active_desktop
;
602 if (wnum
>= _num_desktops
)
603 wnum
= _num_desktops
- 1;
607 wnum
= _active_desktop
+ width
;
608 if (wnum
>= _num_desktops
) {
609 wnum
= (_active_desktop
+ width
) % _num_desktops
- 1;
614 changeWorkspace(wnum
);
617 void screen::changeWorkspaceHorz(const int num
) const {
619 const Config
*conf
= _epist
->getConfig();
620 int width
= conf
->getNumberValue(Config::workspaceColumns
);
623 if (width
> _num_desktops
|| width
<= 0)
627 if (_active_desktop
% width
!= 0)
628 changeWorkspace(_active_desktop
- 1);
630 wnum
= _active_desktop
+ width
- 1;
631 if (wnum
>= _num_desktops
)
632 wnum
= _num_desktops
- 1;
636 if (_active_desktop
% width
!= width
- 1) {
637 wnum
= _active_desktop
+ 1;
638 if (wnum
>= _num_desktops
)
639 wnum
= _num_desktops
/ width
* width
;
642 wnum
= _active_desktop
- width
+ 1;
644 changeWorkspace(wnum
);
647 void screen::grabKey(const KeyCode keyCode
, const int modifierMask
) const {
649 Display
*display
= _epist
->getXDisplay();
650 int numlockMask
, scrolllockMask
;
652 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
654 XGrabKey(display
, keyCode
, modifierMask
,
655 _root
, True
, GrabModeAsync
, GrabModeAsync
);
656 XGrabKey(display
, keyCode
,
657 modifierMask
|LockMask
,
658 _root
, True
, GrabModeAsync
, GrabModeAsync
);
659 XGrabKey(display
, keyCode
,
660 modifierMask
|scrolllockMask
,
661 _root
, True
, GrabModeAsync
, GrabModeAsync
);
662 XGrabKey(display
, keyCode
,
663 modifierMask
|numlockMask
,
664 _root
, True
, GrabModeAsync
, GrabModeAsync
);
666 XGrabKey(display
, keyCode
,
667 modifierMask
|LockMask
|scrolllockMask
,
668 _root
, True
, GrabModeAsync
, GrabModeAsync
);
669 XGrabKey(display
, keyCode
,
670 modifierMask
|scrolllockMask
|numlockMask
,
671 _root
, True
, GrabModeAsync
, GrabModeAsync
);
672 XGrabKey(display
, keyCode
,
673 modifierMask
|numlockMask
|LockMask
,
674 _root
, True
, GrabModeAsync
, GrabModeAsync
);
676 XGrabKey(display
, keyCode
,
677 modifierMask
|numlockMask
|LockMask
|scrolllockMask
,
678 _root
, True
, GrabModeAsync
, GrabModeAsync
);
681 void screen::ungrabKey(const KeyCode keyCode
, const int modifierMask
) const {
683 Display
*display
= _epist
->getXDisplay();
684 int numlockMask
, scrolllockMask
;
686 _epist
->getLockModifiers(numlockMask
, scrolllockMask
);
688 XUngrabKey(display
, keyCode
, modifierMask
, _root
);
689 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
, _root
);
690 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
, _root
);
691 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
, _root
);
692 XUngrabKey(display
, keyCode
, modifierMask
|LockMask
|scrolllockMask
, _root
);
693 XUngrabKey(display
, keyCode
, modifierMask
|scrolllockMask
|numlockMask
, _root
);
694 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
, _root
);
695 XUngrabKey(display
, keyCode
, modifierMask
|numlockMask
|LockMask
|
696 scrolllockMask
, _root
);