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