]> icculus.org git repositories - mikachu/openbox.git/blob - src/openbox.cc
dont try import shit for now it dont work
[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   // i wish...
183   //PyRun_String("from _otk import *; from _openbox import *;", Py_file_input,
184   //             Py_None, Py_None);
185   FILE *rcpyfd = fopen("/home/natas/.openbox/user.py", "r");
186   if (!rcpyfd) {
187     printf("failed to load python file /home/natas/.openbox/user.py\n");
188   } else {
189     PyRun_SimpleFile(rcpyfd, "/home/natas/.openbox/user.py");
190     fclose(rcpyfd);
191   }
192   
193
194   _state = State_Normal; // done starting
195 }
196
197
198 Openbox::~Openbox()
199 {
200   _state = State_Exiting; // time to kill everything
201
202   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
203   
204   // close the X display
205   otk::OBDisplay::destroy();
206 }
207
208
209 void Openbox::parseCommandLine(int argc, char **argv)
210 {
211   bool err = false;
212
213   for (int i = 1; i < argc; ++i) {
214     std::string arg(argv[i]);
215
216     if (arg == "-display") {
217       if (++i >= argc)
218         err = true;
219       else
220         _displayreq = argv[i];
221     } else if (arg == "-rc") {
222       if (++i >= argc)
223         err = true;
224       else
225         _rcfilepath = argv[i];
226     } else if (arg == "-menu") {
227       if (++i >= argc)
228         err = true;
229       else
230         _menufilepath = argv[i];
231     } else if (arg == "-version") {
232       showVersion();
233       ::exit(0);
234     } else if (arg == "-help") {
235       showHelp();
236       ::exit(0);
237     } else
238       err = true;
239
240     if (err) {
241       showHelp();
242       exit(1);
243     }
244   }
245 }
246
247
248 void Openbox::showVersion()
249 {
250   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
251   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
252 }
253
254
255 void Openbox::showHelp()
256 {
257   showVersion(); // show the version string and copyright
258
259   // print program usage and command line options
260   printf(_("Usage: %s [OPTIONS...]\n\
261   Options:\n\
262   -display <string>  use display connection.\n\
263   -rc <string>       use alternate resource file.\n\
264   -menu <string>     use alternate menu file.\n\
265   -version           display version and exit.\n\
266   -help              display this help text and exit.\n\n"), _argv0);
267
268   printf(_("Compile time options:\n\
269   Debugging: %s\n\
270   Shape:     %s\n\
271   Xinerama:  %s\n"),
272 #ifdef    DEBUG
273          _("yes"),
274 #else // !DEBUG
275          _("no"),
276 #endif // DEBUG
277
278 #ifdef    SHAPE
279          _("yes"),
280 #else // !SHAPE
281          _("no"),
282 #endif // SHAPE
283
284 #ifdef    XINERAMA
285          _("yes")
286 #else // !XINERAMA
287          _("no")
288 #endif // XINERAMA
289     );
290 }
291
292
293 void Openbox::eventLoop()
294 {
295   while (!_doshutdown) {
296     dispatchEvents(); // from OtkEventDispatcher
297     _timermanager.fire();
298   }
299 }
300
301
302 void Openbox::addClient(Window window, OBClient *client)
303 {
304   _clients[window] = client;
305 }
306
307
308 void Openbox::removeClient(Window window)
309 {
310   _clients.erase(window);
311 }
312
313
314 OBClient *Openbox::findClient(Window window)
315 {
316   /*
317     NOTE: we dont use _clients[] to find the value because that will insert
318     a new null into the hash, which really sucks when we want to clean up the
319     hash at shutdown!
320   */
321   ClientMap::iterator it = _clients.find(window);
322   if (it != _clients.end())
323     return it->second;
324   else
325     return (OBClient*) 0;
326 }
327
328 }
329