1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
11 #include "otk/property.hh"
12 #include "otk/assassin.hh"
13 #include "otk/property.hh"
14 #include "otk/util.hh"
15 #include "otk/rendercolor.hh"
16 #include "otk/renderstyle.hh"
17 #include "otk/messagedialog.hh"
20 #include <X11/cursorfont.h>
24 #endif // HAVE_SIGNAL_H
28 #endif // HAVE_FCNTL_H
30 #ifdef HAVE_SYS_WAIT_H
31 # include <sys/wait.h>
32 #endif // HAVE_SYS_WAIT_H
35 #define _(str) gettext(str)
43 extern void initialize();
44 extern void destroy();
49 Openbox *openbox = (Openbox *) 0;
52 void Openbox::signalHandler(int signal)
56 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(),
84 struct sigaction action;
86 _state = State_Starting; // initializing everything
93 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
94 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
100 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 _actions = new Actions();
123 _bindings = new Bindings();
125 setMasterHandler(_actions); // set as the master event handler
127 // create the mouse cursors we'll use
128 _cursors.session = XCreateFontCursor(**otk::display, XC_left_ptr);
129 _cursors.move = XCreateFontCursor(**otk::display, XC_fleur);
130 _cursors.ll_angle = XCreateFontCursor(**otk::display, XC_ll_angle);
131 _cursors.lr_angle = XCreateFontCursor(**otk::display, XC_lr_angle);
132 _cursors.ul_angle = XCreateFontCursor(**otk::display, XC_ul_angle);
133 _cursors.ur_angle = XCreateFontCursor(**otk::display, XC_ur_angle);
135 // initialize all the screens
139 for (int i = 0, max = ScreenCount(**otk::display); i < max; ++i) {
141 // in single mode skip the screens we don't want to manage
142 if (_single && i != DefaultScreen(**otk::display)) {
143 _screens.push_back(0);
146 // try manage the screen
147 screen = new Screen(i);
148 if (screen->managed()) {
149 _screens.push_back(screen);
150 if (!_focused_screen) // set this to the first screen managed
151 _focused_screen = screen;
155 _screens.push_back(0);
159 if (!_managed_count) {
160 printf(_("No screens were found without a window manager. Exiting.\n"));
164 assert(_focused_screen);
166 // initialize scripting
167 python_init(argv[0]);
169 // load the theme XXX TEMP SHIT
170 otk::RenderStyle::setStyle(0, "");
172 int ret = python_exec(_scriptfilepath.c_str());
175 msg += _("An error occured while executing the python scripts.");
177 msg += _("See the exact error message in Openbox's output for details.");
178 otk::MessageDialog dia(this, _("Python Error"), msg);
179 otk::DialogButton ok(_("Okay"), true);
180 otk::DialogButton retry(_("Restart"));
182 dia.addButton(retry);
185 const otk::DialogButton &res = dia.run();
187 _restart = _shutdown = true;
193 python_exec(SCRIPTDIR"/defaults.py"); // system default bahaviors
195 ScreenList::iterator it, end = _screens.end();
196 for (it = _screens.begin(); it != end; ++it)
197 if (*it) (*it)->manageExisting();
199 // grab any keys set up before the screens existed
200 //_bindings->grabKeys(true);
202 // set up input focus
205 _state = State_Normal; // done starting
211 _state = State_Exiting; // time to kill everything
213 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
220 XSetInputFocus(**otk::display, PointerRoot, RevertToNone,
222 XSync(**otk::display, false);
228 void Openbox::parseCommandLine(int argc, char **argv)
232 for (int i = 1; i < argc; ++i) {
233 std::string arg(argv[i]);
239 _rcfilepath = argv[i];
240 } else if (arg == "-menu") {
244 _menufilepath = argv[i];
245 } else if (arg == "-script") {
249 _scriptfilepath = argv[i];
250 } else if (arg == "-sync") {
252 } else if (arg == "-single") {
254 } else if (arg == "-remote") {
256 } else if (arg == "-version") {
259 } else if (arg == "-help") {
273 void Openbox::showVersion()
275 printf(_("Openbox - version %s\n"), VERSION);
276 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
280 void Openbox::showHelp()
282 showVersion(); // show the version string and copyright
284 // print program usage and command line options
285 printf(_("Usage: %s [OPTIONS...]\n\
287 -remote optimize for a remote (low bandwidth) connection to the\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 X errors).\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(false); // from otk::EventDispatcher
333 // XFlush(**otk::display); // flush here before we go wait for timers
334 // .. the XPending() should have done this last
335 // already, it does a flush when it returns 0
336 // don't wait if we're to shutdown
337 if (_shutdown) break;
338 otk::Timer::dispatchTimers(!_sync); // wait if not in sync mode
343 void Openbox::addClient(Window window, Client *client)
345 _clients[window] = client;
349 void Openbox::removeClient(Window window)
351 _clients.erase(window);
355 Client *Openbox::findClient(Window window)
358 NOTE: we dont use _clients[] to find the value because that will insert
359 a new null into the hash, which really sucks when we want to clean up the
362 ClientMap::iterator it = _clients.find(window);
363 if (it != _clients.end())
370 void Openbox::setFocusedClient(Client *c)
372 // sometimes this is called with the already-focused window, this is
373 // important for the python scripts to work (eg, c = 0 twice). don't just
374 // return if _focused_client == c
376 assert(_focused_screen);
378 // uninstall the old colormap
380 _focused_client->installColormap(false);
382 _focused_screen->installColormap(false);
386 _focused_screen = _screens[c->screen()];
388 // install the client's colormap
389 c->installColormap(true);
391 XSetInputFocus(**otk::display, _focused_screen->focuswindow(),
392 RevertToNone, CurrentTime);
394 // install the root window colormap
395 _focused_screen->installColormap(true);
397 // set the NET_ACTIVE_WINDOW hint for all screens
398 ScreenList::iterator it, end = _screens.end();
399 for (it = _screens.begin(); it != end; ++it) {
400 int num = (*it)->number();
401 Window root = otk::display->screenInfo(num)->rootWindow();
402 otk::Property::set(root, otk::Property::atoms.net_active_window,
403 otk::Property::atoms.window,
404 (c && _focused_screen == *it) ? c->window() : None);
407 // call the python Focus callbacks
408 EventData data(_focused_screen->number(), c, EventAction::Focus, 0);
409 _bindings->fireEvent(&data);