1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
7 #include "../version.h"
12 #include "bindings.hh"
14 #include "otk/property.hh"
15 #include "otk/display.hh"
16 #include "otk/assassin.hh"
17 #include "otk/util.hh" // TEMPORARY
20 #include <X11/cursorfont.h>
24 #endif // HAVE_STDIO_H
28 #endif // HAVE_STDLIB_H
32 #endif // HAVE_SIGNAL_H
36 #endif // HAVE_FCNTL_H
39 # include <sys/types.h>
41 #endif // HAVE_UNISTD_H
43 #ifdef HAVE_SYS_SELECT_H
44 # include <sys/select.h>
45 #endif // HAVE_SYS_SELECT_H
48 #define _(str) gettext(str)
55 Openbox *Openbox::instance = (Openbox *) 0;
58 void Openbox::signalHandler(int signal)
62 // XXX: Do something with HUP? Really shouldn't, we get this when X shuts
63 // down and hangs-up on us.
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::OtkEventDispatcher(),
82 otk::OtkEventHandler()
84 struct sigaction action;
86 _state = State_Starting; // initializing everything
88 Openbox::instance = this;
90 _displayreq = (char*) 0;
94 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
95 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
99 parseCommandLine(argc, argv);
101 // open the X display (and gets some info about it, and its screens)
102 otk::OBDisplay::initialize(_displayreq);
103 assert(otk::OBDisplay::display);
105 XSynchronize(otk::OBDisplay::display, _sync);
107 // set up the signal handler
108 action.sa_handler = Openbox::signalHandler;
109 action.sa_mask = sigset_t();
110 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
111 sigaction(SIGPIPE, &action, (struct sigaction *) 0);
112 sigaction(SIGSEGV, &action, (struct sigaction *) 0);
113 sigaction(SIGFPE, &action, (struct sigaction *) 0);
114 sigaction(SIGTERM, &action, (struct sigaction *) 0);
115 sigaction(SIGINT, &action, (struct sigaction *) 0);
116 sigaction(SIGHUP, &action, (struct sigaction *) 0);
118 _property = new otk::OBProperty();
119 _actions = new OBActions();
120 _bindings = new OBBindings();
122 setMasterHandler(_actions); // set as the master event handler
124 // create the mouse cursors we'll use
125 _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
126 _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
127 _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
128 _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
129 _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
130 _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
132 // initialize scripting
133 python_init(argv[0]);
135 // load config values
136 python_exec(SCRIPTDIR"/config.py"); // load openbox config values
137 // run all of the python scripts
138 python_exec(SCRIPTDIR"/builtins.py"); // builtin callbacks
139 // run the user's script
140 python_exec(_scriptfilepath.c_str());
142 // initialize all the screens
144 screen = new OBScreen(0);
145 if (screen->managed()) {
146 _screens.push_back(screen);
147 // XXX: "change to" the first workspace on the screen to initialize stuff
151 if (_screens.empty()) {
152 printf(_("No screens were found without a window manager. Exiting.\n"));
156 ScreenList::iterator it, end = _screens.end();
157 for (it = _screens.begin(); it != end; ++it) {
158 (*it)->manageExisting();
161 // grab any keys set up before the screens existed
162 _bindings->grabKeys(true);
164 // set up input focus
165 _focused_screen = _screens[0];
168 _state = State_Normal; // done starting
174 _state = State_Exiting; // time to kill everything
176 int first_screen = _screens.front()->number();
178 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
186 XSetInputFocus(otk::OBDisplay::display, PointerRoot, RevertToNone,
188 XSync(otk::OBDisplay::display, false);
190 // this tends to block.. i honestly am not sure why. causing an x error in
191 // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
192 // all im gunna do is the same.
193 //otk::OBDisplay::destroy();
196 if (!_restart_prog.empty()) {
197 const std::string &dstr =
198 otk::OBDisplay::screenInfo(first_screen)->displayString();
199 putenv(const_cast<char *>(dstr.c_str()));
200 execlp(_restart_prog.c_str(), _restart_prog.c_str(), NULL);
201 perror(_restart_prog.c_str());
204 // fall back in case the above execlp doesn't work
205 execvp(_argv[0], _argv);
206 execvp(otk::basename(_argv[0]).c_str(), _argv);
211 void Openbox::parseCommandLine(int argc, char **argv)
215 for (int i = 1; i < argc; ++i) {
216 std::string arg(argv[i]);
218 if (arg == "-display") {
222 _displayreq = argv[i];
223 } else if (arg == "-rc") {
227 _rcfilepath = argv[i];
228 } else if (arg == "-menu") {
232 _menufilepath = argv[i];
233 } else if (arg == "-script") {
237 _scriptfilepath = argv[i];
238 } else if (arg == "-sync") {
240 } else if (arg == "-version") {
243 } else if (arg == "-help") {
257 void Openbox::showVersion()
259 printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
260 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
264 void Openbox::showHelp()
266 showVersion(); // show the version string and copyright
268 // print program usage and command line options
269 printf(_("Usage: %s [OPTIONS...]\n\
271 -display <string> use display connection.\n\
272 -rc <string> use alternate resource file.\n\
273 -menu <string> use alternate menu file.\n\
274 -script <string> use alternate startup script file.\n\
275 -version display version and exit.\n\
276 -help display this help text and exit.\n\n"), _argv[0]);
278 printf(_("Compile time options:\n\
303 void Openbox::eventLoop()
306 _timermanager.fire();
307 dispatchEvents(); // from OtkEventDispatcher
308 XFlush(otk::OBDisplay::display); // flush here before we go wait for timers
313 void Openbox::addClient(Window window, OBClient *client)
315 _clients[window] = client;
319 void Openbox::removeClient(Window window)
321 _clients.erase(window);
325 OBClient *Openbox::findClient(Window window)
328 NOTE: we dont use _clients[] to find the value because that will insert
329 a new null into the hash, which really sucks when we want to clean up the
332 ClientMap::iterator it = _clients.find(window);
333 if (it != _clients.end())
336 return (OBClient*) 0;
340 void Openbox::setFocusedClient(OBClient *c)
344 _focused_screen = _screens[c->screen()];
346 assert(_focused_screen);
347 XSetInputFocus(otk::OBDisplay::display, _focused_screen->focuswindow(),
348 RevertToNone, CurrentTime);
352 void Openbox::execute(int screen, const std::string &bin)
355 // XXX: whats this for? windows?
356 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", bin.c_str(), NULL);
358 if (screen >= ScreenCount(otk::OBDisplay::display))
360 const std::string &dstr =
361 otk::OBDisplay::screenInfo(screen)->displayString();
365 int ret = putenv(const_cast<char *>(dstr.c_str()));
367 ret = execl("/bin/sh", "/bin/sh", "-c", bin.c_str(), NULL);