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