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