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