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