]> icculus.org git repositories - dana/openbox.git/blob - src/openbox.cc
define the openbox variable
[dana/openbox.git] / src / openbox.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "../version.h"
8 #include "openbox.hh"
9 #include "client.hh"
10 #include "screen.hh"
11 #include "actions.hh"
12 #include "otk/property.hh"
13 #include "otk/display.hh"
14 #include "otk/assassin.hh"
15 #include "otk/util.hh" // TEMPORARY
16
17 extern "C" {
18 #include <X11/cursorfont.h>
19
20 #ifdef    HAVE_STDIO_H
21 #  include <stdio.h>
22 #endif // HAVE_STDIO_H
23
24 #ifdef    HAVE_STDLIB_H
25 #  include <stdlib.h>
26 #endif // HAVE_STDLIB_H
27
28 #ifdef    HAVE_SIGNAL_H
29 #  include <signal.h>
30 #endif // HAVE_SIGNAL_H
31
32 #ifdef    HAVE_FCNTL_H
33 #  include <fcntl.h>
34 #endif // HAVE_FCNTL_H
35
36 #ifdef    HAVE_UNISTD_H
37 #  include <sys/types.h>
38 #  include <unistd.h>
39 #endif // HAVE_UNISTD_H
40
41 #ifdef    HAVE_SYS_SELECT_H
42 #  include <sys/select.h>
43 #endif // HAVE_SYS_SELECT_H
44
45 #include <Python.h>
46   
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);
51
52 #include "gettext.h"
53 #define _(str) gettext(str)
54 }
55
56 #include <algorithm>
57
58 namespace ob {
59
60 Openbox *Openbox::instance  = (Openbox *) 0;
61
62
63 void Openbox::signalHandler(int signal)
64 {
65   switch (signal) {
66   case SIGHUP:
67     // XXX: Do something with HUP? Really shouldn't, we get this when X shuts
68     //      down and hangs-up on us.
69     
70   case SIGINT:
71   case SIGTERM:
72   case SIGPIPE:
73     printf("Caught signal %d. Exiting.\n", signal);
74     instance->shutdown();
75
76     break;
77   case SIGFPE:
78   case SIGSEGV:
79     printf("Caught signal %d. Aborting and dumping core.\n", signal);
80     abort();
81   }
82 }
83
84
85 Openbox::Openbox(int argc, char **argv)
86   : otk::OtkEventDispatcher(),
87     otk::OtkEventHandler()
88 {
89   struct sigaction action;
90
91   _state = State_Starting; // initializing everything
92
93   Openbox::instance = this;
94
95   _displayreq = (char*) 0;
96   _argv0 = argv[0];
97   _doshutdown = false;
98   _rcfilepath = otk::expandTilde("~/.openbox/rc3");
99   _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
100
101   parseCommandLine(argc, argv);
102
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());
107     ::exit(2);
108   }
109   std::string s;
110   _config.getValue("session.styleFile", s);
111   _config.setFile(s);
112   if (!_config.load()) {
113     printf("failed to load style %s\n", _config.file().c_str());
114     ::exit(2);
115   }
116
117   // open the X display (and gets some info about it, and its screens)
118   otk::OBDisplay::initialize(_displayreq);
119   assert(otk::OBDisplay::display);
120     
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);
131
132   _property = new otk::OBProperty();
133
134   _actions = new OBActions();
135
136   setMasterHandler(_actions); // set as the master event handler
137
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);
145
146   // start up python and run the user's startup script
147   Py_SetProgramName(argv[0]);
148   Py_Initialize();
149   init_otk();
150   init_openbox();
151   PyRun_SimpleString("from _otk import *; from _openbox import *;");
152   PyRun_SimpleString("openbox = Openbox_instance()");
153   FILE *rcpyfd = fopen(_scriptfilepath.c_str(), "r");
154   if (!rcpyfd) {
155     printf("failed to load python file %s\n", _scriptfilepath.c_str());
156   } else {
157     PyRun_SimpleFile(rcpyfd, const_cast<char*>(_scriptfilepath.c_str()));
158     fclose(rcpyfd);
159   }
160  
161   // initialize all the screens
162   OBScreen *screen;
163   screen = new OBScreen(0, _config);
164   if (screen->managed()) {
165     _screens.push_back(screen);
166     _screens[0]->manageExisting();
167     // XXX: "change to" the first workspace on the screen to initialize stuff
168   } else
169     delete screen;
170
171   if (_screens.empty()) {
172     printf(_("No screens were found without a window manager. Exiting.\n"));
173     ::exit(1);
174   }
175
176   _state = State_Normal; // done starting
177 }
178
179
180 Openbox::~Openbox()
181 {
182   _state = State_Exiting; // time to kill everything
183
184   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
185   
186   // close the X display
187   otk::OBDisplay::destroy();
188 }
189
190
191 void Openbox::parseCommandLine(int argc, char **argv)
192 {
193   bool err = false;
194
195   for (int i = 1; i < argc; ++i) {
196     std::string arg(argv[i]);
197
198     if (arg == "-display") {
199       if (++i >= argc)
200         err = true;
201       else
202         _displayreq = argv[i];
203     } else if (arg == "-rc") {
204       if (++i >= argc)
205         err = true;
206       else
207         _rcfilepath = argv[i];
208     } else if (arg == "-menu") {
209       if (++i >= argc)
210         err = true;
211       else
212         _menufilepath = argv[i];
213     } else if (arg == "-script") {
214       if (++i >= argc)
215         err = true;
216       else
217         _scriptfilepath = argv[i];
218     } else if (arg == "-version") {
219       showVersion();
220       ::exit(0);
221     } else if (arg == "-help") {
222       showHelp();
223       ::exit(0);
224     } else
225       err = true;
226
227     if (err) {
228       showHelp();
229       exit(1);
230     }
231   }
232 }
233
234
235 void Openbox::showVersion()
236 {
237   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
238   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
239 }
240
241
242 void Openbox::showHelp()
243 {
244   showVersion(); // show the version string and copyright
245
246   // print program usage and command line options
247   printf(_("Usage: %s [OPTIONS...]\n\
248   Options:\n\
249   -display <string>  use display connection.\n\
250   -rc <string>       use alternate resource file.\n\
251   -menu <string>     use alternate menu file.\n\
252   -script <string>   use alternate startup script file.\n\
253   -version           display version and exit.\n\
254   -help              display this help text and exit.\n\n"), _argv0);
255
256   printf(_("Compile time options:\n\
257   Debugging: %s\n\
258   Shape:     %s\n\
259   Xinerama:  %s\n"),
260 #ifdef    DEBUG
261          _("yes"),
262 #else // !DEBUG
263          _("no"),
264 #endif // DEBUG
265
266 #ifdef    SHAPE
267          _("yes"),
268 #else // !SHAPE
269          _("no"),
270 #endif // SHAPE
271
272 #ifdef    XINERAMA
273          _("yes")
274 #else // !XINERAMA
275          _("no")
276 #endif // XINERAMA
277     );
278 }
279
280
281 void Openbox::eventLoop()
282 {
283   while (!_doshutdown) {
284     dispatchEvents(); // from OtkEventDispatcher
285     _timermanager.fire();
286   }
287 }
288
289
290 void Openbox::addClient(Window window, OBClient *client)
291 {
292   _clients[window] = client;
293 }
294
295
296 void Openbox::removeClient(Window window)
297 {
298   _clients.erase(window);
299 }
300
301
302 OBClient *Openbox::findClient(Window window)
303 {
304   /*
305     NOTE: we dont use _clients[] to find the value because that will insert
306     a new null into the hash, which really sucks when we want to clean up the
307     hash at shutdown!
308   */
309   ClientMap::iterator it = _clients.find(window);
310   if (it != _clients.end())
311     return it->second;
312   else
313     return (OBClient*) 0;
314 }
315
316 }
317