]> icculus.org git repositories - mikachu/openbox.git/blob - src/openbox.cc
update to cleaned up otk api
[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 "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 "gettext.h"
46 #define _(str) gettext(str)
47 }
48
49 #include <algorithm>
50
51 namespace ob {
52
53 Openbox *Openbox::instance  = (Openbox *) 0;
54
55
56 void Openbox::signalHandler(int signal)
57 {
58   switch (signal) {
59   case SIGHUP:
60     // XXX: Do something with HUP? Really shouldn't, we get this when X shuts
61     //      down and hangs-up on us.
62     
63   case SIGINT:
64   case SIGTERM:
65   case SIGPIPE:
66     printf("Caught signal %d. Exiting.\n", signal);
67     instance->shutdown();
68
69     break;
70   case SIGFPE:
71   case SIGSEGV:
72     printf("Caught signal %d. Aborting and dumping core.\n", signal);
73     abort();
74   }
75 }
76
77
78 Openbox::Openbox(int argc, char **argv)
79   : otk::OtkEventDispatcher(),
80     otk::OtkEventHandler()
81 {
82   struct sigaction action;
83
84   _state = State_Starting; // initializing everything
85
86   Openbox::instance = this;
87
88   _displayreq = (char*) 0;
89   _argv0 = argv[0];
90   _doshutdown = false;
91   _rcfilepath = otk::expandTilde("~/.openbox/rc3");
92
93   _clients = (PyDictObject*) PyDict_New();
94   assert(_clients);
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   // maintain the python list here too
279   PyDict_SetItem((PyObject*)_clients, PyLong_FromLong(window),
280                  (PyObject*)client);
281 }
282
283
284 void Openbox::removeClient(Window window)
285 {
286   PyDict_DelItem((PyObject*)_clients, PyLong_FromLong(window));
287 }
288
289
290 OBClient *Openbox::findClient(Window window)
291 {
292   PyClientObject *client = PyDist_GetItem((PyObject*)_clients,
293                                           PyLong_FromLong(window));
294   if (client)
295     return client->client;
296   return 0;
297 }
298
299 }
300