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
29 # include <sys/types.h>
31 #endif // HAVE_UNISTD_H
43 #include "../../src/BaseDisplay.hh"
44 #include "../../src/XAtom.hh"
49 screen::screen(epist
*epist
, int number
) {
51 _xatom
= _epist
->xatom();
53 _active
= _clients
.end();
54 _info
= _epist
->getScreenInfo(_number
);
55 _root
= _info
->getRootWindow();
57 // find a window manager supporting NETWM, waiting for it to load if we must
58 int count
= 20; // try for 20 seconds
60 while (! (_epist
->doShutdown() || _managed
|| count
<= 0)) {
61 if (! (_managed
= findSupportingWM()))
66 cout
<< "Found compatible window manager '" << _wm_name
<< "' for screen "
69 cout
<< "Unable to find a compatible window manager for screen " <<
74 XSelectInput(_epist
->getXDisplay(), _root
, PropertyChangeMask
);
77 updateActiveDesktop();
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
);
120 if (e
.xany
.window
!= _root
) {
121 window
= findWindow(e
); // find the window
122 assert(window
); // we caught an event for a window we don't know about!?
128 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_number_of_desktops
))
130 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_current_desktop
))
131 updateActiveDesktop();
132 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_active_window
))
133 updateActiveWindow();
134 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_client_list
)) {
135 // catch any window unmaps first
137 if (XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
138 DestroyNotify
, &ev
) ||
139 XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
153 void screen::handleKeypress(const XEvent
&e
) {
154 ActionList::const_iterator it
= _epist
->actions().begin();
155 ActionList::const_iterator end
= _epist
->actions().end();
156 for (; it
!= end
; ++it
) {
157 if (e
.xkey
.keycode
== it
->keycode() &&
158 e
.xkey
.state
== it
->modifierMask()) {
159 switch (it
->type()) {
160 case Action::nextWorkspace
:
161 cycleWorkspace(true);
164 case Action::prevWorkspace
:
165 cycleWorkspace(false);
168 case Action::nextWindow
:
172 case Action::prevWindow
:
176 case Action::nextWindowOnAllWorkspaces
:
177 cycleWindow(true, true);
180 case Action::prevWindowOnAllWorkspaces
:
181 cycleWindow(false, true);
184 case Action::nextWindowOfClass
:
185 cycleWindow(true, false, true);
188 case Action::prevWindowOfClass
:
189 cycleWindow(false, false, true);
192 case Action::nextWindowOfClassOnAllWorkspaces
:
193 cycleWindow(true, true, true);
196 case Action::prevWindowOfClassOnAllWorkspaces
:
197 cycleWindow(false, true, true);
200 case Action::changeWorkspace
:
201 changeWorkspace(it
->number());
204 case Action::execute
:
205 execCommand(it
->string());
209 // these actions require an active window
210 if (_active
!= _clients
.end()) {
211 XWindow
*window
= *_active
;
213 switch (it
->type()) {
214 case Action::iconify
:
230 case Action::sendToWorkspace
:
231 window
->sendTo(it
->number());
234 case Action::toggleomnipresent
:
235 if (window
->desktop() == 0xffffffff)
236 window
->sendTo(_active_desktop
);
238 window
->sendTo(0xffffffff);
241 case Action::toggleshade
:
242 window
->shade(! window
->shaded());
250 // do we want to add this window to our list?
251 bool screen::doAddWindow(Window window
) const {
255 if (! _xatom
->getValue(window
, XAtom::net_wm_window_type
, XAtom::atom
,
259 if (type
== _xatom
->getAtom(XAtom::net_wm_window_type_dock
) ||
260 type
== _xatom
->getAtom(XAtom::net_wm_window_type_menu
))
267 void screen::updateNumDesktops() {
270 if (! _xatom
->getValue(_root
, XAtom::net_number_of_desktops
, XAtom::cardinal
,
271 (unsigned long)_num_desktops
))
272 _num_desktops
= 1; // assume that there is at least 1 desktop!
276 void screen::updateActiveDesktop() {
279 if (! _xatom
->getValue(_root
, XAtom::net_current_desktop
, XAtom::cardinal
,
280 (unsigned long)_active_desktop
))
281 _active_desktop
= 0; // there must be at least one desktop, and it must
282 // be the current one
286 void screen::updateClientList() {
289 WindowList::iterator insert_point
= _active
;
290 if (insert_point
!= _clients
.end())
291 ++insert_point
; // get to the item client the focused client
293 // get the client list from the root window
294 Window
*rootclients
= 0;
295 unsigned long num
= (unsigned) -1;
296 if (! _xatom
->getValue(_root
, XAtom::net_client_list
, XAtom::window
, num
,
298 while (! _clients
.empty()) {
299 delete _clients
.front();
300 _clients
.erase(_clients
.begin());
302 if (rootclients
) delete [] rootclients
;
306 WindowList::iterator it
, end
= _clients
.end();
309 // insert new clients after the active window
310 for (i
= 0; i
< num
; ++i
) {
311 for (it
= _clients
.begin(); it
!= end
; ++it
)
312 if (**it
== rootclients
[i
])
314 if (it
== end
) { // didn't already exist
315 if (doAddWindow(rootclients
[i
])) {
316 cout
<< "Added window: 0x" << hex
<< rootclients
[i
] << dec
<< endl
;
317 _clients
.insert(insert_point
, new XWindow(_epist
, this,
323 // remove clients that no longer exist
324 for (it
= _clients
.begin(); it
!= end
;) {
325 WindowList::iterator it2
= it
++;
326 for (i
= 0; i
< num
; ++i
)
327 if (**it2
== rootclients
[i
])
329 if (i
== num
) { // no longer exists
330 cout
<< "Removed window: 0x" << hex
<< (*it2
)->window() << dec
<< endl
;
336 if (rootclients
) delete [] rootclients
;
340 void screen::updateActiveWindow() {
344 _xatom
->getValue(_root
, XAtom::net_active_window
, XAtom::window
, a
);
346 WindowList::iterator it
, end
= _clients
.end();
347 for (it
= _clients
.begin(); it
!= end
; ++it
) {
353 cout
<< "Active window is now: ";
354 if (_active
== _clients
.end()) cout
<< "None\n";
355 else cout
<< "0x" << hex
<< (*_active
)->window() << dec
<< endl
;
359 void screen::execCommand(const std::string
&cmd
) const {
361 if ((pid
= fork()) == 0) {
362 extern char **environ
;
364 char *const argv
[] = {
367 const_cast<char *>(cmd
.c_str()),
370 // make the command run on the correct screen
371 if (putenv(const_cast<char*>(_info
->displayString().c_str()))) {
372 cout
<< "warning: couldn't set environment variable 'DISPLAY'\n";
375 execve("/bin/sh", argv
, environ
);
377 } else if (pid
== -1) {
378 cout
<< _epist
->getApplicationName() <<
379 ": Could not fork a process for executing a command\n";
384 void screen::cycleWindow(const bool forward
, const bool alldesktops
,
385 const bool sameclass
) const {
388 if (_clients
.empty()) return;
390 WindowList::const_iterator target
= _active
;
392 if (target
== _clients
.end())
393 target
= _clients
.begin();
398 if (target
== _clients
.end())
399 target
= _clients
.begin();
401 if (target
== _clients
.begin())
402 target
= _clients
.end();
405 } while (target
== _clients
.end() ||
406 (*target
)->iconic() ||
407 (! alldesktops
&& (*target
)->desktop() != _active_desktop
) ||
408 (sameclass
&& _active
!= _clients
.end() &&
409 (*target
)->appClass() != (*_active
)->appClass()));
411 if (target
!= _clients
.end())
416 void screen::cycleWorkspace(const bool forward
, const bool loop
) const {
419 unsigned int destination
= _active_desktop
;
422 if (destination
< _num_desktops
- 1)
430 destination
= _num_desktops
- 1;
433 if (destination
!= _active_desktop
)
434 changeWorkspace(destination
);
438 void screen::changeWorkspace(const int num
) const {
441 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);