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);
124 if (e.xany.window != _root) {
125 window = findWindow(e); // find the window
126 assert(window); // we caught an event for a window we don't know about!?
132 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_number_of_desktops))
134 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_current_desktop))
135 updateActiveDesktop();
136 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
137 updateActiveWindow();
138 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list)) {
139 // catch any window unmaps first
141 if (XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
142 DestroyNotify, &ev) ||
143 XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
157 void screen::handleKeypress(const XEvent &e) {
158 ActionList::const_iterator it = _epist->actions().begin();
159 ActionList::const_iterator end = _epist->actions().end();
160 for (; it != end; ++it) {
161 if (e.xkey.keycode == it->keycode() &&
162 e.xkey.state == it->modifierMask()) {
163 switch (it->type()) {
164 case Action::nextWorkspace:
165 cycleWorkspace(true);
168 case Action::prevWorkspace:
169 cycleWorkspace(false);
172 case Action::nextWindow:
176 case Action::prevWindow:
180 case Action::nextWindowOnAllWorkspaces:
181 cycleWindow(true, true);
184 case Action::prevWindowOnAllWorkspaces:
185 cycleWindow(false, true);
188 case Action::nextWindowOfClass:
189 cycleWindow(true, false, true, it->string());
192 case Action::prevWindowOfClass:
193 cycleWindow(false, false, true, it->string());
196 case Action::nextWindowOfClassOnAllWorkspaces:
197 cycleWindow(true, true, true, it->string());
200 case Action::prevWindowOfClassOnAllWorkspaces:
201 cycleWindow(false, true, true, it->string());
204 case Action::changeWorkspace:
205 changeWorkspace(it->number());
208 case Action::execute:
209 execCommand(it->string());
216 // these actions require an active window
217 if (_active != _clients.end()) {
218 XWindow *window = *_active;
220 switch (it->type()) {
221 case Action::iconify:
237 case Action::sendToWorkspace:
238 window->sendTo(it->number());
241 case Action::toggleomnipresent:
242 if (window->desktop() == 0xffffffff)
243 window->sendTo(_active_desktop);
245 window->sendTo(0xffffffff);
248 case Action::moveWindowUp:
249 window->move(0, -it->number());
252 case Action::moveWindowDown:
253 window->move(0, it->number());
256 case Action::moveWindowLeft:
257 window->move(-it->number(), 0);
260 case Action::moveWindowRight:
261 window->move(it->number(), 0);
264 case Action::toggleshade:
265 window->shade(! window->shaded());
268 case Action::toggleMaximizeHorizontal:
269 window->toggleMaximize(XWindow::Max_Horz);
272 case Action::toggleMaximizeVertical:
273 window->toggleMaximize(XWindow::Max_Vert);
276 case Action::toggleMaximizeFull:
277 window->toggleMaximize(XWindow::Max_Full);
281 assert(false); // unhandled action type!
289 // do we want to add this window to our list?
290 bool screen::doAddWindow(Window window) const {
294 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
298 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
299 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
306 void screen::updateNumDesktops() {
309 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
310 (unsigned long)_num_desktops))
311 _num_desktops = 1; // assume that there is at least 1 desktop!
315 void screen::updateActiveDesktop() {
318 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
319 (unsigned long)_active_desktop))
320 _active_desktop = 0; // there must be at least one desktop, and it must
321 // be the current one
325 void screen::updateClientList() {
328 WindowList::iterator insert_point = _active;
329 if (insert_point != _clients.end())
330 ++insert_point; // get to the item client the focused client
332 // get the client list from the root window
333 Window *rootclients = 0;
334 unsigned long num = (unsigned) -1;
335 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
337 while (! _clients.empty()) {
338 delete _clients.front();
339 _clients.erase(_clients.begin());
341 if (rootclients) delete [] rootclients;
345 WindowList::iterator it, end = _clients.end();
348 // insert new clients after the active window
349 for (i = 0; i < num; ++i) {
350 for (it = _clients.begin(); it != end; ++it)
351 if (**it == rootclients[i])
353 if (it == end) { // didn't already exist
354 if (doAddWindow(rootclients[i])) {
355 cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
356 _clients.insert(insert_point, new XWindow(_epist, this,
362 // remove clients that no longer exist
363 for (it = _clients.begin(); it != end;) {
364 WindowList::iterator it2 = it++;
365 for (i = 0; i < num; ++i)
366 if (**it2 == rootclients[i])
368 if (i == num) { // no longer exists
369 cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
375 if (rootclients) delete [] rootclients;
379 void screen::updateActiveWindow() {
383 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
385 WindowList::iterator it, end = _clients.end();
386 for (it = _clients.begin(); it != end; ++it) {
392 cout << "Active window is now: ";
393 if (_active == _clients.end()) cout << "None\n";
394 else cout << "0x" << hex << (*_active)->window() << dec << endl;
398 void screen::execCommand(const std::string &cmd) const {
400 if ((pid = fork()) == 0) {
401 extern char **environ;
403 char *const argv[] = {
406 const_cast<char *>(cmd.c_str()),
409 // make the command run on the correct screen
410 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
411 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
414 execve("/bin/sh", argv, environ);
416 } else if (pid == -1) {
417 cout << _epist->getApplicationName() <<
418 ": Could not fork a process for executing a command\n";
423 void screen::cycleWindow(const bool forward, const bool alldesktops,
424 const bool sameclass, const string &cn) const {
427 if (_clients.empty()) return;
429 WindowList::const_iterator target = _active;
431 string classname = cn;
432 if (sameclass && classname.empty() && target != _clients.end())
433 classname = (*target)->appClass();
435 if (target == _clients.end())
436 target = _clients.begin();
441 if (target == _clients.end())
442 target = _clients.begin();
444 if (target == _clients.begin())
445 target = _clients.end();
448 } while (target == _clients.end() ||
449 (*target)->iconic() ||
450 (! alldesktops && (*target)->desktop() != _active_desktop) ||
451 (sameclass && ! classname.empty() &&
452 (*target)->appClass() != classname));
454 if (target != _clients.end())
459 void screen::cycleWorkspace(const bool forward, const bool loop) const {
462 unsigned int destination = _active_desktop;
465 if (destination < _num_desktops - 1)
473 destination = _num_desktops - 1;
476 if (destination != _active_desktop)
477 changeWorkspace(destination);
481 void screen::changeWorkspace(const int num) const {
484 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);