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