]> icculus.org git repositories - mikachu/openbox.git/blob - src/openbox.cc
documenting classes!
[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 "otk/display.hh"
10
11 extern "C" {
12 #ifdef    HAVE_STDIO_H
13 #  include <stdio.h>
14 #endif // HAVE_STDIO_H
15
16 #ifdef    HAVE_STDLIB_H
17 #  include <stdlib.h>
18 #endif // HAVE_STDLIB_H
19
20 #ifdef    HAVE_SIGNAL_H
21 #  include <signal.h>
22 #endif // HAVE_SIGNAL_H
23
24 #ifdef    HAVE_FCNTL_H
25 #  include <fcntl.h>
26 #endif // HAVE_FCNTL_H
27
28 #ifdef    HAVE_UNISTD_H
29 #  include <sys/types.h>
30 #  include <unistd.h>
31 #endif // HAVE_UNISTD_H
32
33 #ifdef    HAVE_SYS_SELECT_H
34 #  include <sys/select.h>
35 #endif // HAVE_SYS_SELECT_H
36
37 #include "gettext.h"
38 #define _(str) gettext(str)
39 }
40
41 namespace ob {
42
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.", signal);
58     instance->shutdown();
59
60     break;
61   case SIGFPE:
62   case SIGSEGV:
63     printf("Caught signal %d. Aborting and dumping core.", 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
80   parseCommandLine(argc, argv);
81
82   // open the X display (and gets some info about it, and its screens)
83   otk::OBDisplay::initialize(_displayreq);
84   assert(otk::OBDisplay::display);
85     
86   // set up the signal handler
87   action.sa_handler = Openbox::signalHandler;
88   action.sa_mask = sigset_t();
89   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
90   sigaction(SIGPIPE, &action, (struct sigaction *) 0);
91   sigaction(SIGSEGV, &action, (struct sigaction *) 0);
92   sigaction(SIGFPE, &action, (struct sigaction *) 0);
93   sigaction(SIGTERM, &action, (struct sigaction *) 0);
94   sigaction(SIGINT, &action, (struct sigaction *) 0);
95   sigaction(SIGHUP, &action, (struct sigaction *) 0);
96
97
98   
99   _state = State_Normal; // done starting
100 }
101
102
103 Openbox::~Openbox()
104 {
105   _state = State_Exiting; // time to kill everything
106   
107   // close the X display
108   otk::OBDisplay::destroy();
109 }
110
111
112 void Openbox::parseCommandLine(int argc, char **argv)
113 {
114   bool err = false;
115
116   for (int i = 1; i < argc; ++i) {
117     std::string arg(argv[i]);
118
119     if (arg == "-display") {
120       if (++i >= argc)
121         err = true;
122       else
123         _displayreq = argv[i];
124     } else if (arg == "-rc") {
125       if (++i >= argc)
126         err = true;
127       else
128         _rcfilepath = argv[i];
129     } else if (arg == "-menu") {
130       if (++i >= argc)
131         err = true;
132       else
133         _menufilepath = argv[i];
134     } else if (arg == "-version") {
135       showVersion();
136       ::exit(0);
137     } else if (arg == "-help") {
138       showHelp();
139       ::exit(0);
140     } else
141       err = true;
142
143     if (err) {
144       showHelp();
145       exit(1);
146     }
147   }
148 }
149
150
151 void Openbox::showVersion()
152 {
153   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
154   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
155 }
156
157
158 void Openbox::showHelp()
159 {
160   showVersion(); // show the version string and copyright
161
162   // print program usage and command line options
163   printf(_("Usage: %s [OPTIONS...]\n\
164   Options:\n\
165   -display <string>  use display connection.\n\
166   -rc <string>       use alternate resource file.\n\
167   -menu <string>     use alternate menu file.\n\
168   -version           display version and exit.\n\
169   -help              display this help text and exit.\n\n"), _argv0);
170
171   printf(_("Compile time options:\n\
172   Debugging: %s\n\
173   Shape:     %s\n\
174   Xinerama:  %s\n"),
175 #ifdef    DEBUG
176          _("yes"),
177 #else // !DEBUG
178          _("no"),
179 #endif // DEBUG
180
181 #ifdef    SHAPE
182          _("yes"),
183 #else // !SHAPE
184          _("no"),
185 #endif // SHAPE
186
187 #ifdef    XINERAMA
188          _("yes")
189 #else // !XINERAMA
190          _("no")
191 #endif // XINERAMA
192     );
193 }
194
195
196 void Openbox::eventLoop()
197 {
198   while (!_doshutdown) {
199     if (XPending(otk::OBDisplay::display)) {
200       XEvent e;
201       XNextEvent(otk::OBDisplay::display, &e);
202       process_event(&e);
203     } else {
204       _timermanager.fire();
205     }
206   }
207 }
208
209
210 }
211