]> Dogcows Code - chaz/openbox/blob - util/epist/screen.cc
add iconifying
[chaz/openbox] / 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>
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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.
22
23 #ifdef HAVE_CONFIG_H
24 # include "../../config.h"
25 #endif // HAVE_CONFIG_H
26
27 extern "C" {
28 #ifdef HAVE_UNISTD_H
29 # include <sys/types.h>
30 # include <unistd.h>
31 #endif // HAVE_UNISTD_H
32 }
33
34 #include <iostream>
35 #include <string>
36
37 using std::cout;
38 using std::endl;
39 using std::hex;
40 using std::dec;
41 using std::string;
42
43 #include "../../src/XAtom.hh"
44 #include "screen.hh"
45 #include "epist.hh"
46
47
48 screen::screen(epist *epist, int number) {
49 _epist = epist;
50 _xatom = _epist->xatom();
51 _number = number;
52 _active = _clients.end();
53 _root = RootWindow(_epist->getXDisplay(), _number);
54
55 // find a window manager supporting NETWM, waiting for it to load if we must
56 int count = 20; // try for 20 seconds
57 _managed = false;
58 while (! (_epist->doShutdown() || _managed || count <= 0)) {
59 if (! (_managed = findSupportingWM()))
60 usleep(1000);
61 --count;
62 }
63 if (_managed)
64 cout << "Found compatible window manager '" << _wm_name << "' for screen "
65 << _number << ".\n";
66 else {
67 cout << "Unable to find a compatible window manager for screen " <<
68 _number << ".\n";
69 return;
70 }
71
72 XSelectInput(_epist->getXDisplay(), _root, PropertyChangeMask);
73
74 updateClientList();
75 updateActiveWindow();
76 }
77
78
79 screen::~screen() {
80 if (_managed)
81 XSelectInput(_epist->getXDisplay(), _root, None);
82 }
83
84
85 bool screen::findSupportingWM() {
86 Window support_win;
87 if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
88 support_win) || support_win == None)
89 return false;
90
91 string title;
92 _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
93 _wm_name = title;
94 return true;
95 }
96
97
98 XWindow *screen::findWindow(const XEvent &e) const {
99 assert(_managed);
100
101 WindowList::const_iterator it, end = _clients.end();
102 for (it = _clients.begin(); it != end; ++it)
103 if (**it == e.xany.window)
104 break;
105 if(it == end)
106 return 0;
107 return *it;
108 }
109
110
111 void screen::processEvent(const XEvent &e) {
112 assert(_managed);
113 assert(e.xany.window == _root);
114
115 XWindow *window = 0;
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!?
119 }
120
121 switch (e.type) {
122 case PropertyNotify:
123 // root window
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
128 XEvent ev;
129 if (XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
130 DestroyNotify, &ev) ||
131 XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
132 UnmapNotify, &ev)) {
133 processEvent(ev);
134 }
135
136 updateClientList();
137 }
138 break;
139 case KeyPress:
140 handleKeypress(e);
141 break;
142 }
143 }
144
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);
154 return;
155
156 case Action::prevWorkspace:
157 cycleWorkspace(false);
158 return;
159
160 case Action::nextWindow:
161 cycleWindow(true);
162 return;
163
164 case Action::prevWindow:
165 cycleWindow(false);
166 return;
167
168 case Action::changeWorkspace:
169 changeWorkspace(it->number());
170 return;
171 }
172
173 // these actions require an active window
174 if (_active != _clients.end()) {
175 XWindow *window = *_active;
176
177 switch (it->type()) {
178 case Action::iconify:
179 window->iconify();
180 return;
181
182 case Action::close:
183 window->close();
184 return;
185
186 case Action::raise:
187 window->raise();
188 return;
189
190 case Action::lower:
191 window->lower();
192 return;
193
194 case Action::toggleshade:
195 window->shade(! window->shaded());
196 return;
197 }
198 }
199 }
200 }
201 }
202
203 // do we want to add this window to our list?
204 bool screen::doAddWindow(Window window) const {
205 assert(_managed);
206
207 Atom type;
208 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
209 type))
210 return True;
211
212 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
213 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
214 return False;
215
216 return True;
217 }
218
219
220 void screen::updateClientList() {
221 assert(_managed);
222
223 WindowList::iterator insert_point = _active;
224 if (insert_point != _clients.end())
225 ++insert_point; // get to the item client the focused client
226
227 // get the client list from the root window
228 Window *rootclients = 0;
229 unsigned long num = (unsigned) -1;
230 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
231 &rootclients)) {
232 while (! _clients.empty()) {
233 delete _clients.front();
234 _clients.erase(_clients.begin());
235 }
236 if (rootclients) delete [] rootclients;
237 return;
238 }
239
240 WindowList::iterator it, end = _clients.end();
241 unsigned long i;
242
243 // insert new clients after the active window
244 for (i = 0; i < num; ++i) {
245 for (it = _clients.begin(); it != end; ++it)
246 if (**it == rootclients[i])
247 break;
248 if (it == end) { // didn't already exist
249 if (doAddWindow(rootclients[i])) {
250 cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
251 _clients.insert(insert_point, new XWindow(_epist, this,
252 rootclients[i]));
253 }
254 }
255 }
256
257 // remove clients that no longer exist
258 for (it = _clients.begin(); it != end;) {
259 WindowList::iterator it2 = it++;
260 for (i = 0; i < num; ++i)
261 if (**it2 == rootclients[i])
262 break;
263 if (i == num) { // no longer exists
264 cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
265 delete *it2;
266 _clients.erase(it2);
267 }
268 }
269
270 if (rootclients) delete [] rootclients;
271 }
272
273
274 void screen::updateActiveWindow() {
275 assert(_managed);
276
277 Window a = None;
278 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
279
280 WindowList::iterator it, end = _clients.end();
281 for (it = _clients.begin(); it != end; ++it) {
282 if (**it == a)
283 break;
284 }
285 _active = it;
286
287 cout << "Active window is now: ";
288 if (_active == _clients.end()) cout << "None\n";
289 else cout << "0x" << hex << (*_active)->window() << dec << endl;
290 }
291
292 /*
293 * use this when execing a command to have it on the right screen
294 string dtmp = (string)"DISPLAY=" + display_name;
295 if (putenv(const_cast<char*>(dtmp.c_str()))) {
296 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
297 perror("putenv()");
298 }
299 */
300
301
302 void screen::cycleWindow(const bool forward) const {
303 if (_clients.empty()) return;
304
305 WindowList::const_iterator target = _active;
306
307 if (target == _clients.end())
308 target = _clients.begin();
309
310 do {
311 if (forward) {
312 ++target;
313 if (target == _clients.end())
314 target = _clients.begin();
315 } else {
316 if (target == _clients.begin())
317 target = _clients.end();
318 --target;
319 }
320 } while (target == _clients.end() || (*target)->iconic());
321
322 if (target != _clients.end()) {
323 // we dont send an ACTIVE_WINDOW client message because that would also
324 // unshade the window if it was shaded
325 XSetInputFocus(_epist->getXDisplay(), (*target)->window(), RevertToNone,
326 CurrentTime);
327 XRaiseWindow(_epist->getXDisplay(), (*target)->window());
328 }
329 }
330
331
332 void screen::cycleWorkspace(const bool forward) const {
333 unsigned long currentDesktop = 0;
334 unsigned long numDesktops = 0;
335
336 if (_xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
337 currentDesktop)) {
338 if (forward)
339 ++currentDesktop;
340 else
341 --currentDesktop;
342
343 _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
344 numDesktops);
345
346 if ( ( (signed)currentDesktop) == -1)
347 currentDesktop = numDesktops - 1;
348 else if (currentDesktop >= numDesktops)
349 currentDesktop = 0;
350
351 changeWorkspace(currentDesktop);
352 }
353 }
354
355
356 void screen::changeWorkspace(const int num) const {
357 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
358 }
This page took 0.053819 seconds and 5 git commands to generate.