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)
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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.
23 // stupid macros needed to access some functions in version 2 of the GNU C
30 # include "../config.h"
31 #endif // HAVE_CONFIG_H
34 #include <X11/Xutil.h>
35 #include <X11/Xresource.h>
36 #include <X11/Xatom.h>
37 #include <X11/keysym.h>
40 #include <X11/extensions/shape.h>
46 #include "Clientmenu.h"
56 #include "Workspace.h"
57 #include "Workspacemenu.h"
65 #endif // HAVE_STDIO_H
69 #endif // HAVE_STDLIB_H
73 #endif // HAVE_STRING_H
76 # include <sys/types.h>
78 #endif // HAVE_UNISTD_H
80 #ifdef HAVE_SYS_PARAM_H
81 # include <sys/param.h>
82 #endif // HAVE_SYS_PARAM_H
85 #define MAXPATHLEN 255
88 #ifdef HAVE_SYS_SELECT_H
89 # include <sys/select.h>
90 #endif // HAVE_SYS_SELECT_H
94 #endif // HAVE_SIGNAL_H
96 #ifdef HAVE_SYS_SIGNAL_H
97 # include <sys/signal.h>
98 #endif // HAVE_SYS_SIGNAL_H
100 #ifdef HAVE_SYS_STAT_H
101 # include <sys/types.h>
102 # include <sys/stat.h>
103 #endif // HAVE_SYS_STAT_H
105 #ifdef TIME_WITH_SYS_TIME
106 # include <sys/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
113 # endif // HAVE_SYS_TIME_H
114 #endif // TIME_WITH_SYS_TIME
118 #endif // HAVE_LIBGEN_H
120 #ifndef HAVE_BASENAME
121 static inline char *basename (char *s) {
124 while (*s) if (*s++ == '/') save = s;
128 #endif // HAVE_BASENAME
131 // X event scanner for enter/leave notifies - adapted from twm
132 typedef struct scanargs {
134 Bool leave, inferior, enter;
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;
154 Openbox::Openbox(int m_argc, char **m_argv, char *dpy_name, char *rc)
155 : BaseDisplay(m_argv[0], dpy_name) {
158 if (! XSupportsLocale())
159 fprintf(stderr, "X server does not support locale\n");
161 if (XSetLocaleModifiers("") == NULL)
162 fprintf(stderr, "cannot set locale modifiers\n");
168 char *homedir = getenv("HOME");
170 rc_file = new char[strlen(homedir) + strlen("/.openbox/rc") + 1];
171 sprintf(rc_file, "%s/.openbox", homedir);
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);
177 sprintf(rc_file, "%s/.openbox/rc", homedir);
179 rc_file = bstrdup(rc);
181 config.setFile(rc_file);
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;
189 focused_window = masked_window = (OpenboxWindow *) 0;
192 windowSearchList = new LinkedList<WindowSearch>;
193 menuSearchList = new LinkedList<MenuSearch>;
196 slitSearchList = new LinkedList<SlitSearch>;
199 toolbarSearchList = new LinkedList<ToolbarSearch>;
200 groupSearchList = new LinkedList<WindowSearch>;
202 menuTimestamps = new LinkedList<MenuTimestamp>;
207 openbox_pid = XInternAtom(getXDisplay(), "_BLACKBOX_PID", False);
208 #endif // HAVE_GETPID
210 screenList = new LinkedList<BScreen>;
211 for (int i = 0; i < getNumberOfScreens(); i++) {
212 BScreen *screen = new BScreen(*this, i, config);
214 if (! screen->isScreenManaged()) {
219 screenList->insert(screen);
222 if (! screenList->count()) {
224 i18n->getMessage(openboxSet, openboxNoManagableScreens,
225 "Openbox::Openbox: no managable screens found, aborting.\n"));
229 XSynchronize(getXDisplay(), False);
230 XSync(getXDisplay(), False);
232 reconfigure_wait = reread_menu_wait = False;
234 timer = new BTimer(*this, *this);
235 timer->setTimeout(0);
236 timer->fireOnce(True);
242 Openbox::~Openbox(void) {
243 while (screenList->count())
244 delete screenList->remove(0);
246 while (menuTimestamps->count()) {
247 MenuTimestamp *ts = menuTimestamps->remove(0);
250 delete [] ts->filename;
255 if (resource.menu_file)
256 delete [] resource.menu_file;
258 if (resource.style_file)
259 delete [] resource.style_file;
264 delete menuTimestamps;
266 delete windowSearchList;
267 delete menuSearchList;
268 delete toolbarSearchList;
269 delete groupSearchList;
274 delete slitSearchList;
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);
290 // strip the lock key modifiers
291 e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
293 last_time = e->xbutton.time;
295 OpenboxWindow *win = (OpenboxWindow *) 0;
296 Basemenu *menu = (Basemenu *) 0;
299 Slit *slit = (Slit *) 0;
302 Toolbar *tbar = (Toolbar *) 0;
304 if ((win = searchWindow(e->xbutton.window))) {
305 win->buttonPressEvent(&e->xbutton);
307 if (e->xbutton.button == 1)
308 win->installColormap(True);
309 } else if ((menu = searchMenu(e->xbutton.window))) {
310 menu->buttonPressEvent(&e->xbutton);
313 } else if ((slit = searchSlit(e->xbutton.window))) {
314 slit->buttonPressEvent(&e->xbutton);
317 } else if ((tbar = searchToolbar(e->xbutton.window))) {
318 tbar->buttonPressEvent(&e->xbutton);
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();
328 if (screen->getWorkspacemenu()->isVisible())
329 screen->getWorkspacemenu()->hide();
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);
342 if (mx + screen->getWorkspacemenu()->getWidth() >
344 mx = screen->size().w() -
345 screen->getWorkspacemenu()->getWidth() -
346 screen->getBorderWidth();
348 if (my + screen->getWorkspacemenu()->getHeight() >
350 my = screen->size().h() -
351 screen->getWorkspacemenu()->getHeight() -
352 screen->getBorderWidth();
354 screen->getWorkspacemenu()->move(mx, my);
356 if (! screen->getWorkspacemenu()->isVisible()) {
357 screen->getWorkspacemenu()->removeParent();
358 screen->getWorkspacemenu()->show();
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);
369 if (mx + screen->getRootmenu()->getWidth() > screen->size().w())
370 mx = screen->size().w() -
371 screen->getRootmenu()->getWidth() -
372 screen->getBorderWidth();
374 if (my + screen->getRootmenu()->getHeight() > screen->size().h())
375 my = screen->size().h() -
376 screen->getRootmenu()->getHeight() -
377 screen->getBorderWidth();
379 screen->getRootmenu()->move(mx, my);
381 if (! screen->getRootmenu()->isVisible()) {
383 screen->getRootmenu()->show();
385 } else if (e->xbutton.button == 4) {
386 if ((screen->getCurrentWorkspaceID() + 1) >
387 screen->getWorkspaceCount() - 1)
388 screen->changeWorkspaceID(0);
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);
395 screen->changeWorkspaceID(screen->getCurrentWorkspaceID() - 1);
404 case ButtonRelease: {
405 // strip the lock key modifiers
406 e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
408 last_time = e->xbutton.time;
410 OpenboxWindow *win = (OpenboxWindow *) 0;
411 Basemenu *menu = (Basemenu *) 0;
412 Toolbar *tbar = (Toolbar *) 0;
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);
424 case ConfigureRequest: {
425 OpenboxWindow *win = (OpenboxWindow *) 0;
428 Slit *slit = (Slit *) 0;
431 if ((win = searchWindow(e->xconfigurerequest.window))) {
432 win->configureRequestEvent(&e->xconfigurerequest);
435 } else if ((slit = searchSlit(e->xconfigurerequest.window))) {
436 slit->configureRequestEvent(&e->xconfigurerequest);
442 if (validateWindow(e->xconfigurerequest.window)) {
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;
453 XConfigureWindow(getXDisplay(), e->xconfigurerequest.window,
454 e->xconfigurerequest.value_mask, &xwc);
466 i18n->getMessage(openboxSet, openboxMapRequest,
467 "Openbox::process_event(): MapRequest for 0x%lx\n"),
468 e->xmaprequest.window);
471 OpenboxWindow *win = searchWindow(e->xmaprequest.window);
474 win = new OpenboxWindow(*this, e->xmaprequest.window);
476 if ((win = searchWindow(e->xmaprequest.window)))
477 win->mapRequestEvent(&e->xmaprequest);
483 OpenboxWindow *win = searchWindow(e->xmap.window);
486 win->mapNotifyEvent(&e->xmap);
492 OpenboxWindow *win = (OpenboxWindow *) 0;
495 Slit *slit = (Slit *) 0;
498 if ((win = searchWindow(e->xunmap.window))) {
499 win->unmapNotifyEvent(&e->xunmap);
500 if (focused_window == win)
501 focused_window = (OpenboxWindow *) 0;
503 } else if ((slit = searchSlit(e->xunmap.window))) {
504 slit->removeClient(e->xunmap.window);
512 case DestroyNotify: {
513 OpenboxWindow *win = (OpenboxWindow *) 0;
516 Slit *slit = (Slit *) 0;
519 if ((win = searchWindow(e->xdestroywindow.window))) {
520 win->destroyNotifyEvent(&e->xdestroywindow);
521 if (focused_window == win)
522 focused_window = (OpenboxWindow *) 0;
524 } else if ((slit = searchSlit(e->xdestroywindow.window))) {
525 slit->removeClient(e->xdestroywindow.window, False);
533 // strip the lock key modifiers
534 e->xbutton.state &= ~(NumLockMask | ScrollLockMask | LockMask);
536 last_time = e->xmotion.time;
538 OpenboxWindow *win = (OpenboxWindow *) 0;
539 Basemenu *menu = (Basemenu *) 0;
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);
549 case PropertyNotify: {
550 last_time = e->xproperty.time;
552 if (e->xproperty.state != PropertyDelete) {
553 OpenboxWindow *win = searchWindow(e->xproperty.window);
556 win->propertyNotifyEvent(e->xproperty.atom);
563 last_time = e->xcrossing.time;
565 BScreen *screen = (BScreen *) 0;
566 OpenboxWindow *win = (OpenboxWindow *) 0;
567 Basemenu *menu = (Basemenu *) 0;
568 Toolbar *tbar = (Toolbar *) 0;
571 Slit *slit = (Slit *) 0;
574 if (e->xcrossing.mode == NotifyGrab) break;
578 sa.w = e->xcrossing.window;
579 sa.enter = sa.leave = False;
580 XCheckIfEvent(getXDisplay(), &dummy, queueScanner, (char *) &sa);
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)) {
590 if (((! sa.leave) || sa.inferior) && win->isVisible() &&
591 win->setInputFocus())
592 win->installColormap(True);
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);
601 } else if ((slit = searchSlit(e->xcrossing.window))) {
602 slit->enterNotifyEvent(&e->xcrossing);
609 last_time = e->xcrossing.time;
611 OpenboxWindow *win = (OpenboxWindow *) 0;
612 Basemenu *menu = (Basemenu *) 0;
613 Toolbar *tbar = (Toolbar *) 0;
616 Slit *slit = (Slit *) 0;
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);
626 else if ((slit = searchSlit(e->xcrossing.window)))
627 slit->leaveNotifyEvent(&e->xcrossing);
634 OpenboxWindow *win = (OpenboxWindow *) 0;
635 Basemenu *menu = (Basemenu *) 0;
636 Toolbar *tbar = (Toolbar *) 0;
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);
649 Toolbar *tbar = searchToolbar(e->xkey.window);
651 if (tbar && tbar->isEditing())
652 tbar->keyPressEvent(&e->xkey);
657 case ColormapNotify: {
658 BScreen *screen = searchScreen(e->xcolormap.window);
661 screen->setRootColormapInstalled((e->xcolormap.state ==
662 ColormapInstalled) ? True : False);
668 if (e->xfocus.mode == NotifyUngrab || e->xfocus.detail == NotifyPointer)
671 OpenboxWindow *win = searchWindow(e->xfocus.window);
672 if (win && ! win->isFocused())
673 setFocusedWindow(win);
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;
687 if (e->xclient.data.l[0] == IconicState)
689 if (e->xclient.data.l[0] == NormalState)
691 } else if (e->xclient.message_type == getOpenboxChangeWorkspaceAtom()) {
692 BScreen *screen = searchScreen(e->xclient.window);
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);
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);
706 if (! e->xclient.data.l[0])
711 } else if (e->xclient.message_type == getOpenboxChangeAttributesAtom()) {
712 OpenboxWindow *win = searchWindow(e->xclient.window);
714 if (win && win->validateClient()) {
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];
722 win->changeOpenboxHints(&net);
733 if (e->type == getShapeEventBase()) {
734 XShapeEvent *shape_event = (XShapeEvent *) e;
735 OpenboxWindow *win = (OpenboxWindow *) 0;
737 if ((win = searchWindow(e->xany.window)) ||
738 (shape_event->kind != ShapeBounding))
739 win->shapeEvent(shape_event);
748 Bool Openbox::handleSignal(int sig) {
777 BScreen *Openbox::searchScreen(Window window) {
778 LinkedListIterator<BScreen> it(screenList);
780 for (BScreen *curr = it.current(); curr; it++, curr = it.current()) {
781 if (curr->getRootWindow() == window) {
786 return (BScreen *) 0;
790 OpenboxWindow *Openbox::searchWindow(Window window) {
791 LinkedListIterator<WindowSearch> it(windowSearchList);
793 for (WindowSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
794 if (tmp->getWindow() == window) {
795 return tmp->getData();
799 return (OpenboxWindow *) 0;
803 OpenboxWindow *Openbox::searchGroup(Window window, OpenboxWindow *win) {
804 OpenboxWindow *w = (OpenboxWindow *) 0;
805 LinkedListIterator<WindowSearch> it(groupSearchList);
807 for (WindowSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
808 if (tmp->getWindow() == window) {
810 if (w->getClientWindow() != win->getClientWindow())
815 return (OpenboxWindow *) 0;
819 Basemenu *Openbox::searchMenu(Window window) {
820 LinkedListIterator<MenuSearch> it(menuSearchList);
822 for (MenuSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
823 if (tmp->getWindow() == window)
824 return tmp->getData();
827 return (Basemenu *) 0;
831 Toolbar *Openbox::searchToolbar(Window window) {
832 LinkedListIterator<ToolbarSearch> it(toolbarSearchList);
834 for (ToolbarSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
835 if (tmp->getWindow() == window)
836 return tmp->getData();
839 return (Toolbar *) 0;
844 Slit *Openbox::searchSlit(Window window) {
845 LinkedListIterator<SlitSearch> it(slitSearchList);
847 for (SlitSearch *tmp = it.current(); tmp; it++, tmp = it.current()) {
848 if (tmp->getWindow() == window)
849 return tmp->getData();
857 void Openbox::saveWindowSearch(Window window, OpenboxWindow *data) {
858 windowSearchList->insert(new WindowSearch(window, data));
862 void Openbox::saveGroupSearch(Window window, OpenboxWindow *data) {
863 groupSearchList->insert(new WindowSearch(window, data));
867 void Openbox::saveMenuSearch(Window window, Basemenu *data) {
868 menuSearchList->insert(new MenuSearch(window, data));
872 void Openbox::saveToolbarSearch(Window window, Toolbar *data) {
873 toolbarSearchList->insert(new ToolbarSearch(window, data));
878 void Openbox::saveSlitSearch(Window window, Slit *data) {
879 slitSearchList->insert(new SlitSearch(window, data));
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);
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);
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);
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);
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);
946 void Openbox::restart(const char *prog) {
950 execlp(prog, prog, NULL);
954 // fall back in case the above execlp doesn't work
955 execvp(argv[0], argv);
956 execvp(basename(argv[0]), argv);
960 void Openbox::shutdown(void) {
961 BaseDisplay::shutdown();
963 XSetInputFocus(getXDisplay(), PointerRoot, None, CurrentTime);
965 LinkedListIterator<BScreen> it(screenList);
966 for (BScreen *s = it.current(); s; it++, s = it.current())
969 XSync(getXDisplay(), False);
975 void Openbox::save_rc(void) {
976 config.setAutoSave(false);
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);
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();
997 config.setValue("session.opaqueMove",
998 (screen->doOpaqueMove()) ? "True" : "False");
999 config.setValue("session.imageDither",
1000 (screen->getImageControl()->doDither()) ? "True" : "False");
1002 sprintf(rc_string, "session.screen%d.fullMaximization", screen_number);
1003 config.setValue(rc_string, screen->doFullMax() ? "True" : "False");
1005 sprintf(rc_string, "session.screen%d.focusNewWindows", screen_number);
1006 config.setValue(rc_string, screen->doFocusNew() ? "True" : "False");
1008 sprintf(rc_string, "session.screen%d.focusLastWindow", screen_number);
1009 config.setValue(rc_string, screen->doFocusLast() ? "True" : "False");
1011 sprintf(rc_string, "session.screen%d.rowPlacementDirection", screen_number);
1012 config.setValue(rc_string,
1013 screen->getRowPlacementDirection() == BScreen::LeftRight ?
1014 "LeftToRight" : "RightToLeft");
1016 sprintf(rc_string, "session.screen%d.colPlacementDirection", screen_number);
1017 config.setValue(rc_string,
1018 screen->getColPlacementDirection() == BScreen::TopBottom ?
1019 "TopToBottom" : "BottomToTop");
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;
1027 case BScreen::RowSmartPlacement: placement = "RowSmartPlacement"; break;
1029 sprintf(rc_string, "session.screen%d.windowPlacement", screen_number);
1030 config.setValue(rc_string, placement);
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"));
1038 sprintf(rc_string, "session.screen%d.workspaces", screen_number);
1039 config.setValue(rc_string, screen->getWorkspaceCount());
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");
1049 sprintf(rc_string, "session.screen%d.clockFormat", screen_number);
1050 config.setValue(rc_string, screen->isClock24Hour() ? 24 : 12);
1051 #endif // HAVE_STRFTIME
1053 sprintf(rc_string, "session.screen%d.edgeSnapThreshold", screen_number);
1054 config.setValue(rc_string, screen->getEdgeSnapThreshold());
1056 // write out the user's workspace names
1058 for (i = 0; i < screen->getWorkspaceCount(); i++)
1059 len += strlen((screen->getWorkspace(i)->getName()) ?
1060 screen->getWorkspace(i)->getName() : "Null") + 1;
1062 char *resource_string = new char[len + 1024],
1063 *save_string = new char[len], *save_string_pos = save_string,
1066 for (i = 0; i < screen->getWorkspaceCount(); i++) {
1067 len = strlen((screen->getWorkspace(i)->getName()) ?
1068 screen->getWorkspace(i)->getName() : "Null") + 1;
1070 (char *) ((screen->getWorkspace(i)->getName()) ?
1071 screen->getWorkspace(i)->getName() : "Null");
1073 while (--len) *(save_string_pos++) = *(name_string_pos++);
1074 *(save_string_pos++) = ',';
1078 *(--save_string_pos) = '\0';
1080 sprintf(resource_string, "session.screen%d.workspaceNames", screen_number);
1081 config.setValue(resource_string, save_string);
1083 delete [] resource_string;
1084 delete [] save_string;
1086 std::string save_string = screen->getWorkspace(0)->getName();
1087 for (unsigned int i = 1; i < screen->getWorkspaceCount(); ++i) {
1089 save_string += screen->getWorkspace(i)->getName();
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);
1096 delete [] resource_string;*/
1099 config.setAutoSave(true);
1103 void Openbox::load_rc(void) {
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());
1115 resource.menu_file = bstrdup(DEFAULTMENU);
1117 if (config.getValue("session.colorsPerChannel", "Session.ColorsPerChannel",
1119 resource.colors_per_channel = (l < 2 ? 2 : (l > 6 ? 6 : l)); // >= 2, <= 6
1121 resource.colors_per_channel = 4;
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());
1128 resource.style_file = bstrdup(DEFAULTSTYLE);
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());
1135 resource.titlebar_layout = bstrdup("ILMC");
1137 if (config.getValue("session.doubleClickInterval",
1138 "Session.DoubleClickInterval", l))
1139 resource.double_click_interval = l;
1141 resource.double_click_interval = 250;
1143 if (!config.getValue("session.autoRaiseDelay", "Session.AutoRaiseDelay", l))
1144 resource.auto_raise_delay.tv_usec = l;
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;
1152 if (config.getValue("session.cacheLife", "Session.CacheLife", l))
1153 resource.cache_life = l;
1155 resource.cache_life = 51;
1156 resource.cache_life *= 60000;
1158 if (config.getValue("session.cacheMax", "Session.CacheMax", l))
1159 resource.cache_max = l;
1161 resource.cache_max = 200;
1165 void Openbox::load_rc(BScreen *screen) {
1166 ASSERT (screen != NULL);
1167 const int screen_number = screen->getScreenNumber();
1168 ASSERT (screen_number >= 0);
1176 char name_lookup[1024], class_lookup[1024];
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);
1183 screen->saveFullMax(False);
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);
1190 screen->saveFocusNew(False);
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);
1197 screen->saveFocusLast(False);
1199 sprintf(name_lookup, "session.screen%d.rowPlacementDirection",
1201 sprintf(class_lookup, "Session.Screen%d.RowPlacementDirection",
1203 if (config.getValue(name_lookup, class_lookup, s)) {
1204 if (0 == strncasecmp(s.c_str(), "righttoleft", s.length()))
1205 screen->saveRowPlacementDirection(BScreen::RightLeft);
1207 screen->saveRowPlacementDirection(BScreen::LeftRight);
1209 screen->saveRowPlacementDirection(BScreen::LeftRight);
1211 sprintf(name_lookup, "session.screen%d.colPlacementDirection",
1213 sprintf(class_lookup, "Session.Screen%d.ColPlacementDirection",
1215 if (config.getValue(name_lookup, class_lookup, s)) {
1216 if (0 == strncasecmp(s.c_str(), "bottomtotop", s.length()))
1217 screen->saveColPlacementDirection(BScreen::BottomTop);
1219 screen->saveColPlacementDirection(BScreen::TopBottom);
1221 screen->saveColPlacementDirection(BScreen::TopBottom);
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);
1228 screen->saveWorkspaces(1);
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();
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());
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",
1255 screen->saveSloppyFocus(True);
1256 screen->saveAutoRaise(True);
1258 screen->saveSloppyFocus(True);
1259 screen->saveAutoRaise(False);
1262 screen->saveSloppyFocus(True);
1263 screen->saveAutoRaise(False);
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);
1271 screen->saveWindowZones(1);
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);
1283 screen->savePlacementPolicy(BScreen::CascadePlacement);
1285 screen->savePlacementPolicy(BScreen::RowSmartPlacement);
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());
1296 screen->saveStrftimeFormat("%I:%M %p");
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);
1305 screen->saveDateFormat(B_EuropeanDate);
1307 screen->saveDateFormat(B_AmericanDate);
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)) {
1313 screen->saveClock24Hour(True);
1315 screen->saveClock24Hour(False);
1317 screen->saveClock24Hour(False);
1318 #endif // HAVE_STRFTIME
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);
1325 screen->saveEdgeSnapThreshold(4);
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);
1332 screen->saveImageDither(True);
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());
1339 screen->saveRootCommand(NULL);
1341 if (config.getValue("session.opaqueMove", "Session.OpaqueMove", b))
1342 screen->saveOpaqueMove((Bool)b);
1344 screen->saveOpaqueMove(False);
1348 void Openbox::reload_rc(void) {
1354 void Openbox::reconfigure(void) {
1355 reconfigure_wait = True;
1357 if (! timer->isTiming()) timer->start();
1361 void Openbox::real_reconfigure(void) {
1365 config.setValue("session.styleFile", resource.style_file); // autosave's
1367 for (int i = 0, n = menuTimestamps->count(); i < n; i++) {
1368 MenuTimestamp *ts = menuTimestamps->remove(0);
1372 delete [] ts->filename;
1378 LinkedListIterator<BScreen> it(screenList);
1379 for (BScreen *screen = it.current(); screen; it++, screen = it.current()) {
1380 screen->reconfigure();
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()) {
1394 if (! stat(tmp->filename, &buf)) {
1395 if (tmp->timestamp != buf.st_ctime)
1402 if (reread) rereadMenu();
1406 void Openbox::rereadMenu(void) {
1407 reread_menu_wait = True;
1409 if (! timer->isTiming()) timer->start();
1413 void Openbox::real_rereadMenu(void) {
1414 for (int i = 0, n = menuTimestamps->count(); i < n; i++) {
1415 MenuTimestamp *ts = menuTimestamps->remove(0);
1419 delete [] ts->filename;
1425 LinkedListIterator<BScreen> it(screenList);
1426 for (BScreen *screen = it.current(); screen; it++, screen = it.current())
1427 screen->rereadMenu();
1431 void Openbox::saveStyleFilename(const char *filename) {
1432 if (resource.style_file)
1433 delete [] resource.style_file;
1435 resource.style_file = bstrdup(filename);
1439 void Openbox::saveMenuFilename(const char *filename) {
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;
1450 if (! stat(filename, &buf)) {
1451 MenuTimestamp *ts = new MenuTimestamp;
1453 ts->filename = bstrdup(filename);
1454 ts->timestamp = buf.st_ctime;
1456 menuTimestamps->insert(ts);
1462 void Openbox::timeout(void) {
1463 if (reconfigure_wait)
1466 if (reread_menu_wait)
1469 reconfigure_wait = reread_menu_wait = False;
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;
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());
1485 old_win->setFocusFlag(False);
1486 old_wkspc->getMenu()->setItemSelected(old_win->getWindowNumber(), False);
1489 if (win && ! win->isIconic()) {
1490 screen = win->getScreen();
1491 tbar = screen->getToolbar();
1492 wkspc = screen->getWorkspace(win->getWorkspaceNumber());
1494 focused_window = win;
1496 win->setFocusFlag(True);
1497 wkspc->getMenu()->setItemSelected(win->getWindowNumber(), True);
1499 focused_window = (OpenboxWindow *) 0;
1503 tbar->redrawWindowLabel(True);
1505 screen->updateNetizenWindowFocus();
1507 if (old_tbar && old_tbar != tbar)
1508 old_tbar->redrawWindowLabel(True);
1509 if (old_screen && old_screen != screen)
1510 old_screen->updateNetizenWindowFocus();