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