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->getValue(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 &) {
373 // the only keyrelease event we care about (for now) is when we do stacked
374 // cycling and the modifier is released
375 if (_stacked_cycling && _cycling && nothingIsPressed()) {
376 XWindow *w = *_active;
378 // all modifiers have been released. ungrab the keyboard, move the
379 // focused window to the top of the Z-order and raise it
383 _clients.push_front(w);
391 // do we want to add this window to our list?
392 bool screen::doAddWindow(Window window) const {
396 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
400 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
401 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
408 void screen::updateEverything() {
410 updateActiveDesktop();
412 updateActiveWindow();
416 void screen::updateNumDesktops() {
419 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
420 (unsigned long)_num_desktops))
421 _num_desktops = 1; // assume that there is at least 1 desktop!
425 void screen::updateActiveDesktop() {
428 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
429 (unsigned long)_active_desktop))
430 _active_desktop = 0; // there must be at least one desktop, and it must
431 // be the current one
435 void screen::updateClientList() {
438 WindowList::iterator insert_point = _active;
439 if (insert_point != _clients.end())
440 ++insert_point; // get to the item client the focused client
442 // get the client list from the root window
443 Window *rootclients = 0;
444 unsigned long num = (unsigned) -1;
445 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
449 WindowList::iterator it;
450 const WindowList::iterator end = _clients.end();
453 // insert new clients after the active window
454 for (i = 0; i < num; ++i) {
455 for (it = _clients.begin(); it != end; ++it)
456 if (**it == rootclients[i])
458 if (it == end) { // didn't already exist
459 if (doAddWindow(rootclients[i])) {
460 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
461 _clients.insert(insert_point, new XWindow(_epist, this,
467 // remove clients that no longer exist (that belong to this screen)
468 for (it = _clients.begin(); it != end;) {
469 WindowList::iterator it2 = it;
472 // is on another screen?
473 if ((*it2)->getScreen() != this)
476 for (i = 0; i < num; ++i)
477 if (**it2 == rootclients[i])
479 if (i == num) { // no longer exists
480 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
481 // watch for the active and last-active window
483 _active = _clients.end();
484 if (it2 == _last_active)
485 _last_active = _clients.end();
491 if (rootclients) delete [] rootclients;
495 const XWindow *screen::lastActiveWindow() const {
496 if (_last_active != _clients.end())
497 return *_last_active;
499 // find a window if one exists
500 WindowList::const_iterator it, end = _clients.end();
501 for (it = _clients.begin(); it != end; ++it)
502 if ((*it)->getScreen() == this && ! (*it)->iconic() &&
503 ((*it)->desktop() == 0xffffffff || (*it)->desktop() == _active_desktop))
506 // no windows on this screen
511 void screen::updateActiveWindow() {
515 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
517 WindowList::iterator it, end = _clients.end();
518 for (it = _clients.begin(); it != end; ++it) {
520 if ((*it)->getScreen() != this)
529 /* cout << "Active window is now: ";
530 if (_active == _clients.end()) cout << "None\n";
531 else cout << "0x" << hex << (*_active)->window() << dec << endl;
536 void screen::execCommand(const string &cmd) const {
538 if ((pid = fork()) == 0) {
539 // make the command run on the correct screen
540 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
541 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
544 execl("/bin/sh", "sh", "-c", cmd.c_str(), NULL);
546 } else if (pid == -1) {
547 cout << _epist->getApplicationName() <<
548 ": Could not fork a process for executing a command\n";
553 void screen::cycleWindow(unsigned int state, const bool forward,
554 const int increment, const bool allscreens,
555 const bool alldesktops, const bool sameclass,
559 assert(increment > 0);
561 if (_clients.empty()) return;
563 string classname(cn);
564 if (sameclass && classname.empty() && _active != _clients.end())
565 classname = (*_active)->appClass();
567 WindowList::const_iterator target = _active,
568 begin = _clients.begin(),
569 end = _clients.end();
573 for (int x = 0; x < increment; ++x) {
587 // must be no window to focus
588 if (target == _active)
591 // start back at the beginning of the loop
595 // determine if this window is invalid for cycling to
597 if (t->iconic()) continue;
598 if (! allscreens && t->getScreen() != this) continue;
599 if (! alldesktops && ! (t->desktop() == _active_desktop ||
600 t->desktop() == 0xffffffff)) continue;
601 if (sameclass && ! classname.empty() &&
602 t->appClass() != classname) continue;
603 if (! t->canFocus()) continue;
605 // found a good window so break out of the while, and perhaps continue
611 // phew. we found the window, so focus it.
612 if (_stacked_cycling && state) {
614 // grab modifiers so we can intercept KeyReleases from them
619 // if the window is on another desktop, we can't use XSetInputFocus, since
620 // it doesn't imply a woskpace change.
621 if (t->desktop() == _active_desktop)
622 t->focus(false); // focus, but don't raise
624 t->focus(); // change workspace and focus
632 void screen::cycleWorkspace(const bool forward, const int increment,
633 const bool loop) const {
635 assert(increment > 0);
637 unsigned int destination = _active_desktop;
639 for (int x = 0; x < increment; ++x) {
641 if (destination < _num_desktops - 1)
649 destination = _num_desktops - 1;
653 if (destination != _active_desktop)
654 changeWorkspace(destination);
658 void screen::changeWorkspace(const int num) const {
661 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
664 void screen::changeWorkspaceVert(const int num) const {
667 int num_desktops = (signed)_num_desktops;
668 int active_desktop = (signed)_active_desktop;
671 _config->getValue(Config::workspaceColumns, width);
673 if (width > num_desktops || width <= 0)
676 // a cookie to the person that makes this pretty
678 wnum = active_desktop - width;
680 wnum = num_desktops/width * width + active_desktop;
681 if (wnum >= num_desktops)
682 wnum = num_desktops - 1;
686 wnum = active_desktop + width;
687 if (wnum >= num_desktops) {
688 wnum = (active_desktop + width) % num_desktops - 1;
693 changeWorkspace(wnum);
696 void screen::changeWorkspaceHorz(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)
709 if (active_desktop % width != 0)
710 changeWorkspace(active_desktop - 1);
712 wnum = active_desktop + width - 1;
713 if (wnum >= num_desktops)
714 wnum = num_desktops - 1;
718 if (active_desktop % width != width - 1) {
719 wnum = active_desktop + 1;
720 if (wnum >= num_desktops)
721 wnum = num_desktops / width * width;
724 wnum = active_desktop - width + 1;
726 changeWorkspace(wnum);
729 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
731 Display *display = _epist->getXDisplay();
732 int numlockMask, scrolllockMask;
734 _epist->getLockModifiers(numlockMask, scrolllockMask);
736 XGrabKey(display, keyCode, modifierMask,
737 _root, True, GrabModeAsync, GrabModeAsync);
738 XGrabKey(display, keyCode,
739 modifierMask|LockMask,
740 _root, True, GrabModeAsync, GrabModeAsync);
741 XGrabKey(display, keyCode,
742 modifierMask|scrolllockMask,
743 _root, True, GrabModeAsync, GrabModeAsync);
744 XGrabKey(display, keyCode,
745 modifierMask|numlockMask,
746 _root, True, GrabModeAsync, GrabModeAsync);
748 XGrabKey(display, keyCode,
749 modifierMask|LockMask|scrolllockMask,
750 _root, True, GrabModeAsync, GrabModeAsync);
751 XGrabKey(display, keyCode,
752 modifierMask|scrolllockMask|numlockMask,
753 _root, True, GrabModeAsync, GrabModeAsync);
754 XGrabKey(display, keyCode,
755 modifierMask|numlockMask|LockMask,
756 _root, True, GrabModeAsync, GrabModeAsync);
758 XGrabKey(display, keyCode,
759 modifierMask|numlockMask|LockMask|scrolllockMask,
760 _root, True, GrabModeAsync, GrabModeAsync);
763 void screen::ungrabKey(const KeyCode keyCode, const int modifierMask) const {
765 Display *display = _epist->getXDisplay();
766 int numlockMask, scrolllockMask;
768 _epist->getLockModifiers(numlockMask, scrolllockMask);
770 XUngrabKey(display, keyCode, modifierMask, _root);
771 XUngrabKey(display, keyCode, modifierMask|LockMask, _root);
772 XUngrabKey(display, keyCode, modifierMask|scrolllockMask, _root);
773 XUngrabKey(display, keyCode, modifierMask|numlockMask, _root);
774 XUngrabKey(display, keyCode, modifierMask|LockMask|scrolllockMask, _root);
775 XUngrabKey(display, keyCode, modifierMask|scrolllockMask|numlockMask, _root);
776 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask, _root);
777 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask|
778 scrolllockMask, _root);
782 void screen::grabModifiers() const {
783 Display *display = _epist->getXDisplay();
785 XGrabKeyboard(display, rootWindow(), True, GrabModeAsync,
786 GrabModeAsync, CurrentTime);
790 void screen::ungrabModifiers() const {
791 Display *display = _epist->getXDisplay();
793 XUngrabKeyboard(display, CurrentTime);
797 bool screen::nothingIsPressed(void) const
800 XQueryKeymap(_epist->getXDisplay(), keys);
802 for (int i = 0; i < 32; ++i) {