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) {
55 _xatom = _epist->xatom();
57 _active = _clients.end();
58 _info = _epist->getScreenInfo(_number);
59 _root = _info->getRootWindow();
61 // find a window manager supporting NETWM, waiting for it to load if we must
62 int count = 20; // try for 20 seconds
64 while (! (_epist->doShutdown() || _managed || count <= 0)) {
65 if (! (_managed = findSupportingWM()))
70 cout << "Found compatible window manager '" << _wm_name << "' for screen "
73 cout << "Unable to find a compatible window manager for screen " <<
78 XSelectInput(_epist->getXDisplay(), _root, PropertyChangeMask);
81 updateActiveDesktop();
89 XSelectInput(_epist->getXDisplay(), _root, None);
93 bool screen::findSupportingWM() {
95 if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
96 support_win) || support_win == None)
100 _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
106 XWindow *screen::findWindow(const XEvent &e) const {
109 WindowList::const_iterator it, end = _clients.end();
110 for (it = _clients.begin(); it != end; ++it)
111 if (**it == e.xany.window)
119 void screen::processEvent(const XEvent &e) {
121 assert(e.xany.window == _root);
126 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_number_of_desktops))
128 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_current_desktop))
129 updateActiveDesktop();
130 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
131 updateActiveWindow();
132 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list)) {
133 // catch any window unmaps first
135 if (XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
136 DestroyNotify, &ev) ||
137 XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
151 void screen::handleKeypress(const XEvent &e) {
152 int scrolllockMask, numlockMask;
154 ActionList::const_iterator it = _epist->actions().begin();
155 ActionList::const_iterator end = _epist->actions().end();
157 _epist->getLockModifiers(numlockMask, scrolllockMask);
159 for (; it != end; ++it) {
160 unsigned int state = e.xkey.state & ~(LockMask|scrolllockMask|numlockMask);
162 if (e.xkey.keycode == it->keycode() &&
163 state == it->modifierMask()) {
164 switch (it->type()) {
165 case Action::nextWorkspace:
166 cycleWorkspace(true);
169 case Action::prevWorkspace:
170 cycleWorkspace(false);
173 case Action::nextWindow:
177 case Action::prevWindow:
181 case Action::nextWindowOnAllWorkspaces:
182 cycleWindow(true, true);
185 case Action::prevWindowOnAllWorkspaces:
186 cycleWindow(false, true);
189 case Action::nextWindowOfClass:
190 cycleWindow(true, false, true, it->string());
193 case Action::prevWindowOfClass:
194 cycleWindow(false, false, true, it->string());
197 case Action::nextWindowOfClassOnAllWorkspaces:
198 cycleWindow(true, true, true, it->string());
201 case Action::prevWindowOfClassOnAllWorkspaces:
202 cycleWindow(false, true, true, it->string());
205 case Action::changeWorkspace:
206 changeWorkspace(it->number());
209 case Action::execute:
210 execCommand(it->string());
217 // these actions require an active window
218 if (_active != _clients.end()) {
219 XWindow *window = *_active;
221 switch (it->type()) {
222 case Action::iconify:
238 case Action::sendToWorkspace:
239 window->sendTo(it->number());
242 case Action::toggleomnipresent:
243 if (window->desktop() == 0xffffffff)
244 window->sendTo(_active_desktop);
246 window->sendTo(0xffffffff);
249 case Action::moveWindowUp:
250 window->move(window->x(), window->y() - it->number());
253 case Action::moveWindowDown:
254 window->move(window->x(), window->y() + it->number());
257 case Action::moveWindowLeft:
258 window->move(window->x() - it->number(), window->y());
261 case Action::moveWindowRight:
262 window->move(window->x() + it->number(), window->y());
265 case Action::resizeWindowWidth:
266 window->resize(window->width() + it->number(), window->height());
269 case Action::resizeWindowHeight:
270 window->resize(window->width(), window->height() + it->number());
273 case Action::toggleshade:
274 window->shade(! window->shaded());
277 case Action::toggleMaximizeHorizontal:
278 window->toggleMaximize(XWindow::Max_Horz);
281 case Action::toggleMaximizeVertical:
282 window->toggleMaximize(XWindow::Max_Vert);
285 case Action::toggleMaximizeFull:
286 window->toggleMaximize(XWindow::Max_Full);
290 assert(false); // unhandled action type!
298 // do we want to add this window to our list?
299 bool screen::doAddWindow(Window window) const {
303 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
307 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
308 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
315 void screen::updateNumDesktops() {
318 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
319 (unsigned long)_num_desktops))
320 _num_desktops = 1; // assume that there is at least 1 desktop!
324 void screen::updateActiveDesktop() {
327 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
328 (unsigned long)_active_desktop))
329 _active_desktop = 0; // there must be at least one desktop, and it must
330 // be the current one
334 void screen::updateClientList() {
337 WindowList::iterator insert_point = _active;
338 if (insert_point != _clients.end())
339 ++insert_point; // get to the item client the focused client
341 // get the client list from the root window
342 Window *rootclients = 0;
343 unsigned long num = (unsigned) -1;
344 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
348 WindowList::iterator it;
349 const WindowList::iterator end = _clients.end();
352 // insert new clients after the active window
353 for (i = 0; i < num; ++i) {
354 for (it = _clients.begin(); it != end; ++it)
355 if (**it == rootclients[i])
357 if (it == end) { // didn't already exist
358 if (doAddWindow(rootclients[i])) {
359 //cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
360 _clients.insert(insert_point, new XWindow(_epist, this,
366 // remove clients that no longer exist
367 for (it = _clients.begin(); it != end;) {
368 WindowList::iterator it2 = it;
371 for (i = 0; i < num; ++i)
372 if (**it2 == rootclients[i])
374 if (i == num) { // no longer exists
375 //cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
376 // watch for the active window
378 _active = _clients.end();
384 if (rootclients) delete [] rootclients;
388 void screen::updateActiveWindow() {
392 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
394 WindowList::iterator it, end = _clients.end();
395 for (it = _clients.begin(); it != end; ++it) {
401 //cout << "Active window is now: ";
402 //if (_active == _clients.end()) cout << "None\n";
403 //else cout << "0x" << hex << (*_active)->window() << dec << endl;
407 void screen::execCommand(const std::string &cmd) const {
409 if ((pid = fork()) == 0) {
410 extern char **environ;
412 char *const argv[] = {
415 const_cast<char *>(cmd.c_str()),
418 // make the command run on the correct screen
419 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
420 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
423 execve("/bin/sh", argv, environ);
425 } else if (pid == -1) {
426 cout << _epist->getApplicationName() <<
427 ": Could not fork a process for executing a command\n";
432 void screen::cycleWindow(const bool forward, const bool alldesktops,
433 const bool sameclass, const string &cn) const {
436 WindowList::const_iterator target = _active;
438 string classname = cn;
439 if (sameclass && classname.empty() && target != _clients.end())
440 classname = (*target)->appClass();
442 if (target == _clients.end())
443 target = _clients.begin();
445 WindowList::const_iterator begin = target;
450 if (target == _clients.end())
451 target = _clients.begin();
453 if (target == _clients.begin())
454 target = _clients.end();
458 // no window to focus
461 } while (target == _clients.end() ||
462 (*target)->iconic() ||
463 (! alldesktops && (*target)->desktop() != _active_desktop) ||
464 (sameclass && ! classname.empty() &&
465 (*target)->appClass() != classname));
467 if (target != _clients.end())
472 void screen::cycleWorkspace(const bool forward, const bool loop) const {
475 unsigned int destination = _active_desktop;
478 if (destination < _num_desktops - 1)
486 destination = _num_desktops - 1;
489 if (destination != _active_desktop)
490 changeWorkspace(destination);
494 void screen::changeWorkspace(const int num) const {
497 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
500 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
502 Display *display = _epist->getXDisplay();
503 int numlockMask, scrolllockMask;
505 _epist->getLockModifiers(numlockMask, scrolllockMask);
507 XGrabKey(display, keyCode, modifierMask,
508 _root, True, GrabModeAsync, GrabModeAsync);
509 XGrabKey(display, keyCode,
510 modifierMask|LockMask,
511 _root, True, GrabModeAsync, GrabModeAsync);
512 XGrabKey(display, keyCode,
513 modifierMask|scrolllockMask,
514 _root, True, GrabModeAsync, GrabModeAsync);
515 XGrabKey(display, keyCode,
516 modifierMask|numlockMask,
517 _root, True, GrabModeAsync, GrabModeAsync);
519 XGrabKey(display, keyCode,
520 modifierMask|LockMask|scrolllockMask,
521 _root, True, GrabModeAsync, GrabModeAsync);
522 XGrabKey(display, keyCode,
523 modifierMask|scrolllockMask|numlockMask,
524 _root, True, GrabModeAsync, GrabModeAsync);
525 XGrabKey(display, keyCode,
526 modifierMask|numlockMask|LockMask,
527 _root, True, GrabModeAsync, GrabModeAsync);
529 XGrabKey(display, keyCode,
530 modifierMask|numlockMask|LockMask|scrolllockMask,
531 _root, True, GrabModeAsync, GrabModeAsync);