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