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