]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/screen.cc
add --copy
[mikachu/openbox.git] / 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 /* A few comments about stacked cycling:
24  * When stacked cycling is turned on, the focused window is always at the top
25  * (front) of the list (_clients), EXCEPT for when we are in cycling mode.
26  * (_cycling is true) If we were to add the focused window to the top of the
27  * stack while we were cycling, we would end in a deadlock between 2 windows.
28  * When the modifiers are released, the window that has focus (but it's not
29  * at the top of the stack, since we are cycling) is placed at the top of the
30  * stack and raised.
31  * Hooray and Bummy. - Marius
32  */
33
34 #ifdef    HAVE_CONFIG_H
35 #  include "../../config.h"
36 #endif // HAVE_CONFIG_H
37
38 extern "C" {
39 #ifdef    HAVE_STDIO_H
40 #  include <stdio.h>
41 #endif // HAVE_STDIO_H
42
43 #ifdef    HAVE_UNISTD_H
44 #  include <sys/types.h>
45 #  include <unistd.h>
46 #endif // HAVE_UNISTD_H
47
48 #include <X11/keysym.h>
49 }
50
51 #include <iostream>
52 #include <string>
53
54 using std::cout;
55 using std::endl;
56 using std::hex;
57 using std::dec;
58 using std::string;
59
60 #include "../../src/basedisplay.hh"
61 #include "../../src/xatom.hh"
62 #include "screen.hh"
63 #include "epist.hh"
64 #include "config.hh"
65
66 screen::screen(epist *epist, int number) 
67   : _clients(epist->clientsList()), _active(epist->activeWindow()),
68     _config(epist->getConfig()), _grabbed(true), _cycling(false),
69     _stacked_cycling(false), _stacked_raise(false)
70 {
71   _epist = epist;
72   _xatom = _epist->xatom();
73   _last_active = _clients.end();
74   _number = number;
75   _info = _epist->getScreenInfo(_number);
76   _root = _info->getRootWindow();
77
78   _config->getValue(Config::stackedCycling, _stacked_cycling);
79   if (_stacked_cycling)
80     _config->getValue(Config::stackedCyclingRaise, _stacked_raise);
81
82   // find a window manager supporting NETWM, waiting for it to load if we must
83   int count = 20;  // try for 20 seconds
84   _managed = false;
85   while (! (_epist->doShutdown() || _managed || count <= 0)) {
86     if (! (_managed = findSupportingWM()))
87       sleep(1);
88     --count;
89   }
90   if (_managed)
91     cout << "Found compatible window manager '" << _wm_name << "' for screen "
92          << _number << ".\n";
93   else {
94     cout << "Unable to find a compatible window manager for screen " <<
95       _number << ".\n";
96     return;
97   }
98  
99   XSelectInput(_epist->getXDisplay(), _root, PropertyChangeMask);
100 }
101
102 screen::~screen() {
103   if (_managed)
104     XSelectInput(_epist->getXDisplay(), _root, None);
105 }
106
107
108 bool screen::findSupportingWM() {
109   Window support_win;
110   if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
111                          support_win) || support_win == None)
112     return false;
113
114   string title;
115   _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
116   _wm_name = title;
117   return true;
118 }
119
120
121 XWindow *screen::findWindow(const XEvent &e) const {
122   assert(_managed);
123
124   WindowList::const_iterator it, end = _clients.end();
125   for (it = _clients.begin(); it != end; ++it)
126     if (**it == e.xany.window)
127       break;
128   if(it == end)
129     return 0;
130   return *it;
131 }
132
133
134 void screen::processEvent(const XEvent &e) {
135   assert(_managed);
136   assert(e.xany.window == _root);
137
138   switch (e.type) {
139   case PropertyNotify:
140     // root window
141     if (e.xproperty.atom == _xatom->getAtom(XAtom::net_number_of_desktops))
142       updateNumDesktops();
143     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_current_desktop))
144       updateActiveDesktop();
145     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
146       updateActiveWindow();
147     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list)) {
148       // catch any window unmaps first
149       XEvent ev;
150       if (XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
151                                  DestroyNotify, &ev) ||
152           XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
153                                  UnmapNotify, &ev)) {
154
155         XWindow *win = _epist->findWindow(e.xany.window);
156         if (win) win->processEvent(ev);
157       }
158
159       updateClientList();
160     }
161     break;
162   case KeyPress:
163     handleKeypress(e);
164     break;
165
166   case KeyRelease:
167     handleKeyrelease(e);
168     break;
169
170   default:
171     break;
172   }
173 }
174
175 void screen::handleKeypress(const XEvent &e) {
176   int scrolllockMask, numlockMask;
177   _epist->getLockModifiers(numlockMask, scrolllockMask);
178   
179   // Mask out the lock modifiers. We want our keys to always work
180   // This should be made an option
181   unsigned int state = e.xkey.state & ~(LockMask|scrolllockMask|numlockMask);
182   keytree &ktree = _epist->getKeyTree();
183   const Action *it = ktree.getAction(e, state, this);
184
185   if (!it)
186     return;
187
188   switch (it->type()) {
189   case Action::nextScreen:
190     _epist->cycleScreen(_number, true);
191     return;
192
193   case Action::prevScreen:
194     _epist->cycleScreen(_number, false);
195     return;
196
197   case Action::nextWorkspace:
198     cycleWorkspace(true, it->number() != 0 ? it->number(): 1);
199     return;
200
201   case Action::prevWorkspace:
202     cycleWorkspace(false, it->number() != 0 ? it->number(): 1);
203     return;
204
205   case Action::nextWindow:
206     
207     cycleWindow(state, true, it->number() != 0 ? it->number(): 1);
208     return;
209
210   case Action::prevWindow:
211     cycleWindow(state, false, it->number() != 0 ? it->number(): 1);
212     return;
213
214   case Action::nextWindowOnAllWorkspaces:
215     cycleWindow(state, true, it->number() != 0 ? it->number(): 1,  false, true);
216     return;
217
218   case Action::prevWindowOnAllWorkspaces:
219     cycleWindow(state, false, it->number() != 0 ? it->number(): 1, false, true);
220     return;
221
222   case Action::nextWindowOnAllScreens:
223     cycleWindow(state, true, it->number() != 0 ? it->number(): 1, true);
224     return;
225
226   case Action::prevWindowOnAllScreens:
227     cycleWindow(state, false, it->number() != 0 ? it->number(): 1, true);
228     return;
229
230   case Action::nextWindowOfClass:
231     cycleWindow(state, true, it->number() != 0 ? it->number(): 1,
232                 false, false, true, it->string());
233     return;
234
235   case Action::prevWindowOfClass:
236     cycleWindow(state, false, it->number() != 0 ? it->number(): 1,
237                 false, false, true, it->string());
238     return;
239       
240   case Action::nextWindowOfClassOnAllWorkspaces:
241     cycleWindow(state, true, it->number() != 0 ? it->number(): 1,
242                 false, true, true, it->string());
243     return;
244       
245   case Action::prevWindowOfClassOnAllWorkspaces:
246     cycleWindow(state, false, it->number() != 0 ? it->number(): 1,
247                 false, true, true, it->string());
248     return;
249
250   case Action::changeWorkspace:
251     changeWorkspace(it->number());
252     return;
253
254   case Action::upWorkspace:
255     changeWorkspaceVert(-1);
256     return;
257
258   case Action::downWorkspace:
259     changeWorkspaceVert(1);
260     return;
261
262   case Action::leftWorkspace:
263     changeWorkspaceHorz(-1);
264     return;
265
266   case Action::rightWorkspace:
267     changeWorkspaceHorz(1);
268     return;
269
270   case Action::execute:
271     execCommand(it->string());
272     return;
273
274   case Action::showRootMenu:
275     _xatom->sendClientMessage(rootWindow(), XAtom::openbox_show_root_menu,
276                               None);
277     return;
278
279   case Action::showWorkspaceMenu:
280     _xatom->sendClientMessage(rootWindow(), XAtom::openbox_show_workspace_menu,
281                               None);
282     return;
283
284   case Action::toggleGrabs: {
285     if (_grabbed) {
286       ktree.ungrabDefaults(this);
287       _grabbed = false;
288     } else {
289       ktree.grabDefaults(this);
290       _grabbed = true;
291     }
292     return;
293   }
294
295   default:
296     break;
297   }
298
299   // these actions require an active window
300   if (_active != _clients.end()) {
301     XWindow *window = *_active;
302       
303     switch (it->type()) {
304     case Action::iconify:
305       window->iconify();
306       return;
307
308     case Action::close:
309       window->close();
310       return;
311
312     case Action::raise:
313       window->raise();
314       return;
315
316     case Action::lower:
317       window->lower();
318       return;
319
320     case Action::sendToWorkspace:
321       window->sendTo(it->number());
322       return;
323
324     case Action::toggleOmnipresent:
325       if (window->desktop() == 0xffffffff)
326         window->sendTo(_active_desktop);
327       else
328         window->sendTo(0xffffffff);
329       return;
330
331     case Action::moveWindowUp:
332       window->move(window->x(), window->y() -
333                    (it->number() != 0 ? it->number(): 1));
334       return;
335       
336     case Action::moveWindowDown:
337       window->move(window->x(), window->y() +
338                    (it->number() != 0 ? it->number(): 1));
339       return;
340       
341     case Action::moveWindowLeft:
342       window->move(window->x() - (it->number() != 0 ? it->number(): 1),
343                    window->y());
344       return;
345       
346     case Action::moveWindowRight:
347       window->move(window->x() + (it->number() != 0 ? it->number(): 1),
348                    window->y());
349       return;
350       
351     case Action::resizeWindowWidth:
352       window->resizeRel(it->number(), 0);
353       return;
354       
355     case Action::resizeWindowHeight:
356       window->resizeRel(0, it->number());
357       return;
358       
359     case Action::toggleShade:
360       window->shade(! window->shaded());
361       return;
362       
363     case Action::toggleMaximizeHorizontal:
364       window->toggleMaximize(XWindow::Max_Horz);
365       return;
366       
367     case Action::toggleMaximizeVertical:
368       window->toggleMaximize(XWindow::Max_Vert);
369       return;
370       
371     case Action::toggleMaximizeFull:
372       window->toggleMaximize(XWindow::Max_Full);
373       return;
374
375     case Action::toggleDecorations:
376       window->decorate(! window->decorated());
377       return;
378       
379     default:
380       assert(false);  // unhandled action type!
381       break;
382     }
383   }
384 }
385
386
387 void screen::handleKeyrelease(const XEvent &) {
388   // the only keyrelease event we care about (for now) is when we do stacked
389   // cycling and the modifier is released
390   if (_stacked_cycling && _cycling && nothingIsPressed()) {
391     // all modifiers have been released. ungrab the keyboard, move the
392     // focused window to the top of the Z-order and raise it
393     ungrabModifiers();
394
395     if (_active != _clients.end()) {
396       XWindow *w = *_active;
397       bool e = _last_active == _active;
398       _clients.remove(w);
399       _clients.push_front(w);
400       _active = _clients.begin();
401       if (e) _last_active = _active;
402       w->raise();
403     }
404
405     _cycling = false;
406   }
407 }
408
409
410 // do we want to add this window to our list?
411 bool screen::doAddWindow(Window window) const {
412   assert(_managed);
413
414   Atom type;
415   if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
416                          type))
417     return True;
418
419   if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
420       type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
421     return False;
422
423   return True;
424 }
425
426
427 void screen::updateEverything() {
428   updateNumDesktops();
429   updateActiveDesktop();
430   updateClientList();
431   updateActiveWindow();
432 }
433
434
435 void screen::updateNumDesktops() {
436   assert(_managed);
437
438   if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
439                          (unsigned long)_num_desktops))
440     _num_desktops = 1;  // assume that there is at least 1 desktop!
441 }
442
443
444 void screen::updateActiveDesktop() {
445   assert(_managed);
446
447   if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
448                          (unsigned long)_active_desktop))
449     _active_desktop = 0;  // there must be at least one desktop, and it must
450                           // be the current one
451 }
452
453
454 void screen::updateClientList() {
455   assert(_managed);
456
457   WindowList::iterator insert_point = _active;
458   if (insert_point != _clients.end())
459     ++insert_point; // get to the item client the focused client
460   
461   // get the client list from the root window
462   Window *rootclients = 0;
463   unsigned long num = (unsigned) -1;
464   if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
465                          &rootclients))
466     num = 0;
467
468   WindowList::iterator it;
469   const WindowList::iterator end = _clients.end();
470   unsigned long i;
471   
472   for (i = 0; i < num; ++i) {
473     for (it = _clients.begin(); it != end; ++it)
474       if (**it == rootclients[i])
475         break;
476     if (it == end) {  // didn't already exist
477       if (doAddWindow(rootclients[i])) {
478 //        cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
479         // insert new clients after the active window
480         _clients.insert(insert_point, new XWindow(_epist, this,
481                                                   rootclients[i]));
482       }
483     }
484   }
485
486   // remove clients that no longer exist (that belong to this screen)
487   for (it = _clients.begin(); it != end;) {
488     WindowList::iterator it2 = it;
489     ++it;
490
491     // is on another screen?
492     if ((*it2)->getScreen() != this)
493       continue;
494
495     for (i = 0; i < num; ++i)
496       if (**it2 == rootclients[i])
497         break;
498     if (i == num)  { // no longer exists
499       //      cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
500       // watch for the active and last-active window
501       if (it2 == _active)
502         _active = _clients.end();
503       if (it2 == _last_active)
504         _last_active = _clients.end();
505       delete *it2;
506       _clients.erase(it2);
507     }
508   }
509
510   if (rootclients) delete [] rootclients;
511 }
512
513
514 const XWindow *screen::lastActiveWindow() const {
515   if (_last_active != _clients.end())
516     return *_last_active;
517
518   // find a window if one exists
519   WindowList::const_iterator it, end = _clients.end();
520   for (it = _clients.begin(); it != end; ++it)
521     if ((*it)->getScreen() == this && ! (*it)->iconic() &&
522         (*it)->canFocus() &&
523         ((*it)->desktop() == 0xffffffff ||
524          (*it)->desktop() == _active_desktop))
525       return *it;
526
527   // no windows on this screen
528   return 0;
529 }
530
531
532 void screen::updateActiveWindow() {
533   assert(_managed);
534
535   Window a = None;
536   _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
537   
538   WindowList::iterator it, end = _clients.end();
539   for (it = _clients.begin(); it != end; ++it) {
540     if (**it == a) {
541       if ((*it)->getScreen() != this)
542         return;
543       break;
544     }
545   }
546
547   _active = it;
548
549   if (_active != end) {
550     /* if we're not cycling and a window gets focus, add it to the top of the
551      * cycle stack.
552      */
553     if (_stacked_cycling && !_cycling) {
554       XWindow *win = *_active;
555       _clients.remove(win);
556       _clients.push_front(win);
557       _active = _clients.begin();
558
559       _last_active = _active;
560     }
561   }
562
563   /*  cout << "Active window is now: ";
564       if (_active == _clients.end()) cout << "None\n";
565       else cout << "0x" << hex << (*_active)->window() << dec << endl;
566   */
567 }
568
569
570 void screen::execCommand(const string &cmd) const {
571   pid_t pid;
572   if ((pid = fork()) == 0) {
573     // disconnect the child from epist's session and the tty
574     if (setsid() == -1) {
575       cout << "warning: could not start a new process group\n";
576       perror("setsid");
577     }
578
579     // make the command run on the correct screen
580     if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
581       cout << "warning: couldn't set environment variable 'DISPLAY'\n";
582       perror("putenv()");
583     }
584     execl("/bin/sh", "sh", "-c", cmd.c_str(), NULL);
585     exit(-1);
586   } else if (pid == -1) {
587     cout << _epist->getApplicationName() <<
588       ": Could not fork a process for executing a command\n";
589   }
590 }
591
592
593 void screen::cycleWindow(unsigned int state, const bool forward,
594                          const int increment, const bool allscreens,
595                          const bool alldesktops, const bool sameclass,
596                          const string &cn)
597 {
598   assert(_managed);
599   assert(increment > 0);
600
601   if (_clients.empty()) return;
602
603   string classname(cn);
604   if (sameclass && classname.empty() && _active != _clients.end())
605     classname = (*_active)->appClass();
606
607   WindowList::const_iterator target = _active,
608     begin = _clients.begin(),
609     end = _clients.end();
610
611   XWindow *t = 0;
612   
613   for (int x = 0; x < increment; ++x) {
614     while (1) {
615       if (forward) {
616         if (target == end) {
617           target = begin;
618         } else {
619           ++target;
620         }
621       } else {
622         if (target == begin)
623           target = end;
624         --target;
625       }
626
627       // must be no window to focus
628       if (target == _active)
629         return;
630
631       // start back at the beginning of the loop
632       if (target == end)
633         continue;
634
635       // determine if this window is invalid for cycling to
636       t = *target;
637       if (t->iconic()) continue;
638       if (! allscreens && t->getScreen() != this) continue;
639       if (! alldesktops && ! (t->desktop() == _active_desktop ||
640                               t->desktop() == 0xffffffff)) continue;
641       if (sameclass && ! classname.empty() &&
642           t->appClass() != classname) continue;
643       if (! t->canFocus()) continue;
644
645       // found a good window so break out of the while, and perhaps continue
646       // with the for loop
647       break;
648     }
649   }
650
651   // phew. we found the window, so focus it.
652   if (_stacked_cycling && state) {
653     if (!_cycling) {
654       // grab modifiers so we can intercept KeyReleases from them
655       grabModifiers();
656       _cycling = true;
657     }
658
659     // if the window is on another desktop, we can't use XSetInputFocus, since
660     // it doesn't imply a workspace change.
661     if (_stacked_raise || (t->desktop() != _active_desktop &&
662                            t->desktop() != 0xffffffff))
663       t->focus(); // raise
664     else
665       t->focus(false); // don't raise
666   }  
667   else {
668     t->focus();
669   }
670 }
671
672
673 void screen::cycleWorkspace(const bool forward, const int increment,
674                             const bool loop) const {
675   assert(_managed);
676   assert(increment > 0);
677
678   unsigned int destination = _active_desktop;
679
680   for (int x = 0; x < increment; ++x) {
681     if (forward) {
682       if (destination < _num_desktops - 1)
683         ++destination;
684       else if (loop)
685         destination = 0;
686     } else {
687       if (destination > 0)
688         --destination;
689       else if (loop)
690         destination = _num_desktops - 1;
691     }
692   }
693
694   if (destination != _active_desktop) 
695     changeWorkspace(destination);
696 }
697
698
699 void screen::changeWorkspace(const int num) const {
700   assert(_managed);
701
702   _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
703 }
704
705 void screen::changeWorkspaceVert(const int num) const {
706   assert(_managed);
707   int width = 0;
708   int num_desktops = (signed)_num_desktops;
709   int active_desktop = (signed)_active_desktop;
710   int wnum = 0;
711
712   _config->getValue(Config::workspaceColumns, width);
713
714   if (width > num_desktops || width <= 0)
715     return;
716
717   // a cookie to the person that makes this pretty
718   if (num < 0) {
719     wnum = active_desktop - width;
720     if (wnum < 0) {
721       wnum = num_desktops/width * width + active_desktop;
722       if (wnum >= num_desktops)
723         wnum = num_desktops - 1;
724     }
725   }
726   else {
727     wnum = active_desktop + width;
728     if (wnum >= num_desktops) {
729       wnum = (active_desktop + width) % num_desktops - 1;
730       if (wnum < 0)
731         wnum = 0;
732     }
733   }
734   changeWorkspace(wnum);
735 }
736
737 void screen::changeWorkspaceHorz(const int num) const {
738   assert(_managed);
739   int width = 0;
740   int num_desktops = (signed)_num_desktops;
741   int active_desktop = (signed)_active_desktop;
742   int wnum = 0;
743
744   _config->getValue(Config::workspaceColumns, width);
745
746   if (width > num_desktops || width <= 0)
747     return;
748
749   if (num < 0) {
750     if (active_desktop % width != 0)
751       changeWorkspace(active_desktop - 1);
752     else {
753       wnum = active_desktop + width - 1;
754       if (wnum >= num_desktops)
755         wnum = num_desktops - 1;
756     }
757   }
758   else {
759     if (active_desktop % width != width - 1) {
760       wnum = active_desktop + 1;
761       if (wnum >= num_desktops)
762         wnum = num_desktops / width * width;
763     }
764     else
765       wnum = active_desktop - width + 1;
766   }
767   changeWorkspace(wnum);
768 }
769
770 void screen::grabKey(const KeyCode keyCode, const int modifierMask) const {
771
772   Display *display = _epist->getXDisplay();
773   int numlockMask, scrolllockMask;
774
775   _epist->getLockModifiers(numlockMask, scrolllockMask);
776
777   XGrabKey(display, keyCode, modifierMask,
778            _root, True, GrabModeAsync, GrabModeAsync);
779   XGrabKey(display, keyCode, 
780            modifierMask|LockMask,
781            _root, True, GrabModeAsync, GrabModeAsync);
782   XGrabKey(display, keyCode, 
783            modifierMask|scrolllockMask,
784            _root, True, GrabModeAsync, GrabModeAsync);
785   XGrabKey(display, keyCode, 
786            modifierMask|numlockMask,
787            _root, True, GrabModeAsync, GrabModeAsync);
788     
789   XGrabKey(display, keyCode, 
790            modifierMask|LockMask|scrolllockMask,
791            _root, True, GrabModeAsync, GrabModeAsync);
792   XGrabKey(display, keyCode, 
793            modifierMask|scrolllockMask|numlockMask,
794            _root, True, GrabModeAsync, GrabModeAsync);
795   XGrabKey(display, keyCode, 
796            modifierMask|numlockMask|LockMask,
797            _root, True, GrabModeAsync, GrabModeAsync);
798     
799   XGrabKey(display, keyCode, 
800            modifierMask|numlockMask|LockMask|scrolllockMask,
801            _root, True, GrabModeAsync, GrabModeAsync);
802 }
803
804 void screen::ungrabKey(const KeyCode keyCode, const int modifierMask) const {
805
806   Display *display = _epist->getXDisplay();
807   int numlockMask, scrolllockMask;
808
809   _epist->getLockModifiers(numlockMask, scrolllockMask);
810
811   XUngrabKey(display, keyCode, modifierMask, _root);
812   XUngrabKey(display, keyCode, modifierMask|LockMask, _root);
813   XUngrabKey(display, keyCode, modifierMask|scrolllockMask, _root);
814   XUngrabKey(display, keyCode, modifierMask|numlockMask, _root);
815   XUngrabKey(display, keyCode, modifierMask|LockMask|scrolllockMask, _root);
816   XUngrabKey(display, keyCode, modifierMask|scrolllockMask|numlockMask, _root);
817   XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask, _root);
818   XUngrabKey(display, keyCode, modifierMask|numlockMask|LockMask|
819              scrolllockMask, _root);
820 }
821
822
823 void screen::grabModifiers() const {
824   Display *display = _epist->getXDisplay();
825
826   XGrabKeyboard(display, rootWindow(), True, GrabModeAsync,
827                 GrabModeAsync, CurrentTime);
828 }
829
830
831 void screen::ungrabModifiers() const {
832   Display *display = _epist->getXDisplay();
833
834   XUngrabKeyboard(display, CurrentTime);
835 }
836
837
838 bool screen::nothingIsPressed(void) const
839 {
840   char keys[32];
841   XQueryKeymap(_epist->getXDisplay(), keys);
842
843   for (int i = 0; i < 32; ++i) {
844     if (keys[i] != 0)
845       return false;
846   }
847
848   return true;
849 }