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