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 cout << "root window on screen " << _number << ": 0x" << hex << _root <<
66 // find a window manager supporting NETWM, waiting for it to load if we must
67 int count = 20; // try for 20 seconds
69 while (! (_epist->doShutdown() || _managed || count <= 0)) {
70 if (! (_managed = findSupportingWM()))
75 cout << "Found compatible window manager '" << _wm_name << "' for screen "
78 cout << "Unable to find a compatible window manager for screen " <<
83 XSelectInput(_epist->getXDisplay(), _root, PropertyChangeMask);
88 XSelectInput(_epist->getXDisplay(), _root, None);
92 bool screen::findSupportingWM() {
94 if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
95 support_win) || support_win == None)
99 _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
105 XWindow *screen::findWindow(const XEvent &e) const {
108 WindowList::const_iterator it, end = _clients.end();
109 for (it = _clients.begin(); it != end; ++it)
110 if (**it == e.xany.window)
118 void screen::processEvent(const XEvent &e) {
120 assert(e.xany.window == _root);
125 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_number_of_desktops))
127 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_current_desktop))
128 updateActiveDesktop();
129 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
130 updateActiveWindow();
131 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list)) {
132 // catch any window unmaps first
134 if (XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
135 DestroyNotify, &ev) ||
136 XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
150 void screen::handleKeypress(const XEvent &e) {
151 int scrolllockMask, numlockMask;
153 ActionList::const_iterator it = _epist->actions().begin();
154 ActionList::const_iterator end = _epist->actions().end();
156 _epist->getLockModifiers(numlockMask, scrolllockMask);
158 for (; it != end; ++it) {
159 unsigned int state = e.xkey.state & ~(LockMask|scrolllockMask|numlockMask);
161 if (e.xkey.keycode == it->keycode() &&
162 state == it->modifierMask()) {
163 switch (it->type()) {
164 case Action::nextScreen:
165 _epist->cycleScreen(_number, true);
168 case Action::prevScreen:
169 _epist->cycleScreen(_number, false);
172 case Action::nextWorkspace:
173 cycleWorkspace(true);
176 case Action::prevWorkspace:
177 cycleWorkspace(false);
180 case Action::nextWindow:
184 case Action::prevWindow:
188 case Action::nextWindowOnAllWorkspaces:
189 cycleWindow(true, false, true);
192 case Action::prevWindowOnAllWorkspaces:
193 cycleWindow(false, false, true);
196 case Action::nextWindowOnAllScreens:
197 cycleWindow(true, true);
200 case Action::prevWindowOnAllScreens:
201 cycleWindow(false, true);
204 case Action::nextWindowOfClass:
205 cycleWindow(true, false, false, true, it->string());
208 case Action::prevWindowOfClass:
209 cycleWindow(false, false, false, true, it->string());
212 case Action::nextWindowOfClassOnAllWorkspaces:
213 cycleWindow(true, false, true, true, it->string());
216 case Action::prevWindowOfClassOnAllWorkspaces:
217 cycleWindow(false, false, true, true, it->string());
220 case Action::changeWorkspace:
221 changeWorkspace(it->number());
224 case Action::execute:
225 execCommand(it->string());
232 // these actions require an active window
233 if (_active != _clients.end()) {
234 XWindow *window = *_active;
236 switch (it->type()) {
237 case Action::iconify:
253 case Action::sendToWorkspace:
254 window->sendTo(it->number());
257 case Action::toggleomnipresent:
258 if (window->desktop() == 0xffffffff)
259 window->sendTo(_active_desktop);
261 window->sendTo(0xffffffff);
264 case Action::moveWindowUp:
265 window->move(window->x(), window->y() - it->number());
268 case Action::moveWindowDown:
269 window->move(window->x(), window->y() + it->number());
272 case Action::moveWindowLeft:
273 window->move(window->x() - it->number(), window->y());
276 case Action::moveWindowRight:
277 window->move(window->x() + it->number(), window->y());
280 case Action::resizeWindowWidth:
281 window->resize(window->width() + it->number(), window->height());
284 case Action::resizeWindowHeight:
285 window->resize(window->width(), window->height() + it->number());
288 case Action::toggleshade:
289 window->shade(! window->shaded());
292 case Action::toggleMaximizeHorizontal:
293 window->toggleMaximize(XWindow::Max_Horz);
296 case Action::toggleMaximizeVertical:
297 window->toggleMaximize(XWindow::Max_Vert);
300 case Action::toggleMaximizeFull:
301 window->toggleMaximize(XWindow::Max_Full);
305 assert(false); // unhandled action type!
313 // do we want to add this window to our list?
314 bool screen::doAddWindow(Window window) const {
318 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
322 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
323 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
330 void screen::updateEverything() {
332 updateActiveDesktop();
334 updateActiveWindow();
338 void screen::updateNumDesktops() {
341 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
342 (unsigned long)_num_desktops))
343 _num_desktops = 1; // assume that there is at least 1 desktop!
347 void screen::updateActiveDesktop() {
350 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
351 (unsigned long)_active_desktop))
352 _active_desktop = 0; // there must be at least one desktop, and it must
353 // be the current one
357 void screen::updateClientList() {
360 WindowList::iterator insert_point = _active;
361 if (insert_point != _clients.end())
362 ++insert_point; // get to the item client the focused client
364 // get the client list from the root window
365 Window *rootclients = 0;
366 unsigned long num = (unsigned) -1;
367 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
371 WindowList::iterator it;
372 const WindowList::iterator end = _clients.end();
375 // insert new clients after the active window
376 for (i = 0; i < num; ++i) {
377 for (it = _clients.begin(); it != end; ++it)
378 if (**it == rootclients[i])
380 if (it == end) { // didn't already exist
381 if (doAddWindow(rootclients[i])) {
382 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
383 _clients.insert(insert_point, new XWindow(_epist, this,
389 // remove clients that no longer exist (that belong to this screen)
390 for (it = _clients.begin(); it != end;) {
391 WindowList::iterator it2 = it;
394 // is on another screen?
395 if ((*it2)->getScreen() != this)
398 for (i = 0; i < num; ++i)
399 if (**it2 == rootclients[i])
401 if (i == num) { // no longer exists
402 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
403 // watch for the active and last-active window
405 _active = _clients.end();
406 if (it2 == _last_active)
407 _last_active = _clients.end();
413 if (rootclients) delete [] rootclients;
417 const XWindow *screen::lastActiveWindow() const {
418 if (_last_active != _clients.end())
419 return *_last_active;
421 // find a window if one exists
422 WindowList::const_iterator it, end = _clients.end();
423 for (it = _clients.begin(); it != end; ++it)
424 if ((*it)->getScreen() == this)
427 // no windows on this screen
432 void screen::updateActiveWindow() {
436 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
438 WindowList::iterator it, end = _clients.end();
439 for (it = _clients.begin(); it != end; ++it) {
441 if ((*it)->getScreen() != this)
450 /* cout << "Active window is now: ";
451 if (_active == _clients.end()) cout << "None\n";
452 else cout << "0x" << hex << (*_active)->window() << dec << endl;
457 void screen::execCommand(const string &cmd) const {
459 if ((pid = fork()) == 0) {
460 extern char **environ;
462 char *const argv[] = {
465 const_cast<char *>(cmd.c_str()),
468 // make the command run on the correct screen
469 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
470 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
473 execve("/bin/sh", argv, environ);
475 } else if (pid == -1) {
476 cout << _epist->getApplicationName() <<
477 ": Could not fork a process for executing a command\n";
482 void screen::cycleWindow(const bool forward, const bool allscreens,
483 const bool alldesktops, const bool sameclass,
484 const string &cn) const {
487 if (_clients.empty()) return;
489 string classname(cn);
490 if (sameclass && classname.empty() && _active != _clients.end())
491 classname = (*_active)->appClass();
493 WindowList::const_iterator target = _active,
495 begin = _clients.begin(),
496 end = _clients.end();
513 // must be no window to focus
516 } while ((*target)->iconic() ||
517 (! allscreens && (*target)->getScreen() != this) ||
519 (*target)->desktop() != _active_desktop &&
520 (*target)->desktop() != 0xffffffff) ||
521 (sameclass && ! classname.empty() &&
522 (*target)->appClass() != classname));
524 if (target != _clients.end())
529 void screen::cycleWorkspace(const bool forward, const bool loop) const {
532 unsigned int destination = _active_desktop;
535 if (destination < _num_desktops - 1)
543 destination = _num_desktops - 1;
546 if (destination != _active_desktop)
547 changeWorkspace(destination);
551 void screen::changeWorkspace(const int num) const {
554 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
557 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
559 Display *display = _epist->getXDisplay();
560 int numlockMask, scrolllockMask;
562 _epist->getLockModifiers(numlockMask, scrolllockMask);
564 XGrabKey(display, keyCode, modifierMask,
565 _root, True, GrabModeAsync, GrabModeAsync);
566 XGrabKey(display, keyCode,
567 modifierMask|LockMask,
568 _root, True, GrabModeAsync, GrabModeAsync);
569 XGrabKey(display, keyCode,
570 modifierMask|scrolllockMask,
571 _root, True, GrabModeAsync, GrabModeAsync);
572 XGrabKey(display, keyCode,
573 modifierMask|numlockMask,
574 _root, True, GrabModeAsync, GrabModeAsync);
576 XGrabKey(display, keyCode,
577 modifierMask|LockMask|scrolllockMask,
578 _root, True, GrabModeAsync, GrabModeAsync);
579 XGrabKey(display, keyCode,
580 modifierMask|scrolllockMask|numlockMask,
581 _root, True, GrabModeAsync, GrabModeAsync);
582 XGrabKey(display, keyCode,
583 modifierMask|numlockMask|LockMask,
584 _root, True, GrabModeAsync, GrabModeAsync);
586 XGrabKey(display, keyCode,
587 modifierMask|numlockMask|LockMask|scrolllockMask,
588 _root, True, GrabModeAsync, GrabModeAsync);