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