1 // -*- mode: C++; indent-tabs-mode: nil; -*-
4 # include "../config.h"
7 #include "../version.h"
13 #include "otk/property.hh"
14 #include "otk/display.hh"
15 #include "otk/assassin.hh"
16 #include "otk/util.hh" // TEMPORARY
19 #include <X11/cursorfont.h>
23 #endif // HAVE_STDIO_H
27 #endif // HAVE_STDLIB_H
31 #endif // HAVE_SIGNAL_H
35 #endif // HAVE_FCNTL_H
38 # include <sys/types.h>
40 #endif // HAVE_UNISTD_H
42 #ifdef HAVE_SYS_SELECT_H
43 # include <sys/select.h>
44 #endif // HAVE_SYS_SELECT_H
46 #include <python2.2/Python.h>
49 #define _(str) gettext(str)
56 Openbox *Openbox::instance = (Openbox *) 0;
59 void Openbox::signalHandler(int signal)
63 // XXX: Do something with HUP? Really shouldn't, we get this when X shuts
64 // down and hangs-up on us.
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::OtkEventDispatcher(),
83 otk::OtkEventHandler()
85 struct sigaction action;
87 _state = State_Starting; // initializing everything
89 Openbox::instance = this;
91 _displayreq = (char*) 0;
94 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
96 parseCommandLine(argc, argv);
98 // TEMPORARY: using the xrdb rc3
99 _config.setFile(_rcfilepath);
100 if (!_config.load()) {
101 printf("failed to load rc file %s\n", _config.file().c_str());
105 _config.getValue("session.styleFile", s);
107 if (!_config.load()) {
108 printf("failed to load style %s\n", _config.file().c_str());
112 // open the X display (and gets some info about it, and its screens)
113 otk::OBDisplay::initialize(_displayreq);
114 assert(otk::OBDisplay::display);
116 // set up the signal handler
117 action.sa_handler = Openbox::signalHandler;
118 action.sa_mask = sigset_t();
119 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
120 sigaction(SIGPIPE, &action, (struct sigaction *) 0);
121 sigaction(SIGSEGV, &action, (struct sigaction *) 0);
122 sigaction(SIGFPE, &action, (struct sigaction *) 0);
123 sigaction(SIGTERM, &action, (struct sigaction *) 0);
124 sigaction(SIGINT, &action, (struct sigaction *) 0);
125 sigaction(SIGHUP, &action, (struct sigaction *) 0);
127 _property = new otk::OBProperty();
129 _actions = new OBActions();
131 setMasterHandler(_actions); // set as the master event handler
133 // create the mouse cursors we'll use
134 _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
135 _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
136 _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
137 _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
138 _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
139 _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
141 // initialize all the screens
143 screen = new OBScreen(0, _config);
144 if (screen->managed()) {
145 _screens.push_back(screen);
146 _screens[0]->manageExisting();
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 // initialize the python interface
157 Py_SetProgramName(argv[0]);
159 initopenbox(); // initialize the static 'openbox' module
160 FILE *rcpyfd = fopen("/home/natas/.openbox/user.py", "r");
162 printf("failed to load python file /home/natas/.openbox/user.py\n");
164 PyRun_SimpleFile(rcpyfd, "/home/natas/.openbox/user.py");
168 _state = State_Normal; // done starting
174 _state = State_Exiting; // time to kill everything
176 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
178 // close the X display
179 otk::OBDisplay::destroy();
183 void Openbox::parseCommandLine(int argc, char **argv)
187 for (int i = 1; i < argc; ++i) {
188 std::string arg(argv[i]);
190 if (arg == "-display") {
194 _displayreq = argv[i];
195 } else if (arg == "-rc") {
199 _rcfilepath = argv[i];
200 } else if (arg == "-menu") {
204 _menufilepath = argv[i];
205 } else if (arg == "-version") {
208 } else if (arg == "-help") {
222 void Openbox::showVersion()
224 printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
225 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
229 void Openbox::showHelp()
231 showVersion(); // show the version string and copyright
233 // print program usage and command line options
234 printf(_("Usage: %s [OPTIONS...]\n\
236 -display <string> use display connection.\n\
237 -rc <string> use alternate resource file.\n\
238 -menu <string> use alternate menu file.\n\
239 -version display version and exit.\n\
240 -help display this help text and exit.\n\n"), _argv0);
242 printf(_("Compile time options:\n\
267 void Openbox::eventLoop()
269 while (!_doshutdown) {
270 dispatchEvents(); // from OtkEventDispatcher
271 _timermanager.fire();
276 void Openbox::addClient(Window window, OBClient *client)
278 _clients[window] = client;
282 void Openbox::removeClient(Window window)
284 _clients.erase(window);
288 OBClient *Openbox::findClient(Window window)
291 NOTE: we dont use _clients[] to find the value because that will insert
292 a new null into the hash, which really sucks when we want to clean up the
295 ClientMap::iterator it = _clients.find(window);
296 if (it != _clients.end())
299 return (OBClient*) 0;