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 printf("Caught SIGUSR1 signal. Restarting.\n");
70 printf("Caught signal %d. Exiting.\n", signal);
75 printf("Caught signal %d. Aborting and dumping core.\n", signal);
81 Openbox::Openbox(int argc, char **argv)
82 : otk::OtkEventDispatcher(),
83 otk::OtkEventHandler()
85 struct sigaction action;
87 _state = State_Starting; // initializing everything
89 Openbox::instance = this;
91 _displayreq = (char*) 0;
95 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
96 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
100 parseCommandLine(argc, argv);
102 // open the X display (and gets some info about it, and its screens)
103 otk::OBDisplay::initialize(_displayreq);
104 assert(otk::OBDisplay::display);
106 XSynchronize(otk::OBDisplay::display, _sync);
108 // set up the signal handler
109 action.sa_handler = Openbox::signalHandler;
110 action.sa_mask = sigset_t();
111 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
112 sigaction(SIGPIPE, &action, (struct sigaction *) 0);
113 sigaction(SIGSEGV, &action, (struct sigaction *) 0);
114 sigaction(SIGFPE, &action, (struct sigaction *) 0);
115 sigaction(SIGTERM, &action, (struct sigaction *) 0);
116 sigaction(SIGINT, &action, (struct sigaction *) 0);
117 sigaction(SIGHUP, &action, (struct sigaction *) 0);
119 _property = new otk::OBProperty();
120 _actions = new OBActions();
121 _bindings = new OBBindings();
123 setMasterHandler(_actions); // set as the master event handler
125 // create the mouse cursors we'll use
126 _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
127 _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
128 _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
129 _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
130 _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
131 _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
133 // initialize scripting
134 python_init(argv[0]);
136 // load config values
137 python_exec(SCRIPTDIR"/config.py"); // load openbox config values
138 // run all of the python scripts
139 python_exec(SCRIPTDIR"/builtins.py"); // builtin callbacks
140 // run the user's script
141 python_exec(_scriptfilepath.c_str());
143 // initialize all the screens
145 screen = new OBScreen(0);
146 if (screen->managed()) {
147 _screens.push_back(screen);
148 // XXX: "change to" the first workspace on the screen to initialize stuff
152 if (_screens.empty()) {
153 printf(_("No screens were found without a window manager. Exiting.\n"));
157 ScreenList::iterator it, end = _screens.end();
158 for (it = _screens.begin(); it != end; ++it) {
159 (*it)->manageExisting();
162 // grab any keys set up before the screens existed
163 _bindings->grabKeys(true);
165 // set up input focus
166 _focused_screen = _screens[0];
169 _state = State_Normal; // done starting
175 _state = State_Exiting; // time to kill everything
177 int first_screen = _screens.front()->number();
179 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
187 XSetInputFocus(otk::OBDisplay::display, PointerRoot, RevertToNone,
189 XSync(otk::OBDisplay::display, false);
191 // this tends to block.. i honestly am not sure why. causing an x error in
192 // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
193 // all im gunna do is the same.
194 //otk::OBDisplay::destroy();
197 if (!_restart_prog.empty()) {
198 const std::string &dstr =
199 otk::OBDisplay::screenInfo(first_screen)->displayString();
200 putenv(const_cast<char *>(dstr.c_str()));
201 execlp(_restart_prog.c_str(), _restart_prog.c_str(), NULL);
202 perror(_restart_prog.c_str());
205 // fall back in case the above execlp doesn't work
206 execvp(_argv[0], _argv);
207 execvp(otk::basename(_argv[0]).c_str(), _argv);
212 void Openbox::parseCommandLine(int argc, char **argv)
216 for (int i = 1; i < argc; ++i) {
217 std::string arg(argv[i]);
219 if (arg == "-display") {
223 _displayreq = argv[i];
224 } else if (arg == "-rc") {
228 _rcfilepath = argv[i];
229 } else if (arg == "-menu") {
233 _menufilepath = argv[i];
234 } else if (arg == "-script") {
238 _scriptfilepath = argv[i];
239 } else if (arg == "-sync") {
241 } else if (arg == "-version") {
244 } else if (arg == "-help") {
258 void Openbox::showVersion()
260 printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
261 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
265 void Openbox::showHelp()
267 showVersion(); // show the version string and copyright
269 // print program usage and command line options
270 printf(_("Usage: %s [OPTIONS...]\n\
272 -display <string> use display connection.\n\
273 -rc <string> use alternate resource file.\n\
274 -menu <string> use alternate menu file.\n\
275 -script <string> use alternate startup script file.\n\
276 -version display version and exit.\n\
277 -help display this help text and exit.\n\n"), _argv[0]);
279 printf(_("Compile time options:\n\
304 void Openbox::eventLoop()
307 _timermanager.fire();
308 dispatchEvents(); // from OtkEventDispatcher
309 XFlush(otk::OBDisplay::display); // flush here before we go wait for timers
314 void Openbox::addClient(Window window, OBClient *client)
316 _clients[window] = client;
320 void Openbox::removeClient(Window window)
322 _clients.erase(window);
326 OBClient *Openbox::findClient(Window window)
329 NOTE: we dont use _clients[] to find the value because that will insert
330 a new null into the hash, which really sucks when we want to clean up the
333 ClientMap::iterator it = _clients.find(window);
334 if (it != _clients.end())
337 return (OBClient*) 0;
341 void Openbox::setFocusedClient(OBClient *c)
345 _focused_screen = _screens[c->screen()];
347 assert(_focused_screen);
348 XSetInputFocus(otk::OBDisplay::display, _focused_screen->focuswindow(),
349 RevertToNone, CurrentTime);
351 // set the NET_ACTIVE_WINDOW hint for all screens
352 ScreenList::iterator it, end = _screens.end();
353 for (it = _screens.begin(); it != end; ++it) {
354 int num = (*it)->number();
355 Window root = otk::OBDisplay::screenInfo(num)->rootWindow();
356 _property->set(root, otk::OBProperty::net_active_window,
357 otk::OBProperty::Atom_Window,
358 (c && _focused_screen == *it) ? c->window() : None);
362 void Openbox::execute(int screen, const std::string &bin)
365 // XXX: whats this for? windows?
366 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", bin.c_str(), NULL);
368 if (screen >= ScreenCount(otk::OBDisplay::display))
370 const std::string &dstr =
371 otk::OBDisplay::screenInfo(screen)->displayString();
375 int ret = putenv(const_cast<char *>(dstr.c_str()));
377 ret = execl("/bin/sh", "/bin/sh", "-c", bin.c_str(), NULL);