removed all Xrm database calls from Screen.cc (style loading).
[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
59 #include <string>
60 #include <algorithm>
61
62 #ifdef    HAVE_STDIO_H
63 #  include <stdio.h>
64 #endif // HAVE_STDIO_H
65
66 #ifdef    STDC_HEADERS
67 #  include <stdlib.h>
68 #  include <string.h>
69 #endif // STDC_HEADERS
70
71 #ifdef    HAVE_UNISTD_H
72 #  include <sys/types.h>
73 #  include <unistd.h>
74 #endif // HAVE_UNISTD_H
75
76 #ifdef    HAVE_SYS_PARAM_H
77 #  include <sys/param.h>
78 #endif // HAVE_SYS_PARAM_H
79
80 #ifndef   MAXPATHLEN
81 #define   MAXPATHLEN 255
82 #endif // MAXPATHLEN
83
84 #ifdef    HAVE_SYS_SELECT_H
85 #  include <sys/select.h>
86 #endif // HAVE_SYS_SELECT_H
87
88 #ifdef    HAVE_SIGNAL_H
89 #  include <signal.h>
90 #endif // HAVE_SIGNAL_H
91
92 #ifdef    HAVE_SYS_SIGNAL_H
93 #  include <sys/signal.h>
94 #endif // HAVE_SYS_SIGNAL_H
95
96 #ifdef    HAVE_SYS_STAT_H
97 #  include <sys/types.h>
98 #  include <sys/stat.h>
99 #endif // HAVE_SYS_STAT_H
100
101 #ifdef    TIME_WITH_SYS_TIME
102 #  include <sys/time.h>
103 #  include <time.h>
104 #else // !TIME_WITH_SYS_TIME
105 #  ifdef    HAVE_SYS_TIME_H
106 #    include <sys/time.h>
107 #  else // !HAVE_SYS_TIME_H
108 #    include <time.h>
109 #  endif // HAVE_SYS_TIME_H
110 #endif // TIME_WITH_SYS_TIME
111
112 #ifdef    HAVE_LIBGEN_H
113 #  include <libgen.h>
114 #endif // HAVE_LIBGEN_H
115
116 #ifndef   HAVE_BASENAME
117 static inline char *basename (char *s) {
118   char *save = s;
119
120   while (*s) if (*s++ == '/') save = s;
121
122   return save;
123 }
124 #endif // HAVE_BASENAME
125
126
127 // X event scanner for enter/leave notifies - adapted from twm
128 typedef struct scanargs {
129   Window w;
130   Bool leave, inferior, enter;
131 } scanargs;
132
133 static Bool queueScanner(Display *, XEvent *e, char *args) {
134   if ((e->type == LeaveNotify) &&
135       (e->xcrossing.window == ((scanargs *) args)->w) &&
136       (e->xcrossing.mode == NotifyNormal)) {
137     ((scanargs *) args)->leave = True;
138     ((scanargs *) args)->inferior = (e->xcrossing.detail == NotifyInferior);
139   } else if ((e->type == EnterNotify) &&
140              (e->xcrossing.mode == NotifyUngrab)) {
141     ((scanargs *) args)->enter = True;
142   }
143
144   return False;
145 }
146
147 Openbox *openbox;
148
149
150 Openbox::Openbox(int m_argc, char **m_argv, char *dpy_name, char *rc)
151   : BaseDisplay(m_argv[0], dpy_name) {
152   grab();
153
154   if (! XSupportsLocale())
155     fprintf(stderr, "X server does not support locale\n");
156
157   if (XSetLocaleModifiers("") == NULL)
158     fprintf(stderr, "cannot set locale modifiers\n");
159
160   ::openbox = this;
161   argc = m_argc;
162   argv = m_argv;
163   if (rc == NULL) {
164     char *homedir = getenv("HOME");
165
166     rc_file = new char[strlen(homedir) + strlen("/.openbox/rc") + 1];
167     sprintf(rc_file, "%s/.openbox", homedir);
168
169     // try to make sure the ~/.openbox directory exists
170     mkdir(rc_file, S_IREAD | S_IWRITE | S_IEXEC | S_IRGRP | S_IWGRP | S_IXGRP |
171           S_IROTH | S_IWOTH | S_IXOTH);
172     
173     sprintf(rc_file, "%s/.openbox/rc", homedir);
174   } else {
175     rc_file = bstrdup(rc);
176   }
177   symlink_rc(rc_file);
178   config.setFile(rc_file);
179
180   no_focus = False;
181
182   resource.menu_file = resource.style_file = (char *) 0;
183   resource.titlebar_layout = (char *) NULL;
184   resource.auto_raise_delay.tv_sec = resource.auto_raise_delay.tv_usec = 0;
185
186   focused_window = masked_window = (OpenboxWindow *) 0;
187   masked = None;
188
189   windowSearchList = new LinkedList<WindowSearch>;
190   menuSearchList = new LinkedList<MenuSearch>;
191
192 #ifdef    SLIT
193   slitSearchList = new LinkedList<SlitSearch>;
194 #endif // SLIT
195
196   toolbarSearchList = new LinkedList<ToolbarSearch>;
197   groupSearchList = new LinkedList<WindowSearch>;
198
199   menuTimestamps = new LinkedList<MenuTimestamp>;
200
201   load_rc();
202
203 #ifdef    HAVE_GETPID
204   openbox_pid = XInternAtom(getXDisplay(), "_BLACKBOX_PID", False);
205 #endif // HAVE_GETPID
206
207   screenList = new LinkedList<BScreen>;
208   for (int i = 0; i < getNumberOfScreens(); i++) {
209     BScreen *screen = new BScreen(this, i);
210
211     if (! screen->isScreenManaged()) {
212       delete screen;
213       continue;
214     }
215
216     screenList->insert(screen);
217   }
218
219   if (! screenList->count()) {
220     fprintf(stderr,
221             i18n->getMessage(openboxSet, openboxNoManagableScreens,
222                "Openbox::Openbox: no managable screens found, aborting.\n"));
223     ::exit(3);
224   }
225
226   XSynchronize(getXDisplay(), False);
227   XSync(getXDisplay(), False);
228
229   reconfigure_wait = reread_menu_wait = False;
230
231   timer = new BTimer(this, this);
232   timer->setTimeout(0);
233   timer->fireOnce(True);
234
235   ungrab();
236 }
237
238
239 Openbox::~Openbox(void) {
240   while (screenList->count())
241     delete screenList->remove(0);
242
243   while (menuTimestamps->count()) {
244     MenuTimestamp *ts = menuTimestamps->remove(0);
245
246     if (ts->filename)
247       delete [] ts->filename;
248
249     delete ts;
250   }
251
252   if (resource.menu_file)
253     delete [] resource.menu_file;
254
255   if (resource.style_file)
256     delete [] resource.style_file;
257
258   delete timer;
259
260   delete screenList;
261   delete menuTimestamps;
262
263   delete windowSearchList;
264   delete menuSearchList;
265   delete toolbarSearchList;
266   delete groupSearchList;
267
268   delete [] rc_file;
269
270 #ifdef    SLIT
271   delete slitSearchList;
272 #endif // SLIT
273 }
274
275
276 void Openbox::process_event(XEvent *e) {
277   if ((masked == e->xany.window) && masked_window &&
278       (e->type == MotionNotify)) {
279     last_time = e->xmotion.time;
280     masked_window->motionNotifyEvent(&e->xmotion);
281
282     return;
283   }
284
285   switch (e->type) {
286   case ButtonPress: {
287     // strip the lock key modifiers
288     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
289
290     last_time = e->xbutton.time;
291
292     OpenboxWindow *win = (OpenboxWindow *) 0;
293     Basemenu *menu = (Basemenu *) 0;
294
295 #ifdef    SLIT
296     Slit *slit = (Slit *) 0;
297 #endif // SLIT
298
299     Toolbar *tbar = (Toolbar *) 0;
300
301     if ((win = searchWindow(e->xbutton.window))) {
302       win->buttonPressEvent(&e->xbutton);
303
304       if (e->xbutton.button == 1)
305         win->installColormap(True);
306     } else if ((menu = searchMenu(e->xbutton.window))) {
307       menu->buttonPressEvent(&e->xbutton);
308
309 #ifdef    SLIT
310     } else if ((slit = searchSlit(e->xbutton.window))) {
311       slit->buttonPressEvent(&e->xbutton);
312 #endif // SLIT
313
314     } else if ((tbar = searchToolbar(e->xbutton.window))) {
315       tbar->buttonPressEvent(&e->xbutton);
316     } else {
317       LinkedListIterator<BScreen> it(screenList);
318       BScreen *screen = it.current();
319       for (; screen; it++, screen = it.current()) {
320         if (e->xbutton.window == screen->getRootWindow()) {
321           if (e->xbutton.button == 1) {
322             if (! screen->isRootColormapInstalled())
323               screen->getImageControl()->installRootColormap();
324
325             if (screen->getWorkspacemenu()->isVisible())
326               screen->getWorkspacemenu()->hide();
327
328             if (screen->getRootmenu()->isVisible())
329               screen->getRootmenu()->hide();
330           } else if (e->xbutton.button == 2) {
331             int mx = e->xbutton.x_root -
332               (screen->getWorkspacemenu()->getWidth() / 2);
333             int my = e->xbutton.y_root -
334               (screen->getWorkspacemenu()->getTitleHeight() / 2);
335
336             if (mx < 0) mx = 0;
337             if (my < 0) my = 0;
338
339             if (mx + screen->getWorkspacemenu()->getWidth() >
340                 screen->getWidth())
341               mx = screen->getWidth() -
342                 screen->getWorkspacemenu()->getWidth() -
343                 screen->getBorderWidth();
344
345             if (my + screen->getWorkspacemenu()->getHeight() >
346                 screen->getHeight())
347               my = screen->getHeight() -
348                 screen->getWorkspacemenu()->getHeight() -
349                 screen->getBorderWidth();
350
351             screen->getWorkspacemenu()->move(mx, my);
352
353             if (! screen->getWorkspacemenu()->isVisible()) {
354               screen->getWorkspacemenu()->removeParent();
355               screen->getWorkspacemenu()->show();
356             }
357           } else if (e->xbutton.button == 3) {
358             int mx = e->xbutton.x_root -
359               (screen->getRootmenu()->getWidth() / 2);
360             int my = e->xbutton.y_root -
361               (screen->getRootmenu()->getTitleHeight() / 2);
362
363             if (mx < 0) mx = 0;
364             if (my < 0) my = 0;
365
366             if (mx + screen->getRootmenu()->getWidth() > screen->getWidth())
367               mx = screen->getWidth() -
368                 screen->getRootmenu()->getWidth() -
369                 screen->getBorderWidth();
370
371             if (my + screen->getRootmenu()->getHeight() > screen->getHeight())
372                 my = screen->getHeight() -
373                   screen->getRootmenu()->getHeight() -
374                   screen->getBorderWidth();
375
376             screen->getRootmenu()->move(mx, my);
377
378             if (! screen->getRootmenu()->isVisible()) {
379               checkMenu();
380               screen->getRootmenu()->show();
381             }
382           } else if (e->xbutton.button == 4) {
383             if ((screen->getCurrentWorkspaceID()-1)<0)
384               screen->changeWorkspaceID(screen->getWorkspaceCount()-1);
385             else
386               screen->changeWorkspaceID(screen->getCurrentWorkspaceID()-1);
387           } else if (e->xbutton.button == 5) {
388             if ((screen->getCurrentWorkspaceID()+1)>screen->getWorkspaceCount()-1)
389               screen->changeWorkspaceID(0);
390             else
391               screen->changeWorkspaceID(screen->getCurrentWorkspaceID()+1);
392           }
393         }
394       }
395     }
396
397     break;
398   }
399
400   case ButtonRelease: {
401     // strip the lock key modifiers
402     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
403
404     last_time = e->xbutton.time;
405
406     OpenboxWindow *win = (OpenboxWindow *) 0;
407     Basemenu *menu = (Basemenu *) 0;
408     Toolbar *tbar = (Toolbar *) 0;
409
410     if ((win = searchWindow(e->xbutton.window)))
411       win->buttonReleaseEvent(&e->xbutton);
412     else if ((menu = searchMenu(e->xbutton.window)))
413       menu->buttonReleaseEvent(&e->xbutton);
414     else if ((tbar = searchToolbar(e->xbutton.window)))
415       tbar->buttonReleaseEvent(&e->xbutton);
416
417     break;
418   }
419
420   case ConfigureRequest: {
421     OpenboxWindow *win = (OpenboxWindow *) 0;
422
423 #ifdef    SLIT
424     Slit *slit = (Slit *) 0;
425 #endif // SLIT
426
427     if ((win = searchWindow(e->xconfigurerequest.window))) {
428       win->configureRequestEvent(&e->xconfigurerequest);
429
430 #ifdef    SLIT
431     } else if ((slit = searchSlit(e->xconfigurerequest.window))) {
432       slit->configureRequestEvent(&e->xconfigurerequest);
433 #endif // SLIT
434
435     } else {
436       grab();
437
438       if (validateWindow(e->xconfigurerequest.window)) {
439         XWindowChanges xwc;
440
441         xwc.x = e->xconfigurerequest.x;
442         xwc.y = e->xconfigurerequest.y;
443         xwc.width = e->xconfigurerequest.width;
444         xwc.height = e->xconfigurerequest.height;
445         xwc.border_width = e->xconfigurerequest.border_width;
446         xwc.sibling = e->xconfigurerequest.above;
447         xwc.stack_mode = e->xconfigurerequest.detail;
448
449         XConfigureWindow(getXDisplay(), e->xconfigurerequest.window,
450                          e->xconfigurerequest.value_mask, &xwc);
451       }
452
453       ungrab();
454     }
455
456     break;
457   }
458
459   case MapRequest: {
460 #ifdef    DEBUG
461     fprintf(stderr,
462             i18n->getMessage(openboxSet, openboxMapRequest,
463                  "Openbox::process_event(): MapRequest for 0x%lx\n"),
464             e->xmaprequest.window);
465 #endif // DEBUG
466
467     OpenboxWindow *win = searchWindow(e->xmaprequest.window);
468
469     if (! win)
470       win = new OpenboxWindow(this, e->xmaprequest.window);
471
472     if ((win = searchWindow(e->xmaprequest.window)))
473       win->mapRequestEvent(&e->xmaprequest);
474
475     break;
476   }
477
478   case MapNotify: {
479     OpenboxWindow *win = searchWindow(e->xmap.window);
480
481     if (win)
482       win->mapNotifyEvent(&e->xmap);
483
484       break;
485   }
486
487   case UnmapNotify: {
488     OpenboxWindow *win = (OpenboxWindow *) 0;
489
490 #ifdef    SLIT
491     Slit *slit = (Slit *) 0;
492 #endif // SLIT
493
494     if ((win = searchWindow(e->xunmap.window))) {
495       win->unmapNotifyEvent(&e->xunmap);
496       if (focused_window == win)
497         focused_window = (OpenboxWindow *) 0;
498 #ifdef    SLIT
499     } else if ((slit = searchSlit(e->xunmap.window))) {
500       slit->removeClient(e->xunmap.window);
501 #endif // SLIT
502
503     }
504
505     break;
506   }
507
508   case DestroyNotify: {
509     OpenboxWindow *win = (OpenboxWindow *) 0;
510
511 #ifdef    SLIT
512     Slit *slit = (Slit *) 0;
513 #endif // SLIT
514
515     if ((win = searchWindow(e->xdestroywindow.window))) {
516       win->destroyNotifyEvent(&e->xdestroywindow);
517       if (focused_window == win)
518         focused_window = (OpenboxWindow *) 0;
519 #ifdef    SLIT
520     } else if ((slit = searchSlit(e->xdestroywindow.window))) {
521       slit->removeClient(e->xdestroywindow.window, False);
522 #endif // SLIT
523     }
524
525     break;
526   }
527
528   case MotionNotify: {
529     // strip the lock key modifiers
530     e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
531     
532     last_time = e->xmotion.time;
533
534     OpenboxWindow *win = (OpenboxWindow *) 0;
535     Basemenu *menu = (Basemenu *) 0;
536
537     if ((win = searchWindow(e->xmotion.window)))
538       win->motionNotifyEvent(&e->xmotion);
539     else if ((menu = searchMenu(e->xmotion.window)))
540       menu->motionNotifyEvent(&e->xmotion);
541
542     break;
543   }
544
545   case PropertyNotify: {
546     last_time = e->xproperty.time;
547
548     if (e->xproperty.state != PropertyDelete) {
549       OpenboxWindow *win = searchWindow(e->xproperty.window);
550
551       if (win)
552         win->propertyNotifyEvent(e->xproperty.atom);
553     }
554
555     break;
556   }
557
558   case EnterNotify: {
559     last_time = e->xcrossing.time;
560
561     BScreen *screen = (BScreen *) 0;
562     OpenboxWindow *win = (OpenboxWindow *) 0;
563     Basemenu *menu = (Basemenu *) 0;
564     Toolbar *tbar = (Toolbar *) 0;
565
566 #ifdef    SLIT
567     Slit *slit = (Slit *) 0;
568 #endif // SLIT
569
570     if (e->xcrossing.mode == NotifyGrab) break;
571
572     XEvent dummy;
573     scanargs sa;
574     sa.w = e->xcrossing.window;
575     sa.enter = sa.leave = False;
576     XCheckIfEvent(getXDisplay(), &dummy, queueScanner, (char *) &sa);
577
578     if ((e->xcrossing.window == e->xcrossing.root) &&
579         (screen = searchScreen(e->xcrossing.window))) {
580       screen->getImageControl()->installRootColormap();
581     } else if ((win = searchWindow(e->xcrossing.window))) {
582       if (win->getScreen()->isSloppyFocus() &&
583           (! win->isFocused()) && (! no_focus)) {
584         grab();
585
586         if (((! sa.leave) || sa.inferior) && win->isVisible() &&
587             win->setInputFocus())
588           win->installColormap(True);
589
590         ungrab();
591       }
592     } else if ((menu = searchMenu(e->xcrossing.window))) {
593       menu->enterNotifyEvent(&e->xcrossing);
594     } else if ((tbar = searchToolbar(e->xcrossing.window))) {
595       tbar->enterNotifyEvent(&e->xcrossing);
596 #ifdef    SLIT
597     } else if ((slit = searchSlit(e->xcrossing.window))) {
598       slit->enterNotifyEvent(&e->xcrossing);
599 #endif // SLIT
600     }
601     break;
602   }
603
604   case LeaveNotify: {
605     last_time = e->xcrossing.time;
606
607     OpenboxWindow *win = (OpenboxWindow *) 0;
608     Basemenu *menu = (Basemenu *) 0;
609     Toolbar *tbar = (Toolbar *) 0;
610
611 #ifdef    SLIT
612     Slit *slit = (Slit *) 0;
613 #endif // SLIT
614
615     if ((menu = searchMenu(e->xcrossing.window)))
616       menu->leaveNotifyEvent(&e->xcrossing);
617     else if ((win = searchWindow(e->xcrossing.window)))
618       win->installColormap(False);
619     else if ((tbar = searchToolbar(e->xcrossing.window)))
620       tbar->leaveNotifyEvent(&e->xcrossing);
621 #ifdef    SLIT
622     else if ((slit = searchSlit(e->xcrossing.window)))
623       slit->leaveNotifyEvent(&e->xcrossing);
624 #endif // SLIT
625
626     break;
627   }
628
629   case Expose: {
630     OpenboxWindow *win = (OpenboxWindow *) 0;
631     Basemenu *menu = (Basemenu *) 0;
632     Toolbar *tbar = (Toolbar *) 0;
633
634     if ((win = searchWindow(e->xexpose.window)))
635       win->exposeEvent(&e->xexpose);
636     else if ((menu = searchMenu(e->xexpose.window)))
637       menu->exposeEvent(&e->xexpose);
638     else if ((tbar = searchToolbar(e->xexpose.window)))
639       tbar->exposeEvent(&e->xexpose);
640
641     break;
642   }
643
644   case KeyPress: {
645     Toolbar *tbar = searchToolbar(e->xkey.window);
646
647     if (tbar && tbar->isEditing())
648       tbar->keyPressEvent(&e->xkey);
649
650     break;
651   }
652
653   case ColormapNotify: {
654     BScreen *screen = searchScreen(e->xcolormap.window);
655
656     if (screen)
657       screen->setRootColormapInstalled((e->xcolormap.state ==
658                                         ColormapInstalled) ? True : False);
659
660     break;
661   }
662
663   case FocusIn: {
664     if (e->xfocus.mode == NotifyUngrab || e->xfocus.detail == NotifyPointer)
665       break;
666
667     OpenboxWindow *win = searchWindow(e->xfocus.window);
668     if (win && ! win->isFocused())
669       setFocusedWindow(win);
670
671     break;
672   }
673
674   case FocusOut:
675     break;
676
677   case ClientMessage: {
678     if (e->xclient.format == 32) {
679       if (e->xclient.message_type == getWMChangeStateAtom()) {
680         OpenboxWindow *win = searchWindow(e->xclient.window);
681         if (! win || ! win->validateClient()) return;
682
683         if (e->xclient.data.l[0] == IconicState)
684           win->iconify();
685         if (e->xclient.data.l[0] == NormalState)
686           win->deiconify();
687       } else if (e->xclient.message_type == getOpenboxChangeWorkspaceAtom()) {
688         BScreen *screen = searchScreen(e->xclient.window);
689
690         if (screen && e->xclient.data.l[0] >= 0 &&
691             e->xclient.data.l[0] < screen->getWorkspaceCount())
692           screen->changeWorkspaceID(e->xclient.data.l[0]);
693       } else if (e->xclient.message_type == getOpenboxChangeWindowFocusAtom()) {
694         OpenboxWindow *win = searchWindow(e->xclient.window);
695
696         if (win && win->isVisible() && win->setInputFocus())
697           win->installColormap(True);
698       } else if (e->xclient.message_type == getOpenboxCycleWindowFocusAtom()) {
699         BScreen *screen = searchScreen(e->xclient.window);
700
701         if (screen) {
702           if (! e->xclient.data.l[0])
703             screen->prevFocus();
704           else
705             screen->nextFocus();
706         }
707       } else if (e->xclient.message_type == getOpenboxChangeAttributesAtom()) {
708         OpenboxWindow *win = searchWindow(e->xclient.window);
709
710         if (win && win->validateClient()) {
711           OpenboxHints net;
712           net.flags = e->xclient.data.l[0];
713           net.attrib = e->xclient.data.l[1];
714           net.workspace = e->xclient.data.l[2];
715           net.stack = e->xclient.data.l[3];
716           net.decoration = e->xclient.data.l[4];
717
718           win->changeOpenboxHints(&net);
719         }
720       }
721     }
722
723     break;
724   }
725
726
727   default: {
728 #ifdef    SHAPE
729     if (e->type == getShapeEventBase()) {
730       XShapeEvent *shape_event = (XShapeEvent *) e;
731       OpenboxWindow *win = (OpenboxWindow *) 0;
732
733       if ((win = searchWindow(e->xany.window)) ||
734           (shape_event->kind != ShapeBounding))
735         win->shapeEvent(shape_event);
736     }
737 #endif // SHAPE
738
739   }
740   } // switch
741 }
742
743
744 Bool Openbox::handleSignal(int sig) {
745   switch (sig) {
746   case SIGHUP:
747     reconfigure();
748     break;
749
750   case SIGUSR1:
751     reload_rc();
752     break;
753
754   case SIGUSR2:
755     rereadMenu();
756     break;
757
758   case SIGPIPE:
759   case SIGSEGV:
760   case SIGFPE:
761   case SIGINT:
762   case SIGTERM:
763     shutdown();
764
765   default:
766     return False;
767   }
768
769   return True;
770 }
771
772
773 BScreen *Openbox::searchScreen(Window window) {
774   LinkedListIterator<BScreen> it(screenList);
775
776   for (BScreen *curr = it.current(); curr; it++, curr = it.current()) {
777     if (curr->getRootWindow() == window) {
778       return curr;
779     }
780   }
781
782   return (BScreen *) 0;
783 }
784
785
786 OpenboxWindow *Openbox::searchWindow(Window window) {
787   LinkedListIterator<WindowSearch> it(windowSearchList);
788
789   for (WindowSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
790       if (tmp->getWindow() == window) {
791         return tmp->getData();
792       }
793   }
794
795   return (OpenboxWindow *) 0;
796 }
797
798
799 OpenboxWindow *Openbox::searchGroup(Window window, OpenboxWindow *win) {
800   OpenboxWindow *w = (OpenboxWindow *) 0;
801   LinkedListIterator<WindowSearch> it(groupSearchList);
802
803   for (WindowSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
804     if (tmp->getWindow() == window) {
805       w = tmp->getData();
806       if (w->getClientWindow() != win->getClientWindow())
807         return win;
808     }
809   }
810
811   return (OpenboxWindow *) 0;
812 }
813
814
815 Basemenu *Openbox::searchMenu(Window window) {
816   LinkedListIterator<MenuSearch> it(menuSearchList);
817
818   for (MenuSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
819     if (tmp->getWindow() == window)
820       return tmp->getData();
821   }
822
823   return (Basemenu *) 0;
824 }
825
826
827 Toolbar *Openbox::searchToolbar(Window window) {
828   LinkedListIterator<ToolbarSearch> it(toolbarSearchList);
829
830   for (ToolbarSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
831     if (tmp->getWindow() == window)
832       return tmp->getData();
833   }
834
835   return (Toolbar *) 0;
836 }
837
838
839 #ifdef    SLIT
840 Slit *Openbox::searchSlit(Window window) {
841   LinkedListIterator<SlitSearch> it(slitSearchList);
842
843   for (SlitSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
844     if (tmp->getWindow() == window)
845       return tmp->getData();
846   }
847
848   return (Slit *) 0;
849 }
850 #endif // SLIT
851
852
853 void Openbox::saveWindowSearch(Window window, OpenboxWindow *data) {
854   windowSearchList->insert(new WindowSearch(window, data));
855 }
856
857
858 void Openbox::saveGroupSearch(Window window, OpenboxWindow *data) {
859   groupSearchList->insert(new WindowSearch(window, data));
860 }
861
862
863 void Openbox::saveMenuSearch(Window window, Basemenu *data) {
864   menuSearchList->insert(new MenuSearch(window, data));
865 }
866
867
868 void Openbox::saveToolbarSearch(Window window, Toolbar *data) {
869   toolbarSearchList->insert(new ToolbarSearch(window, data));
870 }
871
872
873 #ifdef    SLIT
874 void Openbox::saveSlitSearch(Window window, Slit *data) {
875   slitSearchList->insert(new SlitSearch(window, data));
876 }
877 #endif // SLIT
878
879
880 void Openbox::removeWindowSearch(Window window) {
881   LinkedListIterator<WindowSearch> it(windowSearchList);
882   for (WindowSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
883     if (tmp->getWindow() == window) {
884       windowSearchList->remove(tmp);
885       delete tmp;
886       break;
887     }
888   }
889 }
890
891
892 void Openbox::removeGroupSearch(Window window) {
893   LinkedListIterator<WindowSearch> it(groupSearchList);
894   for (WindowSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
895     if (tmp->getWindow() == window) {
896       groupSearchList->remove(tmp);
897       delete tmp;
898       break;
899     }
900   }
901 }
902
903
904 void Openbox::removeMenuSearch(Window window) {
905   LinkedListIterator<MenuSearch> it(menuSearchList);
906   for (MenuSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
907     if (tmp->getWindow() == window) {
908       menuSearchList->remove(tmp);
909       delete tmp;
910       break;
911     }
912   }
913 }
914
915
916 void Openbox::removeToolbarSearch(Window window) {
917   LinkedListIterator<ToolbarSearch> it(toolbarSearchList);
918   for (ToolbarSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
919     if (tmp->getWindow() == window) {
920       toolbarSearchList->remove(tmp);
921       delete tmp;
922       break;
923     }
924   }
925 }
926
927
928 #ifdef    SLIT
929 void Openbox::removeSlitSearch(Window window) {
930   LinkedListIterator<SlitSearch> it(slitSearchList);
931   for (SlitSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
932     if (tmp->getWindow() == window) {
933       slitSearchList->remove(tmp);
934       delete tmp;
935       break;
936     }
937   }
938 }
939 #endif // SLIT
940
941
942 void Openbox::restart(const char *prog) {
943   shutdown();
944
945   if (prog) {
946     execlp(prog, prog, NULL);
947     perror(prog);
948   }
949
950   // fall back in case the above execlp doesn't work
951   execvp(argv[0], argv);
952   execvp(basename(argv[0]), argv);
953 }
954
955
956 void Openbox::shutdown(void) {
957   BaseDisplay::shutdown();
958
959   XSetInputFocus(getXDisplay(), PointerRoot, None, CurrentTime);
960
961   LinkedListIterator<BScreen> it(screenList);
962   for (BScreen *s = it.current(); s; it++, s = it.current())
963     s->shutdown();
964
965   XSync(getXDisplay(), False);
966
967   save_rc();
968 }
969
970
971 void Openbox::save_rc(void) {
972   config.setAutoSave(false);
973   
974   config.setValue("session.menuFile", getMenuFilename());
975   config.setValue("session.colorsPerChannel",
976                   resource.colors_per_channel);
977   config.setValue("session.doubleClickInterval",
978                   (long)resource.double_click_interval);
979   config.setValue("session.autoRaiseDelay",
980           ((resource.auto_raise_delay.tv_sec * 1000) +
981            (resource.auto_raise_delay.tv_usec / 1000)));
982   config.setValue("session.cacheLife", (long)resource.cache_life / 60000);
983   config.setValue("session.cacheMax", (long)resource.cache_max);
984
985   LinkedListIterator<BScreen> it(screenList);
986   for (BScreen *screen = it.current(); screen; it++, screen = it.current()) {
987 //  ScreenList::iterator it = screenList.begin();
988 //  for (; it != screenList.end(); ++it) {
989 //    BScreen *screen = *it;
990     char rc_string[1024];
991     const int screen_number = screen->getScreenNumber();
992
993     char *placement = (char *) 0;
994
995     switch (screen->getSlitPlacement()) {
996     case Slit::TopLeft: placement = "TopLeft"; break;
997     case Slit::CenterLeft: placement = "CenterLeft"; break;
998     case Slit::BottomLeft: placement = "BottomLeft"; break;
999     case Slit::TopCenter: placement = "TopCenter"; break;
1000     case Slit::BottomCenter: placement = "BottomCenter"; break;
1001     case Slit::TopRight: placement = "TopRight"; break;
1002     case Slit::BottomRight: placement = "BottomRight"; break;
1003     case Slit::CenterRight: default: placement = "CenterRight"; break;
1004     }
1005     sprintf(rc_string, "session.screen%d.slit.placement", screen_number);
1006     config.setValue(rc_string, placement);
1007
1008     sprintf(rc_string, "session.screen%d.slit.direction", screen_number);
1009     config.setValue(rc_string,
1010                     screen->getSlitDirection() == Slit::Horizontal ?
1011                     "Horizontal" : "Vertical");
1012
1013     sprintf(rc_string, "session.screen%d.slit.onTop", screen_number);
1014     config.setValue(rc_string, screen->getSlit()->isOnTop() ? "True" : "False");
1015
1016     sprintf(rc_string, "session.screen%d.slit.autoHide", screen_number);
1017     config.setValue(rc_string, screen->getSlit()->doAutoHide() ?
1018                     "True" : "False");
1019
1020     config.setValue("session.opaqueMove",
1021                     (screen->doOpaqueMove()) ? "True" : "False");
1022     config.setValue("session.imageDither",
1023                     (screen->getImageControl()->doDither()) ? "True" : "False");
1024
1025     sprintf(rc_string, "session.screen%d.fullMaximization", screen_number);
1026     config.setValue(rc_string, screen->doFullMax() ? "True" : "False");
1027
1028     sprintf(rc_string, "session.screen%d.focusNewWindows", screen_number);
1029     config.setValue(rc_string, screen->doFocusNew() ? "True" : "False");
1030
1031     sprintf(rc_string, "session.screen%d.focusLastWindow", screen_number);
1032     config.setValue(rc_string, screen->doFocusLast() ? "True" : "False");
1033
1034     sprintf(rc_string, "session.screen%d.rowPlacementDirection", screen_number);
1035     config.setValue(rc_string,
1036                     screen->getRowPlacementDirection() == BScreen::LeftRight ?
1037                     "LeftToRight" : "RightToLeft");
1038
1039     sprintf(rc_string, "session.screen%d.colPlacementDirection", screen_number);
1040     config.setValue(rc_string,
1041                     screen->getColPlacementDirection() == BScreen::TopBottom ?
1042                     "TopToBottom" : "BottomToTop");
1043
1044     switch (screen->getPlacementPolicy()) {
1045     case BScreen::CascadePlacement: placement = "CascadePlacement"; break;
1046     case BScreen::ColSmartPlacement: placement = "ColSmartPlacement"; break;
1047     default:
1048     case BScreen::RowSmartPlacement: placement = "RowSmartPlacement"; break;
1049     }
1050     sprintf(rc_string, "session.screen%d.windowPlacement", screen_number);
1051     config.setValue(rc_string, placement);
1052
1053     sprintf(rc_string, "session.screen%d.focusModel", screen_number);
1054     config.setValue(rc_string,
1055                     (screen->isSloppyFocus() ?
1056                      (screen->doAutoRaise() ? "AutoRaiseSloppyFocus" :
1057                       "SloppyFocus") : "ClickToFocus"));
1058
1059     sprintf(rc_string, "session.screen%d.workspaces", screen_number);
1060     config.setValue(rc_string, screen->getWorkspaceCount());
1061
1062     sprintf(rc_string, "session.screen%d.toolbar.onTop", screen_number);
1063     config.setValue(rc_string, screen->getToolbar()->isOnTop() ?
1064                     "True" : "False");
1065
1066     sprintf(rc_string, "session.screen%d.toolbar.autoHide", screen_number);
1067     config.setValue(rc_string, screen->getToolbar()->doAutoHide() ?
1068                     "True" : "False");
1069
1070     switch (screen->getToolbarPlacement()) {
1071     case Toolbar::TopLeft: placement = "TopLeft"; break;
1072     case Toolbar::BottomLeft: placement = "BottomLeft"; break;
1073     case Toolbar::TopCenter: placement = "TopCenter"; break;
1074     case Toolbar::TopRight: placement = "TopRight"; break;
1075     case Toolbar::BottomRight: placement = "BottomRight"; break;
1076     default:
1077     case Toolbar::BottomCenter: placement = "BottomCenter"; break;
1078     }
1079
1080     sprintf(rc_string, "session.screen%d.toolbar.placement", screen_number);
1081     config.setValue(rc_string, placement);
1082
1083 #ifdef    HAVE_STRFTIME
1084     sprintf(rc_string, "session.screen%d.strftimeFormat", screen_number);
1085     config.setValue(rc_string, screen->getStrftimeFormat());
1086 #else // !HAVE_STRFTIME
1087     sprintf(rc_string, "session.screen%d.dateFormat", screen_number);
1088     config.setValue(rc_string, screen->getDateFormat() == B_EuropeanDate ?
1089                     "European" : "American");
1090
1091     sprintf(rc_string, "session.screen%d.clockFormat", screen_number);
1092     config.setValue(rc_string, screen->isClock24Hour() ? 24 : 12);
1093 #endif // HAVE_STRFTIME
1094
1095     sprintf(rc_string, "session.screen%d.edgeSnapThreshold", screen_number);
1096     config.setValue(rc_string, screen->getEdgeSnapThreshold());
1097
1098     sprintf(rc_string, "session.screen%d.toolbar.widthPercent", screen_number);
1099     config.setValue(rc_string, screen->getToolbarWidthPercent());
1100
1101     // write out the user's workspace names
1102     int i, len = 0;
1103     for (i = 0; i < screen->getWorkspaceCount(); i++)
1104       len += strlen((screen->getWorkspace(i)->getName()) ?
1105         screen->getWorkspace(i)->getName() : "Null") + 1;
1106
1107     char *resource_string = new char[len + 1024],
1108       *save_string = new char[len], *save_string_pos = save_string,
1109       *name_string_pos;
1110     if (save_string) {
1111       for (i = 0; i < screen->getWorkspaceCount(); i++) {
1112         len = strlen((screen->getWorkspace(i)->getName()) ?
1113          screen->getWorkspace(i)->getName() : "Null") + 1;
1114         name_string_pos =
1115           (char *) ((screen->getWorkspace(i)->getName()) ?
1116                     screen->getWorkspace(i)->getName() : "Null");
1117         
1118         while (--len) *(save_string_pos++) = *(name_string_pos++);
1119         *(save_string_pos++) = ',';
1120       }
1121     }
1122
1123     *(--save_string_pos) = '\0';
1124
1125     sprintf(resource_string, "session.screen%d.workspaceNames", screen_number);
1126     config.setValue(resource_string, save_string);
1127
1128     delete [] resource_string;
1129     delete [] save_string;
1130 /*
1131     std::string save_string = screen->getWorkspace(0)->getName();
1132     for (unsigned int i = 1; i < screen->getWorkspaceCount(); ++i) {
1133       save_string += ',';
1134       save_string += screen->getWorkspace(i)->getName();
1135     }
1136
1137     char *resource_string = new char[save_string.length() + 48];
1138     sprintf(resource_string, "session.screen%d.workspaceNames", screen_number);
1139     config.setValue(rc_string, save_string);
1140
1141     delete [] resource_string;*/
1142   }
1143
1144   config.setAutoSave(true);
1145   config.save();
1146 }
1147
1148 void Openbox::load_rc(void) {
1149   if (!config.load())
1150     return;
1151
1152   std::string s;
1153   long l;
1154   
1155   if (resource.menu_file)
1156     delete [] resource.menu_file;
1157   if (config.getValue("session.menuFile", "Session.MenuFile", s))
1158     resource.menu_file = bstrdup(s.c_str());
1159   else
1160     resource.menu_file = bstrdup(DEFAULTMENU);
1161
1162   if (config.getValue("session.colorsPerChannel", "Session.ColorsPerChannel",
1163                       l))
1164     resource.colors_per_channel = (l < 2 ? 2 : (l > 6 ? 6 : l)); // >= 2, <= 6
1165   else
1166     resource.colors_per_channel = 4;
1167
1168   if (resource.style_file)
1169     delete [] resource.style_file;
1170   if (config.getValue("session.styleFile", "Session.StyleFile", s))
1171     resource.style_file = bstrdup(s.c_str());
1172   else
1173     resource.style_file = bstrdup(DEFAULTSTYLE);
1174
1175   if (resource.titlebar_layout)
1176     delete [] resource.titlebar_layout;
1177   if (config.getValue("session.titlebarLayout", "Session.TitlebarLayout", s))
1178     resource.titlebar_layout = bstrdup(s.c_str());
1179   else
1180     resource.titlebar_layout = bstrdup("ILMC");
1181
1182   if (config.getValue("session.doubleClickInterval",
1183                       "Session.DoubleClickInterval", l))
1184     resource.double_click_interval = l;
1185   else
1186     resource.double_click_interval = 250;
1187
1188   if (!config.getValue("session.autoRaiseDelay", "Session.AutoRaiseDelay", l))
1189     resource.auto_raise_delay.tv_usec = l;
1190   else
1191     resource.auto_raise_delay.tv_usec = 400;
1192   resource.auto_raise_delay.tv_sec = resource.auto_raise_delay.tv_usec / 1000;
1193   resource.auto_raise_delay.tv_usec -=
1194     (resource.auto_raise_delay.tv_sec * 1000);
1195   resource.auto_raise_delay.tv_usec *= 1000;
1196
1197   if (config.getValue("session.cacheLife", "Session.CacheLife", l))
1198     resource.cache_life = l;
1199   else
1200     resource.cache_life = 51;
1201   resource.cache_life *= 60000;
1202
1203   if (config.getValue("session.cacheMax", "Session.CacheMax", l))
1204     resource.cache_max = l;
1205   else
1206     resource.cache_max = 200;
1207 }
1208
1209
1210 void Openbox::load_rc(BScreen *screen) {
1211   assert (screen != NULL);
1212   const int screen_number = screen->getScreenNumber();
1213   assert (screen_number >= 0);
1214
1215   if (!config.load())
1216     return;
1217
1218   std::string s;
1219   long l;
1220   bool b;
1221   char name_lookup[1024], class_lookup[1024];
1222    
1223   sprintf(name_lookup,  "session.screen%d.fullMaximization", screen_number);
1224   sprintf(class_lookup, "Session.Screen%d.FullMaximization", screen_number);
1225   if (config.getValue(name_lookup, class_lookup, b))
1226     screen->saveFullMax((Bool)b);
1227   else
1228     screen->saveFullMax(False);
1229
1230   sprintf(name_lookup,  "session.screen%d.focusNewWindows", screen_number);
1231   sprintf(class_lookup, "Session.Screen%d.FocusNewWindows", screen_number);
1232   if (config.getValue(name_lookup, class_lookup, b))
1233     screen->saveFocusNew((Bool)b);
1234   else
1235     screen->saveFocusNew(False);
1236   
1237   sprintf(name_lookup,  "session.screen%d.focusLastWindow", screen_number);
1238   sprintf(class_lookup, "Session.Screen%d.focusLastWindow", screen_number);
1239   if (config.getValue(name_lookup, class_lookup, b))
1240     screen->saveFocusLast((Bool)b);
1241   else
1242     screen->saveFocusLast(False);
1243   
1244   sprintf(name_lookup,  "session.screen%d.rowPlacementDirection",
1245           screen_number);
1246   sprintf(class_lookup, "Session.Screen%d.RowPlacementDirection",
1247           screen_number);
1248   if (config.getValue(name_lookup, class_lookup, s)) {
1249     if (0 == strncasecmp(s.c_str(), "righttoleft", s.length()))
1250       screen->saveRowPlacementDirection(BScreen::RightLeft);
1251     else
1252       screen->saveRowPlacementDirection(BScreen::LeftRight);
1253   } else
1254     screen->saveRowPlacementDirection(BScreen::LeftRight);
1255   
1256   sprintf(name_lookup,  "session.screen%d.colPlacementDirection",
1257           screen_number);
1258   sprintf(class_lookup, "Session.Screen%d.ColPlacementDirection",
1259           screen_number);
1260   if (config.getValue(name_lookup, class_lookup, s)) {
1261     if (0 == strncasecmp(s.c_str(), "bottomtotop", s.length()))
1262       screen->saveColPlacementDirection(BScreen::BottomTop);
1263     else
1264       screen->saveColPlacementDirection(BScreen::TopBottom);
1265   } else
1266     screen->saveColPlacementDirection(BScreen::TopBottom);
1267
1268   sprintf(name_lookup,  "session.screen%d.workspaces", screen_number);
1269   sprintf(class_lookup, "Session.Screen%d.Workspaces", screen_number);
1270   if (config.getValue(name_lookup, class_lookup, l))
1271     screen->saveWorkspaces(l);
1272   else
1273     screen->saveWorkspaces(1);
1274   
1275   sprintf(name_lookup,  "session.screen%d.toolbar.widthPercent",
1276           screen_number);
1277   sprintf(class_lookup, "Session.Screen%d.Toolbar.WidthPercent",
1278           screen_number);
1279   if (config.getValue(name_lookup, class_lookup, l) && (l > 0 && l <= 100))
1280     screen->saveToolbarWidthPercent(l);
1281   else
1282     screen->saveToolbarWidthPercent(66);
1283
1284   sprintf(name_lookup, "session.screen%d.toolbar.placement", screen_number);
1285   sprintf(class_lookup, "Session.Screen%d.Toolbar.Placement", screen_number);
1286   if (config.getValue(name_lookup, class_lookup, s)) {
1287     if (0 == strncasecmp(s.c_str(), "TopLeft", s.length()))
1288       screen->saveToolbarPlacement(Toolbar::TopLeft);
1289     else if (0 == strncasecmp(s.c_str(), "BottomLeft", s.length()))
1290       screen->saveToolbarPlacement(Toolbar::BottomLeft);
1291     else if (0 == strncasecmp(s.c_str(), "TopCenter", s.length()))
1292       screen->saveToolbarPlacement(Toolbar::TopCenter);
1293     else if (0 == strncasecmp(s.c_str(), "TopRight", s.length()))
1294       screen->saveToolbarPlacement(Toolbar::TopRight);
1295     else if ( 0 == strncasecmp(s.c_str(), "BottomRight", s.length()))
1296       screen->saveToolbarPlacement(Toolbar::BottomRight);
1297     else
1298       screen->saveToolbarPlacement(Toolbar::BottomCenter);
1299   } else
1300     screen->saveToolbarPlacement(Toolbar::BottomCenter);
1301
1302   screen->removeWorkspaceNames();
1303   sprintf(name_lookup,  "session.screen%d.workspaceNames", screen_number);
1304   sprintf(class_lookup, "Session.Screen%d.WorkspaceNames", screen_number);
1305   if (config.getValue(name_lookup, class_lookup, s)) {
1306   //  for (int i = 0; i < screen->getNumberOfWorkspaces(); i++) {
1307     std::string::const_iterator it = s.begin(), end = s.end();
1308     while(1) {
1309       std::string::const_iterator tmp = it;// current string.begin()
1310       it = std::find(tmp, end, ',');       // look for comma between tmp and end
1311       std::string name(tmp, it);           // name = s[tmp:it]
1312       screen->addWorkspaceName(name.c_str());
1313       if (it == end)
1314         break;
1315       ++it;
1316     }
1317   }
1318
1319   sprintf(name_lookup,  "session.screen%d.toolbar.onTop", screen_number);
1320   sprintf(class_lookup, "Session.Screen%d.Toolbar.OnTop", screen_number);
1321   if (config.getValue(name_lookup, class_lookup, b))
1322     screen->saveToolbarOnTop((Bool)b);
1323   else
1324     screen->saveToolbarOnTop(False);
1325
1326   sprintf(name_lookup,  "session.screen%d.toolbar.autoHide", screen_number);
1327   sprintf(class_lookup, "Session.Screen%d.Toolbar.autoHide", screen_number);
1328   if (config.getValue(name_lookup, class_lookup, b))
1329     screen->saveToolbarAutoHide((Bool)b);
1330   else
1331     screen->saveToolbarAutoHide(False);
1332
1333   sprintf(name_lookup,  "session.screen%d.focusModel", screen_number);
1334   sprintf(class_lookup, "Session.Screen%d.FocusModel", screen_number);
1335   if (config.getValue(name_lookup, class_lookup, s)) {
1336     if (0 == strncasecmp(s.c_str(), "clicktofocus", s.length())) {
1337       screen->saveAutoRaise(False);
1338       screen->saveSloppyFocus(False);
1339     } else if (0 == strncasecmp(s.c_str(), "autoraisesloppyfocus",
1340                                 s.length())) {
1341       screen->saveSloppyFocus(True);
1342       screen->saveAutoRaise(True);
1343     } else {
1344       screen->saveSloppyFocus(True);
1345       screen->saveAutoRaise(False);
1346     }
1347   } else {
1348     screen->saveSloppyFocus(True);
1349     screen->saveAutoRaise(False);
1350   }
1351
1352   sprintf(name_lookup,  "session.screen%d.windowZones", screen_number);
1353   sprintf(class_lookup, "Session.Screen%d.WindowZones", screen_number);
1354   if (config.getValue(name_lookup, class_lookup, l))
1355     screen->saveWindowZones((l == 1 || l == 2 || l == 4) ? l : 1);
1356   else
1357     screen->saveWindowZones(1);
1358   
1359   sprintf(name_lookup,  "session.screen%d.windowPlacement", screen_number);
1360   sprintf(class_lookup, "Session.Screen%d.WindowPlacement", screen_number);
1361   if (config.getValue(name_lookup, class_lookup, s)) {
1362     if (0 == strncasecmp(s.c_str(), "RowSmartPlacement", s.length()))
1363       screen->savePlacementPolicy(BScreen::RowSmartPlacement);
1364     else if (0 == strncasecmp(s.c_str(), "ColSmartPlacement", s.length()))
1365       screen->savePlacementPolicy(BScreen::ColSmartPlacement);
1366     else
1367       screen->savePlacementPolicy(BScreen::CascadePlacement);
1368   } else
1369     screen->savePlacementPolicy(BScreen::RowSmartPlacement);
1370
1371 #ifdef    SLIT
1372   sprintf(name_lookup, "session.screen%d.slit.placement", screen_number);
1373   sprintf(class_lookup, "Session.Screen%d.Slit.Placement", screen_number);
1374   if (config.getValue(name_lookup, class_lookup, s)) {
1375     if (0 == strncasecmp(s.c_str(), "TopLeft", s.length()))
1376       screen->saveSlitPlacement(Slit::TopLeft);
1377     else if (0 == strncasecmp(s.c_str(), "CenterLeft", s.length()))
1378       screen->saveSlitPlacement(Slit::CenterLeft);
1379     else if (0 == strncasecmp(s.c_str(), "BottomLeft", s.length()))
1380       screen->saveSlitPlacement(Slit::BottomLeft);
1381     else if (0 == strncasecmp(s.c_str(), "TopCenter", s.length()))
1382       screen->saveSlitPlacement(Slit::TopCenter);
1383     else if (0 == strncasecmp(s.c_str(), "BottomCenter", s.length()))
1384       screen->saveSlitPlacement(Slit::BottomCenter);
1385     else if (0 == strncasecmp(s.c_str(), "TopRight", s.length()))
1386       screen->saveSlitPlacement(Slit::TopRight);
1387     else if (0 == strncasecmp(s.c_str(), "BottomRight", s.length()))
1388       screen->saveSlitPlacement(Slit::BottomRight);
1389     else
1390       screen->saveSlitPlacement(Slit::CenterRight);
1391   } else
1392     screen->saveSlitPlacement(Slit::CenterRight);
1393
1394   sprintf(name_lookup, "session.screen%d.slit.direction", screen_number);
1395   sprintf(class_lookup, "Session.Screen%d.Slit.Direction", screen_number);
1396   if (config.getValue(name_lookup, class_lookup, s)) {
1397     if (0 == strncasecmp(s.c_str(), "Horizontal", s.length()))
1398       screen->saveSlitDirection(Slit::Horizontal);
1399     else
1400       screen->saveSlitDirection(Slit::Vertical);
1401   } else
1402     screen->saveSlitDirection(Slit::Vertical);
1403   
1404   sprintf(name_lookup, "session.screen%d.slit.onTop", screen_number);
1405   sprintf(class_lookup, "Session.Screen%d.Slit.OnTop", screen_number);
1406   if (config.getValue(name_lookup, class_lookup, b))
1407     screen->saveSlitOnTop((Bool)b);
1408   else
1409     screen->saveSlitOnTop(False);
1410
1411   sprintf(name_lookup, "session.screen%d.slit.autoHide", screen_number);
1412   sprintf(class_lookup, "Session.Screen%d.Slit.AutoHide", screen_number);
1413   if (config.getValue(name_lookup, class_lookup, b))
1414     screen->saveSlitAutoHide((Bool)b);
1415   else
1416     screen->saveSlitAutoHide(False);
1417 #endif // SLIT
1418
1419 #ifdef    HAVE_STRFTIME
1420   sprintf(name_lookup,  "session.screen%d.strftimeFormat", screen_number);
1421   sprintf(class_lookup, "Session.Screen%d.StrftimeFormat", screen_number);
1422   if (config.getValue(name_lookup, class_lookup, s))
1423     screen->saveStrftimeFormat(s.c_str());
1424   else
1425     screen->saveStrftimeFormat("%I:%M %p");
1426
1427 #else //  HAVE_STRFTIME
1428   sprintf(name_lookup,  "session.screen%d.dateFormat", screen_number);
1429   sprintf(class_lookup, "Session.Screen%d.DateFormat", screen_number);
1430   if (config.getValue(name_lookup, class_lookup, s)) {
1431     if (strncasecmp(s.c_str(), "european", s.length()))
1432       screen->saveDateFormat(B_AmericanDate);
1433     else
1434       screen->saveDateFormat(B_EuropeanDate);
1435   } else
1436     screen->saveDateFormat(B_AmericanDate);
1437
1438   sprintf(name_lookup,  "session.screen%d.clockFormat", screen_number);
1439   sprintf(class_lookup, "Session.Screen%d.ClockFormat", screen_number);
1440   if (config.getValue(name_lookup, class_lookup, l)) {
1441     if (clock == 24)
1442       screen->saveClock24Hour(True);
1443     else
1444       screen->saveClock24Hour(False);
1445   } else
1446     screen->saveClock24Hour(False);
1447 #endif // HAVE_STRFTIME
1448
1449   sprintf(name_lookup,  "session.screen%d.edgeSnapThreshold", screen_number);
1450   sprintf(class_lookup, "Session.Screen%d.EdgeSnapThreshold", screen_number);
1451   if (config.getValue(name_lookup, class_lookup, l))
1452     screen->saveEdgeSnapThreshold(l);
1453   else
1454     screen->saveEdgeSnapThreshold(4);
1455
1456   sprintf(name_lookup,  "session.screen%d.imageDither", screen_number);
1457   sprintf(class_lookup, "Session.Screen%d.ImageDither", screen_number);
1458   if (config.getValue("session.imageDither", "Session.ImageDither", b))
1459     screen->saveImageDither((Bool)b);
1460   else
1461     screen->saveImageDither(True);
1462
1463   sprintf(name_lookup, "session.screen%d.rootCommand", screen_number);
1464   sprintf(class_lookup, "Session.Screen%d.RootCommand", screen_number);
1465   if (config.getValue(name_lookup, class_lookup, s))
1466     screen->saveRootCommand(s.c_str());
1467   else
1468     screen->saveRootCommand(NULL);
1469
1470   if (config.getValue("session.opaqueMove", "Session.OpaqueMove", b))
1471     screen->saveOpaqueMove((Bool)b);
1472   else
1473     screen->saveOpaqueMove(False);
1474 }
1475
1476
1477 void Openbox::reload_rc(void) {
1478   load_rc();
1479   reconfigure();
1480 }
1481
1482
1483 void Openbox::reconfigure(void) {
1484   reconfigure_wait = True;
1485
1486   if (! timer->isTiming()) timer->start();
1487 }
1488
1489
1490 void Openbox::real_reconfigure(void) {
1491   grab();
1492
1493   config.load();
1494   config.setValue("session.styleFile", resource.style_file);    // autosave's
1495   
1496   for (int i = 0, n = menuTimestamps->count(); i < n; i++) {
1497     MenuTimestamp *ts = menuTimestamps->remove(0);
1498
1499     if (ts) {
1500       if (ts->filename)
1501         delete [] ts->filename;
1502
1503       delete ts;
1504     }
1505   }
1506
1507   LinkedListIterator<BScreen> it(screenList);
1508   for (BScreen *screen = it.current(); screen; it++, screen = it.current()) {
1509     screen->reconfigure();
1510   }
1511
1512   ungrab();
1513 }
1514
1515
1516 void Openbox::checkMenu(void) {
1517   Bool reread = False;
1518   LinkedListIterator<MenuTimestamp> it(menuTimestamps);
1519   for (MenuTimestamp *tmp = it.current(); tmp && (! reread);
1520        it++, tmp = it.current()) {
1521     struct stat buf;
1522
1523     if (! stat(tmp->filename, &buf)) {
1524       if (tmp->timestamp != buf.st_ctime)
1525         reread = True;
1526     } else {
1527       reread = True;
1528     }
1529   }
1530
1531   if (reread) rereadMenu();
1532 }
1533
1534
1535 void Openbox::rereadMenu(void) {
1536   reread_menu_wait = True;
1537
1538   if (! timer->isTiming()) timer->start();
1539 }
1540
1541
1542 void Openbox::real_rereadMenu(void) {
1543   for (int i = 0, n = menuTimestamps->count(); i < n; i++) {
1544     MenuTimestamp *ts = menuTimestamps->remove(0);
1545
1546     if (ts) {
1547       if (ts->filename)
1548         delete [] ts->filename;
1549
1550       delete ts;
1551     }
1552   }
1553
1554   LinkedListIterator<BScreen> it(screenList);
1555   for (BScreen *screen = it.current(); screen; it++, screen = it.current())
1556     screen->rereadMenu();
1557 }
1558
1559
1560 void Openbox::saveStyleFilename(const char *filename) {
1561   if (resource.style_file)
1562     delete [] resource.style_file;
1563
1564   resource.style_file = bstrdup(filename);
1565 }
1566
1567
1568 void Openbox::saveMenuFilename(const char *filename) {
1569   Bool found = False;
1570
1571   LinkedListIterator<MenuTimestamp> it(menuTimestamps);
1572   for (MenuTimestamp *tmp = it.current(); tmp && (! found);
1573        it++, tmp = it.current()) {
1574     if (! strcmp(tmp->filename, filename)) found = True;
1575   }
1576   if (! found) {
1577     struct stat buf;
1578
1579     if (! stat(filename, &buf)) {
1580       MenuTimestamp *ts = new MenuTimestamp;
1581
1582       ts->filename = bstrdup(filename);
1583       ts->timestamp = buf.st_ctime;
1584
1585       menuTimestamps->insert(ts);
1586     }
1587   }
1588 }
1589
1590
1591 void Openbox::timeout(void) {
1592   if (reconfigure_wait)
1593     real_reconfigure();
1594
1595   if (reread_menu_wait)
1596     real_rereadMenu();
1597
1598   reconfigure_wait = reread_menu_wait = False;
1599 }
1600
1601
1602 void Openbox::setFocusedWindow(OpenboxWindow *win) {
1603   BScreen *old_screen = (BScreen *) 0, *screen = (BScreen *) 0;
1604   OpenboxWindow *old_win = (OpenboxWindow *) 0;
1605   Toolbar *old_tbar = (Toolbar *) 0, *tbar = (Toolbar *) 0;
1606   Workspace *old_wkspc = (Workspace *) 0, *wkspc = (Workspace *) 0;
1607
1608   if (focused_window) {
1609     old_win = focused_window;
1610     old_screen = old_win->getScreen();
1611     old_tbar = old_screen->getToolbar();
1612     old_wkspc = old_screen->getWorkspace(old_win->getWorkspaceNumber());
1613
1614     old_win->setFocusFlag(False);
1615     old_wkspc->getMenu()->setItemSelected(old_win->getWindowNumber(), False);
1616   }
1617
1618   if (win && ! win->isIconic()) {
1619     screen = win->getScreen();
1620     tbar = screen->getToolbar();
1621     wkspc = screen->getWorkspace(win->getWorkspaceNumber());
1622
1623     focused_window = win;
1624
1625     win->setFocusFlag(True);
1626     wkspc->getMenu()->setItemSelected(win->getWindowNumber(), True);
1627   } else {
1628     focused_window = (OpenboxWindow *) 0;
1629   }
1630
1631   if (tbar)
1632     tbar->redrawWindowLabel(True);
1633   if (screen)
1634     screen->updateNetizenWindowFocus();
1635
1636   if (old_tbar && old_tbar != tbar)
1637     old_tbar->redrawWindowLabel(True);
1638   if (old_screen && old_screen != screen)
1639     old_screen->updateNetizenWindowFocus();
1640 }
1641
1642 #warning TODO: wrap in appropriate #ifdefs.
1643
1644 void Openbox::symlink_rc(const char*rcfile)const{
1645   char *homedir = getenv("HOME");
1646   
1647   char *link_file = new char[strlen(homedir) + strlen("/.blackoxrc") + 1];
1648   sprintf(link_file, "%s/.blackboxrc", homedir);
1649   if(symlink(rcfile, link_file) == -1){
1650     perror("Cannot create symlink");
1651   }
1652 }