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