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