]> icculus.org git repositories - dana/openbox.git/blob - src/openbox.cc
manage and unmanage windows in OBScreen
[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, _config));
132   
133   _state = State_Normal; // done starting
134 }
135
136
137 Openbox::~Openbox()
138 {
139   _state = State_Exiting; // time to kill everything
140
141   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
142   
143   // close the X display
144   otk::OBDisplay::destroy();
145 }
146
147
148 void Openbox::parseCommandLine(int argc, char **argv)
149 {
150   bool err = false;
151
152   for (int i = 1; i < argc; ++i) {
153     std::string arg(argv[i]);
154
155     if (arg == "-display") {
156       if (++i >= argc)
157         err = true;
158       else
159         _displayreq = argv[i];
160     } else if (arg == "-rc") {
161       if (++i >= argc)
162         err = true;
163       else
164         _rcfilepath = argv[i];
165     } else if (arg == "-menu") {
166       if (++i >= argc)
167         err = true;
168       else
169         _menufilepath = argv[i];
170     } else if (arg == "-version") {
171       showVersion();
172       ::exit(0);
173     } else if (arg == "-help") {
174       showHelp();
175       ::exit(0);
176     } else
177       err = true;
178
179     if (err) {
180       showHelp();
181       exit(1);
182     }
183   }
184 }
185
186
187 void Openbox::showVersion()
188 {
189   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
190   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
191 }
192
193
194 void Openbox::showHelp()
195 {
196   showVersion(); // show the version string and copyright
197
198   // print program usage and command line options
199   printf(_("Usage: %s [OPTIONS...]\n\
200   Options:\n\
201   -display <string>  use display connection.\n\
202   -rc <string>       use alternate resource file.\n\
203   -menu <string>     use alternate menu file.\n\
204   -version           display version and exit.\n\
205   -help              display this help text and exit.\n\n"), _argv0);
206
207   printf(_("Compile time options:\n\
208   Debugging: %s\n\
209   Shape:     %s\n\
210   Xinerama:  %s\n"),
211 #ifdef    DEBUG
212          _("yes"),
213 #else // !DEBUG
214          _("no"),
215 #endif // DEBUG
216
217 #ifdef    SHAPE
218          _("yes"),
219 #else // !SHAPE
220          _("no"),
221 #endif // SHAPE
222
223 #ifdef    XINERAMA
224          _("yes")
225 #else // !XINERAMA
226          _("no")
227 #endif // XINERAMA
228     );
229 }
230
231
232 void Openbox::eventLoop()
233 {
234   while (!_doshutdown) {
235     if (XPending(otk::OBDisplay::display)) {
236       XEvent e;
237       XNextEvent(otk::OBDisplay::display, &e);
238       //process_event(&e);
239       _xeventhandler.handle(e);
240     } else {
241       _timermanager.fire();
242     }
243   }
244 }
245
246
247 void Openbox::addClient(Window window, OBClient *client)
248 {
249   _clients[window] = client;
250 }
251
252
253 void Openbox::removeClient(Window window)
254 {
255   ClientMap::iterator it = _clients.find(window);
256   if (it != _clients.end())
257     _clients.erase(it);
258 }
259
260
261 OBClient *Openbox::findClient(Window window)
262 {
263   /*
264     NOTE: we dont use _clients[] to find the value because that will insert
265     a new null into the hash, which really sucks when we want to clean up the
266     hash at shutdown!
267   */
268   ClientMap::iterator it = _clients.find(window);
269   if (it != _clients.end())
270     return it->second;
271   else
272     return (OBClient*) 0;
273 }
274
275 }
276