1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
11 #include "bindings.hh"
13 #include "otk/property.hh"
14 #include "otk/assassin.hh"
15 #include "otk/property.hh"
16 #include "otk/util.hh"
19 #include <X11/cursorfont.h>
23 #endif // HAVE_STDIO_H
27 #endif // HAVE_STDLIB_H
31 #endif // HAVE_SIGNAL_H
35 #endif // HAVE_FCNTL_H
38 # include <sys/types.h>
40 #endif // HAVE_UNISTD_H
42 #ifdef HAVE_SYS_SELECT_H
43 # include <sys/select.h>
44 #endif // HAVE_SYS_SELECT_H
46 #ifdef HAVE_SYS_WAIT_H
47 # include <sys/wait.h>
48 #endif // HAVE_SYS_WAIT_H
51 #define _(str) gettext(str)
58 Openbox *openbox = (Openbox *) 0;
61 void Openbox::signalHandler(int signal)
65 printf("Caught SIGUSR1 signal. Restarting.\n");
77 printf("Caught signal %d. Exiting.\n", signal);
83 printf("Caught signal %d. Aborting and dumping core.\n", signal);
89 Openbox::Openbox(int argc, char **argv)
90 : otk::EventDispatcher(),
94 struct sigaction action;
96 _state = State_Starting; // initializing everything
100 _displayreq = (char*) 0;
104 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
105 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
109 parseCommandLine(argc, argv);
111 XSynchronize(**otk::display, _sync);
113 // set up the signal handler
114 action.sa_handler = Openbox::signalHandler;
115 action.sa_mask = sigset_t();
116 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
117 sigaction(SIGUSR1, &action, (struct sigaction *) 0);
118 sigaction(SIGPIPE, &action, (struct sigaction *) 0);
119 sigaction(SIGSEGV, &action, (struct sigaction *) 0);
120 sigaction(SIGFPE, &action, (struct sigaction *) 0);
121 sigaction(SIGTERM, &action, (struct sigaction *) 0);
122 sigaction(SIGINT, &action, (struct sigaction *) 0);
123 sigaction(SIGHUP, &action, (struct sigaction *) 0);
124 sigaction(SIGCHLD, &action, (struct sigaction *) 0);
126 // anything that died while we were restarting won't give us a SIGCHLD
127 while (waitpid(-1, NULL, WNOHANG) > 0);
129 otk::Timer::initialize();
130 otk::Property::initialize();
131 _actions = new Actions();
132 _bindings = new Bindings();
134 setMasterHandler(_actions); // set as the master event handler
136 // create the mouse cursors we'll use
137 _cursors.session = XCreateFontCursor(**otk::display, XC_left_ptr);
138 _cursors.move = XCreateFontCursor(**otk::display, XC_fleur);
139 _cursors.ll_angle = XCreateFontCursor(**otk::display, XC_ll_angle);
140 _cursors.lr_angle = XCreateFontCursor(**otk::display, XC_lr_angle);
141 _cursors.ul_angle = XCreateFontCursor(**otk::display, XC_ul_angle);
142 _cursors.ur_angle = XCreateFontCursor(**otk::display, XC_ur_angle);
144 // initialize scripting
145 python_init(argv[0]);
147 // load config values
148 python_exec(SCRIPTDIR"/config.py"); // load openbox config values
149 // run all of the python scripts
150 python_exec(SCRIPTDIR"/builtins.py"); // builtin callbacks
151 // run the user's script or the system defaults if that fails
152 if (!python_exec(_scriptfilepath.c_str()))
153 python_exec(SCRIPTDIR"/defaults.py"); // system default bahaviors
155 // initialize all the screens
157 int i = _single ? DefaultScreen(**otk::display) : 0;
158 int max = _single ? i + 1 : ScreenCount(**otk::display);
159 for (; i < max; ++i) {
160 screen = new Screen(i);
161 if (screen->managed())
162 _screens.push_back(screen);
167 if (_screens.empty()) {
168 printf(_("No screens were found without a window manager. Exiting.\n"));
172 ScreenList::iterator it, end = _screens.end();
173 for (it = _screens.begin(); it != end; ++it) {
174 (*it)->manageExisting();
177 // grab any keys set up before the screens existed
178 _bindings->grabKeys(true);
180 // set up input focus
181 _focused_screen = _screens[0];
184 _state = State_Normal; // done starting
190 _state = State_Exiting; // time to kill everything
192 int first_screen = _screens.front()->number();
194 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
201 XSetInputFocus(**otk::display, PointerRoot, RevertToNone,
203 XSync(**otk::display, false);
205 // this tends to block.. i honestly am not sure why. causing an x error in
206 // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
207 // all im gunna do is the same.
208 //otk::display->destroy();
210 otk::Timer::destroy();
213 if (!_restart_prog.empty()) {
214 otk::putenv(otk::display->screenInfo(first_screen)->displayString());
215 execl("/bin/sh", "/bin/sh", "-c", _restart_prog.c_str(), NULL);
216 perror(_restart_prog.c_str());
219 // fall back in case the above execlp doesn't work
220 execvp(_argv[0], _argv);
221 execvp(otk::basename(_argv[0]).c_str(), _argv);
226 void Openbox::parseCommandLine(int argc, char **argv)
230 for (int i = 1; i < argc; ++i) {
231 std::string arg(argv[i]);
233 if (arg == "-display") {
237 _displayreq = argv[i];
238 } else if (arg == "-rc") {
242 _rcfilepath = argv[i];
243 } else if (arg == "-menu") {
247 _menufilepath = argv[i];
248 } else if (arg == "-script") {
252 _scriptfilepath = argv[i];
253 } else if (arg == "-sync") {
255 } else if (arg == "-single") {
257 } else if (arg == "-version") {
260 } else if (arg == "-help") {
274 void Openbox::showVersion()
276 printf(_("Openbox - version %s\n"), VERSION);
277 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
281 void Openbox::showHelp()
283 showVersion(); // show the version string and copyright
285 // print program usage and command line options
286 printf(_("Usage: %s [OPTIONS...]\n\
288 -display <string> use display connection.\n\
289 -single run on a single screen (default is to run every one).\n\
290 -rc <string> use alternate resource file.\n\
291 -menu <string> use alternate menu file.\n\
292 -script <string> use alternate startup script file.\n\
293 -sync run in synchronous mode (for debugging).\n\
294 -version display version and exit.\n\
295 -help display this help text and exit.\n\n"), _argv[0]);
297 printf(_("Compile time options:\n\
329 void Openbox::eventLoop()
332 dispatchEvents(); // from otk::EventDispatcher
333 XFlush(**otk::display); // flush here before we go wait for timers
334 // don't wait if we're to shutdown
335 if (_shutdown) break;
336 otk::Timer::dispatchTimers(!_sync); // wait if not in sync mode
341 void Openbox::addClient(Window window, Client *client)
343 _clients[window] = client;
347 void Openbox::removeClient(Window window)
349 _clients.erase(window);
353 Client *Openbox::findClient(Window window)
356 NOTE: we dont use _clients[] to find the value because that will insert
357 a new null into the hash, which really sucks when we want to clean up the
360 ClientMap::iterator it = _clients.find(window);
361 if (it != _clients.end())
368 void Openbox::setFocusedClient(Client *c)
372 _focused_screen = _screens[c->screen()];
374 assert(_focused_screen);
375 XSetInputFocus(**otk::display, _focused_screen->focuswindow(),
376 RevertToNone, CurrentTime);
378 // set the NET_ACTIVE_WINDOW hint for all screens
379 ScreenList::iterator it, end = _screens.end();
380 for (it = _screens.begin(); it != end; ++it) {
381 int num = (*it)->number();
382 Window root = otk::display->screenInfo(num)->rootWindow();
383 otk::Property::set(root, otk::Property::atoms.net_active_window,
384 otk::Property::atoms.window,
385 (c && _focused_screen == *it) ? c->window() : None);
388 // call the python Focus callbacks
389 EventData data(_focused_screen->number(), c, EventFocus, 0);
390 _bindings->fireEvent(&data);
393 void Openbox::execute(int screen, const std::string &bin)
395 if (screen >= ScreenCount(**otk::display))
397 otk::bexec(bin, otk::display->screenInfo(screen)->displayString());