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
37 #include <X11/keysym.h>
49 #include "../../src/BaseDisplay.hh"
50 #include "../../src/XAtom.hh"
55 screen::screen(epist *epist, int number)
56 : _clients(epist->clientsList()), _active(epist->activeWindow()),
57 _config(epist->getConfig()), _grabbed(true), _cycling(false),
58 _stacked_cycling(false)
61 _xatom = _epist->xatom();
62 _last_active = _clients.end();
64 _info = _epist->getScreenInfo(_number);
65 _root = _info->getRootWindow();
67 _config->getBoolValue(Config::stackedCycling, _stacked_cycling);
69 // find a window manager supporting NETWM, waiting for it to load if we must
70 int count = 20; // try for 20 seconds
72 while (! (_epist->doShutdown() || _managed || count <= 0)) {
73 if (! (_managed = findSupportingWM()))
78 cout << "Found compatible window manager '" << _wm_name << "' for screen "
81 cout << "Unable to find a compatible window manager for screen " <<
86 XSelectInput(_epist->getXDisplay(), _root, PropertyChangeMask);
91 XSelectInput(_epist->getXDisplay(), _root, None);
95 bool screen::findSupportingWM() {
97 if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
98 support_win) || support_win == None)
102 _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
108 XWindow *screen::findWindow(const XEvent &e) const {
111 WindowList::const_iterator it, end = _clients.end();
112 for (it = _clients.begin(); it != end; ++it)
113 if (**it == e.xany.window)
121 void screen::processEvent(const XEvent &e) {
123 assert(e.xany.window == _root);
128 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_number_of_desktops))
130 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_current_desktop))
131 updateActiveDesktop();
132 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
133 updateActiveWindow();
134 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list)) {
135 // catch any window unmaps first
137 if (XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
138 DestroyNotify, &ev) ||
139 XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
160 void screen::handleKeypress(const XEvent &e) {
161 int scrolllockMask, numlockMask;
162 _epist->getLockModifiers(numlockMask, scrolllockMask);
164 // Mask out the lock modifiers. We want our keys to always work
165 // This should be made an option
166 unsigned int state = e.xkey.state & ~(LockMask|scrolllockMask|numlockMask);
167 keytree &ktree = _epist->getKeyTree();
168 const Action *it = ktree.getAction(e, state, this);
173 switch (it->type()) {
174 case Action::nextScreen:
175 _epist->cycleScreen(_number, true);
178 case Action::prevScreen:
179 _epist->cycleScreen(_number, false);
182 case Action::nextWorkspace:
183 cycleWorkspace(true, it->number() != 0 ? it->number(): 1);
186 case Action::prevWorkspace:
187 cycleWorkspace(false, it->number() != 0 ? it->number(): 1);
190 case Action::nextWindow:
192 cycleWindow(state, true, it->number() != 0 ? it->number(): 1);
195 case Action::prevWindow:
196 cycleWindow(state, false, it->number() != 0 ? it->number(): 1);
199 case Action::nextWindowOnAllWorkspaces:
200 cycleWindow(state, true, it->number() != 0 ? it->number(): 1, false, true);
203 case Action::prevWindowOnAllWorkspaces:
204 cycleWindow(state, false, it->number() != 0 ? it->number(): 1, false, true);
207 case Action::nextWindowOnAllScreens:
208 cycleWindow(state, true, it->number() != 0 ? it->number(): 1, true);
211 case Action::prevWindowOnAllScreens:
212 cycleWindow(state, false, it->number() != 0 ? it->number(): 1, true);
215 case Action::nextWindowOfClass:
216 cycleWindow(state, true, it->number() != 0 ? it->number(): 1,
217 false, false, true, it->string());
220 case Action::prevWindowOfClass:
221 cycleWindow(state, false, it->number() != 0 ? it->number(): 1,
222 false, false, true, it->string());
225 case Action::nextWindowOfClassOnAllWorkspaces:
226 cycleWindow(state, true, it->number() != 0 ? it->number(): 1,
227 false, true, true, it->string());
230 case Action::prevWindowOfClassOnAllWorkspaces:
231 cycleWindow(state, false, it->number() != 0 ? it->number(): 1,
232 false, true, true, it->string());
235 case Action::changeWorkspace:
236 changeWorkspace(it->number());
239 case Action::upWorkspace:
240 changeWorkspaceVert(-1);
243 case Action::downWorkspace:
244 changeWorkspaceVert(1);
247 case Action::leftWorkspace:
248 changeWorkspaceHorz(-1);
251 case Action::rightWorkspace:
252 changeWorkspaceHorz(1);
255 case Action::execute:
256 execCommand(it->string());
259 case Action::showRootMenu:
260 _xatom->sendClientMessage(rootWindow(), XAtom::openbox_show_root_menu,
264 case Action::showWorkspaceMenu:
265 _xatom->sendClientMessage(rootWindow(), XAtom::openbox_show_workspace_menu,
269 case Action::toggleGrabs: {
271 ktree.ungrabDefaults(this);
274 ktree.grabDefaults(this);
284 // these actions require an active window
285 if (_active != _clients.end()) {
286 XWindow *window = *_active;
288 switch (it->type()) {
289 case Action::iconify:
305 case Action::sendToWorkspace:
306 window->sendTo(it->number());
309 case Action::toggleOmnipresent:
310 if (window->desktop() == 0xffffffff)
311 window->sendTo(_active_desktop);
313 window->sendTo(0xffffffff);
316 case Action::moveWindowUp:
317 window->move(window->x(), window->y() -
318 (it->number() != 0 ? it->number(): 1));
321 case Action::moveWindowDown:
322 window->move(window->x(), window->y() +
323 (it->number() != 0 ? it->number(): 1));
326 case Action::moveWindowLeft:
327 window->move(window->x() - (it->number() != 0 ? it->number(): 1),
331 case Action::moveWindowRight:
332 window->move(window->x() + (it->number() != 0 ? it->number(): 1),
336 case Action::resizeWindowWidth:
337 window->resizeRel(it->number(), 0);
340 case Action::resizeWindowHeight:
341 window->resizeRel(0, it->number());
344 case Action::toggleShade:
345 window->shade(! window->shaded());
348 case Action::toggleMaximizeHorizontal:
349 window->toggleMaximize(XWindow::Max_Horz);
352 case Action::toggleMaximizeVertical:
353 window->toggleMaximize(XWindow::Max_Vert);
356 case Action::toggleMaximizeFull:
357 window->toggleMaximize(XWindow::Max_Full);
360 case Action::toggleDecorations:
361 window->decorate(! window->decorated());
365 assert(false); // unhandled action type!
372 void screen::handleKeyrelease(const XEvent &e) {
373 // we're not interested in non-modifiers
374 if (!isModifier(e.xkey.keycode))
377 // the only keyrelease event we care about (for now) is when we do stacked
378 // cycling and the modifier is released
379 if (_stacked_cycling && _cycling && nothingIsPressed()) {
380 XWindow *w = *_active;
382 // all modifiers have been released. ungrab the keyboard, move the
383 // focused window to the top of the Z-order and raise it
387 _clients.push_front(w);
395 // do we want to add this window to our list?
396 bool screen::doAddWindow(Window window) const {
400 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
404 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
405 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
412 void screen::updateEverything() {
414 updateActiveDesktop();
416 updateActiveWindow();
420 void screen::updateNumDesktops() {
423 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
424 (unsigned long)_num_desktops))
425 _num_desktops = 1; // assume that there is at least 1 desktop!
429 void screen::updateActiveDesktop() {
432 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
433 (unsigned long)_active_desktop))
434 _active_desktop = 0; // there must be at least one desktop, and it must
435 // be the current one
439 void screen::updateClientList() {
442 WindowList::iterator insert_point = _active;
443 if (insert_point != _clients.end())
444 ++insert_point; // get to the item client the focused client
446 // get the client list from the root window
447 Window *rootclients = 0;
448 unsigned long num = (unsigned) -1;
449 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
453 WindowList::iterator it;
454 const WindowList::iterator end = _clients.end();
457 // insert new clients after the active window
458 for (i = 0; i < num; ++i) {
459 for (it = _clients.begin(); it != end; ++it)
460 if (**it == rootclients[i])
462 if (it == end) { // didn't already exist
463 if (doAddWindow(rootclients[i])) {
464 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
465 _clients.insert(insert_point, new XWindow(_epist, this,
471 // remove clients that no longer exist (that belong to this screen)
472 for (it = _clients.begin(); it != end;) {
473 WindowList::iterator it2 = it;
476 // is on another screen?
477 if ((*it2)->getScreen() != this)
480 for (i = 0; i < num; ++i)
481 if (**it2 == rootclients[i])
483 if (i == num) { // no longer exists
484 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
485 // watch for the active and last-active window
487 _active = _clients.end();
488 if (it2 == _last_active)
489 _last_active = _clients.end();
495 if (rootclients) delete [] rootclients;
499 const XWindow *screen::lastActiveWindow() const {
500 if (_last_active != _clients.end())
501 return *_last_active;
503 // find a window if one exists
504 WindowList::const_iterator it, end = _clients.end();
505 for (it = _clients.begin(); it != end; ++it)
506 if ((*it)->getScreen() == this && ! (*it)->iconic() &&
507 ((*it)->desktop() == 0xffffffff || (*it)->desktop() == _active_desktop))
510 // no windows on this screen
515 void screen::updateActiveWindow() {
519 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
521 WindowList::iterator it, end = _clients.end();
522 for (it = _clients.begin(); it != end; ++it) {
524 if ((*it)->getScreen() != this)
533 /* cout << "Active window is now: ";
534 if (_active == _clients.end()) cout << "None\n";
535 else cout << "0x" << hex << (*_active)->window() << dec << endl;
540 void screen::execCommand(const string &cmd) const {
542 if ((pid = fork()) == 0) {
543 // make the command run on the correct screen
544 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
545 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
548 execl("/bin/sh", "sh", "-c", cmd.c_str(), NULL);
550 } else if (pid == -1) {
551 cout << _epist->getApplicationName() <<
552 ": Could not fork a process for executing a command\n";
557 void screen::cycleWindow(unsigned int state, const bool forward,
558 const int increment, const bool allscreens,
559 const bool alldesktops, const bool sameclass,
563 assert(increment > 0);
565 if (_clients.empty()) return;
567 string classname(cn);
568 if (sameclass && classname.empty() && _active != _clients.end())
569 classname = (*_active)->appClass();
571 WindowList::const_iterator target = _active,
572 begin = _clients.begin(),
573 end = _clients.end();
577 for (int x = 0; x < increment; ++x) {
591 // must be no window to focus
592 if (target == _active)
595 // start back at the beginning of the loop
599 // determine if this window is invalid for cycling to
601 if (t->iconic()) continue;
602 if (! allscreens && t->getScreen() != this) continue;
603 if (! alldesktops && ! (t->desktop() == _active_desktop ||
604 t->desktop() == 0xffffffff)) continue;
605 if (sameclass && ! classname.empty() &&
606 t->appClass() != classname) continue;
607 if (! t->canFocus()) continue;
609 // found a good window so break out of the while, and perhaps continue
615 // phew. we found the window, so focus it.
616 if (_stacked_cycling && state) {
618 // grab modifiers so we can intercept KeyReleases from them
623 // if the window is on another desktop, we can't use XSetInputFocus, since
624 // it doesn't imply a woskpace change.
625 if (t->desktop() == _active_desktop)
626 t->focus(false); // focus, but don't raise
628 t->focus(); // change workspace and focus
636 void screen::cycleWorkspace(const bool forward, const int increment,
637 const bool loop) const {
639 assert(increment > 0);
641 unsigned int destination = _active_desktop;
643 for (int x = 0; x < increment; ++x) {
645 if (destination < _num_desktops - 1)
653 destination = _num_desktops - 1;
657 if (destination != _active_desktop)
658 changeWorkspace(destination);
662 void screen::changeWorkspace(const int num) const {
665 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
668 void screen::changeWorkspaceVert(const int num) const {
671 int num_desktops = (signed)_num_desktops;
672 int active_desktop = (signed)_active_desktop;
675 _config->getNumberValue(Config::workspaceColumns, width);
677 if (width > num_desktops || width <= 0)
680 // a cookie to the person that makes this pretty
682 wnum = active_desktop - width;
684 wnum = num_desktops/width * width + active_desktop;
685 if (wnum >= num_desktops)
686 wnum = num_desktops - 1;
690 wnum = active_desktop + width;
691 if (wnum >= num_desktops) {
692 wnum = (active_desktop + width) % num_desktops - 1;
697 changeWorkspace(wnum);
700 void screen::changeWorkspaceHorz(const int num) const {
703 int num_desktops = (signed)_num_desktops;
704 int active_desktop = (signed)_active_desktop;
707 _config->getNumberValue(Config::workspaceColumns, width);
709 if (width > num_desktops || width <= 0)
713 if (active_desktop % width != 0)
714 changeWorkspace(active_desktop - 1);
716 wnum = active_desktop + width - 1;
717 if (wnum >= num_desktops)
718 wnum = num_desktops - 1;
722 if (active_desktop % width != width - 1) {
723 wnum = active_desktop + 1;
724 if (wnum >= num_desktops)
725 wnum = num_desktops / width * width;
728 wnum = active_desktop - width + 1;
730 changeWorkspace(wnum);
733 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
735 Display *display = _epist->getXDisplay();
736 int numlockMask, scrolllockMask;
738 _epist->getLockModifiers(numlockMask, scrolllockMask);
740 XGrabKey(display, keyCode, modifierMask,
741 _root, True, GrabModeAsync, GrabModeAsync);
742 XGrabKey(display, keyCode,
743 modifierMask|LockMask,
744 _root, True, GrabModeAsync, GrabModeAsync);
745 XGrabKey(display, keyCode,
746 modifierMask|scrolllockMask,
747 _root, True, GrabModeAsync, GrabModeAsync);
748 XGrabKey(display, keyCode,
749 modifierMask|numlockMask,
750 _root, True, GrabModeAsync, GrabModeAsync);
752 XGrabKey(display, keyCode,
753 modifierMask|LockMask|scrolllockMask,
754 _root, True, GrabModeAsync, GrabModeAsync);
755 XGrabKey(display, keyCode,
756 modifierMask|scrolllockMask|numlockMask,
757 _root, True, GrabModeAsync, GrabModeAsync);
758 XGrabKey(display, keyCode,
759 modifierMask|numlockMask|LockMask,
760 _root, True, GrabModeAsync, GrabModeAsync);
762 XGrabKey(display, keyCode,
763 modifierMask|numlockMask|LockMask|scrolllockMask,
764 _root, True, GrabModeAsync, GrabModeAsync);
767 void screen::ungrabKey(const KeyCode keyCode, const int modifierMask) const {
769 Display *display = _epist->getXDisplay();
770 int numlockMask, scrolllockMask;
772 _epist->getLockModifiers(numlockMask, scrolllockMask);
774 XUngrabKey(display, keyCode, modifierMask, _root);
775 XUngrabKey(display, keyCode, modifierMask|LockMask, _root);
776 XUngrabKey(display, keyCode, modifierMask|scrolllockMask, _root);
777 XUngrabKey(display, keyCode, modifierMask|numlockMask, _root);
778 XUngrabKey(display, keyCode, modifierMask|LockMask|scrolllockMask, _root);
779 XUngrabKey(display, keyCode, modifierMask|scrolllockMask|numlockMask, _root);
780 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask, _root);
781 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask|
782 scrolllockMask, _root);
786 void screen::grabModifiers() const {
787 Display *display = _epist->getXDisplay();
789 XGrabKeyboard(display, rootWindow(), True, GrabModeAsync,
790 GrabModeAsync, CurrentTime);
794 void screen::ungrabModifiers() const {
795 Display *display = _epist->getXDisplay();
797 XUngrabKeyboard(display, CurrentTime);
801 bool screen::isModifier(const KeyCode kc) const {
802 KeySym ks = XKeycodeToKeysym(_epist->getXDisplay(), kc, 0);
804 if (ks == XK_Shift_L || ks == XK_Shift_R ||
805 ks == XK_Control_L || ks == XK_Control_R ||
806 ks == XK_Meta_L || ks == XK_Meta_R ||
807 ks == XK_Alt_L || ks == XK_Alt_R ||
808 ks == XK_Super_L || ks == XK_Super_R ||
809 ks == XK_Hyper_L || ks == XK_Hyper_R)
816 bool screen::nothingIsPressed(void) const
819 XQueryKeymap(_epist->getXDisplay(), keys);
821 for (int i = 0; i < 32; ++i) {