]> Dogcows Code - chaz/openbox/blob - util/epist/screen.cc
toggleDecorations!
[chaz/openbox] / util / epist / screen.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
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 #include "config.hh"
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 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_current_desktop))
125 updateActiveDesktop();
126 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
127 updateActiveWindow();
128 else 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::upWorkspace:
226 changeWorkspaceVert(-1);
227 return;
228
229 case Action::downWorkspace:
230 changeWorkspaceVert(1);
231 return;
232
233 case Action::leftWorkspace:
234 changeWorkspaceHorz(-1);
235 return;
236
237 case Action::rightWorkspace:
238 changeWorkspaceHorz(1);
239 return;
240
241 case Action::execute:
242 execCommand(it->string());
243 return;
244
245 default:
246 break;
247 }
248
249 // these actions require an active window
250 if (_active != _clients.end()) {
251 XWindow *window = *_active;
252
253 switch (it->type()) {
254 case Action::iconify:
255 window->iconify();
256 return;
257
258 case Action::close:
259 window->close();
260 return;
261
262 case Action::raise:
263 window->raise();
264 return;
265
266 case Action::lower:
267 window->lower();
268 return;
269
270 case Action::sendToWorkspace:
271 window->sendTo(it->number());
272 return;
273
274 case Action::toggleomnipresent:
275 if (window->desktop() == 0xffffffff)
276 window->sendTo(_active_desktop);
277 else
278 window->sendTo(0xffffffff);
279 return;
280
281 case Action::moveWindowUp:
282 window->move(window->x(), window->y() -
283 (it->number() != 0 ? it->number(): 1));
284 return;
285
286 case Action::moveWindowDown:
287 window->move(window->x(), window->y() +
288 (it->number() != 0 ? it->number(): 1));
289 return;
290
291 case Action::moveWindowLeft:
292 window->move(window->x() - (it->number() != 0 ? it->number(): 1),
293 window->y());
294 return;
295
296 case Action::moveWindowRight:
297 window->move(window->x() + (it->number() != 0 ? it->number(): 1),
298 window->y());
299 return;
300
301 case Action::resizeWindowWidth:
302 window->resizeRel(it->number(), 0);
303 return;
304
305 case Action::resizeWindowHeight:
306 window->resizeRel(0, it->number());
307 return;
308
309 case Action::toggleshade:
310 window->shade(! window->shaded());
311 return;
312
313 case Action::toggleMaximizeHorizontal:
314 window->toggleMaximize(XWindow::Max_Horz);
315 return;
316
317 case Action::toggleMaximizeVertical:
318 window->toggleMaximize(XWindow::Max_Vert);
319 return;
320
321 case Action::toggleMaximizeFull:
322 window->toggleMaximize(XWindow::Max_Full);
323 return;
324
325 case Action::toggleDecorations:
326 window->decorate(! window->decorated());
327 return;
328
329 default:
330 assert(false); // unhandled action type!
331 break;
332 }
333 }
334 }
335
336 // do we want to add this window to our list?
337 bool screen::doAddWindow(Window window) const {
338 assert(_managed);
339
340 Atom type;
341 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
342 type))
343 return True;
344
345 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
346 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
347 return False;
348
349 return True;
350 }
351
352
353 void screen::updateEverything() {
354 updateNumDesktops();
355 updateActiveDesktop();
356 updateClientList();
357 updateActiveWindow();
358 }
359
360
361 void screen::updateNumDesktops() {
362 assert(_managed);
363
364 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
365 (unsigned long)_num_desktops))
366 _num_desktops = 1; // assume that there is at least 1 desktop!
367 }
368
369
370 void screen::updateActiveDesktop() {
371 assert(_managed);
372
373 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
374 (unsigned long)_active_desktop))
375 _active_desktop = 0; // there must be at least one desktop, and it must
376 // be the current one
377 }
378
379
380 void screen::updateClientList() {
381 assert(_managed);
382
383 WindowList::iterator insert_point = _active;
384 if (insert_point != _clients.end())
385 ++insert_point; // get to the item client the focused client
386
387 // get the client list from the root window
388 Window *rootclients = 0;
389 unsigned long num = (unsigned) -1;
390 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
391 &rootclients))
392 num = 0;
393
394 WindowList::iterator it;
395 const WindowList::iterator end = _clients.end();
396 unsigned long i;
397
398 // insert new clients after the active window
399 for (i = 0; i < num; ++i) {
400 for (it = _clients.begin(); it != end; ++it)
401 if (**it == rootclients[i])
402 break;
403 if (it == end) { // didn't already exist
404 if (doAddWindow(rootclients[i])) {
405 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
406 _clients.insert(insert_point, new XWindow(_epist, this,
407 rootclients[i]));
408 }
409 }
410 }
411
412 // remove clients that no longer exist (that belong to this screen)
413 for (it = _clients.begin(); it != end;) {
414 WindowList::iterator it2 = it;
415 ++it;
416
417 // is on another screen?
418 if ((*it2)->getScreen() != this)
419 continue;
420
421 for (i = 0; i < num; ++i)
422 if (**it2 == rootclients[i])
423 break;
424 if (i == num) { // no longer exists
425 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
426 // watch for the active and last-active window
427 if (it2 == _active)
428 _active = _clients.end();
429 if (it2 == _last_active)
430 _last_active = _clients.end();
431 delete *it2;
432 _clients.erase(it2);
433 }
434 }
435
436 if (rootclients) delete [] rootclients;
437 }
438
439
440 const XWindow *screen::lastActiveWindow() const {
441 if (_last_active != _clients.end())
442 return *_last_active;
443
444 // find a window if one exists
445 WindowList::const_iterator it, end = _clients.end();
446 for (it = _clients.begin(); it != end; ++it)
447 if ((*it)->getScreen() == this && ! (*it)->iconic() &&
448 ((*it)->desktop() == 0xffffffff || (*it)->desktop() == _active_desktop))
449 return *it;
450
451 // no windows on this screen
452 return 0;
453 }
454
455
456 void screen::updateActiveWindow() {
457 assert(_managed);
458
459 Window a = None;
460 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
461
462 WindowList::iterator it, end = _clients.end();
463 for (it = _clients.begin(); it != end; ++it) {
464 if (**it == a) {
465 if ((*it)->getScreen() != this)
466 return;
467 break;
468 }
469 }
470 _active = it;
471 if (it != end)
472 _last_active = it;
473
474 /* cout << "Active window is now: ";
475 if (_active == _clients.end()) cout << "None\n";
476 else cout << "0x" << hex << (*_active)->window() << dec << endl;
477 */
478 }
479
480
481 void screen::execCommand(const string &cmd) const {
482 pid_t pid;
483 if ((pid = fork()) == 0) {
484 // make the command run on the correct screen
485 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
486 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
487 perror("putenv()");
488 }
489 execl("/bin/sh", "sh", "-c", cmd.c_str(), NULL);
490 exit(-1);
491 } else if (pid == -1) {
492 cout << _epist->getApplicationName() <<
493 ": Could not fork a process for executing a command\n";
494 }
495 }
496
497
498 void screen::cycleWindow(const bool forward, const int increment,
499 const bool allscreens, const bool alldesktops,
500 const bool sameclass, const string &cn) const {
501 assert(_managed);
502 assert(increment > 0);
503
504 if (_clients.empty()) return;
505
506 string classname(cn);
507 if (sameclass && classname.empty() && _active != _clients.end())
508 classname = (*_active)->appClass();
509
510 WindowList::const_iterator target = _active,
511 begin = _clients.begin(),
512 end = _clients.end();
513
514 const XWindow *t = 0;
515
516 for (int x = 0; x < increment; ++x) {
517 while (1) {
518 if (forward) {
519 if (target == end) {
520 target = begin;
521 } else {
522 ++target;
523 }
524 } else {
525 if (target == begin)
526 target = end;
527 --target;
528 }
529
530 // must be no window to focus
531 if (target == _active)
532 return;
533
534 // start back at the beginning of the loop
535 if (target == end)
536 continue;
537
538 // determine if this window is invalid for cycling to
539 t = *target;
540 if (t->iconic()) continue;
541 if (! allscreens && t->getScreen() != this) continue;
542 if (! alldesktops && ! (t->desktop() == _active_desktop ||
543 t->desktop() == 0xffffffff)) continue;
544 if (sameclass && ! classname.empty() &&
545 t->appClass() != classname) continue;
546 if (! t->canFocus()) continue;
547
548 // found a good window so break out of the while, and perhaps continue
549 // with the for loop
550 break;
551 }
552 }
553
554 // phew. we found the window, so focus it.
555 t->focus();
556 }
557
558
559 void screen::cycleWorkspace(const bool forward, const int increment,
560 const bool loop) const {
561 assert(_managed);
562 assert(increment > 0);
563
564 unsigned int destination = _active_desktop;
565
566 for (int x = 0; x < increment; ++x) {
567 if (forward) {
568 if (destination < _num_desktops - 1)
569 ++destination;
570 else if (loop)
571 destination = 0;
572 } else {
573 if (destination > 0)
574 --destination;
575 else if (loop)
576 destination = _num_desktops - 1;
577 }
578 }
579
580 if (destination != _active_desktop)
581 changeWorkspace(destination);
582 }
583
584
585 void screen::changeWorkspace(const int num) const {
586 assert(_managed);
587
588 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
589 }
590
591 void screen::changeWorkspaceVert(const int num) const {
592 assert(_managed);
593 const Config *conf = _epist->getConfig();
594 int width = conf->getNumberValue(Config::workspaceColumns);
595
596 if (width > _num_desktops || width <= 0)
597 return;
598
599 int wnum;
600
601 // a cookie to the person that makes this pretty
602 if (num < 0) {
603 wnum = _active_desktop - width;
604 if (wnum < 0) {
605 wnum = _num_desktops/width * width + _active_desktop;
606 if (wnum >= _num_desktops)
607 wnum = _num_desktops - 1;
608 }
609 }
610 else {
611 wnum = _active_desktop + width;
612 if (wnum >= _num_desktops) {
613 wnum = (_active_desktop + width) % _num_desktops - 1;
614 if (wnum < 0)
615 wnum = 0;
616 }
617 }
618 changeWorkspace(wnum);
619 }
620
621 void screen::changeWorkspaceHorz(const int num) const {
622 assert(_managed);
623 const Config *conf = _epist->getConfig();
624 int width = conf->getNumberValue(Config::workspaceColumns);
625 int wnum;
626
627 if (width > _num_desktops || width <= 0)
628 return;
629
630 if (num < 0) {
631 if (_active_desktop % width != 0)
632 changeWorkspace(_active_desktop - 1);
633 else {
634 wnum = _active_desktop + width - 1;
635 if (wnum >= _num_desktops)
636 wnum = _num_desktops - 1;
637 }
638 }
639 else {
640 if (_active_desktop % width != width - 1) {
641 wnum = _active_desktop + 1;
642 if (wnum >= _num_desktops)
643 wnum = _num_desktops / width * width;
644 }
645 else
646 wnum = _active_desktop - width + 1;
647 }
648 changeWorkspace(wnum);
649 }
650
651 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
652
653 Display *display = _epist->getXDisplay();
654 int numlockMask, scrolllockMask;
655
656 _epist->getLockModifiers(numlockMask, scrolllockMask);
657
658 XGrabKey(display, keyCode, modifierMask,
659 _root, True, GrabModeAsync, GrabModeAsync);
660 XGrabKey(display, keyCode,
661 modifierMask|LockMask,
662 _root, True, GrabModeAsync, GrabModeAsync);
663 XGrabKey(display, keyCode,
664 modifierMask|scrolllockMask,
665 _root, True, GrabModeAsync, GrabModeAsync);
666 XGrabKey(display, keyCode,
667 modifierMask|numlockMask,
668 _root, True, GrabModeAsync, GrabModeAsync);
669
670 XGrabKey(display, keyCode,
671 modifierMask|LockMask|scrolllockMask,
672 _root, True, GrabModeAsync, GrabModeAsync);
673 XGrabKey(display, keyCode,
674 modifierMask|scrolllockMask|numlockMask,
675 _root, True, GrabModeAsync, GrabModeAsync);
676 XGrabKey(display, keyCode,
677 modifierMask|numlockMask|LockMask,
678 _root, True, GrabModeAsync, GrabModeAsync);
679
680 XGrabKey(display, keyCode,
681 modifierMask|numlockMask|LockMask|scrolllockMask,
682 _root, True, GrabModeAsync, GrabModeAsync);
683 }
684
685 void screen::ungrabKey(const KeyCode keyCode, const int modifierMask) const {
686
687 Display *display = _epist->getXDisplay();
688 int numlockMask, scrolllockMask;
689
690 _epist->getLockModifiers(numlockMask, scrolllockMask);
691
692 XUngrabKey(display, keyCode, modifierMask, _root);
693 XUngrabKey(display, keyCode, modifierMask|LockMask, _root);
694 XUngrabKey(display, keyCode, modifierMask|scrolllockMask, _root);
695 XUngrabKey(display, keyCode, modifierMask|numlockMask, _root);
696 XUngrabKey(display, keyCode, modifierMask|LockMask|scrolllockMask, _root);
697 XUngrabKey(display, keyCode, modifierMask|scrolllockMask|numlockMask, _root);
698 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask, _root);
699 XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask|
700 scrolllockMask, _root);
701 }
This page took 0.066633 seconds and 5 git commands to generate.