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