]> icculus.org git repositories - dana/openbox.git/blob - src/blackbox.cc
new checkmark for the menus
[dana/openbox.git] / src / blackbox.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // blackbox.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef    HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #include <X11/Xatom.h>
32 #include <X11/cursorfont.h>
33 #include <X11/keysym.h>
34
35 #ifdef    SHAPE
36 #include <X11/extensions/shape.h>
37 #endif // SHAPE
38
39 #ifdef    HAVE_STDIO_H
40 #  include <stdio.h>
41 #endif // HAVE_STDIO_H
42
43 #ifdef HAVE_STDLIB_H
44 #  include <stdlib.h>
45 #endif // HAVE_STDLIB_H
46
47 #ifdef HAVE_STRING_H
48 #  include <string.h>
49 #endif // HAVE_STRING_H
50
51 #ifdef    HAVE_UNISTD_H
52 #  include <sys/types.h>
53 #  include <unistd.h>
54 #endif // HAVE_UNISTD_H
55
56 #ifdef    HAVE_SYS_PARAM_H
57 #  include <sys/param.h>
58 #endif // HAVE_SYS_PARAM_H
59
60 #ifdef    HAVE_SYS_SELECT_H
61 #  include <sys/select.h>
62 #endif // HAVE_SYS_SELECT_H
63
64 #ifdef    HAVE_SIGNAL_H
65 #  include <signal.h>
66 #endif // HAVE_SIGNAL_H
67
68 #ifdef    HAVE_SYS_SIGNAL_H
69 #  include <sys/signal.h>
70 #endif // HAVE_SYS_SIGNAL_H
71
72 #ifdef    HAVE_SYS_STAT_H
73 #  include <sys/types.h>
74 #  include <sys/stat.h>
75 #endif // HAVE_SYS_STAT_H
76
77 #ifdef    TIME_WITH_SYS_TIME
78 #  include <sys/time.h>
79 #  include <time.h>
80 #else // !TIME_WITH_SYS_TIME
81 #  ifdef    HAVE_SYS_TIME_H
82 #    include <sys/time.h>
83 #  else // !HAVE_SYS_TIME_H
84 #    include <time.h>
85 #  endif // HAVE_SYS_TIME_H
86 #endif // TIME_WITH_SYS_TIME
87
88 #ifdef    HAVE_LIBGEN_H
89 #  include <libgen.h>
90 #endif // HAVE_LIBGEN_H
91 }
92
93 #include <assert.h>
94
95 #include <algorithm>
96 #include <string>
97 using std::string;
98
99 #include "i18n.hh"
100 #include "blackbox.hh"
101 #include "Basemenu.hh"
102 #include "Clientmenu.hh"
103 #include "GCCache.hh"
104 #include "Image.hh"
105 #include "Rootmenu.hh"
106 #include "Screen.hh"
107 #include "Slit.hh"
108 #include "Toolbar.hh"
109 #include "Util.hh"
110 #include "Window.hh"
111 #include "Workspace.hh"
112 #include "Workspacemenu.hh"
113 #include "XAtom.hh"
114
115 // X event scanner for enter/leave notifies - adapted from twm
116 struct scanargs {
117   Window w;
118   bool leave, inferior, enter;
119 };
120
121 static Bool queueScanner(Display *, XEvent *e, char *args) {
122   scanargs *scan = (scanargs *) args;
123   if ((e->type == LeaveNotify) &&
124       (e->xcrossing.window == scan->w) &&
125       (e->xcrossing.mode == NotifyNormal)) {
126     scan->leave = True;
127     scan->inferior = (e->xcrossing.detail == NotifyInferior);
128   } else if ((e->type == EnterNotify) && (e->xcrossing.mode == NotifyUngrab)) {
129     scan->enter = True;
130   }
131
132   return False;
133 }
134
135 Blackbox *blackbox;
136
137
138 Blackbox::Blackbox(char **m_argv, char *dpy_name, char *rc, char *menu)
139   : BaseDisplay(m_argv[0], dpy_name) {
140   if (! XSupportsLocale())
141     fprintf(stderr, "X server does not support locale\n");
142
143   if (XSetLocaleModifiers("") == NULL)
144     fprintf(stderr, "cannot set locale modifiers\n");
145
146   ::blackbox = this;
147   argv = m_argv;
148
149   // try to make sure the ~/.openbox directory exists
150   mkdir(expandTilde("~/.openbox").c_str(), S_IREAD | S_IWRITE | S_IEXEC |
151                                            S_IRGRP | S_IWGRP | S_IXGRP |
152                                            S_IROTH | S_IWOTH | S_IXOTH);
153   
154   if (! rc) rc = "~/.openbox/rc";
155   rc_file = expandTilde(rc);
156   config.setFile(rc_file);  
157   if (! menu) menu = "~/.openbox/menu";
158   menu_file = expandTilde(menu);
159
160   no_focus = False;
161
162   resource.auto_raise_delay.tv_sec = resource.auto_raise_delay.tv_usec = 0;
163
164   active_screen = 0;
165   focused_window = changing_window = (BlackboxWindow *) 0;
166
167   load_rc();
168
169   xatom = new XAtom(getXDisplay());
170
171   cursor.session = XCreateFontCursor(getXDisplay(), XC_left_ptr);
172   cursor.move = XCreateFontCursor(getXDisplay(), XC_fleur);
173   cursor.ll_angle = XCreateFontCursor(getXDisplay(), XC_ll_angle);
174   cursor.lr_angle = XCreateFontCursor(getXDisplay(), XC_lr_angle);
175   cursor.ul_angle = XCreateFontCursor(getXDisplay(), XC_ul_angle);
176   cursor.ur_angle = XCreateFontCursor(getXDisplay(), XC_ur_angle);
177
178   for (unsigned int i = 0; i < getNumberOfScreens(); i++) {
179     BScreen *screen = new BScreen(this, i);
180
181     if (! screen->isScreenManaged()) {
182       delete screen;
183       continue;
184     }
185
186     screenList.push_back(screen);
187   }
188
189   if (screenList.empty()) {
190     fprintf(stderr,
191             i18n(blackboxSet, blackboxNoManagableScreens,
192               "Blackbox::Blackbox: no managable screens found, aborting.\n"));
193     ::exit(3);
194   }
195
196   // save current settings and default values
197   save_rc();
198
199   // set the screen with mouse to the first managed screen
200   active_screen = screenList.front();
201   setFocusedWindow(0);
202
203   XSynchronize(getXDisplay(), False);
204   XSync(getXDisplay(), False);
205
206   reconfigure_wait = reread_menu_wait = False;
207
208   timer = new BTimer(this, this);
209   timer->setTimeout(0l);
210 }
211
212
213 Blackbox::~Blackbox(void) {
214   std::for_each(screenList.begin(), screenList.end(), PointerAssassin());
215
216   std::for_each(menuTimestamps.begin(), menuTimestamps.end(),
217                 PointerAssassin());
218
219   delete xatom;
220
221   delete timer;
222 }
223
224
225 void Blackbox::process_event(XEvent *e) {
226   switch (e->type) {
227   case ButtonPress: {
228     // strip the lock key modifiers
229     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
230
231     last_time = e->xbutton.time;
232
233     BlackboxWindow *win = (BlackboxWindow *) 0;
234     Basemenu *menu = (Basemenu *) 0;
235     Slit *slit = (Slit *) 0;
236     Toolbar *tbar = (Toolbar *) 0;
237     BScreen *scrn = (BScreen *) 0;
238
239     if ((win = searchWindow(e->xbutton.window))) {
240       win->buttonPressEvent(&e->xbutton);
241
242       /* XXX: is this sane on low colour desktops? */
243       if (e->xbutton.button == 1)
244         win->installColormap(True);
245     } else if ((menu = searchMenu(e->xbutton.window))) {
246       menu->buttonPressEvent(&e->xbutton);
247     } else if ((slit = searchSlit(e->xbutton.window))) {
248       slit->buttonPressEvent(&e->xbutton);
249     } else if ((tbar = searchToolbar(e->xbutton.window))) {
250       tbar->buttonPressEvent(&e->xbutton);
251     } else if ((scrn = searchScreen(e->xbutton.window))) {
252       scrn->buttonPressEvent(&e->xbutton);
253       if (active_screen != scrn) {
254         active_screen = scrn;
255         // first, set no focus window on the old screen
256         setFocusedWindow(0);
257         // and move focus to this screen
258         setFocusedWindow(0);
259       }
260     }
261     break;
262   }
263
264   case ButtonRelease: {
265     // strip the lock key modifiers
266     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
267
268     last_time = e->xbutton.time;
269
270     BlackboxWindow *win = (BlackboxWindow *) 0;
271     Basemenu *menu = (Basemenu *) 0;
272     Toolbar *tbar = (Toolbar *) 0;
273
274     if ((win = searchWindow(e->xbutton.window)))
275       win->buttonReleaseEvent(&e->xbutton);
276     else if ((menu = searchMenu(e->xbutton.window)))
277       menu->buttonReleaseEvent(&e->xbutton);
278     else if ((tbar = searchToolbar(e->xbutton.window)))
279       tbar->buttonReleaseEvent(&e->xbutton);
280
281     break;
282   }
283
284   case ConfigureRequest: {
285     // compress configure requests...
286     XEvent realevent;
287     unsigned int i = 0;
288     while(XCheckTypedWindowEvent(getXDisplay(), e->xconfigurerequest.window,
289                                  ConfigureRequest, &realevent)) {
290       i++;
291     }
292     if ( i > 0 )
293       e = &realevent;
294
295     BlackboxWindow *win = (BlackboxWindow *) 0;
296     Slit *slit = (Slit *) 0;
297
298     if ((win = searchWindow(e->xconfigurerequest.window))) {
299       win->configureRequestEvent(&e->xconfigurerequest);
300     } else if ((slit = searchSlit(e->xconfigurerequest.window))) {
301       slit->configureRequestEvent(&e->xconfigurerequest);
302     } else {
303       if (validateWindow(e->xconfigurerequest.window)) {
304         XWindowChanges xwc;
305
306         xwc.x = e->xconfigurerequest.x;
307         xwc.y = e->xconfigurerequest.y;
308         xwc.width = e->xconfigurerequest.width;
309         xwc.height = e->xconfigurerequest.height;
310         xwc.border_width = e->xconfigurerequest.border_width;
311         xwc.sibling = e->xconfigurerequest.above;
312         xwc.stack_mode = e->xconfigurerequest.detail;
313
314         XConfigureWindow(getXDisplay(), e->xconfigurerequest.window,
315                          e->xconfigurerequest.value_mask, &xwc);
316       }
317     }
318
319     break;
320   }
321
322   case MapRequest: {
323 #ifdef    DEBUG
324     fprintf(stderr, "Blackbox::process_event(): MapRequest for 0x%lx\n",
325             e->xmaprequest.window);
326 #endif // DEBUG
327
328     BlackboxWindow *win = searchWindow(e->xmaprequest.window);
329
330     if (win) {
331       bool focus = False;
332       if (win->isIconic()) {
333         win->deiconify();
334         focus = True;
335       }
336       if (win->isShaded()) {
337         win->shade();
338         focus = True;
339       }
340
341       if (focus && (win->isTransient() || win->getScreen()->doFocusNew()))
342         win->setInputFocus();
343     } else {
344       BScreen *screen = searchScreen(e->xmaprequest.parent);
345
346       if (! screen) {
347         /*
348           we got a map request for a window who's parent isn't root. this
349           can happen in only one circumstance:
350
351             a client window unmapped a managed window, and then remapped it
352             somewhere between unmapping the client window and reparenting it
353             to root.
354
355           regardless of how it happens, we need to find the screen that
356           the window is on
357         */
358         XWindowAttributes wattrib;
359         if (! XGetWindowAttributes(getXDisplay(), e->xmaprequest.window,
360                                    &wattrib)) {
361           // failed to get the window attributes, perhaps the window has
362           // now been destroyed?
363           break;
364         }
365
366         screen = searchScreen(wattrib.root);
367         assert(screen != 0); // this should never happen
368       }
369
370       screen->manageWindow(e->xmaprequest.window);
371     }
372
373     break;
374   }
375
376   case UnmapNotify: {
377     BlackboxWindow *win = (BlackboxWindow *) 0;
378     Slit *slit = (Slit *) 0;
379     BScreen *screen = (BScreen *) 0;
380
381     if ((win = searchWindow(e->xunmap.window))) {
382       win->unmapNotifyEvent(&e->xunmap);
383     } else if ((slit = searchSlit(e->xunmap.window))) {
384       slit->unmapNotifyEvent(&e->xunmap);
385     } else if ((screen = searchSystrayWindow(e->xunmap.window))) {
386       screen->removeSystrayWindow(e->xunmap.window);
387     }
388
389     break;
390   }
391
392   case DestroyNotify: {
393     BlackboxWindow *win = (BlackboxWindow *) 0;
394     Slit *slit = (Slit *) 0;
395     BScreen *screen = (BScreen *) 0;
396     BWindowGroup *group = (BWindowGroup *) 0;
397
398     if ((win = searchWindow(e->xdestroywindow.window))) {
399       win->destroyNotifyEvent(&e->xdestroywindow);
400     } else if ((slit = searchSlit(e->xdestroywindow.window))) {
401       slit->removeClient(e->xdestroywindow.window, False);
402     } else if ((group = searchGroup(e->xdestroywindow.window))) {
403       delete group;
404     } else if ((screen = searchSystrayWindow(e->xunmap.window))) {
405       screen->removeSystrayWindow(e->xunmap.window);
406     }
407
408     break;
409   }
410
411   case ReparentNotify: {
412     /*
413       this event is quite rare and is usually handled in unmapNotify
414       however, if the window is unmapped when the reparent event occurs
415       the window manager never sees it because an unmap event is not sent
416       to an already unmapped window.
417     */
418     BlackboxWindow *win = searchWindow(e->xreparent.window);
419     if (win) {
420       win->reparentNotifyEvent(&e->xreparent);
421     } else {
422       Slit *slit = searchSlit(e->xreparent.window);
423       if (slit && slit->getWindowID() != e->xreparent.parent)
424         slit->removeClient(e->xreparent.window, True);
425     }
426     break;
427   }
428
429   case MotionNotify: {
430     // motion notify compression...
431     XEvent realevent;
432     unsigned int i = 0;
433     while (XCheckTypedWindowEvent(getXDisplay(), e->xmotion.window,
434                                   MotionNotify, &realevent)) {
435       i++;
436     }
437
438     // if we have compressed some motion events, use the last one
439     if ( i > 0 )
440       e = &realevent;
441
442     // strip the lock key modifiers
443     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
444
445     last_time = e->xmotion.time;
446
447     BlackboxWindow *win = (BlackboxWindow *) 0;
448     Basemenu *menu = (Basemenu *) 0;
449
450     if ((win = searchWindow(e->xmotion.window)))
451       win->motionNotifyEvent(&e->xmotion);
452     else if ((menu = searchMenu(e->xmotion.window)))
453       menu->motionNotifyEvent(&e->xmotion);
454
455     break;
456   }
457
458   case PropertyNotify: {
459     last_time = e->xproperty.time;
460
461     BlackboxWindow *win = (BlackboxWindow *) 0;
462     BScreen *screen = (BScreen *) 0;
463
464     if ((win = searchWindow(e->xproperty.window)))
465       win->propertyNotifyEvent(&e->xproperty);
466     else if ((screen = searchScreen(e->xproperty.window)))
467       screen->propertyNotifyEvent(&e->xproperty);
468     break;
469   }
470
471   case EnterNotify: {
472     last_time = e->xcrossing.time;
473
474     BScreen *screen = (BScreen *) 0;
475     BlackboxWindow *win = (BlackboxWindow *) 0;
476     Basemenu *menu = (Basemenu *) 0;
477     Toolbar *tbar = (Toolbar *) 0;
478     Slit *slit = (Slit *) 0;
479
480     if (e->xcrossing.mode == NotifyGrab) break;
481
482     XEvent dummy;
483     scanargs sa;
484     sa.w = e->xcrossing.window;
485     sa.enter = sa.leave = False;
486     XCheckIfEvent(getXDisplay(), &dummy, queueScanner, (char *) &sa);
487
488     if ((e->xcrossing.window == e->xcrossing.root) &&
489         (screen = searchScreen(e->xcrossing.window))) {
490       screen->getImageControl()->installRootColormap();
491     } else if ((win = searchWindow(e->xcrossing.window))) {
492       if (win->getScreen()->isSloppyFocus() &&
493           (! win->isFocused()) && (! no_focus) &&
494           win->isNormal()) {  // don't focus non-normal windows with mouseover
495         if (((! sa.leave) || sa.inferior) && win->isVisible()) {
496           if (win->setInputFocus())
497             win->installColormap(True); // XXX: shouldnt we honour no install?
498         }
499       }
500     } else if ((menu = searchMenu(e->xcrossing.window))) {
501       menu->enterNotifyEvent(&e->xcrossing);
502     } else if ((tbar = searchToolbar(e->xcrossing.window))) {
503       tbar->enterNotifyEvent(&e->xcrossing);
504     } else if ((slit = searchSlit(e->xcrossing.window))) {
505       slit->enterNotifyEvent(&e->xcrossing);
506     }
507     break;
508   }
509
510   case LeaveNotify: {
511     last_time = e->xcrossing.time;
512
513     BlackboxWindow *win = (BlackboxWindow *) 0;
514     Basemenu *menu = (Basemenu *) 0;
515     Toolbar *tbar = (Toolbar *) 0;
516     Slit *slit = (Slit *) 0;
517
518     if ((menu = searchMenu(e->xcrossing.window)))
519       menu->leaveNotifyEvent(&e->xcrossing);
520     else if ((win = searchWindow(e->xcrossing.window)))
521       win->installColormap(False);
522     else if ((tbar = searchToolbar(e->xcrossing.window)))
523       tbar->leaveNotifyEvent(&e->xcrossing);
524     else if ((slit = searchSlit(e->xcrossing.window)))
525       slit->leaveNotifyEvent(&e->xcrossing);
526     break;
527   }
528
529   case Expose: {
530     // compress expose events
531     XEvent realevent;
532     unsigned int i = 0;
533     int ex1, ey1, ex2, ey2;
534     ex1 = e->xexpose.x;
535     ey1 = e->xexpose.y;
536     ex2 = ex1 + e->xexpose.width - 1;
537     ey2 = ey1 + e->xexpose.height - 1;
538     while (XCheckTypedWindowEvent(getXDisplay(), e->xexpose.window,
539                                   Expose, &realevent)) {
540       i++;
541
542       // merge expose area
543       ex1 = std::min(realevent.xexpose.x, ex1);
544       ey1 = std::min(realevent.xexpose.y, ey1);
545       ex2 = std::max(realevent.xexpose.x + realevent.xexpose.width - 1, ex2);
546       ey2 = std::max(realevent.xexpose.y + realevent.xexpose.height - 1, ey2);
547     }
548     if ( i > 0 )
549       e = &realevent;
550
551     // use the merged area
552     e->xexpose.x = ex1;
553     e->xexpose.y = ey1;
554     e->xexpose.width = ex2 - ex1 + 1;
555     e->xexpose.height = ey2 - ey1 + 1;
556
557     BlackboxWindow *win = (BlackboxWindow *) 0;
558     Basemenu *menu = (Basemenu *) 0;
559     Toolbar *tbar = (Toolbar *) 0;
560
561     if ((win = searchWindow(e->xexpose.window)))
562       win->exposeEvent(&e->xexpose);
563     else if ((menu = searchMenu(e->xexpose.window)))
564       menu->exposeEvent(&e->xexpose);
565     else if ((tbar = searchToolbar(e->xexpose.window)))
566       tbar->exposeEvent(&e->xexpose);
567
568     break;
569   }
570
571   case KeyPress: {
572     Toolbar *tbar = searchToolbar(e->xkey.window);
573
574     if (tbar && tbar->isEditing())
575       tbar->keyPressEvent(&e->xkey);
576
577     break;
578   }
579
580   case ColormapNotify: {
581     BScreen *screen = searchScreen(e->xcolormap.window);
582
583     if (screen)
584       screen->setRootColormapInstalled((e->xcolormap.state ==
585                                         ColormapInstalled) ? True : False);
586
587     break;
588   }
589
590   case FocusIn: {
591     if (e->xfocus.detail != NotifyNonlinear &&
592         e->xfocus.detail != NotifyAncestor) {
593       /*
594         don't process FocusIns when:
595         1. the new focus window isn't an ancestor or inferior of the old
596         focus window (NotifyNonlinear)
597         make sure to allow the FocusIn when the old focus window was an
598         ancestor but didn't have a parent, such as root (NotifyAncestor)
599       */
600       break;
601     }
602
603     BlackboxWindow *win = searchWindow(e->xfocus.window);
604     if (win) {
605       if (! win->isFocused())
606         win->setFocusFlag(True);
607
608       /*
609         set the event window to None.  when the FocusOut event handler calls
610         this function recursively, it uses this as an indication that focus
611         has moved to a known window.
612       */
613       e->xfocus.window = None;
614     }
615
616     break;
617   }
618
619   case FocusOut: {
620     if (e->xfocus.detail != NotifyNonlinear) {
621       /*
622         don't process FocusOuts when:
623         2. the new focus window isn't an ancestor or inferior of the old
624         focus window (NotifyNonlinear)
625       */
626       break;
627     }
628
629     BlackboxWindow *win = searchWindow(e->xfocus.window);
630     if (win && win->isFocused()) {
631       /*
632         before we mark "win" as unfocused, we need to verify that focus is
633         going to a known location, is in a known location, or set focus
634         to a known location.
635       */
636
637       XEvent event;
638       // don't check the current focus if FocusOut was generated during a grab
639       bool check_focus = (e->xfocus.mode == NotifyNormal);
640
641       /*
642         First, check if there is a pending FocusIn event waiting.  if there
643         is, process it and determine if focus has moved to another window
644         (the FocusIn event handler sets the window in the event
645         structure to None to indicate this).
646       */
647       if (XCheckTypedEvent(getXDisplay(), FocusIn, &event)) {
648
649         process_event(&event);
650         if (event.xfocus.window == None) {
651           // focus has moved
652           check_focus = False;
653         }
654       }
655
656       if (check_focus) {
657         /*
658           Second, we query the X server for the current input focus.
659           to make sure that we keep a consistent state.
660         */
661         BlackboxWindow *focus;
662         Window w;
663         int revert;
664         XGetInputFocus(getXDisplay(), &w, &revert);
665         focus = searchWindow(w);
666         if (focus) {
667           /*
668             focus got from "win" to "focus" under some very strange
669             circumstances, and we need to make sure that the focus indication
670             is correct.
671           */
672           setFocusedWindow(focus);
673         } else {
674           // we have no idea where focus went... so we set it to somewhere
675           setFocusedWindow(0);
676         }
677       }
678     }
679
680     break;
681   }
682
683   case ClientMessage: {
684     if (e->xclient.format == 32) {
685       if (e->xclient.message_type == xatom->getAtom(XAtom::wm_change_state)) {
686         // WM_CHANGE_STATE message
687         BlackboxWindow *win = searchWindow(e->xclient.window);
688         if (! win || ! win->validateClient()) return;
689
690         if (e->xclient.data.l[0] == IconicState)
691           win->iconify();
692         if (e->xclient.data.l[0] == NormalState)
693           win->deiconify();
694       } else if (e->xclient.message_type == 
695                  xatom->getAtom(XAtom::blackbox_change_workspace) || 
696                  e->xclient.message_type == 
697                  xatom->getAtom(XAtom::net_current_desktop)) {
698         // NET_CURRENT_DESKTOP message
699         BScreen *screen = searchScreen(e->xclient.window);
700
701         unsigned int workspace = e->xclient.data.l[0];
702         if (screen && workspace < screen->getWorkspaceCount())
703           screen->changeWorkspaceID(workspace);
704       } else if (e->xclient.message_type == 
705                  xatom->getAtom(XAtom::blackbox_change_window_focus)) {
706         // TEMP HACK TO KEEP BBKEYS WORKING
707         BlackboxWindow *win = searchWindow(e->xclient.window);
708
709         if (win && win->isVisible() && win->setInputFocus())
710           win->installColormap(True);
711       } else if (e->xclient.message_type == 
712                  xatom->getAtom(XAtom::net_active_window)) {
713         // NET_ACTIVE_WINDOW
714         BlackboxWindow *win = searchWindow(e->xclient.window);
715
716         if (win) {
717           BScreen *screen = win->getScreen();
718
719           if (win->isIconic())
720             win->deiconify(False, True);
721           if (win->isShaded())
722             win->shade();
723           if (win->getWorkspaceNumber() != screen->getCurrentWorkspaceID())
724             screen->changeWorkspaceID(win->getWorkspaceNumber());
725           if (win->isVisible() && win->setInputFocus()) {
726             win->getScreen()->getWorkspace(win->getWorkspaceNumber())->
727               raiseWindow(win);
728             win->installColormap(True);
729           }
730         }
731       } else if (e->xclient.message_type == 
732                  xatom->getAtom(XAtom::blackbox_cycle_window_focus)) {
733         // BLACKBOX_CYCLE_WINDOW_FOCUS
734         BScreen *screen = searchScreen(e->xclient.window);
735
736         if (screen) {
737           if (! e->xclient.data.l[0])
738             screen->prevFocus();
739           else
740             screen->nextFocus();
741         }
742       } else if (e->xclient.message_type == 
743                  xatom->getAtom(XAtom::net_wm_desktop)) {
744         // NET_WM_DESKTOP
745         BlackboxWindow *win = searchWindow(e->xclient.window);
746
747         if (win) {
748           BScreen *screen = win->getScreen();
749           unsigned long wksp = (unsigned) e->xclient.data.l[0];
750           if (wksp < screen->getWorkspaceCount()) {
751             if (win->isIconic()) win->deiconify(False, True);
752             if (win->isStuck()) win->stick();
753             if (wksp != screen->getCurrentWorkspaceID())
754               win->withdraw();
755             else
756               win->show();
757             screen->reassociateWindow(win, wksp, True);
758           } else if (wksp == 0xfffffffe) { // XXX: BUG, BUT DOING THIS SO KDE WORKS FOR NOW!!
759             if (win->isIconic()) win->deiconify(False, True);
760             if (! win->isStuck()) win->stick();
761             if (! win->isVisible()) win->show();
762           }
763         }
764       } else if (e->xclient.message_type == 
765                  xatom->getAtom(XAtom::blackbox_change_attributes)) {
766         // BLACKBOX_CHANGE_ATTRIBUTES
767         BlackboxWindow *win = searchWindow(e->xclient.window);
768
769         if (win && win->validateClient()) {
770           BlackboxHints net;
771           net.flags = e->xclient.data.l[0];
772           net.attrib = e->xclient.data.l[1];
773           net.workspace = e->xclient.data.l[2];
774           net.stack = e->xclient.data.l[3];
775           net.decoration = e->xclient.data.l[4];
776
777           win->changeBlackboxHints(&net);
778         }
779       } else if (e->xclient.message_type == 
780                 xatom->getAtom(XAtom::net_number_of_desktops)) {
781         // NET_NUMBER_OF_DESKTOPS
782         BScreen *screen = searchScreen(e->xclient.window);
783         
784         if (e->xclient.data.l[0] > 0) {
785           if ((unsigned) e->xclient.data.l[0] < screen->getWorkspaceCount()) {
786             // shrink
787             for (int i = screen->getWorkspaceCount();
788                  i > e->xclient.data.l[0]; --i)
789               screen->removeLastWorkspace();
790             // removeLast already sets the current workspace to the 
791             // last available one.
792           } else if ((unsigned) e->xclient.data.l[0] >
793                      screen->getWorkspaceCount()) {
794             // grow
795             for(int i = screen->getWorkspaceCount(); 
796                 i < e->xclient.data.l[0]; ++i)
797               screen->addWorkspace();
798           }
799         }
800       } else if (e->xclient.message_type ==
801                  xatom->getAtom(XAtom::net_close_window)) {
802         // NET_CLOSE_WINDOW
803         BlackboxWindow *win = searchWindow(e->xclient.window);
804         if (win && win->validateClient())
805           win->close(); // could this be smarter?
806       } else if (e->xclient.message_type ==
807                  xatom->getAtom(XAtom::net_wm_moveresize)) {
808         // NET_WM_MOVERESIZE
809         BlackboxWindow *win = searchWindow(e->xclient.window);
810         if (win && win->validateClient()) {
811           int x_root = e->xclient.data.l[0],
812               y_root = e->xclient.data.l[1];
813           if ((Atom) e->xclient.data.l[2] ==
814               xatom->getAtom(XAtom::net_wm_moveresize_move)) {
815             win->beginMove(x_root, y_root);
816           } else {
817             if ((Atom) e->xclient.data.l[2] ==
818                 xatom->getAtom(XAtom::net_wm_moveresize_size_topleft))
819               win->beginResize(x_root, y_root, BlackboxWindow::TopLeft);
820             else if ((Atom) e->xclient.data.l[2] ==
821                      xatom->getAtom(XAtom::net_wm_moveresize_size_topright))
822               win->beginResize(x_root, y_root, BlackboxWindow::TopRight);
823             else if ((Atom) e->xclient.data.l[2] ==
824                      xatom->getAtom(XAtom::net_wm_moveresize_size_bottomleft))
825               win->beginResize(x_root, y_root, BlackboxWindow::BottomLeft);
826             else if ((Atom) e->xclient.data.l[2] ==
827                 xatom->getAtom(XAtom::net_wm_moveresize_size_bottomright))
828               win->beginResize(x_root, y_root, BlackboxWindow::BottomRight);
829           }
830         }
831       } else if (e->xclient.message_type ==
832                  xatom->getAtom(XAtom::net_wm_state)) {
833         // NET_WM_STATE
834         BlackboxWindow *win = searchWindow(e->xclient.window);
835         if (win && win->validateClient()) {
836           const Atom action = (Atom) e->xclient.data.l[0];
837           const Atom state[] = { (Atom) e->xclient.data.l[1],
838                                  (Atom) e->xclient.data.l[2] };
839           
840           for (int i = 0; i < 2; ++i) {
841             if (! state[i])
842               continue;
843
844             if ((Atom) e->xclient.data.l[0] == 1) {
845               // ADD
846               if (state[i] == xatom->getAtom(XAtom::net_wm_state_modal)) {
847                 win->setModal(True);
848               } else if (state[i] ==
849                          xatom->getAtom(XAtom::net_wm_state_maximized_vert)) {
850                 if (win->isMaximizedHoriz()) {
851                   win->maximize(0); // unmaximize
852                   win->maximize(1); // full
853                 } else if (! win->isMaximized()) {
854                   win->maximize(2); // vert
855                 }
856               } else if (state[i] ==
857                          xatom->getAtom(XAtom::net_wm_state_maximized_horz)) {
858                 if (win->isMaximizedVert()) {
859                   win->maximize(0); // unmaximize
860                   win->maximize(1); // full
861                 } else if (! win->isMaximized()) {
862                   win->maximize(3); // horiz
863                 }
864               } else if (state[i] ==
865                          xatom->getAtom(XAtom::net_wm_state_shaded)) {
866                 if (! win->isShaded())
867                   win->shade();
868               } else if (state[i] ==
869                          xatom->getAtom(XAtom::net_wm_state_skip_taskbar)) {
870                 win->setSkipTaskbar(True);
871               } else if (state[i] ==
872                          xatom->getAtom(XAtom::net_wm_state_skip_pager)) {
873                 win->setSkipPager(True);
874               } else if (state[i] ==
875                          xatom->getAtom(XAtom::net_wm_state_fullscreen)) {
876                 win->setFullscreen(True);
877               }
878             } else if (action == 0) {
879               // REMOVE
880               if (state[i] == xatom->getAtom(XAtom::net_wm_state_modal)) {
881                 win->setModal(False);
882               } else if (state[i] ==
883                          xatom->getAtom(XAtom::net_wm_state_maximized_vert)) {
884                 if (win->isMaximizedFull()) {
885                   win->maximize(0); // unmaximize
886                   win->maximize(3); // horiz
887                 } else if (win->isMaximizedVert()) {
888                   win->maximize(0); // unmaximize
889                 }
890               } else if (state[i] ==
891                          xatom->getAtom(XAtom::net_wm_state_maximized_horz)) {
892                 if (win->isMaximizedFull()) {
893                   win->maximize(0); // unmaximize
894                   win->maximize(2); // vert
895                 } else if (win->isMaximizedHoriz()) {
896                   win->maximize(0); // unmaximize
897                 }
898               } else if (state[i] ==
899                          xatom->getAtom(XAtom::net_wm_state_shaded)) {
900                 if (win->isShaded())
901                   win->shade();
902               } else if (state[i] ==
903                          xatom->getAtom(XAtom::net_wm_state_skip_taskbar)) {
904                 win->setSkipTaskbar(False);
905               } else if (state[i] ==
906                          xatom->getAtom(XAtom::net_wm_state_skip_pager)) {
907                 win->setSkipPager(False);
908               } else if (state[i] ==
909                          xatom->getAtom(XAtom::net_wm_state_fullscreen)) {
910                 win->setFullscreen(False);
911               }
912             } else if (action == 2) {
913               // TOGGLE
914               if (state[i] == xatom->getAtom(XAtom::net_wm_state_modal)) {
915                 win->setModal(! win->isModal());
916               } else if (state[i] ==
917                          xatom->getAtom(XAtom::net_wm_state_maximized_vert)) {
918                 if (win->isMaximizedFull()) {
919                   win->maximize(0); // unmaximize
920                   win->maximize(3); // horiz
921                 } else if (win->isMaximizedVert()) {
922                   win->maximize(0); // unmaximize
923                 } else if (win->isMaximizedHoriz()) {
924                   win->maximize(0); // unmaximize
925                   win->maximize(1); // full
926                 } else {
927                   win->maximize(2); // vert
928                 }
929               } else if (state[i] ==
930                          xatom->getAtom(XAtom::net_wm_state_maximized_horz)) {
931                 if (win->isMaximizedFull()) {
932                   win->maximize(0); // unmaximize
933                   win->maximize(2); // vert
934                 } else if (win->isMaximizedHoriz()) {
935                   win->maximize(0); // unmaximize
936                 } else if (win->isMaximizedVert()) {
937                   win->maximize(0); // unmaximize
938                   win->maximize(1); // full
939                 } else {
940                   win->maximize(3); // horiz
941                 }
942               } else if (state[i] ==
943                          xatom->getAtom(XAtom::net_wm_state_shaded)) {
944                 win->shade();
945               } else if (state[i] ==
946                          xatom->getAtom(XAtom::net_wm_state_skip_taskbar)) {
947                 win->setSkipTaskbar(! win->skipTaskbar());
948               } else if (state[i] ==
949                          xatom->getAtom(XAtom::net_wm_state_skip_pager)) {
950                 win->setSkipPager(! win->skipPager());
951               } else if (state[i] ==
952                          xatom->getAtom(XAtom::net_wm_state_fullscreen)) {
953                 win->setFullscreen(! win->isFullscreen());
954               }
955             }
956           }
957         }
958       }
959     }
960
961     break;
962   }
963
964   case NoExpose:
965   case ConfigureNotify:
966   case MapNotify:
967     break; // not handled, just ignore
968
969   default: {
970 #ifdef    SHAPE
971     if (e->type == getShapeEventBase()) {
972       XShapeEvent *shape_event = (XShapeEvent *) e;
973       BlackboxWindow *win = searchWindow(e->xany.window);
974
975       if (win)
976         win->shapeEvent(shape_event);
977     }
978 #endif // SHAPE
979   }
980   } // switch
981 }
982
983
984 bool Blackbox::handleSignal(int sig) {
985   switch (sig) {
986   case SIGHUP:
987     restart();
988     break;
989
990   case SIGUSR1:
991     reconfigure();
992     break;
993
994   case SIGUSR2:
995     rereadMenu();
996     break;
997
998   case SIGPIPE:
999   case SIGSEGV:
1000   case SIGFPE:
1001   case SIGINT:
1002   case SIGTERM:
1003     shutdown();
1004
1005   default:
1006     return False;
1007   }
1008
1009   return True;
1010 }
1011
1012
1013 bool Blackbox::validateWindow(Window window) {
1014   XEvent event;
1015   if (XCheckTypedWindowEvent(getXDisplay(), window, DestroyNotify, &event)) {
1016     XPutBackEvent(getXDisplay(), &event);
1017
1018     return False;
1019   }
1020
1021   return True;
1022 }
1023
1024
1025 BScreen *Blackbox::searchScreen(Window window) {
1026   ScreenList::iterator it = screenList.begin();
1027
1028   for (; it != screenList.end(); ++it) {
1029     BScreen *s = *it;
1030     if (s->getRootWindow() == window)
1031       return s;
1032   }
1033
1034   return (BScreen *) 0;
1035 }
1036
1037
1038 BScreen *Blackbox::searchSystrayWindow(Window window) {
1039   WindowScreenLookup::iterator it = systraySearchList.find(window);
1040   if (it != systraySearchList.end())
1041     return it->second;
1042
1043   return (BScreen*) 0;
1044 }
1045
1046
1047 BlackboxWindow *Blackbox::searchWindow(Window window) {
1048   WindowLookup::iterator it = windowSearchList.find(window);
1049   if (it != windowSearchList.end())
1050     return it->second;
1051
1052   return (BlackboxWindow*) 0;
1053 }
1054
1055
1056 BWindowGroup *Blackbox::searchGroup(Window window) {
1057   GroupLookup::iterator it = groupSearchList.find(window);
1058   if (it != groupSearchList.end())
1059     return it->second;
1060
1061   return (BWindowGroup *) 0;
1062 }
1063
1064
1065 Basemenu *Blackbox::searchMenu(Window window) {
1066   MenuLookup::iterator it = menuSearchList.find(window);
1067   if (it != menuSearchList.end())
1068     return it->second;
1069
1070   return (Basemenu*) 0;
1071 }
1072
1073
1074 Toolbar *Blackbox::searchToolbar(Window window) {
1075   ToolbarLookup::iterator it = toolbarSearchList.find(window);
1076   if (it != toolbarSearchList.end())
1077     return it->second;
1078
1079   return (Toolbar*) 0;
1080 }
1081
1082
1083 Slit *Blackbox::searchSlit(Window window) {
1084   SlitLookup::iterator it = slitSearchList.find(window);
1085   if (it != slitSearchList.end())
1086     return it->second;
1087
1088   return (Slit*) 0;
1089 }
1090
1091
1092 void Blackbox::saveSystrayWindowSearch(Window window, BScreen *screen) {
1093   systraySearchList.insert(WindowScreenLookupPair(window, screen));
1094 }
1095
1096
1097 void Blackbox::saveWindowSearch(Window window, BlackboxWindow *data) {
1098   windowSearchList.insert(WindowLookupPair(window, data));
1099 }
1100
1101
1102 void Blackbox::saveGroupSearch(Window window, BWindowGroup *data) {
1103   groupSearchList.insert(GroupLookupPair(window, data));
1104 }
1105
1106
1107 void Blackbox::saveMenuSearch(Window window, Basemenu *data) {
1108   menuSearchList.insert(MenuLookupPair(window, data));
1109 }
1110
1111
1112 void Blackbox::saveToolbarSearch(Window window, Toolbar *data) {
1113   toolbarSearchList.insert(ToolbarLookupPair(window, data));
1114 }
1115
1116
1117 void Blackbox::saveSlitSearch(Window window, Slit *data) {
1118   slitSearchList.insert(SlitLookupPair(window, data));
1119 }
1120
1121
1122 void Blackbox::removeSystrayWindowSearch(Window window) {
1123   systraySearchList.erase(window);
1124 }
1125
1126
1127 void Blackbox::removeWindowSearch(Window window) {
1128   windowSearchList.erase(window);
1129 }
1130
1131
1132 void Blackbox::removeGroupSearch(Window window) {
1133   groupSearchList.erase(window);
1134 }
1135
1136
1137 void Blackbox::removeMenuSearch(Window window) {
1138   menuSearchList.erase(window);
1139 }
1140
1141
1142 void Blackbox::removeToolbarSearch(Window window) {
1143   toolbarSearchList.erase(window);
1144 }
1145
1146
1147 void Blackbox::removeSlitSearch(Window window) {
1148   slitSearchList.erase(window);
1149 }
1150
1151
1152 void Blackbox::restart(const char *prog) {
1153   shutdown();
1154
1155   if (prog) {
1156     putenv(const_cast<char *>(screenList.front()->displayString().c_str()));
1157     execlp(prog, prog, NULL);
1158     perror(prog);
1159   }
1160
1161   // fall back in case the above execlp doesn't work
1162   execvp(argv[0], argv);
1163   string name = basename(argv[0]);
1164   execvp(name.c_str(), argv);
1165 }
1166
1167
1168 void Blackbox::shutdown(void) {
1169   BaseDisplay::shutdown();
1170
1171   XSetInputFocus(getXDisplay(), PointerRoot, None, CurrentTime);
1172
1173   std::for_each(screenList.begin(), screenList.end(),
1174                 std::mem_fun(&BScreen::shutdown));
1175
1176   XSync(getXDisplay(), False);
1177 }
1178
1179
1180 /*
1181  * Save all values as they are so that the defaults will be written to the rc
1182  * file
1183  */
1184 void Blackbox::save_rc(void) {
1185   config.setAutoSave(false);
1186
1187   config.setValue("session.colorsPerChannel", resource.colors_per_channel);
1188   config.setValue("session.doubleClickInterval",
1189                   resource.double_click_interval);
1190   config.setValue("session.autoRaiseDelay",
1191                   ((resource.auto_raise_delay.tv_sec * 1000) +
1192                    (resource.auto_raise_delay.tv_usec / 1000)));
1193   config.setValue("session.cacheLife", resource.cache_life / 60000);
1194   config.setValue("session.cacheMax", resource.cache_max);
1195   config.setValue("session.styleFile", resource.style_file);
1196   config.setValue("session.titlebarLayout", resource.titlebar_layout);
1197   
1198   std::for_each(screenList.begin(), screenList.end(),
1199                 std::mem_fun(&BScreen::save_rc));
1200  
1201   config.setAutoSave(true);
1202   config.save();
1203 }
1204
1205
1206 void Blackbox::load_rc(void) {
1207   if (! config.load())
1208     config.create();
1209   
1210   string s;
1211
1212   if (! config.getValue("session.colorsPerChannel",
1213                         resource.colors_per_channel))
1214     resource.colors_per_channel = 4;
1215   if (resource.colors_per_channel < 2) resource.colors_per_channel = 2;
1216   else if (resource.colors_per_channel > 6) resource.colors_per_channel = 6;
1217
1218   if (config.getValue("session.styleFile", s))
1219     resource.style_file = expandTilde(s);
1220   else
1221     resource.style_file = DEFAULTSTYLE;
1222
1223   if (! config.getValue("session.doubleClickInterval",
1224                        resource.double_click_interval));
1225     resource.double_click_interval = 250;
1226
1227   if (! config.getValue("session.autoRaiseDelay",
1228                        resource.auto_raise_delay.tv_usec))
1229     resource.auto_raise_delay.tv_usec = 400;
1230   resource.auto_raise_delay.tv_sec = resource.auto_raise_delay.tv_usec / 1000;
1231   resource.auto_raise_delay.tv_usec -=
1232     (resource.auto_raise_delay.tv_sec * 1000);
1233   resource.auto_raise_delay.tv_usec *= 1000;
1234
1235   if (! config.getValue("session.cacheLife", resource.cache_life))
1236     resource.cache_life = 5;
1237   resource.cache_life *= 60000;
1238
1239   if (! config.getValue("session.cacheMax", resource.cache_max))
1240     resource.cache_max = 200;
1241   
1242   if (! config.getValue("session.titlebarLayout", resource.titlebar_layout))
1243     resource.titlebar_layout = "ILMC";
1244 }
1245
1246
1247 void Blackbox::reconfigure(void) {
1248   reconfigure_wait = True;
1249
1250   if (! timer->isTiming()) timer->start();
1251 }
1252
1253
1254 void Blackbox::real_reconfigure(void) {
1255   load_rc();
1256   
1257   std::for_each(menuTimestamps.begin(), menuTimestamps.end(),
1258                 PointerAssassin());
1259   menuTimestamps.clear();
1260
1261   gcCache()->purge();
1262
1263   std::for_each(screenList.begin(), screenList.end(),
1264                 std::mem_fun(&BScreen::reconfigure));
1265 }
1266
1267
1268 void Blackbox::checkMenu(void) {
1269   bool reread = False;
1270   MenuTimestampList::iterator it = menuTimestamps.begin();
1271   for(; it != menuTimestamps.end(); ++it) {
1272     MenuTimestamp *tmp = *it;
1273     struct stat buf;
1274
1275     if (! stat(tmp->filename.c_str(), &buf)) {
1276       if (tmp->timestamp != buf.st_ctime)
1277         reread = True;
1278     } else {
1279       reread = True;
1280     }
1281   }
1282
1283   if (reread) rereadMenu();
1284 }
1285
1286
1287 void Blackbox::rereadMenu(void) {
1288   reread_menu_wait = True;
1289
1290   if (! timer->isTiming()) timer->start();
1291 }
1292
1293
1294 void Blackbox::real_rereadMenu(void) {
1295   std::for_each(menuTimestamps.begin(), menuTimestamps.end(),
1296                 PointerAssassin());
1297   menuTimestamps.clear();
1298
1299   std::for_each(screenList.begin(), screenList.end(),
1300                 std::mem_fun(&BScreen::rereadMenu));
1301 }
1302
1303
1304 void Blackbox::saveStyleFilename(const string& filename) {
1305   assert(! filename.empty());
1306   resource.style_file = filename;
1307   config.setValue("session.styleFile", resource.style_file);
1308 }
1309
1310
1311 void Blackbox::addMenuTimestamp(const string& filename) {
1312   assert(! filename.empty());
1313   bool found = False;
1314
1315   MenuTimestampList::iterator it = menuTimestamps.begin();
1316   for (; it != menuTimestamps.end() && ! found; ++it) {
1317     if ((*it)->filename == filename) found = True;
1318   }
1319   if (! found) {
1320     struct stat buf;
1321
1322     if (! stat(filename.c_str(), &buf)) {
1323       MenuTimestamp *ts = new MenuTimestamp;
1324
1325       ts->filename = filename;
1326       ts->timestamp = buf.st_ctime;
1327
1328       menuTimestamps.push_back(ts);
1329     }
1330   }
1331 }
1332
1333
1334 void Blackbox::timeout(void) {
1335   if (reconfigure_wait)
1336     real_reconfigure();
1337
1338   if (reread_menu_wait)
1339     real_rereadMenu();
1340
1341   reconfigure_wait = reread_menu_wait = False;
1342 }
1343
1344
1345 void Blackbox::setChangingWindow(BlackboxWindow *win) {
1346   // make sure one of the two is null and the other isn't
1347   assert((! changing_window && win) || (! win && changing_window));
1348   changing_window = win;
1349 }
1350
1351
1352 void Blackbox::setFocusedWindow(BlackboxWindow *win) {
1353   if (focused_window && focused_window == win) // nothing to do
1354     return;
1355
1356   BScreen *old_screen = 0;
1357
1358   if (focused_window) {
1359     focused_window->setFocusFlag(False);
1360     old_screen = focused_window->getScreen();
1361   }
1362
1363   if (win && ! win->isIconic()) {
1364     // the active screen is the one with the last focused window...
1365     // this will keep focus on this screen no matter where the mouse goes,
1366     // so multihead keybindings will continue to work on that screen until the
1367     // user focuses a window on a different screen.
1368     active_screen = win->getScreen();
1369     focused_window = win;
1370   } else {
1371     focused_window = 0;
1372     if (! old_screen) {
1373       if (active_screen) {
1374         // set input focus to the toolbar of the screen with mouse
1375         XSetInputFocus(getXDisplay(),
1376                        active_screen->getRootWindow(),
1377                        RevertToPointerRoot, CurrentTime);
1378       } else {
1379         // set input focus to the toolbar of the first managed screen
1380         XSetInputFocus(getXDisplay(),
1381                        screenList.front()->getRootWindow(),
1382                        RevertToPointerRoot, CurrentTime);
1383       }
1384     } else {
1385       // set input focus to the toolbar of the last screen
1386       XSetInputFocus(getXDisplay(), old_screen->getRootWindow(),
1387                      RevertToPointerRoot, CurrentTime);
1388     }
1389   }
1390
1391   if (active_screen && active_screen->isScreenManaged()) {
1392     active_screen->getToolbar()->redrawWindowLabel(True);
1393     active_screen->updateNetizenWindowFocus();
1394   }
1395
1396   if (old_screen && old_screen != active_screen) {
1397     old_screen->getToolbar()->redrawWindowLabel(True);
1398     old_screen->updateNetizenWindowFocus();
1399   }
1400 }