]>
Dogcows Code - chaz/openbox/blob - util/epist/screen.cc
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
);
81 XSelectInput(_epist
->getXDisplay(), _root
, None
);
85 bool screen::findSupportingWM() {
87 if (! _xatom
->getValue(_root
, XAtom::net_supporting_wm_check
, XAtom::window
,
88 support_win
) || support_win
== None
)
92 _xatom
->getValue(support_win
, XAtom::net_wm_name
, XAtom::utf8
, title
);
98 XWindow
*screen::findWindow(const XEvent
&e
) const {
101 WindowList::const_iterator it
, end
= _clients
.end();
102 for (it
= _clients
.begin(); it
!= end
; ++it
)
103 if (**it
== e
.xany
.window
)
111 void screen::processEvent(const XEvent
&e
) {
113 assert(e
.xany
.window
== _root
);
116 if (e
.xany
.window
!= _root
) {
117 window
= findWindow(e
); // find the window
118 assert(window
); // we caught an event for a window we don't know about!?
124 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_active_window
))
125 updateActiveWindow();
126 if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_client_list
)) {
127 // catch any window unmaps first
129 if (XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
130 DestroyNotify
, &ev
) ||
131 XCheckTypedWindowEvent(_epist
->getXDisplay(), e
.xany
.window
,
145 void screen::handleKeypress(const XEvent
&e
) {
146 ActionList::const_iterator it
= _epist
->actions().begin();
147 ActionList::const_iterator end
= _epist
->actions().end();
148 for (; it
!= end
; ++it
) {
149 if (e
.xkey
.keycode
== it
->keycode() &&
150 e
.xkey
.state
== it
->modifierMask()) {
151 switch (it
->type()) {
152 case Action::nextWorkspace
:
153 cycleWorkspace(true);
156 case Action::prevWorkspace
:
157 cycleWorkspace(false);
160 case Action::nextWindow
:
164 case Action::prevWindow
:
168 case Action::changeWorkspace
:
169 changeWorkspace(it
->number());
173 // these actions require an active window
174 if (_active
!= _clients
.end()) {
175 XWindow
*window
= *_active
;
177 switch (it
->type()) {
182 case Action::toggleshade
:
183 window
->shade(! window
->shaded());
191 // do we want to add this window to our list?
192 bool screen::doAddWindow(Window window
) const {
196 if (! _xatom
->getValue(window
, XAtom::net_wm_window_type
, XAtom::atom
,
200 if (type
== _xatom
->getAtom(XAtom::net_wm_window_type_dock
) ||
201 type
== _xatom
->getAtom(XAtom::net_wm_window_type_menu
))
208 void screen::updateClientList() {
211 WindowList::iterator insert_point
= _active
;
212 if (insert_point
!= _clients
.end())
213 ++insert_point
; // get to the item client the focused client
215 // get the client list from the root window
216 Window
*rootclients
= 0;
217 unsigned long num
= (unsigned) -1;
218 if (! _xatom
->getValue(_root
, XAtom::net_client_list
, XAtom::window
, num
,
220 while (! _clients
.empty()) {
221 delete _clients
.front();
222 _clients
.erase(_clients
.begin());
224 if (rootclients
) delete [] rootclients
;
228 WindowList::iterator it
, end
= _clients
.end();
231 // insert new clients after the active window
232 for (i
= 0; i
< num
; ++i
) {
233 for (it
= _clients
.begin(); it
!= end
; ++it
)
234 if (**it
== rootclients
[i
])
236 if (it
== end
) { // didn't already exist
237 if (doAddWindow(rootclients
[i
])) {
238 cout
<< "Added window: 0x" << hex
<< rootclients
[i
] << dec
<< endl
;
239 _clients
.insert(insert_point
, new XWindow(_epist
, this,
245 // remove clients that no longer exist
246 for (it
= _clients
.begin(); it
!= end
;) {
247 WindowList::iterator it2
= it
++;
248 for (i
= 0; i
< num
; ++i
)
249 if (**it2
== rootclients
[i
])
251 if (i
== num
) { // no longer exists
252 cout
<< "Removed window: 0x" << hex
<< (*it2
)->window() << dec
<< endl
;
258 if (rootclients
) delete [] rootclients
;
262 void screen::updateActiveWindow() {
266 _xatom
->getValue(_root
, XAtom::net_active_window
, XAtom::window
, a
);
268 WindowList::iterator it
, end
= _clients
.end();
269 for (it
= _clients
.begin(); it
!= end
; ++it
) {
275 cout
<< "Active window is now: ";
276 if (_active
== _clients
.end()) cout
<< "None\n";
277 else cout
<< "0x" << hex
<< (*_active
)->window() << dec
<< endl
;
281 * use this when execing a command to have it on the right screen
282 string dtmp = (string)"DISPLAY=" + display_name;
283 if (putenv(const_cast<char*>(dtmp.c_str()))) {
284 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
290 void screen::cycleWindow(const bool forward
) const {
291 if (_clients
.empty()) return;
293 WindowList::const_iterator target
= _active
;
295 if (target
== _clients
.end())
296 target
= _clients
.begin();
301 if (target
== _clients
.end())
302 target
= _clients
.begin();
304 if (target
== _clients
.begin())
305 target
= _clients
.end();
308 } while (target
== _clients
.end() || (*target
)->iconic());
310 if (target
!= _clients
.end()) {
311 // we dont send an ACTIVE_WINDOW client message because that would also
312 // unshade the window if it was shaded
313 XSetInputFocus(_epist
->getXDisplay(), (*target
)->window(), RevertToNone
,
315 XRaiseWindow(_epist
->getXDisplay(), (*target
)->window());
320 void screen::cycleWorkspace(const bool forward
) const {
321 unsigned long currentDesktop
= 0;
322 unsigned long numDesktops
= 0;
324 if (_xatom
->getValue(_root
, XAtom::net_current_desktop
, XAtom::cardinal
,
331 _xatom
->getValue(_root
, XAtom::net_number_of_desktops
, XAtom::cardinal
,
334 if ( ( (signed)currentDesktop
) == -1)
335 currentDesktop
= numDesktops
- 1;
336 else if (currentDesktop
>= numDesktops
)
339 changeWorkspace(currentDesktop
);
344 void screen::changeWorkspace(const int num
) const {
345 _xatom
->sendClientMessage(_root
, XAtom::net_current_desktop
, _root
, num
);
This page took 0.05052 seconds and 4 git commands to generate.