1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
11 #include "otk/property.hh"
12 #include "otk/assassin.hh"
13 #include "otk/property.hh"
14 #include "otk/util.hh"
15 #include "otk/rendercolor.hh"
16 #include "otk/renderstyle.hh"
17 #include "otk/messagedialog.hh"
20 #include <X11/cursorfont.h>
24 #endif // HAVE_SIGNAL_H
28 #endif // HAVE_FCNTL_H
30 #ifdef HAVE_SYS_WAIT_H
31 # include <sys/wait.h>
32 #endif // HAVE_SYS_WAIT_H
35 #define _(str) gettext(str)
43 extern void initialize();
44 extern void destroy();
49 Openbox *openbox = (Openbox *) 0;
52 void Openbox::signalHandler(int signal)
56 printf("Caught SIGUSR1 signal. Restarting.\n");
68 printf("Caught signal %d. Exiting.\n", signal);
74 printf("Caught signal %d. Aborting and dumping core.\n", signal);
80 Openbox::Openbox(int argc, char **argv)
81 : otk::EventDispatcher(),
84 struct sigaction action;
86 _state = State_Starting; // initializing everything
93 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
94 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
99 parseCommandLine(argc, argv);
103 XSynchronize(**otk::display, _sync);
105 // set up the signal handler
106 action.sa_handler = Openbox::signalHandler;
107 action.sa_mask = sigset_t();
108 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
109 sigaction(SIGUSR1, &action, (struct sigaction *) 0);
110 sigaction(SIGPIPE, &action, (struct sigaction *) 0);
111 sigaction(SIGSEGV, &action, (struct sigaction *) 0);
112 sigaction(SIGFPE, &action, (struct sigaction *) 0);
113 sigaction(SIGTERM, &action, (struct sigaction *) 0);
114 sigaction(SIGINT, &action, (struct sigaction *) 0);
115 sigaction(SIGHUP, &action, (struct sigaction *) 0);
116 sigaction(SIGCHLD, &action, (struct sigaction *) 0);
118 // anything that died while we were restarting won't give us a SIGCHLD
119 while (waitpid(-1, NULL, WNOHANG) > 0);
121 _actions = new Actions();
122 _bindings = new Bindings();
124 setMasterHandler(_actions); // set as the master event handler
126 // create the mouse cursors we'll use
127 _cursors.session = XCreateFontCursor(**otk::display, XC_left_ptr);
128 _cursors.move = XCreateFontCursor(**otk::display, XC_fleur);
129 _cursors.ll_angle = XCreateFontCursor(**otk::display, XC_ll_angle);
130 _cursors.lr_angle = XCreateFontCursor(**otk::display, XC_lr_angle);
131 _cursors.ul_angle = XCreateFontCursor(**otk::display, XC_ul_angle);
132 _cursors.ur_angle = XCreateFontCursor(**otk::display, XC_ur_angle);
134 // initialize all the screens
137 for (int i = 0, max = ScreenCount(**otk::display); i < max; ++i) {
139 // in single mode skip the screens we don't want to manage
140 if (_single && i != DefaultScreen(**otk::display)) {
141 _screens.push_back(0);
144 // try manage the screen
145 screen = new Screen(i);
146 if (screen->managed()) {
147 _screens.push_back(screen);
148 if (!_focused_screen) // set this to the first screen managed
149 _focused_screen = screen;
152 _screens.push_back(0);
156 assert(_focused_screen);
158 if (_screens.empty()) {
159 printf(_("No screens were found without a window manager. Exiting.\n"));
163 ScreenList::iterator it, end = _screens.end();
165 // run the user's script or the system defaults if that fails
168 // initialize scripting
169 python_init(argv[0]);
171 // load all of the screens' configs here so we have a theme set if
172 // we decide to show the dialog below
173 for (it = _screens.begin(); it != end; ++it)
174 (*it)->config().load(); // load the defaults from config.py
176 pyerr = doretry = false;
178 // reset all the python stuff
179 _bindings->removeAllKeys();
180 _bindings->removeAllButtons();
181 _bindings->removeAllEvents();
183 int ret = python_exec(_scriptfilepath.c_str());
188 // reset all the python stuff
189 _bindings->removeAllKeys();
190 _bindings->removeAllButtons();
191 _bindings->removeAllEvents();
193 if (python_exec(SCRIPTDIR"/defaults.py")) // system default bahaviors
199 msg += _("An error occured while executing the python scripts.");
201 msg += _("See the exact error message in Openbox's output for details.");
202 otk::MessageDialog dia(this, _("Python Error"), msg);
203 otk::DialogButton ok(_("Okay"), true);
204 otk::DialogButton retry(_("Retry"));
206 dia.addButton(retry);
209 const otk::DialogButton &res = dia.run();
212 python_destroy(); // kill all the python modules so they reinit right
215 } while (pyerr && doretry);
217 for (it = _screens.begin(); it != end; ++it) {
218 (*it)->config().load(); // load the config as the scripts may change it
219 (*it)->manageExisting();
222 // grab any keys set up before the screens existed
223 //_bindings->grabKeys(true);
225 // set up input focus
228 _state = State_Normal; // done starting
234 _state = State_Exiting; // time to kill everything
236 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
243 XSetInputFocus(**otk::display, PointerRoot, RevertToNone,
245 XSync(**otk::display, false);
251 void Openbox::parseCommandLine(int argc, char **argv)
255 for (int i = 1; i < argc; ++i) {
256 std::string arg(argv[i]);
262 _rcfilepath = argv[i];
263 } else if (arg == "-menu") {
267 _menufilepath = argv[i];
268 } else if (arg == "-script") {
272 _scriptfilepath = argv[i];
273 } else if (arg == "-sync") {
275 } else if (arg == "-single") {
277 } else if (arg == "-version") {
280 } else if (arg == "-help") {
294 void Openbox::showVersion()
296 printf(_("Openbox - version %s\n"), VERSION);
297 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
301 void Openbox::showHelp()
303 showVersion(); // show the version string and copyright
305 // print program usage and command line options
306 printf(_("Usage: %s [OPTIONS...]\n\
308 -display <string> use display connection.\n\
309 -single run on a single screen (default is to run every one).\n\
310 -rc <string> use alternate resource file.\n\
311 -menu <string> use alternate menu file.\n\
312 -script <string> use alternate startup script file.\n\
313 -sync run in synchronous mode (for debugging X errors).\n\
314 -version display version and exit.\n\
315 -help display this help text and exit.\n\n"), _argv[0]);
317 printf(_("Compile time options:\n\
349 void Openbox::eventLoop()
352 dispatchEvents(); // from otk::EventDispatcher
353 XFlush(**otk::display); // flush here before we go wait for timers
354 // don't wait if we're to shutdown
355 if (_shutdown) break;
356 otk::Timer::dispatchTimers(!_sync); // wait if not in sync mode
361 void Openbox::addClient(Window window, Client *client)
363 _clients[window] = client;
367 void Openbox::removeClient(Window window)
369 _clients.erase(window);
373 Client *Openbox::findClient(Window window)
376 NOTE: we dont use _clients[] to find the value because that will insert
377 a new null into the hash, which really sucks when we want to clean up the
380 ClientMap::iterator it = _clients.find(window);
381 if (it != _clients.end())
388 void Openbox::setFocusedClient(Client *c)
390 // sometimes this is called with the already-focused window, this is
391 // important for the python scripts to work (eg, c = 0 twice). don't just
392 // return if _focused_client == c
394 assert(_focused_screen);
396 // uninstall the old colormap
398 _focused_client->installColormap(false);
400 _focused_screen->installColormap(false);
404 _focused_screen = _screens[c->screen()];
406 // install the client's colormap
407 c->installColormap(true);
409 XSetInputFocus(**otk::display, _focused_screen->focuswindow(),
410 RevertToNone, CurrentTime);
412 // install the root window colormap
413 _focused_screen->installColormap(true);
415 // set the NET_ACTIVE_WINDOW hint for all screens
416 ScreenList::iterator it, end = _screens.end();
417 for (it = _screens.begin(); it != end; ++it) {
418 int num = (*it)->number();
419 Window root = otk::display->screenInfo(num)->rootWindow();
420 otk::Property::set(root, otk::Property::atoms.net_active_window,
421 otk::Property::atoms.window,
422 (c && _focused_screen == *it) ? c->window() : None);
425 // call the python Focus callbacks
426 EventData data(_focused_screen->number(), c, EventAction::Focus, 0);
427 _bindings->fireEvent(&data);