]> icculus.org git repositories - mikachu/openbox.git/blob - src/openbox.cc
using python and swig for now.
[mikachu/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 <guile/gh.h>
46
47 #include <Python.h>
48   
49 // The initializer in openbox_wrap.cc
50 extern void init_openbox(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
100   parseCommandLine(argc, argv);
101
102   // TEMPORARY: using the xrdb rc3
103   _config.setFile(_rcfilepath);
104   if (!_config.load()) {
105     printf("failed to load rc file %s\n", _config.file().c_str());
106     ::exit(2);
107   }
108   std::string s;
109   _config.getValue("session.styleFile", s);
110   _config.setFile(s);
111   if (!_config.load()) {
112     printf("failed to load style %s\n", _config.file().c_str());
113     ::exit(2);
114   }
115
116   // open the X display (and gets some info about it, and its screens)
117   otk::OBDisplay::initialize(_displayreq);
118   assert(otk::OBDisplay::display);
119     
120   // set up the signal handler
121   action.sa_handler = Openbox::signalHandler;
122   action.sa_mask = sigset_t();
123   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
124   sigaction(SIGPIPE, &action, (struct sigaction *) 0);
125   sigaction(SIGSEGV, &action, (struct sigaction *) 0);
126   sigaction(SIGFPE, &action, (struct sigaction *) 0);
127   sigaction(SIGTERM, &action, (struct sigaction *) 0);
128   sigaction(SIGINT, &action, (struct sigaction *) 0);
129   sigaction(SIGHUP, &action, (struct sigaction *) 0);
130
131   _property = new otk::OBProperty();
132
133   _actions = new OBActions();
134
135   setMasterHandler(_actions); // set as the master event handler
136
137   // create the mouse cursors we'll use
138   _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
139   _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
140   _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
141   _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
142   _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
143   _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
144
145   // initialize all the screens
146   OBScreen *screen;
147   screen = new OBScreen(0, _config);
148   if (screen->managed()) {
149     _screens.push_back(screen);
150     _screens[0]->manageExisting();
151     // XXX: "change to" the first workspace on the screen to initialize stuff
152   } else
153     delete screen;
154
155   if (_screens.empty()) {
156     printf(_("No screens were found without a window manager. Exiting.\n"));
157     ::exit(1);
158   }
159
160   /*
161   // make our guile interfaces exist
162   SWIG_init();
163   
164   // run the guile of d3th
165   FILE *rcpyfd = fopen("/home/natas/.openbox/user.scm", "r");
166   if (!rcpyfd) {
167     printf("failed to load guile script /home/natas/.openbox/user.scm\n");
168   } else {
169     fclose(rcpyfd);
170     gh_load("/home/natas/.openbox/user.scm");
171   }
172   */
173   
174   Py_SetProgramName(argv[0]);
175   Py_Initialize();
176   //initopenbox(); // initialize the static 'openbox' module
177   init_openbox();
178   FILE *rcpyfd = fopen("/home/natas/.openbox/user.py", "r");
179   if (!rcpyfd) {
180     printf("failed to load python file /home/natas/.openbox/user.py\n");
181   } else {
182     PyRun_SimpleFile(rcpyfd, "/home/natas/.openbox/user.py");
183     fclose(rcpyfd);
184   }
185   
186
187   _state = State_Normal; // done starting
188 }
189
190
191 Openbox::~Openbox()
192 {
193   _state = State_Exiting; // time to kill everything
194
195   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
196   
197   // close the X display
198   otk::OBDisplay::destroy();
199 }
200
201
202 void Openbox::parseCommandLine(int argc, char **argv)
203 {
204   bool err = false;
205
206   for (int i = 1; i < argc; ++i) {
207     std::string arg(argv[i]);
208
209     if (arg == "-display") {
210       if (++i >= argc)
211         err = true;
212       else
213         _displayreq = argv[i];
214     } else if (arg == "-rc") {
215       if (++i >= argc)
216         err = true;
217       else
218         _rcfilepath = argv[i];
219     } else if (arg == "-menu") {
220       if (++i >= argc)
221         err = true;
222       else
223         _menufilepath = argv[i];
224     } else if (arg == "-version") {
225       showVersion();
226       ::exit(0);
227     } else if (arg == "-help") {
228       showHelp();
229       ::exit(0);
230     } else
231       err = true;
232
233     if (err) {
234       showHelp();
235       exit(1);
236     }
237   }
238 }
239
240
241 void Openbox::showVersion()
242 {
243   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
244   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
245 }
246
247
248 void Openbox::showHelp()
249 {
250   showVersion(); // show the version string and copyright
251
252   // print program usage and command line options
253   printf(_("Usage: %s [OPTIONS...]\n\
254   Options:\n\
255   -display <string>  use display connection.\n\
256   -rc <string>       use alternate resource file.\n\
257   -menu <string>     use alternate menu file.\n\
258   -version           display version and exit.\n\
259   -help              display this help text and exit.\n\n"), _argv0);
260
261   printf(_("Compile time options:\n\
262   Debugging: %s\n\
263   Shape:     %s\n\
264   Xinerama:  %s\n"),
265 #ifdef    DEBUG
266          _("yes"),
267 #else // !DEBUG
268          _("no"),
269 #endif // DEBUG
270
271 #ifdef    SHAPE
272          _("yes"),
273 #else // !SHAPE
274          _("no"),
275 #endif // SHAPE
276
277 #ifdef    XINERAMA
278          _("yes")
279 #else // !XINERAMA
280          _("no")
281 #endif // XINERAMA
282     );
283 }
284
285
286 void Openbox::eventLoop()
287 {
288   while (!_doshutdown) {
289     dispatchEvents(); // from OtkEventDispatcher
290     _timermanager.fire();
291   }
292 }
293
294
295 void Openbox::addClient(Window window, OBClient *client)
296 {
297   _clients[window] = client;
298 }
299
300
301 void Openbox::removeClient(Window window)
302 {
303   _clients.erase(window);
304 }
305
306
307 OBClient *Openbox::findClient(Window window)
308 {
309   /*
310     NOTE: we dont use _clients[] to find the value because that will insert
311     a new null into the hash, which really sucks when we want to clean up the
312     hash at shutdown!
313   */
314   ClientMap::iterator it = _clients.find(window);
315   if (it != _clients.end())
316     return it->second;
317   else
318     return (OBClient*) 0;
319 }
320
321 }
322