]> Dogcows Code - chaz/openbox/blob - util/epist/screen.cc
dont focus more than one window when cycling!
[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_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
150 ActionList::const_iterator it = _epist->actions().begin();
151 ActionList::const_iterator end = _epist->actions().end();
152
153 _epist->getLockModifiers(numlockMask, scrolllockMask);
154
155 for (; it != end; ++it) {
156 unsigned int state = e.xkey.state & ~(LockMask|scrolllockMask|numlockMask);
157
158 if (e.xkey.keycode == it->keycode() &&
159 state == it->modifierMask()) {
160 switch (it->type()) {
161 case Action::nextScreen:
162 _epist->cycleScreen(_number, true);
163 return;
164
165 case Action::prevScreen:
166 _epist->cycleScreen(_number, false);
167 return;
168
169 case Action::nextWorkspace:
170 cycleWorkspace(true);
171 return;
172
173 case Action::prevWorkspace:
174 cycleWorkspace(false);
175 return;
176
177 case Action::nextWindow:
178 cycleWindow(true);
179 return;
180
181 case Action::prevWindow:
182 cycleWindow(false);
183 return;
184
185 case Action::nextWindowOnAllWorkspaces:
186 cycleWindow(true, false, true);
187 return;
188
189 case Action::prevWindowOnAllWorkspaces:
190 cycleWindow(false, false, true);
191 return;
192
193 case Action::nextWindowOnAllScreens:
194 cycleWindow(true, true);
195 return;
196
197 case Action::prevWindowOnAllScreens:
198 cycleWindow(false, true);
199 return;
200
201 case Action::nextWindowOfClass:
202 cycleWindow(true, false, false, true, it->string());
203 return;
204
205 case Action::prevWindowOfClass:
206 cycleWindow(false, false, false, true, it->string());
207 return;
208
209 case Action::nextWindowOfClassOnAllWorkspaces:
210 cycleWindow(true, false, true, true, it->string());
211 return;
212
213 case Action::prevWindowOfClassOnAllWorkspaces:
214 cycleWindow(false, false, true, true, it->string());
215 return;
216
217 case Action::changeWorkspace:
218 changeWorkspace(it->number());
219 return;
220
221 case Action::execute:
222 execCommand(it->string());
223 return;
224
225 default:
226 break;
227 }
228
229 // these actions require an active window
230 if (_active != _clients.end()) {
231 XWindow *window = *_active;
232
233 switch (it->type()) {
234 case Action::iconify:
235 window->iconify();
236 return;
237
238 case Action::close:
239 window->close();
240 return;
241
242 case Action::raise:
243 window->raise();
244 return;
245
246 case Action::lower:
247 window->lower();
248 return;
249
250 case Action::sendToWorkspace:
251 window->sendTo(it->number());
252 return;
253
254 case Action::toggleomnipresent:
255 if (window->desktop() == 0xffffffff)
256 window->sendTo(_active_desktop);
257 else
258 window->sendTo(0xffffffff);
259 return;
260
261 case Action::moveWindowUp:
262 window->move(window->x(), window->y() - it->number());
263 return;
264
265 case Action::moveWindowDown:
266 window->move(window->x(), window->y() + it->number());
267 return;
268
269 case Action::moveWindowLeft:
270 window->move(window->x() - it->number(), window->y());
271 return;
272
273 case Action::moveWindowRight:
274 window->move(window->x() + it->number(), window->y());
275 return;
276
277 case Action::resizeWindowWidth:
278 window->resize(window->width() + it->number(), window->height());
279 return;
280
281 case Action::resizeWindowHeight:
282 window->resize(window->width(), window->height() + it->number());
283 return;
284
285 case Action::toggleshade:
286 window->shade(! window->shaded());
287 return;
288
289 case Action::toggleMaximizeHorizontal:
290 window->toggleMaximize(XWindow::Max_Horz);
291 return;
292
293 case Action::toggleMaximizeVertical:
294 window->toggleMaximize(XWindow::Max_Vert);
295 return;
296
297 case Action::toggleMaximizeFull:
298 window->toggleMaximize(XWindow::Max_Full);
299 return;
300
301 default:
302 assert(false); // unhandled action type!
303 break;
304 }
305 }
306 }
307 }
308 }
309
310 // do we want to add this window to our list?
311 bool screen::doAddWindow(Window window) const {
312 assert(_managed);
313
314 Atom type;
315 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
316 type))
317 return True;
318
319 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
320 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
321 return False;
322
323 return True;
324 }
325
326
327 void screen::updateEverything() {
328 updateNumDesktops();
329 updateActiveDesktop();
330 updateClientList();
331 updateActiveWindow();
332 }
333
334
335 void screen::updateNumDesktops() {
336 assert(_managed);
337
338 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
339 (unsigned long)_num_desktops))
340 _num_desktops = 1; // assume that there is at least 1 desktop!
341 }
342
343
344 void screen::updateActiveDesktop() {
345 assert(_managed);
346
347 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
348 (unsigned long)_active_desktop))
349 _active_desktop = 0; // there must be at least one desktop, and it must
350 // be the current one
351 }
352
353
354 void screen::updateClientList() {
355 assert(_managed);
356
357 WindowList::iterator insert_point = _active;
358 if (insert_point != _clients.end())
359 ++insert_point; // get to the item client the focused client
360
361 // get the client list from the root window
362 Window *rootclients = 0;
363 unsigned long num = (unsigned) -1;
364 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
365 &rootclients))
366 num = 0;
367
368 WindowList::iterator it;
369 const WindowList::iterator end = _clients.end();
370 unsigned long i;
371
372 // insert new clients after the active window
373 for (i = 0; i < num; ++i) {
374 for (it = _clients.begin(); it != end; ++it)
375 if (**it == rootclients[i])
376 break;
377 if (it == end) { // didn't already exist
378 if (doAddWindow(rootclients[i])) {
379 // cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
380 _clients.insert(insert_point, new XWindow(_epist, this,
381 rootclients[i]));
382 }
383 }
384 }
385
386 // remove clients that no longer exist (that belong to this screen)
387 for (it = _clients.begin(); it != end;) {
388 WindowList::iterator it2 = it;
389 ++it;
390
391 // is on another screen?
392 if ((*it2)->getScreen() != this)
393 continue;
394
395 for (i = 0; i < num; ++i)
396 if (**it2 == rootclients[i])
397 break;
398 if (i == num) { // no longer exists
399 // cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
400 // watch for the active and last-active window
401 if (it2 == _active)
402 _active = _clients.end();
403 if (it2 == _last_active)
404 _last_active = _clients.end();
405 delete *it2;
406 _clients.erase(it2);
407 }
408 }
409
410 if (rootclients) delete [] rootclients;
411 }
412
413
414 const XWindow *screen::lastActiveWindow() const {
415 if (_last_active != _clients.end())
416 return *_last_active;
417
418 // find a window if one exists
419 WindowList::const_iterator it, end = _clients.end();
420 for (it = _clients.begin(); it != end; ++it)
421 if ((*it)->getScreen() == this && ! (*it)->iconic() &&
422 ((*it)->desktop() == 0xffffffff || (*it)->desktop() == _active_desktop))
423 return *it;
424
425 // no windows on this screen
426 return 0;
427 }
428
429
430 void screen::updateActiveWindow() {
431 assert(_managed);
432
433 Window a = None;
434 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
435
436 WindowList::iterator it, end = _clients.end();
437 for (it = _clients.begin(); it != end; ++it) {
438 if (**it == a) {
439 if ((*it)->getScreen() != this)
440 return;
441 break;
442 }
443 }
444 _active = it;
445 if (it != end)
446 _last_active = it;
447
448 /* cout << "Active window is now: ";
449 if (_active == _clients.end()) cout << "None\n";
450 else cout << "0x" << hex << (*_active)->window() << dec << endl;
451 */
452 }
453
454
455 void screen::execCommand(const string &cmd) const {
456 pid_t pid;
457 if ((pid = fork()) == 0) {
458 extern char **environ;
459
460 char *const argv[] = {
461 "sh",
462 "-c",
463 const_cast<char *>(cmd.c_str()),
464 0
465 };
466 // make the command run on the correct screen
467 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
468 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
469 perror("putenv()");
470 }
471 execve("/bin/sh", argv, environ);
472 exit(127);
473 } else if (pid == -1) {
474 cout << _epist->getApplicationName() <<
475 ": Could not fork a process for executing a command\n";
476 }
477 }
478
479
480 void screen::cycleWindow(const bool forward, const bool allscreens,
481 const bool alldesktops, const bool sameclass,
482 const string &cn) const {
483 assert(_managed);
484
485 if (_clients.empty()) return;
486
487 string classname(cn);
488 if (sameclass && classname.empty() && _active != _clients.end())
489 classname = (*_active)->appClass();
490
491 WindowList::const_iterator target = _active,
492 begin = _clients.begin(),
493 end = _clients.end();
494
495 while (1) {
496 if (forward) {
497 if (target == end) {
498 target = begin;
499 } else {
500 ++target;
501 if (target == end)
502 target = begin;
503 }
504 } else {
505 if (target == begin)
506 target = end;
507 --target;
508 }
509
510 // must be no window to focus
511 if (target == _active)
512 return;
513
514 // determine if this window is invalid for cycling to
515 const XWindow *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!
525 t->focus();
526 return;
527 }
528 }
529
530
531 void screen::cycleWorkspace(const bool forward, const bool loop) const {
532 assert(_managed);
533
534 unsigned int destination = _active_desktop;
535
536 if (forward) {
537 if (destination < _num_desktops - 1)
538 ++destination;
539 else if (loop)
540 destination = 0;
541 } else {
542 if (destination > 0)
543 --destination;
544 else if (loop)
545 destination = _num_desktops - 1;
546 }
547
548 if (destination != _active_desktop)
549 changeWorkspace(destination);
550 }
551
552
553 void screen::changeWorkspace(const int num) const {
554 assert(_managed);
555
556 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
557 }
558
559 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
560
561 Display *display = _epist->getXDisplay();
562 int numlockMask, scrolllockMask;
563
564 _epist->getLockModifiers(numlockMask, scrolllockMask);
565
566 XGrabKey(display, keyCode, modifierMask,
567 _root, True, GrabModeAsync, GrabModeAsync);
568 XGrabKey(display, keyCode,
569 modifierMask|LockMask,
570 _root, True, GrabModeAsync, GrabModeAsync);
571 XGrabKey(display, keyCode,
572 modifierMask|scrolllockMask,
573 _root, True, GrabModeAsync, GrabModeAsync);
574 XGrabKey(display, keyCode,
575 modifierMask|numlockMask,
576 _root, True, GrabModeAsync, GrabModeAsync);
577
578 XGrabKey(display, keyCode,
579 modifierMask|LockMask|scrolllockMask,
580 _root, True, GrabModeAsync, GrabModeAsync);
581 XGrabKey(display, keyCode,
582 modifierMask|scrolllockMask|numlockMask,
583 _root, True, GrabModeAsync, GrabModeAsync);
584 XGrabKey(display, keyCode,
585 modifierMask|numlockMask|LockMask,
586 _root, True, GrabModeAsync, GrabModeAsync);
587
588 XGrabKey(display, keyCode,
589 modifierMask|numlockMask|LockMask|scrolllockMask,
590 _root, True, GrabModeAsync, GrabModeAsync);
591 }
This page took 0.067371 seconds and 5 git commands to generate.