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"
17 #include "otk/rendercolor.hh"
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
38 #ifdef HAVE_SYS_WAIT_H
39 # include <sys/wait.h>
40 #endif // HAVE_SYS_WAIT_H
43 #define _(str) gettext(str)
50 Openbox *openbox = (Openbox *) 0;
53 void Openbox::signalHandler(int signal)
57 printf("Caught SIGUSR1 signal. Restarting.\n");
69 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::EventDispatcher(),
86 struct sigaction action;
88 _state = State_Starting; // initializing everything
92 _displayreq = (char*) 0;
96 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
97 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
102 parseCommandLine(argc, argv);
104 XSynchronize(**otk::display, _sync);
106 // set up the signal handler
107 action.sa_handler = Openbox::signalHandler;
108 action.sa_mask = sigset_t();
109 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
110 sigaction(SIGUSR1, &action, (struct sigaction *) 0);
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);
117 sigaction(SIGCHLD, &action, (struct sigaction *) 0);
119 // anything that died while we were restarting won't give us a SIGCHLD
120 while (waitpid(-1, NULL, WNOHANG) > 0);
122 otk::RenderColor::initialize();
123 otk::Timer::initialize();
124 otk::Property::initialize();
125 _actions = new Actions();
126 _bindings = new Bindings();
128 setMasterHandler(_actions); // set as the master event handler
130 // create the mouse cursors we'll use
131 _cursors.session = XCreateFontCursor(**otk::display, XC_left_ptr);
132 _cursors.move = XCreateFontCursor(**otk::display, XC_fleur);
133 _cursors.ll_angle = XCreateFontCursor(**otk::display, XC_ll_angle);
134 _cursors.lr_angle = XCreateFontCursor(**otk::display, XC_lr_angle);
135 _cursors.ul_angle = XCreateFontCursor(**otk::display, XC_ul_angle);
136 _cursors.ur_angle = XCreateFontCursor(**otk::display, XC_ur_angle);
138 // initialize scripting
139 python_init(argv[0]);
141 // load config values
142 //python_exec(SCRIPTDIR"/config.py"); // load openbox config values
143 // run all of the python scripts
144 //python_exec(SCRIPTDIR"/builtins.py"); // builtin callbacks
145 //python_exec(SCRIPTDIR"/focus.py"); // focus helpers
146 // run the user's script or the system defaults if that fails
147 if (!python_exec(_scriptfilepath.c_str()))
148 python_exec(SCRIPTDIR"/defaults.py"); // system default bahaviors
150 // initialize all the screens
151 for (int i = 0, max = ScreenCount(**otk::display); i < max; ++i) {
153 if (_single && i != DefaultScreen(**otk::display)) {
154 _screens.push_back(0);
157 screen = new Screen(i);
158 if (screen->managed())
159 _screens.push_back(screen);
162 _screens.push_back(0);
166 if (_screens.empty()) {
167 printf(_("No screens were found without a window manager. Exiting.\n"));
171 ScreenList::iterator it, end = _screens.end();
172 for (it = _screens.begin(); it != end; ++it) {
173 (*it)->manageExisting();
176 // grab any keys set up before the screens existed
177 _bindings->grabKeys(true);
179 // set up input focus
180 _focused_screen = _screens[0];
183 _state = State_Normal; // done starting
189 _state = State_Exiting; // time to kill everything
191 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
198 XSetInputFocus(**otk::display, PointerRoot, RevertToNone,
200 XSync(**otk::display, false);
202 // this tends to block.. i honestly am not sure why. causing an x error in
203 // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
204 // all im gunna do is the same.
207 otk::Timer::destroy();
208 otk::RenderColor::destroy();
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 == "-single") {
243 } else if (arg == "-version") {
246 } else if (arg == "-help") {
260 void Openbox::showVersion()
262 printf(_("Openbox - version %s\n"), VERSION);
263 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
267 void Openbox::showHelp()
269 showVersion(); // show the version string and copyright
271 // print program usage and command line options
272 printf(_("Usage: %s [OPTIONS...]\n\
274 -display <string> use display connection.\n\
275 -single run on a single screen (default is to run every one).\n\
276 -rc <string> use alternate resource file.\n\
277 -menu <string> use alternate menu file.\n\
278 -script <string> use alternate startup script file.\n\
279 -sync run in synchronous mode (for debugging X errors).\n\
280 -version display version and exit.\n\
281 -help display this help text and exit.\n\n"), _argv[0]);
283 printf(_("Compile time options:\n\
315 void Openbox::eventLoop()
318 dispatchEvents(); // from otk::EventDispatcher
319 XFlush(**otk::display); // flush here before we go wait for timers
320 // don't wait if we're to shutdown
321 if (_shutdown) break;
322 otk::Timer::dispatchTimers(!_sync); // wait if not in sync mode
327 void Openbox::addClient(Window window, Client *client)
329 _clients[window] = client;
333 void Openbox::removeClient(Window window)
335 _clients.erase(window);
339 Client *Openbox::findClient(Window window)
342 NOTE: we dont use _clients[] to find the value because that will insert
343 a new null into the hash, which really sucks when we want to clean up the
346 ClientMap::iterator it = _clients.find(window);
347 if (it != _clients.end())
354 void Openbox::setFocusedClient(Client *c)
358 _focused_screen = _screens[c->screen()];
360 assert(_focused_screen);
361 XSetInputFocus(**otk::display, _focused_screen->focuswindow(),
362 RevertToNone, CurrentTime);
364 // set the NET_ACTIVE_WINDOW hint for all screens
365 ScreenList::iterator it, end = _screens.end();
366 for (it = _screens.begin(); it != end; ++it) {
367 int num = (*it)->number();
368 Window root = otk::display->screenInfo(num)->rootWindow();
369 otk::Property::set(root, otk::Property::atoms.net_active_window,
370 otk::Property::atoms.window,
371 (c && _focused_screen == *it) ? c->window() : None);
374 // call the python Focus callbacks
375 EventData data(_focused_screen->number(), c, EventAction::Focus, 0);
376 _bindings->fireEvent(&data);