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