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;
150 ActionList::const_iterator it = _epist->actions().begin();
151 ActionList::const_iterator end = _epist->actions().end();
153 _epist->getLockModifiers(numlockMask, scrolllockMask);
155 for (; it != end; ++it) {
156 unsigned int state = e.xkey.state & ~(LockMask|scrolllockMask|numlockMask);
158 if (e.xkey.keycode == it->keycode() &&
159 state == it->modifierMask()) {
160 switch (it->type()) {
161 case Action::nextScreen:
162 _epist->cycleScreen(_number, true);
165 case Action::prevScreen:
166 _epist->cycleScreen(_number, false);
169 case Action::nextWorkspace:
170 cycleWorkspace(true);
173 case Action::prevWorkspace:
174 cycleWorkspace(false);
177 case Action::nextWindow:
181 case Action::prevWindow:
185 case Action::nextWindowOnAllWorkspaces:
186 cycleWindow(true, false, true);
189 case Action::prevWindowOnAllWorkspaces:
190 cycleWindow(false, false, true);
193 case Action::nextWindowOnAllScreens:
194 cycleWindow(true, true);
197 case Action::prevWindowOnAllScreens:
198 cycleWindow(false, true);
201 case Action::nextWindowOfClass:
202 cycleWindow(true, false, false, true, it->string());
205 case Action::prevWindowOfClass:
206 cycleWindow(false, false, false, true, it->string());
209 case Action::nextWindowOfClassOnAllWorkspaces:
210 cycleWindow(true, false, true, true, it->string());
213 case Action::prevWindowOfClassOnAllWorkspaces:
214 cycleWindow(false, false, true, true, it->string());
217 case Action::changeWorkspace:
218 changeWorkspace(it->number());
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!
310 // do we want to add this window to our list?
311 bool screen::doAddWindow(Window window) const {
315 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
319 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
320 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
327 void screen::updateEverything() {
329 updateActiveDesktop();
331 updateActiveWindow();
335 void screen::updateNumDesktops() {
338 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
339 (unsigned long)_num_desktops))
340 _num_desktops = 1; // assume that there is at least 1 desktop!
344 void screen::updateActiveDesktop() {
347 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
348 (unsigned long)_active_desktop))
349 _active_desktop = 0; // there must be at least one desktop, and it must
350 // be the current one
354 void screen::updateClientList() {
357 WindowList::iterator insert_point = _active;
358 if (insert_point != _clients.end())
359 ++insert_point; // get to the item client the focused client
361 // get the client list from the root window
362 Window *rootclients = 0;
363 unsigned long num = (unsigned) -1;
364 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
368 WindowList::iterator it;
369 const WindowList::iterator end = _clients.end();
372 // insert new clients after the active window
373 for (i = 0; i < num; ++i) {
374 for (it = _clients.begin(); it != end; ++it)
375 if (**it == rootclients[i])
377 if (it == end) { // didn't already exist
378 if (doAddWindow(rootclients[i])) {
379 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
380 _clients.insert(insert_point, new XWindow(_epist, this,
386 // remove clients that no longer exist (that belong to this screen)
387 for (it = _clients.begin(); it != end;) {
388 WindowList::iterator it2 = it;
391 // is on another screen?
392 if ((*it2)->getScreen() != this)
395 for (i = 0; i < num; ++i)
396 if (**it2 == rootclients[i])
398 if (i == num) { // no longer exists
399 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
400 // watch for the active and last-active window
402 _active = _clients.end();
403 if (it2 == _last_active)
404 _last_active = _clients.end();
410 if (rootclients) delete [] rootclients;
414 const XWindow *screen::lastActiveWindow() const {
415 if (_last_active != _clients.end())
416 return *_last_active;
418 // find a window if one exists
419 WindowList::const_iterator it, end = _clients.end();
420 for (it = _clients.begin(); it != end; ++it)
421 if ((*it)->getScreen() == this && ! (*it)->iconic() &&
422 ((*it)->desktop() == 0xffffffff || (*it)->desktop() == _active_desktop))
425 // no windows on this screen
430 void screen::updateActiveWindow() {
434 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
436 WindowList::iterator it, end = _clients.end();
437 for (it = _clients.begin(); it != end; ++it) {
439 if ((*it)->getScreen() != this)
448 /* cout << "Active window is now: ";
449 if (_active == _clients.end()) cout << "None\n";
450 else cout << "0x" << hex << (*_active)->window() << dec << endl;
455 void screen::execCommand(const string &cmd) const {
457 if ((pid = fork()) == 0) {
458 extern char **environ;
460 char *const argv[] = {
463 const_cast<char *>(cmd.c_str()),
466 // make the command run on the correct screen
467 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
468 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
471 execve("/bin/sh", argv, environ);
473 } else if (pid == -1) {
474 cout << _epist->getApplicationName() <<
475 ": Could not fork a process for executing a command\n";
480 void screen::cycleWindow(const bool forward, const bool allscreens,
481 const bool alldesktops, const bool sameclass,
482 const string &cn) const {
485 if (_clients.empty()) return;
487 string classname(cn);
488 if (sameclass && classname.empty() && _active != _clients.end())
489 classname = (*_active)->appClass();
491 WindowList::const_iterator target = _active,
492 begin = _clients.begin(),
493 end = _clients.end();
510 // must be no window to focus
511 if (target == _active)
514 // determine if this window is invalid for cycling to
515 const XWindow *t = *target;
516 if (t->iconic()) continue;
517 if (! allscreens && t->getScreen() != this) continue;
518 if (! alldesktops && ! (t->desktop() == _active_desktop ||
519 t->desktop() == 0xffffffff)) continue;
520 if (sameclass && ! classname.empty() &&
521 t->appClass() != classname) continue;
522 if (! t->canFocus()) continue;
524 // found a good window!
531 void screen::cycleWorkspace(const bool forward, const bool loop) const {
534 unsigned int destination = _active_desktop;
537 if (destination < _num_desktops - 1)
545 destination = _num_desktops - 1;
548 if (destination != _active_desktop)
549 changeWorkspace(destination);
553 void screen::changeWorkspace(const int num) const {
556 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
559 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
561 Display *display = _epist->getXDisplay();
562 int numlockMask, scrolllockMask;
564 _epist->getLockModifiers(numlockMask, scrolllockMask);
566 XGrabKey(display, keyCode, modifierMask,
567 _root, True, GrabModeAsync, GrabModeAsync);
568 XGrabKey(display, keyCode,
569 modifierMask|LockMask,
570 _root, True, GrabModeAsync, GrabModeAsync);
571 XGrabKey(display, keyCode,
572 modifierMask|scrolllockMask,
573 _root, True, GrabModeAsync, GrabModeAsync);
574 XGrabKey(display, keyCode,
575 modifierMask|numlockMask,
576 _root, True, GrabModeAsync, GrabModeAsync);
578 XGrabKey(display, keyCode,
579 modifierMask|LockMask|scrolllockMask,
580 _root, True, GrabModeAsync, GrabModeAsync);
581 XGrabKey(display, keyCode,
582 modifierMask|scrolllockMask|numlockMask,
583 _root, True, GrabModeAsync, GrabModeAsync);
584 XGrabKey(display, keyCode,
585 modifierMask|numlockMask|LockMask,
586 _root, True, GrabModeAsync, GrabModeAsync);
588 XGrabKey(display, keyCode,
589 modifierMask|numlockMask|LockMask|scrolllockMask,
590 _root, True, GrabModeAsync, GrabModeAsync);