1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
7 #include "../version.h"
12 #include "otk/property.hh"
13 #include "otk/display.hh"
14 #include "otk/assassin.hh"
15 #include "otk/util.hh" // TEMPORARY
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
47 // The initializer in openbox_wrap.cc
48 extern void init_openbox(void);
49 // The initializer in otk_wrap.cc
50 extern void init_otk(void);
53 #define _(str) gettext(str)
60 Openbox *Openbox::instance = (Openbox *) 0;
63 void Openbox::signalHandler(int signal)
67 // XXX: Do something with HUP? Really shouldn't, we get this when X shuts
68 // down and hangs-up on us.
73 printf("Caught signal %d. Exiting.\n", signal);
79 printf("Caught signal %d. Aborting and dumping core.\n", signal);
85 Openbox::Openbox(int argc, char **argv)
86 : otk::OtkEventDispatcher(),
87 otk::OtkEventHandler()
89 struct sigaction action;
91 _state = State_Starting; // initializing everything
93 Openbox::instance = this;
95 _displayreq = (char*) 0;
98 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
99 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
101 parseCommandLine(argc, argv);
103 // TEMPORARY: using the xrdb rc3
104 _config.setFile(_rcfilepath);
105 if (!_config.load()) {
106 printf("failed to load rc file %s\n", _config.file().c_str());
110 _config.getValue("session.styleFile", s);
112 if (!_config.load()) {
113 printf("failed to load style %s\n", _config.file().c_str());
117 // open the X display (and gets some info about it, and its screens)
118 otk::OBDisplay::initialize(_displayreq);
119 assert(otk::OBDisplay::display);
121 // set up the signal handler
122 action.sa_handler = Openbox::signalHandler;
123 action.sa_mask = sigset_t();
124 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
125 sigaction(SIGPIPE, &action, (struct sigaction *) 0);
126 sigaction(SIGSEGV, &action, (struct sigaction *) 0);
127 sigaction(SIGFPE, &action, (struct sigaction *) 0);
128 sigaction(SIGTERM, &action, (struct sigaction *) 0);
129 sigaction(SIGINT, &action, (struct sigaction *) 0);
130 sigaction(SIGHUP, &action, (struct sigaction *) 0);
132 _property = new otk::OBProperty();
134 _actions = new OBActions();
136 setMasterHandler(_actions); // set as the master event handler
138 // create the mouse cursors we'll use
139 _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
140 _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
141 _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
142 _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
143 _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
144 _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
146 // start up python and run the user's startup script
147 Py_SetProgramName(argv[0]);
152 //PyRun_String("from _otk import *; from _openbox import *;", Py_file_input,
153 // Py_None, Py_None);
154 FILE *rcpyfd = fopen(_scriptfilepath.c_str(), "r");
156 printf("failed to load python file %s\n", _scriptfilepath.c_str());
158 PyRun_SimpleFile(rcpyfd, const_cast<char*>(_scriptfilepath.c_str()));
162 // initialize all the screens
164 screen = new OBScreen(0, _config);
165 if (screen->managed()) {
166 _screens.push_back(screen);
167 _screens[0]->manageExisting();
168 // XXX: "change to" the first workspace on the screen to initialize stuff
172 if (_screens.empty()) {
173 printf(_("No screens were found without a window manager. Exiting.\n"));
177 _state = State_Normal; // done starting
183 _state = State_Exiting; // time to kill everything
185 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
187 // close the X display
188 otk::OBDisplay::destroy();
192 void Openbox::parseCommandLine(int argc, char **argv)
196 for (int i = 1; i < argc; ++i) {
197 std::string arg(argv[i]);
199 if (arg == "-display") {
203 _displayreq = argv[i];
204 } else if (arg == "-rc") {
208 _rcfilepath = argv[i];
209 } else if (arg == "-menu") {
213 _menufilepath = argv[i];
214 } else if (arg == "-script") {
218 _scriptfilepath = argv[i];
219 } else if (arg == "-version") {
222 } else if (arg == "-help") {
236 void Openbox::showVersion()
238 printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
239 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
243 void Openbox::showHelp()
245 showVersion(); // show the version string and copyright
247 // print program usage and command line options
248 printf(_("Usage: %s [OPTIONS...]\n\
250 -display <string> use display connection.\n\
251 -rc <string> use alternate resource file.\n\
252 -menu <string> use alternate menu file.\n\
253 -script <string> use alternate startup script file.\n\
254 -version display version and exit.\n\
255 -help display this help text and exit.\n\n"), _argv0);
257 printf(_("Compile time options:\n\
282 void Openbox::eventLoop()
284 while (!_doshutdown) {
285 dispatchEvents(); // from OtkEventDispatcher
286 _timermanager.fire();
291 void Openbox::addClient(Window window, OBClient *client)
293 _clients[window] = client;
297 void Openbox::removeClient(Window window)
299 _clients.erase(window);
303 OBClient *Openbox::findClient(Window window)
306 NOTE: we dont use _clients[] to find the value because that will insert
307 a new null into the hash, which really sucks when we want to clean up the
310 ClientMap::iterator it = _clients.find(window);
311 if (it != _clients.end())
314 return (OBClient*) 0;