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