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/XAtom.hh"
48 screen::screen(epist
*epist
, int number
) {
50 _xatom
= _epist
->xatom();
52 _active
= _clients
.end();
53 _root
= RootWindow(_epist
->getXDisplay(), _number
);
55 // find a window manager supporting NETWM, waiting for it to load if we must
56 int count
= 20; // try for 20 seconds
58 while (! (_epist
->doShutdown() || _managed
|| count
<= 0)) {
59 if (! (_managed
= findSupportingWM()))
64 cout
<< "Found compatible window manager '" << _wm_name
<< "' for screen "
67 cout
<< "Unable to find a compatible window manager for screen " <<
72 XSelectInput(_epist
->getXDisplay(), _root
, PropertyChangeMask
);
75 updateActiveDesktop();
83 XSelectInput(_epist
->getXDisplay(), _root
, None
);
87 bool screen::findSupportingWM() {
89 if (! _xatom
->getValue(_root
, XAtom::net_supporting_wm_check
, XAtom::window
,
90 support_win
) || support_win
== None
)
94 _xatom
->getValue(support_win
, XAtom::net_wm_name
, XAtom::utf8
, title
);
100 XWindow
*screen::findWindow(const XEvent
&e
) const {
103 WindowList::const_iterator it
, end
= _clients
.end();
104 for (it
= _clients
.begin(); it
!= end
; ++it
)
105 if (**it
== e
.xany
.window
)
113 void screen::processEvent(const XEvent
&e
) {
115 assert(e
.xany
.window
== _root
);
118 if (e
.xany
.window
!= _root
) {
119 window
= findWindow(e
); // find the window
120 assert(window
); // we caught an event for a window we don't know about!?
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 ActionList::const_iterator it
= _epist
->actions().begin();
153 ActionList::const_iterator end
= _epist
->actions().end();
154 for (; it
!= end
; ++it
) {
155 if (e
.xkey
.keycode
== it
->keycode() &&
156 e
.xkey
.state
== it
->modifierMask()) {
157 switch (it
->type()) {
158 case Action::nextWorkspace
:
159 cycleWorkspace(true);
162 case Action::prevWorkspace
:
163 cycleWorkspace(false);
166 case Action::nextWindow
:
170 case Action::prevWindow
:
174 case Action::nextWindowOnAllWorkspaces
:
175 cycleWindow(true, true);
178 case Action::prevWindowOnAllWorkspaces
:
179 cycleWindow(false, true);
182 case Action::nextWindowOfClass
:
183 cycleWindow(true, false, true);
186 case Action::prevWindowOfClass
:
187 cycleWindow(false, false, true);
190 case Action::nextWindowOfClassOnAllWorkspaces
:
191 cycleWindow(true, true, true);
194 case Action::prevWindowOfClassOnAllWorkspaces
:
195 cycleWindow(false, true, true);
198 case Action::changeWorkspace
:
199 changeWorkspace(it
->number());
203 // these actions require an active window
204 if (_active
!= _clients
.end()) {
205 XWindow
*window
= *_active
;
207 switch (it
->type()) {
208 case Action::iconify
:
224 case Action::toggleomnipresent
:
225 if (window
->desktop() == 0xffffffff)
226 window
->sendTo(_active_desktop
);
228 window
->sendTo(0xffffffff);
231 case Action::toggleshade
:
232 window
->shade(! window
->shaded());
240 // do we want to add this window to our list?
241 bool screen::doAddWindow(Window window
) const {
245 if (! _xatom
->getValue(window
, XAtom::net_wm_window_type
, XAtom::atom
,
249 if (type
== _xatom
->getAtom(XAtom::net_wm_window_type_dock
) ||
250 type
== _xatom
->getAtom(XAtom::net_wm_window_type_menu
))
257 void screen::updateNumDesktops() {
260 if (! _xatom
->getValue(_root
, XAtom::net_number_of_desktops
, XAtom::cardinal
,
261 (unsigned long)_num_desktops
))
262 _num_desktops
= 1; // assume that there is at least 1 desktop!
266 void screen::updateActiveDesktop() {
269 if (! _xatom
->getValue(_root
, XAtom::net_current_desktop
, XAtom::cardinal
,
270 (unsigned long)_active_desktop
))
271 _active_desktop
= 0; // there must be at least one desktop, and it must
272 // be the current one
276 void screen::updateClientList() {
279 WindowList::iterator insert_point
= _active
;
280 if (insert_point
!= _clients
.end())
281 ++insert_point
; // get to the item client the focused client
283 // get the client list from the root window
284 Window
*rootclients
= 0;
285 unsigned long num
= (unsigned) -1;
286 if (! _xatom
->getValue(_root
, XAtom::net_client_list
, XAtom::window
, num
,
288 while (! _clients
.empty()) {
289 delete _clients
.front();
290 _clients
.erase(_clients
.begin());
292 if (rootclients
) delete [] rootclients
;
296 WindowList::iterator it
, end
= _clients
.end();
299 // insert new clients after the active window
300 for (i
= 0; i
< num
; ++i
) {
301 for (it
= _clients
.begin(); it
!= end
; ++it
)
302 if (**it
== rootclients
[i
])
304 if (it
== end
) { // didn't already exist
305 if (doAddWindow(rootclients
[i
])) {
306 cout
<< "Added window: 0x" << hex
<< rootclients
[i
] << dec
<< endl
;
307 _clients
.insert(insert_point
, new XWindow(_epist
, this,
313 // remove clients that no longer exist
314 for (it
= _clients
.begin(); it
!= end
;) {
315 WindowList::iterator it2
= it
++;
316 for (i
= 0; i
< num
; ++i
)
317 if (**it2
== rootclients
[i
])
319 if (i
== num
) { // no longer exists
320 cout
<< "Removed window: 0x" << hex
<< (*it2
)->window() << dec
<< endl
;
326 if (rootclients
) delete [] rootclients
;
330 void screen::updateActiveWindow() {
334 _xatom
->getValue(_root
, XAtom::net_active_window
, XAtom::window
, a
);
336 WindowList::iterator it
, end
= _clients
.end();
337 for (it
= _clients
.begin(); it
!= end
; ++it
) {
343 cout
<< "Active window is now: ";
344 if (_active
== _clients
.end()) cout
<< "None\n";
345 else cout
<< "0x" << hex
<< (*_active
)->window() << dec
<< endl
;
349 * use this when execing a command to have it on the right screen
350 string dtmp = (string)"DISPLAY=" + display_name;
351 if (putenv(const_cast<char*>(dtmp.c_str()))) {
352 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
358 void screen::cycleWindow(const bool forward
, const bool alldesktops
,
359 const bool sameclass
) const {
362 if (_clients
.empty()) return;
364 WindowList::const_iterator target
= _active
;
366 if (target
== _clients
.end())
367 target
= _clients
.begin();
372 if (target
== _clients
.end())
373 target
= _clients
.begin();
375 if (target
== _clients
.begin())
376 target
= _clients
.end();
379 } while (target
== _clients
.end() ||
380 (*target
)->iconic() ||
381 (! alldesktops
&& (*target
)->desktop() != _active_desktop
) ||
382 (sameclass
&& _active
!= _clients
.end() &&
383 (*target
)->appClass() != (*_active
)->appClass()));
385 if (target
!= _clients
.end())
390 void screen::cycleWorkspace(const bool forward
, const bool loop
) const {
393 unsigned int destination
= _active_desktop
;
396 if (destination
< _num_desktops
- 1)
404 destination
= _num_desktops
- 1;
407 if (destination
!= _active_desktop
)
408 changeWorkspace(destination
);
412 void screen::changeWorkspace(const int num
) const {
415 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);