]> Dogcows Code - chaz/openbox/blob - util/epist/screen.cc
dont lose the last-focused window when all windows lose focus
[chaz/openbox] / util / epist / screen.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // screen.cc for Epistrophy - 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_STDIO_H
29 # include <stdio.h>
30 #endif // HAVE_STDIO_H
31
32 #ifdef HAVE_UNISTD_H
33 # include <sys/types.h>
34 # include <unistd.h>
35 #endif // HAVE_UNISTD_H
36 }
37
38 #include <iostream>
39 #include <string>
40
41 using std::cout;
42 using std::endl;
43 using std::hex;
44 using std::dec;
45 using std::string;
46
47 #include "../../src/BaseDisplay.hh"
48 #include "../../src/XAtom.hh"
49 #include "screen.hh"
50 #include "epist.hh"
51
52
53 screen::screen(epist *epist, int number)
54 : _clients(epist->clientsList()),
55 _active(epist->activeWindow()) {
56 _epist = epist;
57 _xatom = _epist->xatom();
58 _last_active = _clients.end();
59 _number = number;
60 _info = _epist->getScreenInfo(_number);
61 _root = _info->getRootWindow();
62
63 // find a window manager supporting NETWM, waiting for it to load if we must
64 int count = 20; // try for 20 seconds
65 _managed = false;
66 while (! (_epist->doShutdown() || _managed || count <= 0)) {
67 if (! (_managed = findSupportingWM()))
68 sleep(1);
69 --count;
70 }
71 if (_managed)
72 cout << "Found compatible window manager '" << _wm_name << "' for screen "
73 << _number << ".\n";
74 else {
75 cout << "Unable to find a compatible window manager for screen " <<
76 _number << ".\n";
77 return;
78 }
79
80 XSelectInput(_epist->getXDisplay(), _root, PropertyChangeMask);
81 }
82
83 screen::~screen() {
84 if (_managed)
85 XSelectInput(_epist->getXDisplay(), _root, None);
86 }
87
88
89 bool screen::findSupportingWM() {
90 Window support_win;
91 if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
92 support_win) || support_win == None)
93 return false;
94
95 string title;
96 _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
97 _wm_name = title;
98 return true;
99 }
100
101
102 XWindow *screen::findWindow(const XEvent &e) const {
103 assert(_managed);
104
105 WindowList::const_iterator it, end = _clients.end();
106 for (it = _clients.begin(); it != end; ++it)
107 if (**it == e.xany.window)
108 break;
109 if(it == end)
110 return 0;
111 return *it;
112 }
113
114
115 void screen::processEvent(const XEvent &e) {
116 assert(_managed);
117 assert(e.xany.window == _root);
118
119 switch (e.type) {
120 case PropertyNotify:
121 // root window
122 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_number_of_desktops))
123 updateNumDesktops();
124 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_current_desktop))
125 updateActiveDesktop();
126 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
127 updateActiveWindow();
128 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list)) {
129 // catch any window unmaps first
130 XEvent ev;
131 if (XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
132 DestroyNotify, &ev) ||
133 XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
134 UnmapNotify, &ev)) {
135 processEvent(ev);
136 }
137
138 updateClientList();
139 }
140 break;
141 case KeyPress:
142 handleKeypress(e);
143 break;
144 }
145 }
146
147 void screen::handleKeypress(const XEvent &e) {
148 int scrolllockMask, numlockMask;
149 _epist->getLockModifiers(numlockMask, scrolllockMask);
150
151 // Mask out the lock modifiers. We want our keys to always work
152 // This should be made an option
153 unsigned int state = e.xkey.state & ~(LockMask|scrolllockMask|numlockMask);
154 const Action *it = _epist->getKeyTree().getAction(e, state, this);
155
156 if (!it)
157 return;
158
159 switch (it->type()) {
160 case Action::nextScreen:
161 _epist->cycleScreen(_number, true);
162 return;
163
164 case Action::prevScreen:
165 _epist->cycleScreen(_number, false);
166 return;
167
168 case Action::nextWorkspace:
169 cycleWorkspace(true, it->number() != 0 ? it->number(): 1);
170 return;
171
172 case Action::prevWorkspace:
173 cycleWorkspace(false, it->number() != 0 ? it->number(): 1);
174 return;
175
176 case Action::nextWindow:
177
178 cycleWindow(true, it->number() != 0 ? it->number(): 1);
179 return;
180
181 case Action::prevWindow:
182 cycleWindow(false, it->number() != 0 ? it->number(): 1);
183 return;
184
185 case Action::nextWindowOnAllWorkspaces:
186 cycleWindow(true, it->number() != 0 ? it->number(): 1, false, true);
187 return;
188
189 case Action::prevWindowOnAllWorkspaces:
190 cycleWindow(false, it->number() != 0 ? it->number(): 1, false, true);
191 return;
192
193 case Action::nextWindowOnAllScreens:
194 cycleWindow(true, it->number() != 0 ? it->number(): 1, true);
195 return;
196
197 case Action::prevWindowOnAllScreens:
198 cycleWindow(false, it->number() != 0 ? it->number(): 1, true);
199 return;
200
201 case Action::nextWindowOfClass:
202 cycleWindow(true, it->number() != 0 ? it->number(): 1,
203 false, false, true, it->string());
204 return;
205
206 case Action::prevWindowOfClass:
207 cycleWindow(false, it->number() != 0 ? it->number(): 1,
208 false, false, true, it->string());
209 return;
210
211 case Action::nextWindowOfClassOnAllWorkspaces:
212 cycleWindow(true, it->number() != 0 ? it->number(): 1,
213 false, true, true, it->string());
214 return;
215
216 case Action::prevWindowOfClassOnAllWorkspaces:
217 cycleWindow(false, it->number() != 0 ? it->number(): 1,
218 false, true, true, it->string());
219 return;
220
221 case Action::changeWorkspace:
222 changeWorkspace(it->number());
223 return;
224
225 case Action::execute:
226 execCommand(it->string());
227 return;
228
229 default:
230 break;
231 }
232
233 // these actions require an active window
234 if (_active != _clients.end()) {
235 XWindow *window = *_active;
236
237 switch (it->type()) {
238 case Action::iconify:
239 window->iconify();
240 return;
241
242 case Action::close:
243 window->close();
244 return;
245
246 case Action::raise:
247 window->raise();
248 return;
249
250 case Action::lower:
251 window->lower();
252 return;
253
254 case Action::sendToWorkspace:
255 window->sendTo(it->number());
256 return;
257
258 case Action::toggleomnipresent:
259 if (window->desktop() == 0xffffffff)
260 window->sendTo(_active_desktop);
261 else
262 window->sendTo(0xffffffff);
263 return;
264
265 case Action::moveWindowUp:
266 window->move(window->x(), window->y() - it->number());
267 return;
268
269 case Action::moveWindowDown:
270 window->move(window->x(), window->y() + it->number());
271 return;
272
273 case Action::moveWindowLeft:
274 window->move(window->x() - it->number(), window->y());
275 return;
276
277 case Action::moveWindowRight:
278 window->move(window->x() + it->number(), window->y());
279 return;
280
281 case Action::resizeWindowWidth:
282 window->resizeRel(it->number(), 0);
283 return;
284
285 case Action::resizeWindowHeight:
286 window->resizeRel(0, it->number());
287 return;
288
289 case Action::toggleshade:
290 window->shade(! window->shaded());
291 return;
292
293 case Action::toggleMaximizeHorizontal:
294 window->toggleMaximize(XWindow::Max_Horz);
295 return;
296
297 case Action::toggleMaximizeVertical:
298 window->toggleMaximize(XWindow::Max_Vert);
299 return;
300
301 case Action::toggleMaximizeFull:
302 window->toggleMaximize(XWindow::Max_Full);
303 return;
304
305 default:
306 assert(false); // unhandled action type!
307 break;
308 }
309 }
310 }
311
312 // do we want to add this window to our list?
313 bool screen::doAddWindow(Window window) const {
314 assert(_managed);
315
316 Atom type;
317 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
318 type))
319 return True;
320
321 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
322 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
323 return False;
324
325 return True;
326 }
327
328
329 void screen::updateEverything() {
330 updateNumDesktops();
331 updateActiveDesktop();
332 updateClientList();
333 updateActiveWindow();
334 }
335
336
337 void screen::updateNumDesktops() {
338 assert(_managed);
339
340 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
341 (unsigned long)_num_desktops))
342 _num_desktops = 1; // assume that there is at least 1 desktop!
343 }
344
345
346 void screen::updateActiveDesktop() {
347 assert(_managed);
348
349 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
350 (unsigned long)_active_desktop))
351 _active_desktop = 0; // there must be at least one desktop, and it must
352 // be the current one
353 }
354
355
356 void screen::updateClientList() {
357 assert(_managed);
358
359 WindowList::iterator insert_point = _active;
360 if (insert_point != _clients.end())
361 ++insert_point; // get to the item client the focused client
362
363 // get the client list from the root window
364 Window *rootclients = 0;
365 unsigned long num = (unsigned) -1;
366 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
367 &rootclients))
368 num = 0;
369
370 WindowList::iterator it;
371 const WindowList::iterator end = _clients.end();
372 unsigned long i;
373
374 // insert new clients after the active window
375 for (i = 0; i < num; ++i) {
376 for (it = _clients.begin(); it != end; ++it)
377 if (**it == rootclients[i])
378 break;
379 if (it == end) { // didn't already exist
380 if (doAddWindow(rootclients[i])) {
381 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
382 _clients.insert(insert_point, new XWindow(_epist, this,
383 rootclients[i]));
384 }
385 }
386 }
387
388 // remove clients that no longer exist (that belong to this screen)
389 for (it = _clients.begin(); it != end;) {
390 WindowList::iterator it2 = it;
391 ++it;
392
393 // is on another screen?
394 if ((*it2)->getScreen() != this)
395 continue;
396
397 for (i = 0; i < num; ++i)
398 if (**it2 == rootclients[i])
399 break;
400 if (i == num) { // no longer exists
401 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
402 // watch for the active and last-active window
403 if (it2 == _active)
404 _active = _clients.end();
405 if (it2 == _last_active)
406 _last_active = _clients.end();
407 delete *it2;
408 _clients.erase(it2);
409 }
410 }
411
412 if (rootclients) delete [] rootclients;
413 }
414
415
416 const XWindow *screen::lastActiveWindow() const {
417 if (_last_active != _clients.end())
418 return *_last_active;
419
420 // find a window if one exists
421 WindowList::const_iterator it, end = _clients.end();
422 for (it = _clients.begin(); it != end; ++it)
423 if ((*it)->getScreen() == this && ! (*it)->iconic() &&
424 ((*it)->desktop() == 0xffffffff || (*it)->desktop() == _active_desktop))
425 return *it;
426
427 // no windows on this screen
428 return 0;
429 }
430
431
432 void screen::updateActiveWindow() {
433 assert(_managed);
434
435 Window a = None;
436 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
437
438 WindowList::iterator it, end = _clients.end();
439 for (it = _clients.begin(); it != end; ++it) {
440 if (**it == a) {
441 if ((*it)->getScreen() != this)
442 return;
443 break;
444 }
445 }
446 _active = it;
447 if (it != end)
448 _last_active = it;
449
450 /* cout << "Active window is now: ";
451 if (_active == _clients.end()) cout << "None\n";
452 else cout << "0x" << hex << (*_active)->window() << dec << endl;
453 */
454 }
455
456
457 void screen::execCommand(const string &cmd) const {
458 pid_t pid;
459 if ((pid = fork()) == 0) {
460 // make the command run on the correct screen
461 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
462 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
463 perror("putenv()");
464 }
465 execl("/bin/sh", "sh", "-c", cmd.c_str(), NULL);
466 exit(-1);
467 } else if (pid == -1) {
468 cout << _epist->getApplicationName() <<
469 ": Could not fork a process for executing a command\n";
470 }
471 }
472
473
474 void screen::cycleWindow(const bool forward, const int increment,
475 const bool allscreens, const bool alldesktops,
476 const bool sameclass, const string &cn) const {
477 assert(_managed);
478 assert(increment > 0);
479
480 if (_clients.empty()) return;
481
482 string classname(cn);
483 if (sameclass && classname.empty() && _active != _clients.end())
484 classname = (*_active)->appClass();
485
486 WindowList::const_iterator target = _active,
487 begin = _clients.begin(),
488 end = _clients.end();
489
490 const XWindow *t = 0;
491
492 for (int x = 0; x < increment; ++x) {
493 while (1) {
494 if (forward) {
495 if (target == end) {
496 target = begin;
497 } else {
498 ++target;
499 }
500 } else {
501 if (target == begin)
502 target = end;
503 --target;
504 }
505
506 // must be no window to focus
507 if (target == _active)
508 return;
509
510 // start back at the beginning of the loop
511 if (target == end)
512 continue;
513
514 // determine if this window is invalid for cycling to
515 t = *target;
516 if (t->iconic()) continue;
517 if (! allscreens && t->getScreen() != this) continue;
518 if (! alldesktops && ! (t->desktop() == _active_desktop ||
519 t->desktop() == 0xffffffff)) continue;
520 if (sameclass && ! classname.empty() &&
521 t->appClass() != classname) continue;
522 if (! t->canFocus()) continue;
523
524 // found a good window so break out of the while, and perhaps continue
525 // with the for loop
526 break;
527 }
528 }
529
530 // phew. we found the window, so focus it.
531 t->focus();
532 }
533
534
535 void screen::cycleWorkspace(const bool forward, const int increment,
536 const bool loop) const {
537 assert(_managed);
538 assert(increment > 0);
539
540 unsigned int destination = _active_desktop;
541
542 for (int x = 0; x < increment; ++x) {
543 if (forward) {
544 if (destination < _num_desktops - 1)
545 ++destination;
546 else if (loop)
547 destination = 0;
548 } else {
549 if (destination > 0)
550 --destination;
551 else if (loop)
552 destination = _num_desktops - 1;
553 }
554 }
555
556 if (destination != _active_desktop)
557 changeWorkspace(destination);
558 }
559
560
561 void screen::changeWorkspace(const int num) const {
562 assert(_managed);
563
564 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
565 }
566
567 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
568
569 Display *display = _epist->getXDisplay();
570 int numlockMask, scrolllockMask;
571
572 _epist->getLockModifiers(numlockMask, scrolllockMask);
573
574 XGrabKey(display, keyCode, modifierMask,
575 _root, True, GrabModeAsync, GrabModeAsync);
576 XGrabKey(display, keyCode,
577 modifierMask|LockMask,
578 _root, True, GrabModeAsync, GrabModeAsync);
579 XGrabKey(display, keyCode,
580 modifierMask|scrolllockMask,
581 _root, True, GrabModeAsync, GrabModeAsync);
582 XGrabKey(display, keyCode,
583 modifierMask|numlockMask,
584 _root, True, GrabModeAsync, GrabModeAsync);
585
586 XGrabKey(display, keyCode,
587 modifierMask|LockMask|scrolllockMask,
588 _root, True, GrabModeAsync, GrabModeAsync);
589 XGrabKey(display, keyCode,
590 modifierMask|scrolllockMask|numlockMask,
591 _root, True, GrabModeAsync, GrabModeAsync);
592 XGrabKey(display, keyCode,
593 modifierMask|numlockMask|LockMask,
594 _root, True, GrabModeAsync, GrabModeAsync);
595
596 XGrabKey(display, keyCode,
597 modifierMask|numlockMask|LockMask|scrolllockMask,
598 _root, True, GrabModeAsync, GrabModeAsync);
599 }
600
601 void screen::ungrabKey(const KeyCode keyCode, const int modifierMask) const {
602
603 Display *display = _epist->getXDisplay();
604 int numlockMask, scrolllockMask;
605
606 _epist->getLockModifiers(numlockMask, scrolllockMask);
607
608 XUngrabKey(display, keyCode, modifierMask, _root);
609 XUngrabKey(display, keyCode, modifierMask|LockMask, _root);
610 XUngrabKey(display, keyCode, modifierMask|scrolllockMask, _root);
611 XUngrabKey(display, keyCode, modifierMask|numlockMask, _root);
612 XUngrabKey(display, keyCode, modifierMask|LockMask|scrolllockMask, _root);
613 XUngrabKey(display, keyCode, modifierMask|scrolllockMask|numlockMask, _root);
614 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask, _root);
615 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask|
616 scrolllockMask, _root);
617 }
This page took 0.068826 seconds and 5 git commands to generate.