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