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/util.hh"
18 #include <X11/cursorfont.h>
22 #endif // HAVE_STDIO_H
26 #endif // HAVE_STDLIB_H
30 #endif // HAVE_SIGNAL_H
34 #endif // HAVE_FCNTL_H
37 # include <sys/types.h>
39 #endif // HAVE_UNISTD_H
41 #ifdef HAVE_SYS_SELECT_H
42 # include <sys/select.h>
43 #endif // HAVE_SYS_SELECT_H
46 #define _(str) gettext(str)
53 Openbox *openbox = (Openbox *) 0;
56 void Openbox::signalHandler(int signal)
60 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(),
85 struct sigaction action;
87 _state = State_Starting; // initializing everything
91 _displayreq = (char*) 0;
95 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
96 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
100 parseCommandLine(argc, argv);
102 XSynchronize(**otk::display, _sync);
104 // set up the signal handler
105 action.sa_handler = Openbox::signalHandler;
106 action.sa_mask = sigset_t();
107 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
108 sigaction(SIGUSR1, &action, (struct sigaction *) 0);
109 sigaction(SIGPIPE, &action, (struct sigaction *) 0);
110 sigaction(SIGSEGV, &action, (struct sigaction *) 0);
111 sigaction(SIGFPE, &action, (struct sigaction *) 0);
112 sigaction(SIGTERM, &action, (struct sigaction *) 0);
113 sigaction(SIGINT, &action, (struct sigaction *) 0);
114 sigaction(SIGHUP, &action, (struct sigaction *) 0);
116 _property = new otk::Property();
117 _actions = new Actions();
118 _bindings = new Bindings();
120 setMasterHandler(_actions); // set as the master event handler
122 // create the mouse cursors we'll use
123 _cursors.session = XCreateFontCursor(**otk::display, XC_left_ptr);
124 _cursors.move = XCreateFontCursor(**otk::display, XC_fleur);
125 _cursors.ll_angle = XCreateFontCursor(**otk::display, XC_ll_angle);
126 _cursors.lr_angle = XCreateFontCursor(**otk::display, XC_lr_angle);
127 _cursors.ul_angle = XCreateFontCursor(**otk::display, XC_ul_angle);
128 _cursors.ur_angle = XCreateFontCursor(**otk::display, XC_ur_angle);
130 // initialize scripting
131 python_init(argv[0]);
133 // load config values
134 python_exec(SCRIPTDIR"/config.py"); // load openbox config values
135 // run all of the python scripts
136 python_exec(SCRIPTDIR"/builtins.py"); // builtin callbacks
137 // run the user's script or the system defaults if that fails
138 if (!python_exec(_scriptfilepath.c_str()))
139 python_exec(SCRIPTDIR"/defaults.py"); // system default bahaviors
141 // initialize all the screens
143 int i = _single ? DefaultScreen(**otk::display) : 0;
144 int max = _single ? i + 1 : ScreenCount(**otk::display);
145 for (; i < max; ++i) {
146 screen = new Screen(i);
147 if (screen->managed())
148 _screens.push_back(screen);
153 if (_screens.empty()) {
154 printf(_("No screens were found without a window manager. Exiting.\n"));
158 ScreenList::iterator it, end = _screens.end();
159 for (it = _screens.begin(); it != end; ++it) {
160 (*it)->manageExisting();
163 // grab any keys set up before the screens existed
164 _bindings->grabKeys(true);
166 // set up input focus
167 _focused_screen = _screens[0];
170 _state = State_Normal; // done starting
176 _state = State_Exiting; // time to kill everything
178 int first_screen = _screens.front()->number();
180 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
188 XSetInputFocus(**otk::display, PointerRoot, RevertToNone,
190 XSync(**otk::display, false);
192 // this tends to block.. i honestly am not sure why. causing an x error in
193 // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
194 // all im gunna do is the same.
195 //otk::display->destroy();
198 if (!_restart_prog.empty()) {
199 const std::string &dstr =
200 otk::display->screenInfo(first_screen)->displayString();
201 otk::putenv(const_cast<char *>(dstr.c_str()));
202 execlp(_restart_prog.c_str(), _restart_prog.c_str(), NULL);
203 perror(_restart_prog.c_str());
206 // fall back in case the above execlp doesn't work
207 execvp(_argv[0], _argv);
208 execvp(otk::basename(_argv[0]).c_str(), _argv);
213 void Openbox::parseCommandLine(int argc, char **argv)
217 for (int i = 1; i < argc; ++i) {
218 std::string arg(argv[i]);
220 if (arg == "-display") {
224 _displayreq = argv[i];
225 } else if (arg == "-rc") {
229 _rcfilepath = argv[i];
230 } else if (arg == "-menu") {
234 _menufilepath = argv[i];
235 } else if (arg == "-script") {
239 _scriptfilepath = argv[i];
240 } else if (arg == "-sync") {
242 } else if (arg == "-single") {
244 } else if (arg == "-version") {
247 } else if (arg == "-help") {
261 void Openbox::showVersion()
263 printf(_("Openbox - version %s\n"), VERSION);
264 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
268 void Openbox::showHelp()
270 showVersion(); // show the version string and copyright
272 // print program usage and command line options
273 printf(_("Usage: %s [OPTIONS...]\n\
275 -display <string> use display connection.\n\
276 -single run on a single screen (default is to run every one).\n\
277 -rc <string> use alternate resource file.\n\
278 -menu <string> use alternate menu file.\n\
279 -script <string> use alternate startup script file.\n\
280 -sync run in synchronous mode (for debugging).\n\
281 -version display version and exit.\n\
282 -help display this help text and exit.\n\n"), _argv[0]);
284 printf(_("Compile time options:\n\
316 void Openbox::eventLoop()
319 dispatchEvents(); // from otk::EventDispatcher
320 XFlush(**otk::display); // flush here before we go wait for timers
321 // don't wait if we're to shutdown
322 if (_shutdown) break;
323 _timermanager.fire(!_sync); // wait if not in sync mode
328 void Openbox::addClient(Window window, Client *client)
330 _clients[window] = client;
334 void Openbox::removeClient(Window window)
336 _clients.erase(window);
340 Client *Openbox::findClient(Window window)
343 NOTE: we dont use _clients[] to find the value because that will insert
344 a new null into the hash, which really sucks when we want to clean up the
347 ClientMap::iterator it = _clients.find(window);
348 if (it != _clients.end())
355 void Openbox::setFocusedClient(Client *c)
359 _focused_screen = _screens[c->screen()];
361 assert(_focused_screen);
362 XSetInputFocus(**otk::display, _focused_screen->focuswindow(),
363 RevertToNone, CurrentTime);
365 // set the NET_ACTIVE_WINDOW hint for all screens
366 ScreenList::iterator it, end = _screens.end();
367 for (it = _screens.begin(); it != end; ++it) {
368 int num = (*it)->number();
369 Window root = otk::display->screenInfo(num)->rootWindow();
370 _property->set(root, otk::Property::net_active_window,
371 otk::Property::Atom_Window,
372 (c && _focused_screen == *it) ? c->window() : None);
375 // call the python Focus callbacks
376 EventData data(_focused_screen->number(), c, EventFocus, 0);
377 _bindings->fireEvent(&data);
380 void Openbox::execute(int screen, const std::string &bin)
382 if (screen >= ScreenCount(**otk::display))
384 otk::bexec(bin, otk::display->screenInfo(screen)->displayString());