]> icculus.org git repositories - dana/openbox.git/blob - src/openbox.cc
dont restart on HUP. badness. restart on USR1.
[dana/openbox.git] / src / openbox.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
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 "bindings.hh"
13 #include "python.hh"
14 #include "otk/property.hh"
15 #include "otk/display.hh"
16 #include "otk/assassin.hh"
17 #include "otk/util.hh" // TEMPORARY
18
19 extern "C" {
20 #include <X11/cursorfont.h>
21
22 #ifdef    HAVE_STDIO_H
23 #  include <stdio.h>
24 #endif // HAVE_STDIO_H
25
26 #ifdef    HAVE_STDLIB_H
27 #  include <stdlib.h>
28 #endif // HAVE_STDLIB_H
29
30 #ifdef    HAVE_SIGNAL_H
31 #  include <signal.h>
32 #endif // HAVE_SIGNAL_H
33
34 #ifdef    HAVE_FCNTL_H
35 #  include <fcntl.h>
36 #endif // HAVE_FCNTL_H
37
38 #ifdef    HAVE_UNISTD_H
39 #  include <sys/types.h>
40 #  include <unistd.h>
41 #endif // HAVE_UNISTD_H
42
43 #ifdef    HAVE_SYS_SELECT_H
44 #  include <sys/select.h>
45 #endif // HAVE_SYS_SELECT_H
46
47 #include "gettext.h"
48 #define _(str) gettext(str)
49 }
50
51 #include <algorithm>
52
53 namespace ob {
54
55 Openbox *Openbox::instance  = (Openbox *) 0;
56
57
58 void Openbox::signalHandler(int signal)
59 {
60   switch (signal) {
61   case SIGUSR1:
62     printf("Caught SIGUSR1 signal. Restarting.\n");
63     instance->restart();
64     break;
65
66   case SUGHUP:
67   case SIGINT:
68   case SIGTERM:
69   case SIGPIPE:
70     printf("Caught signal %d. Exiting.\n", signal);
71     instance->shutdown();
72     break;
73   case SIGFPE:
74   case SIGSEGV:
75     printf("Caught signal %d. Aborting and dumping core.\n", signal);
76     abort();
77   }
78 }
79
80
81 Openbox::Openbox(int argc, char **argv)
82   : otk::OtkEventDispatcher(),
83     otk::OtkEventHandler()
84 {
85   struct sigaction action;
86
87   _state = State_Starting; // initializing everything
88
89   Openbox::instance = this;
90
91   _displayreq = (char*) 0;
92   _argv = argv;
93   _shutdown = false;
94   _restart = false;
95   _rcfilepath = otk::expandTilde("~/.openbox/rc3");
96   _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
97   _focused_client = 0;
98   _sync = false;
99
100   parseCommandLine(argc, argv);
101
102   // open the X display (and gets some info about it, and its screens)
103   otk::OBDisplay::initialize(_displayreq);
104   assert(otk::OBDisplay::display);
105
106   XSynchronize(otk::OBDisplay::display, _sync);
107   
108   // set up the signal handler
109   action.sa_handler = Openbox::signalHandler;
110   action.sa_mask = sigset_t();
111   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
112   sigaction(SIGPIPE, &action, (struct sigaction *) 0);
113   sigaction(SIGSEGV, &action, (struct sigaction *) 0);
114   sigaction(SIGFPE, &action, (struct sigaction *) 0);
115   sigaction(SIGTERM, &action, (struct sigaction *) 0);
116   sigaction(SIGINT, &action, (struct sigaction *) 0);
117   sigaction(SIGHUP, &action, (struct sigaction *) 0);
118
119   _property = new otk::OBProperty();
120   _actions = new OBActions();
121   _bindings = new OBBindings();
122
123   setMasterHandler(_actions); // set as the master event handler
124
125   // create the mouse cursors we'll use
126   _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
127   _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
128   _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
129   _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
130   _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
131   _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
132
133   // initialize scripting
134   python_init(argv[0]);
135   
136   // load config values
137   python_exec(SCRIPTDIR"/config.py"); // load openbox config values
138   // run all of the python scripts
139   python_exec(SCRIPTDIR"/builtins.py"); // builtin callbacks
140   // run the user's script
141   python_exec(_scriptfilepath.c_str());
142
143   // initialize all the screens
144   OBScreen *screen;
145   screen = new OBScreen(0);
146   if (screen->managed()) {
147     _screens.push_back(screen);
148     // XXX: "change to" the first workspace on the screen to initialize stuff
149   } else
150     delete screen;
151
152   if (_screens.empty()) {
153     printf(_("No screens were found without a window manager. Exiting.\n"));
154     ::exit(1);
155   }
156
157   ScreenList::iterator it, end = _screens.end();
158   for (it = _screens.begin(); it != end; ++it) {
159     (*it)->manageExisting();
160   }
161  
162   // grab any keys set up before the screens existed
163   _bindings->grabKeys(true);
164
165   // set up input focus
166   _focused_screen = _screens[0];
167   setFocusedClient(0);
168   
169   _state = State_Normal; // done starting
170 }
171
172
173 Openbox::~Openbox()
174 {
175   _state = State_Exiting; // time to kill everything
176
177   int first_screen = _screens.front()->number();
178   
179   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
180
181   delete _bindings;
182   delete _actions;
183   delete _property;
184
185   python_destroy();
186
187   XSetInputFocus(otk::OBDisplay::display, PointerRoot, RevertToNone,
188                  CurrentTime);
189   XSync(otk::OBDisplay::display, false);
190
191   // this tends to block.. i honestly am not sure why. causing an x error in
192   // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
193   // all im gunna do is the same.
194   //otk::OBDisplay::destroy();
195
196   if (_restart) {
197     if (!_restart_prog.empty()) {
198       const std::string &dstr =
199         otk::OBDisplay::screenInfo(first_screen)->displayString();
200       putenv(const_cast<char *>(dstr.c_str()));
201       execlp(_restart_prog.c_str(), _restart_prog.c_str(), NULL);
202       perror(_restart_prog.c_str());
203     }
204     
205     // fall back in case the above execlp doesn't work
206     execvp(_argv[0], _argv);
207     execvp(otk::basename(_argv[0]).c_str(), _argv);
208   }
209 }
210
211
212 void Openbox::parseCommandLine(int argc, char **argv)
213 {
214   bool err = false;
215
216   for (int i = 1; i < argc; ++i) {
217     std::string arg(argv[i]);
218
219     if (arg == "-display") {
220       if (++i >= argc)
221         err = true;
222       else
223         _displayreq = argv[i];
224     } else if (arg == "-rc") {
225       if (++i >= argc)
226         err = true;
227       else
228         _rcfilepath = argv[i];
229     } else if (arg == "-menu") {
230       if (++i >= argc)
231         err = true;
232       else
233         _menufilepath = argv[i];
234     } else if (arg == "-script") {
235       if (++i >= argc)
236         err = true;
237       else
238         _scriptfilepath = argv[i];
239     } else if (arg == "-sync") {
240       _sync = true;
241     } else if (arg == "-version") {
242       showVersion();
243       ::exit(0);
244     } else if (arg == "-help") {
245       showHelp();
246       ::exit(0);
247     } else
248       err = true;
249
250     if (err) {
251       showHelp();
252       exit(1);
253     }
254   }
255 }
256
257
258 void Openbox::showVersion()
259 {
260   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
261   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
262 }
263
264
265 void Openbox::showHelp()
266 {
267   showVersion(); // show the version string and copyright
268
269   // print program usage and command line options
270   printf(_("Usage: %s [OPTIONS...]\n\
271   Options:\n\
272   -display <string>  use display connection.\n\
273   -rc <string>       use alternate resource file.\n\
274   -menu <string>     use alternate menu file.\n\
275   -script <string>   use alternate startup script file.\n\
276   -version           display version and exit.\n\
277   -help              display this help text and exit.\n\n"), _argv[0]);
278
279   printf(_("Compile time options:\n\
280   Debugging: %s\n\
281   Shape:     %s\n\
282   Xinerama:  %s\n"),
283 #ifdef    DEBUG
284          _("yes"),
285 #else // !DEBUG
286          _("no"),
287 #endif // DEBUG
288
289 #ifdef    SHAPE
290          _("yes"),
291 #else // !SHAPE
292          _("no"),
293 #endif // SHAPE
294
295 #ifdef    XINERAMA
296          _("yes")
297 #else // !XINERAMA
298          _("no")
299 #endif // XINERAMA
300     );
301 }
302
303
304 void Openbox::eventLoop()
305 {
306   while (!_shutdown) {
307     _timermanager.fire();
308     dispatchEvents(); // from OtkEventDispatcher
309     XFlush(otk::OBDisplay::display); // flush here before we go wait for timers
310   }
311 }
312
313
314 void Openbox::addClient(Window window, OBClient *client)
315 {
316   _clients[window] = client;
317 }
318
319
320 void Openbox::removeClient(Window window)
321 {
322   _clients.erase(window);
323 }
324
325
326 OBClient *Openbox::findClient(Window window)
327 {
328   /*
329     NOTE: we dont use _clients[] to find the value because that will insert
330     a new null into the hash, which really sucks when we want to clean up the
331     hash at shutdown!
332   */
333   ClientMap::iterator it = _clients.find(window);
334   if (it != _clients.end())
335     return it->second;
336   else
337     return (OBClient*) 0;
338 }
339
340
341 void Openbox::setFocusedClient(OBClient *c)
342 {
343   _focused_client = c;
344   if (c) {
345     _focused_screen = _screens[c->screen()];
346   } else {
347     assert(_focused_screen);
348     XSetInputFocus(otk::OBDisplay::display, _focused_screen->focuswindow(),
349                    RevertToNone, CurrentTime);
350   }
351   // set the NET_ACTIVE_WINDOW hint for all screens
352   ScreenList::iterator it, end = _screens.end();
353   for (it = _screens.begin(); it != end; ++it) {
354     int num = (*it)->number();
355     Window root = otk::OBDisplay::screenInfo(num)->rootWindow();
356     _property->set(root, otk::OBProperty::net_active_window,
357                    otk::OBProperty::Atom_Window,
358                    (c && _focused_screen == *it) ? c->window() : None);
359   }
360 }
361
362 void Openbox::execute(int screen, const std::string &bin)
363 {
364 #ifdef    __EMX__
365   // XXX: whats this for? windows?
366   spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", bin.c_str(), NULL);
367 #else //  __EMX__
368   if (screen >= ScreenCount(otk::OBDisplay::display))
369     screen = 0;
370   const std::string &dstr =
371     otk::OBDisplay::screenInfo(screen)->displayString();
372   
373   if (! fork()) {
374       setsid();
375       int ret = putenv(const_cast<char *>(dstr.c_str()));
376       assert(ret != -1);
377       ret = execl("/bin/sh", "/bin/sh", "-c", bin.c_str(), NULL);
378       exit(ret);
379     }
380 #endif // __EMX__
381 }
382
383 }
384