1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // screen.cc for Epistophy - 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);
172 case Action::prevWorkspace:
173 cycleWorkspace(false);
176 case Action::nextWindow:
180 case Action::prevWindow:
184 case Action::nextWindowOnAllWorkspaces:
185 cycleWindow(true, false, true);
188 case Action::prevWindowOnAllWorkspaces:
189 cycleWindow(false, false, true);
192 case Action::nextWindowOnAllScreens:
193 cycleWindow(true, true);
196 case Action::prevWindowOnAllScreens:
197 cycleWindow(false, true);
200 case Action::nextWindowOfClass:
201 cycleWindow(true, false, false, true, it->string());
204 case Action::prevWindowOfClass:
205 cycleWindow(false, false, false, true, it->string());
208 case Action::nextWindowOfClassOnAllWorkspaces:
209 cycleWindow(true, false, true, true, it->string());
212 case Action::prevWindowOfClassOnAllWorkspaces:
213 cycleWindow(false, false, true, true, it->string());
216 case Action::changeWorkspace:
217 // we subtract one so counting starts at 1 in the config file
218 changeWorkspace(it->number() - 1);
221 case Action::execute:
222 execCommand(it->string());
229 // these actions require an active window
230 if (_active != _clients.end()) {
231 XWindow *window = *_active;
233 switch (it->type()) {
234 case Action::iconify:
250 case Action::sendToWorkspace:
251 window->sendTo(it->number());
254 case Action::toggleomnipresent:
255 if (window->desktop() == 0xffffffff)
256 window->sendTo(_active_desktop);
258 window->sendTo(0xffffffff);
261 case Action::moveWindowUp:
262 window->move(window->x(), window->y() - it->number());
265 case Action::moveWindowDown:
266 window->move(window->x(), window->y() + it->number());
269 case Action::moveWindowLeft:
270 window->move(window->x() - it->number(), window->y());
273 case Action::moveWindowRight:
274 window->move(window->x() + it->number(), window->y());
277 case Action::resizeWindowWidth:
278 window->resize(window->width() + it->number(), window->height());
281 case Action::resizeWindowHeight:
282 window->resize(window->width(), window->height() + it->number());
285 case Action::toggleshade:
286 window->shade(! window->shaded());
289 case Action::toggleMaximizeHorizontal:
290 window->toggleMaximize(XWindow::Max_Horz);
293 case Action::toggleMaximizeVertical:
294 window->toggleMaximize(XWindow::Max_Vert);
297 case Action::toggleMaximizeFull:
298 window->toggleMaximize(XWindow::Max_Full);
302 assert(false); // unhandled action type!
308 // do we want to add this window to our list?
309 bool screen::doAddWindow(Window window) const {
313 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
317 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
318 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
325 void screen::updateEverything() {
327 updateActiveDesktop();
329 updateActiveWindow();
333 void screen::updateNumDesktops() {
336 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
337 (unsigned long)_num_desktops))
338 _num_desktops = 1; // assume that there is at least 1 desktop!
342 void screen::updateActiveDesktop() {
345 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
346 (unsigned long)_active_desktop))
347 _active_desktop = 0; // there must be at least one desktop, and it must
348 // be the current one
352 void screen::updateClientList() {
355 WindowList::iterator insert_point = _active;
356 if (insert_point != _clients.end())
357 ++insert_point; // get to the item client the focused client
359 // get the client list from the root window
360 Window *rootclients = 0;
361 unsigned long num = (unsigned) -1;
362 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
366 WindowList::iterator it;
367 const WindowList::iterator end = _clients.end();
370 // insert new clients after the active window
371 for (i = 0; i < num; ++i) {
372 for (it = _clients.begin(); it != end; ++it)
373 if (**it == rootclients[i])
375 if (it == end) { // didn't already exist
376 if (doAddWindow(rootclients[i])) {
377 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
378 _clients.insert(insert_point, new XWindow(_epist, this,
384 // remove clients that no longer exist (that belong to this screen)
385 for (it = _clients.begin(); it != end;) {
386 WindowList::iterator it2 = it;
389 // is on another screen?
390 if ((*it2)->getScreen() != this)
393 for (i = 0; i < num; ++i)
394 if (**it2 == rootclients[i])
396 if (i == num) { // no longer exists
397 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
398 // watch for the active and last-active window
400 _active = _clients.end();
401 if (it2 == _last_active)
402 _last_active = _clients.end();
408 if (rootclients) delete [] rootclients;
412 const XWindow *screen::lastActiveWindow() const {
413 if (_last_active != _clients.end())
414 return *_last_active;
416 // find a window if one exists
417 WindowList::const_iterator it, end = _clients.end();
418 for (it = _clients.begin(); it != end; ++it)
419 if ((*it)->getScreen() == this && ! (*it)->iconic() &&
420 ((*it)->desktop() == 0xffffffff || (*it)->desktop() == _active_desktop))
423 // no windows on this screen
428 void screen::updateActiveWindow() {
432 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
434 WindowList::iterator it, end = _clients.end();
435 for (it = _clients.begin(); it != end; ++it) {
437 if ((*it)->getScreen() != this)
446 /* cout << "Active window is now: ";
447 if (_active == _clients.end()) cout << "None\n";
448 else cout << "0x" << hex << (*_active)->window() << dec << endl;
453 void screen::execCommand(const string &cmd) const {
455 if ((pid = fork()) == 0) {
456 // make the command run on the correct screen
457 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
458 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
461 execl("/bin/sh", "sh", "-c", cmd.c_str(), NULL);
463 } else if (pid == -1) {
464 cout << _epist->getApplicationName() <<
465 ": Could not fork a process for executing a command\n";
470 void screen::cycleWindow(const bool forward, const bool allscreens,
471 const bool alldesktops, const bool sameclass,
472 const string &cn) const {
475 if (_clients.empty()) return;
477 string classname(cn);
478 if (sameclass && classname.empty() && _active != _clients.end())
479 classname = (*_active)->appClass();
481 WindowList::const_iterator target = _active,
482 begin = _clients.begin(),
483 end = _clients.end();
498 // must be no window to focus
499 if (target == _active)
502 // start back at the beginning of the loop
506 // determine if this window is invalid for cycling to
507 const XWindow *t = *target;
508 if (t->iconic()) continue;
509 if (! allscreens && t->getScreen() != this) continue;
510 if (! alldesktops && ! (t->desktop() == _active_desktop ||
511 t->desktop() == 0xffffffff)) continue;
512 if (sameclass && ! classname.empty() &&
513 t->appClass() != classname) continue;
514 if (! t->canFocus()) continue;
516 // found a good window!
523 void screen::cycleWorkspace(const bool forward, const bool loop) const {
526 unsigned int destination = _active_desktop;
529 if (destination < _num_desktops - 1)
537 destination = _num_desktops - 1;
540 if (destination != _active_desktop)
541 changeWorkspace(destination);
545 void screen::changeWorkspace(const int num) const {
548 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
551 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
553 Display *display = _epist->getXDisplay();
554 int numlockMask, scrolllockMask;
556 _epist->getLockModifiers(numlockMask, scrolllockMask);
558 XGrabKey(display, keyCode, modifierMask,
559 _root, True, GrabModeAsync, GrabModeAsync);
560 XGrabKey(display, keyCode,
561 modifierMask|LockMask,
562 _root, True, GrabModeAsync, GrabModeAsync);
563 XGrabKey(display, keyCode,
564 modifierMask|scrolllockMask,
565 _root, True, GrabModeAsync, GrabModeAsync);
566 XGrabKey(display, keyCode,
567 modifierMask|numlockMask,
568 _root, True, GrabModeAsync, GrabModeAsync);
570 XGrabKey(display, keyCode,
571 modifierMask|LockMask|scrolllockMask,
572 _root, True, GrabModeAsync, GrabModeAsync);
573 XGrabKey(display, keyCode,
574 modifierMask|scrolllockMask|numlockMask,
575 _root, True, GrabModeAsync, GrabModeAsync);
576 XGrabKey(display, keyCode,
577 modifierMask|numlockMask|LockMask,
578 _root, True, GrabModeAsync, GrabModeAsync);
580 XGrabKey(display, keyCode,
581 modifierMask|numlockMask|LockMask|scrolllockMask,
582 _root, True, GrabModeAsync, GrabModeAsync);
585 void screen::ungrabKey(const KeyCode keyCode, const int modifierMask) const {
587 Display *display = _epist->getXDisplay();
588 int numlockMask, scrolllockMask;
590 _epist->getLockModifiers(numlockMask, scrolllockMask);
592 XUngrabKey(display, keyCode, modifierMask, _root);
593 XUngrabKey(display, keyCode, modifierMask|LockMask, _root);
594 XUngrabKey(display, keyCode, modifierMask|scrolllockMask, _root);
595 XUngrabKey(display, keyCode, modifierMask|numlockMask, _root);
596 XUngrabKey(display, keyCode, modifierMask|LockMask|scrolllockMask, _root);
597 XUngrabKey(display, keyCode, modifierMask|scrolllockMask|numlockMask, _root);
598 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask, _root);
599 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask|
600 scrolllockMask, _root);