]> icculus.org git repositories - dana/openbox.git/blob - src/openbox.cc
focus code updates
[dana/openbox.git] / src / openbox.cc
1 // openbox.cc for Openbox
2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.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 // stupid macros needed to access some functions in version 2 of the GNU C
24 // library
25 #ifndef   _GNU_SOURCE
26 #define   _GNU_SOURCE
27 #endif // _GNU_SOURCE
28
29 #ifdef    HAVE_CONFIG_H
30 #  include "../config.h"
31 #endif // HAVE_CONFIG_H
32
33 #include <X11/Xlib.h>
34 #include <X11/Xutil.h>
35 #include <X11/Xresource.h>
36 #include <X11/Xatom.h>
37 #include <X11/keysym.h>
38
39 #ifdef    SHAPE
40 #include <X11/extensions/shape.h>
41 #endif // SHAPE
42
43 #include "i18n.h"
44 #include "openbox.h"
45 #include "Basemenu.h"
46 #include "Clientmenu.h"
47 #include "Rootmenu.h"
48 #include "Screen.h"
49
50 #ifdef    SLIT
51 #include "Slit.h"
52 #endif // SLIT
53
54 #include "Toolbar.h"
55 #include "Window.h"
56 #include "Workspace.h"
57 #include "Workspacemenu.h"
58 #include "Util.h"
59
60 #include <string>
61 #include <algorithm>
62
63 #ifdef    HAVE_STDIO_H
64 #  include <stdio.h>
65 #endif // HAVE_STDIO_H
66
67 #ifdef    HAVE_STDLIB_H
68 #  include <stdlib.h>
69 #endif // HAVE_STDLIB_H
70
71 #ifdef    HAVE_STRING_H
72 #  include <string.h>
73 #endif // HAVE_STRING_H
74
75 #ifdef    HAVE_UNISTD_H
76 #  include <sys/types.h>
77 #  include <unistd.h>
78 #endif // HAVE_UNISTD_H
79
80 #ifdef    HAVE_SYS_PARAM_H
81 #  include <sys/param.h>
82 #endif // HAVE_SYS_PARAM_H
83
84 #ifndef   MAXPATHLEN
85 #define   MAXPATHLEN 255
86 #endif // MAXPATHLEN
87
88 #ifdef    HAVE_SYS_SELECT_H
89 #  include <sys/select.h>
90 #endif // HAVE_SYS_SELECT_H
91
92 #ifdef    HAVE_SIGNAL_H
93 #  include <signal.h>
94 #endif // HAVE_SIGNAL_H
95
96 #ifdef    HAVE_SYS_SIGNAL_H
97 #  include <sys/signal.h>
98 #endif // HAVE_SYS_SIGNAL_H
99
100 #ifdef    HAVE_SYS_STAT_H
101 #  include <sys/types.h>
102 #  include <sys/stat.h>
103 #endif // HAVE_SYS_STAT_H
104
105 #ifdef    TIME_WITH_SYS_TIME
106 #  include <sys/time.h>
107 #  include <time.h>
108 #else // !TIME_WITH_SYS_TIME
109 #  ifdef    HAVE_SYS_TIME_H
110 #    include <sys/time.h>
111 #  else // !HAVE_SYS_TIME_H
112 #    include <time.h>
113 #  endif // HAVE_SYS_TIME_H
114 #endif // TIME_WITH_SYS_TIME
115
116 #ifdef    HAVE_LIBGEN_H
117 #  include <libgen.h>
118 #endif // HAVE_LIBGEN_H
119
120 #ifndef   HAVE_BASENAME
121 static inline char *basename (char *s) {
122   char *save = s;
123
124   while (*s) if (*s++ == '/') save = s;
125
126   return save;
127 }
128 #endif // HAVE_BASENAME
129
130
131 // X event scanner for enter/leave notifies - adapted from twm
132 typedef struct scanargs {
133   Window w;
134   Bool leave, inferior, enter;
135 } scanargs;
136
137 static Bool queueScanner(Display *, XEvent *e, char *args) {
138   if ((e->type == LeaveNotify) &&
139       (e->xcrossing.window == ((scanargs *) args)->w) &&
140       (e->xcrossing.mode == NotifyNormal)) {
141     ((scanargs *) args)->leave = True;
142     ((scanargs *) args)->inferior = (e->xcrossing.detail == NotifyInferior);
143   } else if ((e->type == EnterNotify) &&
144              (e->xcrossing.mode == NotifyUngrab)) {
145     ((scanargs *) args)->enter = True;
146   }
147
148   return False;
149 }
150
151 Openbox *openbox;
152
153
154 Openbox::Openbox(int m_argc, char **m_argv, char *dpy_name, char *rc)
155   : BaseDisplay(m_argv[0], dpy_name) {
156   grab();
157
158   if (! XSupportsLocale())
159     fprintf(stderr, "X server does not support locale\n");
160
161   if (XSetLocaleModifiers("") == NULL)
162     fprintf(stderr, "cannot set locale modifiers\n");
163
164   ::openbox = this;
165   argc = m_argc;
166   argv = m_argv;
167   if (rc == NULL) {
168     char *homedir = getenv("HOME");
169
170     rc_file = new char[strlen(homedir) + strlen("/.openbox/rc") + 1];
171     sprintf(rc_file, "%s/.openbox", homedir);
172
173     // try to make sure the ~/.openbox directory exists
174     mkdir(rc_file, S_IREAD | S_IWRITE | S_IEXEC | S_IRGRP | S_IWGRP | S_IXGRP |
175           S_IROTH | S_IWOTH | S_IXOTH);
176     
177     sprintf(rc_file, "%s/.openbox/rc", homedir);
178   } else {
179     rc_file = bstrdup(rc);
180   }
181   config.setFile(rc_file);
182
183   no_focus = False;
184
185   resource.menu_file = resource.style_file = NULL;
186   resource.titlebar_layout = NULL;
187   resource.auto_raise_delay.tv_sec = resource.auto_raise_delay.tv_usec = 0;
188
189   masked_window = NULL;
190   masked = None;
191
192   load();
193
194 #ifdef    HAVE_GETPID
195   openbox_pid = XInternAtom(getXDisplay(), "_BLACKBOX_PID", False);
196 #endif // HAVE_GETPID
197
198   for (int i = 0; i < getNumberOfScreens(); i++) {
199     BScreen *screen = new BScreen(*this, i, config);
200
201     if (! screen->isScreenManaged()) {
202       delete screen;
203       continue;
204     }
205
206     screenList.push_back(screen);
207   }
208
209   if (screenList.empty()) {
210     fprintf(stderr,
211             i18n->getMessage(openboxSet, openboxNoManagableScreens,
212                "Openbox::Openbox: no managable screens found, aborting.\n"));
213     ::exit(3);
214   }
215   focused_screen = screenList.front();
216
217   // save current settings and default values
218   save();
219   
220   XSynchronize(getXDisplay(), False);
221   XSync(getXDisplay(), False);
222
223   reconfigure_wait = reread_menu_wait = False;
224
225   timer = new BTimer(*this, *this);
226   timer->setTimeout(0);
227   timer->fireOnce(True);
228
229   ungrab();
230
231   focusWindow((OpenboxWindow *) 0);
232 }
233
234
235 Openbox::~Openbox() {
236   for_each(screenList.begin(), screenList.end(),
237            PointerAssassin());
238
239   for_each(menuTimestamps.begin(), menuTimestamps.end(),
240            PointerAssassin());
241
242   if (resource.menu_file)
243     delete [] resource.menu_file;
244
245   if (resource.style_file)
246     delete [] resource.style_file;
247
248   if (resource.titlebar_layout)
249     delete [] resource.titlebar_layout;
250
251   delete timer;
252
253   delete [] rc_file;
254 }
255
256
257 void Openbox::process_event(XEvent *e) {
258   if ((masked == e->xany.window && masked_window) &&
259       (e->type == MotionNotify)) {
260     last_time = e->xmotion.time;
261     masked_window->motionNotifyEvent(&e->xmotion);
262     return;
263   }
264
265   switch (e->type) {
266   case ButtonPress: {
267     // strip the lock key modifiers
268     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
269
270     last_time = e->xbutton.time;
271
272     OpenboxWindow *win = (OpenboxWindow *) 0;
273     Basemenu *menu = (Basemenu *) 0;
274
275 #ifdef    SLIT
276     Slit *slit = (Slit *) 0;
277 #endif // SLIT
278
279     Toolbar *tbar = (Toolbar *) 0;
280
281     if ((win = searchWindow(e->xbutton.window))) {
282       win->buttonPressEvent(&e->xbutton);
283
284       if (e->xbutton.button == 1)
285         win->installColormap(True);
286     } else if ((menu = searchMenu(e->xbutton.window))) {
287       menu->buttonPressEvent(&e->xbutton);
288
289 #ifdef    SLIT
290     } else if ((slit = searchSlit(e->xbutton.window))) {
291       slit->buttonPressEvent(&e->xbutton);
292 #endif // SLIT
293
294     } else if ((tbar = searchToolbar(e->xbutton.window))) {
295       tbar->buttonPressEvent(&e->xbutton);
296     } else {
297       ScreenList::iterator it;
298       for (it = screenList.begin(); it != screenList.end(); ++it) {
299         BScreen *screen = *it;
300         if (e->xbutton.window == screen->getRootWindow()) {
301           if (e->xbutton.button == 1) {
302             if (! screen->isRootColormapInstalled())
303               screen->getImageControl()->installRootColormap();
304
305             if (screen->getWorkspacemenu()->isVisible())
306               screen->getWorkspacemenu()->hide();
307
308             if (screen->getRootmenu()->isVisible())
309               screen->getRootmenu()->hide();
310           } else if (e->xbutton.button == 2) {
311             int mx = e->xbutton.x_root -
312               (screen->getWorkspacemenu()->getWidth() / 2);
313             int my = e->xbutton.y_root -
314               (screen->getWorkspacemenu()->getTitleHeight() / 2);
315
316             if (mx < 0) mx = 0;
317             if (my < 0) my = 0;
318
319             if (mx + screen->getWorkspacemenu()->getWidth() >
320                 screen->size().w())
321               mx = screen->size().w() -
322                 screen->getWorkspacemenu()->getWidth() -
323                 screen->getBorderWidth();
324
325             if (my + screen->getWorkspacemenu()->getHeight() >
326                 screen->size().h())
327               my = screen->size().h() -
328                 screen->getWorkspacemenu()->getHeight() -
329                 screen->getBorderWidth();
330
331             screen->getWorkspacemenu()->move(mx, my);
332
333             if (! screen->getWorkspacemenu()->isVisible()) {
334               screen->getWorkspacemenu()->removeParent();
335               screen->getWorkspacemenu()->show();
336             }
337           } else if (e->xbutton.button == 3) {
338             int mx = e->xbutton.x_root -
339               (screen->getRootmenu()->getWidth() / 2);
340             int my = e->xbutton.y_root -
341               (screen->getRootmenu()->getTitleHeight() / 2);
342
343             if (mx < 0) mx = 0;
344             if (my < 0) my = 0;
345
346             if (mx + screen->getRootmenu()->getWidth() > screen->size().w())
347               mx = screen->size().w() -
348                 screen->getRootmenu()->getWidth() -
349                 screen->getBorderWidth();
350
351             if (my + screen->getRootmenu()->getHeight() > screen->size().h())
352                 my = screen->size().h() -
353                   screen->getRootmenu()->getHeight() -
354                   screen->getBorderWidth();
355
356             screen->getRootmenu()->move(mx, my);
357
358             if (! screen->getRootmenu()->isVisible()) {
359               checkMenu();
360               screen->getRootmenu()->show();
361             }
362           } else if (e->xbutton.button == 4) {
363             if ((screen->getCurrentWorkspaceID() + 1) >
364                 screen->getWorkspaceCount() - 1)
365               screen->changeWorkspaceID(0);
366             else
367               screen->changeWorkspaceID(screen->getCurrentWorkspaceID() + 1);
368           } else if (e->xbutton.button == 5) {
369             if ((screen->getCurrentWorkspaceID() - 1) < 0)
370               screen->changeWorkspaceID(screen->getWorkspaceCount() - 1);
371             else
372               screen->changeWorkspaceID(screen->getCurrentWorkspaceID() - 1);
373           }
374         }
375       }
376     }
377
378     break;
379   }
380
381   case ButtonRelease: {
382     // strip the lock key modifiers
383     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
384
385     last_time = e->xbutton.time;
386
387     OpenboxWindow *win = (OpenboxWindow *) 0;
388     Basemenu *menu = (Basemenu *) 0;
389     Toolbar *tbar = (Toolbar *) 0;
390
391     if ((win = searchWindow(e->xbutton.window)))
392       win->buttonReleaseEvent(&e->xbutton);
393     else if ((menu = searchMenu(e->xbutton.window)))
394       menu->buttonReleaseEvent(&e->xbutton);
395     else if ((tbar = searchToolbar(e->xbutton.window)))
396       tbar->buttonReleaseEvent(&e->xbutton);
397
398     break;
399   }
400
401   case ConfigureRequest: {
402     OpenboxWindow *win = (OpenboxWindow *) 0;
403
404 #ifdef    SLIT
405     Slit *slit = (Slit *) 0;
406 #endif // SLIT
407
408     if ((win = searchWindow(e->xconfigurerequest.window))) {
409       win->configureRequestEvent(&e->xconfigurerequest);
410
411 #ifdef    SLIT
412     } else if ((slit = searchSlit(e->xconfigurerequest.window))) {
413       slit->configureRequestEvent(&e->xconfigurerequest);
414 #endif // SLIT
415
416     } else {
417       grab();
418
419       if (validateWindow(e->xconfigurerequest.window)) {
420         XWindowChanges xwc;
421
422         xwc.x = e->xconfigurerequest.x;
423         xwc.y = e->xconfigurerequest.y;
424         xwc.width = e->xconfigurerequest.width;
425         xwc.height = e->xconfigurerequest.height;
426         xwc.border_width = e->xconfigurerequest.border_width;
427         xwc.sibling = e->xconfigurerequest.above;
428         xwc.stack_mode = e->xconfigurerequest.detail;
429
430         XConfigureWindow(getXDisplay(), e->xconfigurerequest.window,
431                          e->xconfigurerequest.value_mask, &xwc);
432       }
433
434       ungrab();
435     }
436
437     break;
438   }
439
440   case MapRequest: {
441 #ifdef    DEBUG
442     fprintf(stderr,
443             i18n->getMessage(openboxSet, openboxMapRequest,
444                  "Openbox::process_event(): MapRequest for 0x%lx\n"),
445             e->xmaprequest.window);
446 #endif // DEBUG
447
448     OpenboxWindow *win = searchWindow(e->xmaprequest.window);
449
450     if (! win)
451       win = new OpenboxWindow(*this, e->xmaprequest.window);
452
453     if ((win = searchWindow(e->xmaprequest.window)))
454       win->mapRequestEvent(&e->xmaprequest);
455
456     break;
457   }
458
459   case MapNotify: {
460     OpenboxWindow *win = searchWindow(e->xmap.window);
461
462     if (win)
463       win->mapNotifyEvent(&e->xmap);
464
465       break;
466   }
467
468   case UnmapNotify: {
469     OpenboxWindow *win = (OpenboxWindow *) 0;
470
471 #ifdef    SLIT
472     Slit *slit = (Slit *) 0;
473 #endif // SLIT
474
475     if ((win = searchWindow(e->xunmap.window))) {
476       win->unmapNotifyEvent(&e->xunmap);
477 #ifdef    SLIT
478     } else if ((slit = searchSlit(e->xunmap.window))) {
479       slit->removeClient(e->xunmap.window);
480 #endif // SLIT
481
482     }
483
484     break;
485   }
486
487   case DestroyNotify: {
488     OpenboxWindow *win = (OpenboxWindow *) 0;
489
490 #ifdef    SLIT
491     Slit *slit = (Slit *) 0;
492 #endif // SLIT
493
494     if ((win = searchWindow(e->xdestroywindow.window))) {
495       win->destroyNotifyEvent(&e->xdestroywindow);
496 #ifdef    SLIT
497     } else if ((slit = searchSlit(e->xdestroywindow.window))) {
498       slit->removeClient(e->xdestroywindow.window, False);
499 #endif // SLIT
500     }
501
502     break;
503   }
504
505   case MotionNotify: {
506     // strip the lock key modifiers
507     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
508     
509     last_time = e->xmotion.time;
510
511     OpenboxWindow *win = (OpenboxWindow *) 0;
512     Basemenu *menu = (Basemenu *) 0;
513
514     if ((win = searchWindow(e->xmotion.window)))
515       win->motionNotifyEvent(&e->xmotion);
516     else if ((menu = searchMenu(e->xmotion.window)))
517       menu->motionNotifyEvent(&e->xmotion);
518
519     break;
520   }
521
522   case PropertyNotify: {
523     last_time = e->xproperty.time;
524
525     if (e->xproperty.state != PropertyDelete) {
526       OpenboxWindow *win = searchWindow(e->xproperty.window);
527
528       if (win)
529         win->propertyNotifyEvent(e->xproperty.atom);
530     }
531
532     break;
533   }
534
535   case EnterNotify: {
536     last_time = e->xcrossing.time;
537
538     BScreen *screen = (BScreen *) 0;
539     OpenboxWindow *win = (OpenboxWindow *) 0;
540     Basemenu *menu = (Basemenu *) 0;
541     Toolbar *tbar = (Toolbar *) 0;
542
543 #ifdef    SLIT
544     Slit *slit = (Slit *) 0;
545 #endif // SLIT
546
547     if (e->xcrossing.mode == NotifyGrab) break;
548
549     XEvent dummy;
550     scanargs sa;
551     sa.w = e->xcrossing.window;
552     sa.enter = sa.leave = False;
553     XCheckIfEvent(getXDisplay(), &dummy, queueScanner, (char *) &sa);
554
555     if ((e->xcrossing.window == e->xcrossing.root) &&
556         (screen = searchScreen(e->xcrossing.window))) {
557       screen->getImageControl()->installRootColormap();
558     } else if ((win = searchWindow(e->xcrossing.window))) {
559       if (win->getScreen()->sloppyFocus() &&
560           (! win->isFocused()) && (! no_focus)) {
561         grab();
562
563         if (((! sa.leave) || sa.inferior) && win->isVisible() &&
564             win->setInputFocus())
565           win->installColormap(True);
566
567         ungrab();
568       }
569     } else if ((menu = searchMenu(e->xcrossing.window))) {
570       menu->enterNotifyEvent(&e->xcrossing);
571     } else if ((tbar = searchToolbar(e->xcrossing.window))) {
572       tbar->enterNotifyEvent(&e->xcrossing);
573 #ifdef    SLIT
574     } else if ((slit = searchSlit(e->xcrossing.window))) {
575       slit->enterNotifyEvent(&e->xcrossing);
576 #endif // SLIT
577     }
578     break;
579   }
580
581   case LeaveNotify: {
582     last_time = e->xcrossing.time;
583
584     OpenboxWindow *win = (OpenboxWindow *) 0;
585     Basemenu *menu = (Basemenu *) 0;
586     Toolbar *tbar = (Toolbar *) 0;
587
588 #ifdef    SLIT
589     Slit *slit = (Slit *) 0;
590 #endif // SLIT
591
592     if ((menu = searchMenu(e->xcrossing.window)))
593       menu->leaveNotifyEvent(&e->xcrossing);
594     else if ((win = searchWindow(e->xcrossing.window)))
595       win->installColormap(False);
596     else if ((tbar = searchToolbar(e->xcrossing.window)))
597       tbar->leaveNotifyEvent(&e->xcrossing);
598 #ifdef    SLIT
599     else if ((slit = searchSlit(e->xcrossing.window)))
600       slit->leaveNotifyEvent(&e->xcrossing);
601 #endif // SLIT
602
603     break;
604   }
605
606   case Expose: {
607     OpenboxWindow *win = (OpenboxWindow *) 0;
608     Basemenu *menu = (Basemenu *) 0;
609     Toolbar *tbar = (Toolbar *) 0;
610
611     if ((win = searchWindow(e->xexpose.window)))
612       win->exposeEvent(&e->xexpose);
613     else if ((menu = searchMenu(e->xexpose.window)))
614       menu->exposeEvent(&e->xexpose);
615     else if ((tbar = searchToolbar(e->xexpose.window)))
616       tbar->exposeEvent(&e->xexpose);
617
618     break;
619   }
620
621   case KeyPress: {
622     Toolbar *tbar = searchToolbar(e->xkey.window);
623
624     if (tbar && tbar->isEditing())
625       tbar->keyPressEvent(&e->xkey);
626
627     break;
628   }
629
630   case ColormapNotify: {
631     BScreen *screen = searchScreen(e->xcolormap.window);
632
633     if (screen)
634       screen->setRootColormapInstalled((e->xcolormap.state ==
635                                         ColormapInstalled) ? True : False);
636
637     break;
638   }
639
640   case FocusIn: {
641     if (e->xfocus.mode == NotifyUngrab || e->xfocus.detail == NotifyPointer)
642       break;
643
644     OpenboxWindow *win = searchWindow(e->xfocus.window);
645     if (win && !win->isFocused())
646       focusWindow(win);
647
648     break;
649   }
650
651   case FocusOut:
652     break;
653
654   case ClientMessage: {
655     if (e->xclient.format == 32) {
656       if (e->xclient.message_type == getWMChangeStateAtom()) {
657         OpenboxWindow *win = searchWindow(e->xclient.window);
658         if (! win || ! win->validateClient()) return;
659
660         if (e->xclient.data.l[0] == IconicState)
661           win->iconify();
662         if (e->xclient.data.l[0] == NormalState)
663           win->deiconify();
664       } else if (e->xclient.message_type == getOpenboxChangeWorkspaceAtom()) {
665         BScreen *screen = searchScreen(e->xclient.window);
666
667         if (screen && e->xclient.data.l[0] >= 0 &&
668             e->xclient.data.l[0] < screen->getWorkspaceCount())
669           screen->changeWorkspaceID(e->xclient.data.l[0]);
670       } else if (e->xclient.message_type == getOpenboxChangeWindowFocusAtom()) {
671         OpenboxWindow *win = searchWindow(e->xclient.window);
672
673         if (win && win->isVisible() && win->setInputFocus())
674           win->installColormap(True);
675       } else if (e->xclient.message_type == getOpenboxCycleWindowFocusAtom()) {
676         BScreen *screen = searchScreen(e->xclient.window);
677
678         if (screen) {
679           if (! e->xclient.data.l[0])
680             screen->prevFocus();
681           else
682             screen->nextFocus();
683         }
684       } else if (e->xclient.message_type == getOpenboxChangeAttributesAtom()) {
685         OpenboxWindow *win = searchWindow(e->xclient.window);
686
687         if (win && win->validateClient()) {
688           OpenboxHints net;
689           net.flags = e->xclient.data.l[0];
690           net.attrib = e->xclient.data.l[1];
691           net.workspace = e->xclient.data.l[2];
692           net.stack = e->xclient.data.l[3];
693           net.decoration = e->xclient.data.l[4];
694
695           win->changeOpenboxHints(&net);
696         }
697       }
698     }
699
700     break;
701   }
702
703
704   default: {
705 #ifdef    SHAPE
706     if (e->type == getShapeEventBase()) {
707       XShapeEvent *shape_event = (XShapeEvent *) e;
708       OpenboxWindow *win = (OpenboxWindow *) 0;
709
710       if ((win = searchWindow(e->xany.window)) ||
711           (shape_event->kind != ShapeBounding))
712         win->shapeEvent(shape_event);
713     }
714 #endif // SHAPE
715
716   }
717   } // switch
718 }
719
720
721 Bool Openbox::handleSignal(int sig) {
722   switch (sig) {
723   case SIGHUP:
724   case SIGUSR1:
725     reconfigure();
726     break;
727
728   case SIGUSR2:
729     rereadMenu();
730     break;
731
732   case SIGPIPE:
733   case SIGSEGV:
734   case SIGFPE:
735   case SIGINT:
736   case SIGTERM:
737     shutdown();
738
739   default:
740     return False;
741   }
742
743   return True;
744 }
745
746
747 BScreen *Openbox::searchScreen(Window window) {
748   ScreenList::iterator it;
749   for (it = screenList.begin(); it != screenList.end(); ++it)
750     if ((*it)->getRootWindow() == window)
751       return *it;
752   return (BScreen *) 0;
753 }
754
755
756 OpenboxWindow *Openbox::searchWindow(Window window) {
757   WindowLookup::iterator it = windowSearchList.find(window);
758   if (it == windowSearchList.end())
759     return (OpenboxWindow *) 0;
760   return it->second;
761 }
762
763
764 OpenboxWindow *Openbox::searchGroup(Window window, OpenboxWindow *win) {
765   WindowLookup::iterator it = groupSearchList.find(window);
766   if (it != groupSearchList.end())
767     if (it->second->getClientWindow() != win->getClientWindow())
768       return win;
769   return (OpenboxWindow *) 0;
770 }
771
772
773 Basemenu *Openbox::searchMenu(Window window) {
774   MenuLookup::iterator it = menuSearchList.find(window);
775   if (it == menuSearchList.end())
776     return (Basemenu *) 0;
777   return it->second;
778 }
779
780
781 Toolbar *Openbox::searchToolbar(Window window) {
782   ToolbarLookup::iterator it = toolbarSearchList.find(window);
783   if (it == toolbarSearchList.end())
784     return (Toolbar *) 0;
785   return it->second;
786 }
787
788
789 #ifdef    SLIT
790 Slit *Openbox::searchSlit(Window window) {
791   SlitLookup::iterator it = slitSearchList.find(window);
792   if (it == slitSearchList.end())
793     return (Slit *) 0;
794   return it->second;
795 }
796 #endif // SLIT
797
798
799 void Openbox::saveWindowSearch(Window window, OpenboxWindow *data) {
800   windowSearchList.insert(WindowLookupPair(window, data));
801 }
802
803
804 void Openbox::saveGroupSearch(Window window, OpenboxWindow *data) {
805   groupSearchList.insert(WindowLookupPair(window, data));
806 }
807
808
809 void Openbox::saveMenuSearch(Window window, Basemenu *data) {
810   menuSearchList.insert(MenuLookupPair(window, data));
811 }
812
813
814 void Openbox::saveToolbarSearch(Window window, Toolbar *data) {
815   toolbarSearchList.insert(ToolbarLookupPair(window, data));
816 }
817
818
819 #ifdef    SLIT
820 void Openbox::saveSlitSearch(Window window, Slit *data) {
821   slitSearchList.insert(SlitLookupPair(window, data));
822 }
823 #endif // SLIT
824
825
826 void Openbox::removeWindowSearch(Window window) {
827   windowSearchList.erase(window);
828 }
829
830
831 void Openbox::removeGroupSearch(Window window) {
832   groupSearchList.erase(window);
833 }
834
835
836 void Openbox::removeMenuSearch(Window window) {
837   menuSearchList.erase(window);
838 }
839
840
841 void Openbox::removeToolbarSearch(Window window) {
842   toolbarSearchList.erase(window);
843 }
844
845
846 #ifdef    SLIT
847 void Openbox::removeSlitSearch(Window window) {
848   slitSearchList.erase(window);
849 }
850 #endif // SLIT
851
852
853 void Openbox::restart(const char *prog) {
854   shutdown();
855
856   if (prog) {
857     execlp(prog, prog, NULL);
858     perror(prog);
859   }
860
861   // fall back in case the above execlp doesn't work
862   execvp(argv[0], argv);
863   execvp(basename(argv[0]), argv);
864 }
865
866
867 void Openbox::shutdown() {
868   BaseDisplay::shutdown();
869
870   focusWindow((OpenboxWindow *) 0);
871
872   std::for_each(screenList.begin(), screenList.end(),
873                 std::mem_fun(&BScreen::shutdown));
874
875   XSync(getXDisplay(), False);
876 }
877
878
879 void Openbox::save() {
880   config.setAutoSave(false);
881   
882   // save all values as they are so that the defaults will be written to the rc
883   // file
884   
885   config.setValue("session.menuFile", getMenuFilename());
886   config.setValue("session.colorsPerChannel",
887                   resource.colors_per_channel);
888   config.setValue("session.styleFile", resource.style_file);
889   config.setValue("session.titlebarLayout", resource.titlebar_layout);
890   config.setValue("session.doubleClickInterval",
891                   (long)resource.double_click_interval);
892   config.setValue("session.autoRaiseDelay",
893           ((resource.auto_raise_delay.tv_sec * 1000) +
894            (resource.auto_raise_delay.tv_usec / 1000)));
895   config.setValue("session.cacheLife", (long)resource.cache_life / 60000);
896   config.setValue("session.cacheMax", (long)resource.cache_max);
897
898   std::for_each(screenList.begin(), screenList.end(),
899                 std::mem_fun(&BScreen::save));
900
901   config.setAutoSave(true);
902   config.save();
903 }
904
905 void Openbox::load() {
906   if (!config.load())
907     config.create();
908
909   std::string s;
910   long l;
911   
912   if (resource.menu_file)
913     delete [] resource.menu_file;
914   if (config.getValue("session.menuFile", "Session.MenuFile", s))
915     resource.menu_file = bstrdup(s.c_str());
916   else
917     resource.menu_file = bstrdup(DEFAULTMENU);
918
919   if (config.getValue("session.colorsPerChannel", "Session.ColorsPerChannel",
920                       l))
921     resource.colors_per_channel = (l < 2 ? 2 : (l > 6 ? 6 : l)); // >= 2, <= 6
922   else
923     resource.colors_per_channel = 4;
924
925   if (resource.style_file)
926     delete [] resource.style_file;
927   if (config.getValue("session.styleFile", "Session.StyleFile", s))
928     resource.style_file = bstrdup(s.c_str());
929   else
930     resource.style_file = bstrdup(DEFAULTSTYLE);
931
932   if (resource.titlebar_layout)
933     delete [] resource.titlebar_layout;
934   if (config.getValue("session.titlebarLayout", "Session.TitlebarLayout", s))
935     resource.titlebar_layout = bstrdup(s.c_str());
936   else
937     resource.titlebar_layout = bstrdup("ILMC");
938
939   if (config.getValue("session.doubleClickInterval",
940                       "Session.DoubleClickInterval", l))
941     resource.double_click_interval = l;
942   else
943     resource.double_click_interval = 250;
944
945   if (!config.getValue("session.autoRaiseDelay", "Session.AutoRaiseDelay", l))
946     resource.auto_raise_delay.tv_usec = l;
947   else
948     resource.auto_raise_delay.tv_usec = 400;
949   resource.auto_raise_delay.tv_sec = resource.auto_raise_delay.tv_usec / 1000;
950   resource.auto_raise_delay.tv_usec -=
951     (resource.auto_raise_delay.tv_sec * 1000);
952   resource.auto_raise_delay.tv_usec *= 1000;
953
954   if (config.getValue("session.cacheLife", "Session.CacheLife", l))
955     resource.cache_life = l;
956   else
957     resource.cache_life = 51;
958   resource.cache_life *= 60000;
959
960   if (config.getValue("session.cacheMax", "Session.CacheMax", l))
961     resource.cache_max = l;
962   else
963     resource.cache_max = 200;
964 }
965
966
967 void Openbox::reconfigure() {
968   reconfigure_wait = True;
969
970   if (! timer->isTiming()) timer->start();
971 }
972
973
974 void Openbox::real_reconfigure() {
975   grab();
976
977   load();
978   
979   for_each(menuTimestamps.begin(), menuTimestamps.end(),
980            PointerAssassin());
981   menuTimestamps.clear();
982
983   std::for_each(screenList.begin(), screenList.end(),
984                 std::mem_fun(&BScreen::reconfigure));
985
986   ungrab();
987 }
988
989
990 void Openbox::checkMenu() {
991   MenuTimestampList::iterator it;
992   for (it = menuTimestamps.begin(); it != menuTimestamps.end(); ++it) {
993     struct stat buf;
994
995     if (stat((*it)->filename, &buf) || (*it)->timestamp != buf.st_ctime) {
996       rereadMenu();
997       return;
998     }
999   }
1000 }
1001
1002
1003 void Openbox::rereadMenu() {
1004   reread_menu_wait = True;
1005
1006   if (! timer->isTiming()) timer->start();
1007 }
1008
1009
1010 void Openbox::real_rereadMenu() {
1011   std::for_each(menuTimestamps.begin(), menuTimestamps.end(),
1012                 PointerAssassin());
1013   menuTimestamps.clear();
1014
1015   std::for_each(screenList.begin(), screenList.end(),
1016                 std::mem_fun(&BScreen::rereadMenu));
1017 }
1018
1019
1020 void Openbox::setStyleFilename(const char *filename) {
1021   if (resource.style_file)
1022     delete [] resource.style_file;
1023
1024   resource.style_file = bstrdup(filename);
1025   config.setValue("session.styleFile", resource.style_file);
1026 }
1027
1028
1029 void Openbox::setMenuFilename(const char *filename) {
1030   bool found = false;
1031
1032   MenuTimestampList::iterator it;
1033   for (it = menuTimestamps.begin(); it != menuTimestamps.end(); ++it)
1034     if (! strcmp((*it)->filename, filename)) {
1035       found = true;
1036       break;
1037     }
1038   if (!found) {
1039     struct stat buf;
1040
1041     if (! stat(filename, &buf)) {
1042       MenuTimestamp *ts = new MenuTimestamp;
1043
1044       ts->filename = bstrdup(filename);
1045       ts->timestamp = buf.st_ctime;
1046
1047       menuTimestamps.push_back(ts);
1048     }
1049   }
1050 }
1051
1052
1053 void Openbox::timeout() {
1054   if (reconfigure_wait)
1055     real_reconfigure();
1056
1057   if (reread_menu_wait)
1058     real_rereadMenu();
1059
1060   reconfigure_wait = reread_menu_wait = False;
1061 }
1062
1063
1064 OpenboxWindow *Openbox::focusedWindow() {
1065   if (focused_screen == (BScreen *) 0)
1066     return (OpenboxWindow *) 0;
1067   Workspace *w = focused_screen->getCurrentWorkspace();
1068   if (w == (Workspace *) 0)
1069     return (OpenboxWindow *) 0;
1070   return w->focusedWindow();
1071 }
1072
1073
1074 void Openbox::focusWindow(OpenboxWindow *win) {
1075   BScreen *old_screen = (BScreen *) 0;
1076   Toolbar *old_tbar = (Toolbar *) 0, *tbar = (Toolbar *) 0;
1077   Workspace *old_wkspc = (Workspace *) 0, *wkspc = (Workspace *) 0;
1078
1079   OpenboxWindow *old_win = focusedWindow();
1080   if (old_win != (OpenboxWindow *) 0) {
1081     old_screen = old_win->getScreen();
1082     old_wkspc = old_screen->getWorkspace(old_win->getWorkspaceNumber());
1083     old_tbar = old_screen->getToolbar();
1084
1085     old_win->setFocusFlag(False);
1086     old_wkspc->focusWindow((OpenboxWindow *) 0);
1087   }
1088
1089   if (win && !win->isIconic()) {
1090     focused_screen = win->getScreen();
1091     tbar = focused_screen->getToolbar();
1092     wkspc = focused_screen->getWorkspace(win->getWorkspaceNumber());
1093     win->setFocusFlag(true);
1094     wkspc->focusWindow(win);
1095     
1096     if (tbar)
1097       tbar->redrawWindowLabel(true);
1098     focused_screen->updateNetizenWindowFocus();
1099   } else {
1100     ASSERT(focused_screen != (BScreen *) 0);
1101     XSetInputFocus(getXDisplay(), focused_screen->getRootWindow(),
1102                    None, CurrentTime);
1103   }
1104
1105   if (old_tbar && old_tbar != tbar)
1106     old_tbar->redrawWindowLabel(true);
1107   if (old_screen && old_screen != focused_screen)
1108     old_screen->updateNetizenWindowFocus();
1109 }