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