]> icculus.org git repositories - mikachu/openbox.git/blob - src/openbox.cc
check for the python cflags and libs
[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 or the system defaults if that fails
143   if (!python_exec(_scriptfilepath.c_str()))
144     python_exec(SCRIPTDIR"/defaults.py"); // system default bahaviors
145
146   // initialize all the screens
147   OBScreen *screen;
148   int i = _single ? DefaultScreen(otk::OBDisplay::display) : 0;
149   int max = _single ? i + 1 : ScreenCount(otk::OBDisplay::display);
150   for (; i < max; ++i) {
151     screen = new OBScreen(i);
152     if (screen->managed())
153       _screens.push_back(screen);
154     else
155       delete screen;
156   }
157
158   if (_screens.empty()) {
159     printf(_("No screens were found without a window manager. Exiting.\n"));
160     ::exit(1);
161   }
162
163   ScreenList::iterator it, end = _screens.end();
164   for (it = _screens.begin(); it != end; ++it) {
165     (*it)->manageExisting();
166   }
167  
168   // grab any keys set up before the screens existed
169   _bindings->grabKeys(true);
170
171   // set up input focus
172   _focused_screen = _screens[0];
173   setFocusedClient(0);
174   
175   _state = State_Normal; // done starting
176 }
177
178
179 Openbox::~Openbox()
180 {
181   _state = State_Exiting; // time to kill everything
182
183   int first_screen = _screens.front()->number();
184   
185   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
186
187   delete _bindings;
188   delete _actions;
189   delete _property;
190
191   python_destroy();
192
193   XSetInputFocus(otk::OBDisplay::display, PointerRoot, RevertToNone,
194                  CurrentTime);
195   XSync(otk::OBDisplay::display, false);
196
197   // this tends to block.. i honestly am not sure why. causing an x error in
198   // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
199   // all im gunna do is the same.
200   //otk::OBDisplay::destroy();
201
202   if (_restart) {
203     if (!_restart_prog.empty()) {
204       const std::string &dstr =
205         otk::OBDisplay::screenInfo(first_screen)->displayString();
206       putenv(const_cast<char *>(dstr.c_str()));
207       execlp(_restart_prog.c_str(), _restart_prog.c_str(), NULL);
208       perror(_restart_prog.c_str());
209     }
210     
211     // fall back in case the above execlp doesn't work
212     execvp(_argv[0], _argv);
213     execvp(otk::basename(_argv[0]).c_str(), _argv);
214   }
215 }
216
217
218 void Openbox::parseCommandLine(int argc, char **argv)
219 {
220   bool err = false;
221
222   for (int i = 1; i < argc; ++i) {
223     std::string arg(argv[i]);
224
225     if (arg == "-display") {
226       if (++i >= argc)
227         err = true;
228       else
229         _displayreq = argv[i];
230     } else if (arg == "-rc") {
231       if (++i >= argc)
232         err = true;
233       else
234         _rcfilepath = argv[i];
235     } else if (arg == "-menu") {
236       if (++i >= argc)
237         err = true;
238       else
239         _menufilepath = argv[i];
240     } else if (arg == "-script") {
241       if (++i >= argc)
242         err = true;
243       else
244         _scriptfilepath = argv[i];
245     } else if (arg == "-sync") {
246       _sync = true;
247     } else if (arg == "-single") {
248       _single = true;
249     } else if (arg == "-version") {
250       showVersion();
251       ::exit(0);
252     } else if (arg == "-help") {
253       showHelp();
254       ::exit(0);
255     } else
256       err = true;
257
258     if (err) {
259       showHelp();
260       exit(1);
261     }
262   }
263 }
264
265
266 void Openbox::showVersion()
267 {
268   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
269   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
270 }
271
272
273 void Openbox::showHelp()
274 {
275   showVersion(); // show the version string and copyright
276
277   // print program usage and command line options
278   printf(_("Usage: %s [OPTIONS...]\n\
279   Options:\n\
280   -display <string>  use display connection.\n\
281   -single            run on a single screen (default is to run every one).\n\
282   -rc <string>       use alternate resource file.\n\
283   -menu <string>     use alternate menu file.\n\
284   -script <string>   use alternate startup script file.\n\
285   -sync              run in synchronous mode (for debugging).\n\
286   -version           display version and exit.\n\
287   -help              display this help text and exit.\n\n"), _argv[0]);
288
289   printf(_("Compile time options:\n\
290   Debugging: %s\n\
291   Shape:     %s\n\
292   Xinerama:  %s\n\
293   Xkb:       %s\n"),
294 #ifdef    DEBUG
295          _("yes"),
296 #else // !DEBUG
297          _("no"),
298 #endif // DEBUG
299
300 #ifdef    SHAPE
301          _("yes"),
302 #else // !SHAPE
303          _("no"),
304 #endif // SHAPE
305
306 #ifdef    XINERAMA
307          _("yes"),
308 #else // !XINERAMA
309          _("no"),
310 #endif // XINERAMA
311
312 #ifdef    XKB
313          _("yes")
314 #else // !XKB
315          _("no")
316 #endif // XKB
317     );
318 }
319
320
321 void Openbox::eventLoop()
322 {
323   while (!_shutdown) {
324     _timermanager.fire(!_sync); // wait if not in sync mode
325     dispatchEvents(); // from OtkEventDispatcher
326     XFlush(otk::OBDisplay::display); // flush here before we go wait for timers
327   }
328 }
329
330
331 void Openbox::addClient(Window window, OBClient *client)
332 {
333   _clients[window] = client;
334 }
335
336
337 void Openbox::removeClient(Window window)
338 {
339   _clients.erase(window);
340 }
341
342
343 OBClient *Openbox::findClient(Window window)
344 {
345   /*
346     NOTE: we dont use _clients[] to find the value because that will insert
347     a new null into the hash, which really sucks when we want to clean up the
348     hash at shutdown!
349   */
350   ClientMap::iterator it = _clients.find(window);
351   if (it != _clients.end())
352     return it->second;
353   else
354     return (OBClient*) 0;
355 }
356
357
358 void Openbox::setFocusedClient(OBClient *c)
359 {
360   _focused_client = c;
361   if (c) {
362     _focused_screen = _screens[c->screen()];
363   } else {
364     assert(_focused_screen);
365     XSetInputFocus(otk::OBDisplay::display, _focused_screen->focuswindow(),
366                    RevertToNone, CurrentTime);
367   }
368   // set the NET_ACTIVE_WINDOW hint for all screens
369   ScreenList::iterator it, end = _screens.end();
370   for (it = _screens.begin(); it != end; ++it) {
371     int num = (*it)->number();
372     Window root = otk::OBDisplay::screenInfo(num)->rootWindow();
373     _property->set(root, otk::OBProperty::net_active_window,
374                    otk::OBProperty::Atom_Window,
375                    (c && _focused_screen == *it) ? c->window() : None);
376   }
377
378   // call the python Focus callbacks
379   EventData data(_focused_screen->number(), c, EventFocus, 0);
380   Openbox::instance->bindings()->fireEvent(&data);
381 }
382
383 void Openbox::execute(int screen, const std::string &bin)
384 {
385 #ifdef    __EMX__
386   // XXX: whats this for? windows?
387   spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", bin.c_str(), NULL);
388 #else //  __EMX__
389   if (screen >= ScreenCount(otk::OBDisplay::display))
390     screen = 0;
391   const std::string &dstr =
392     otk::OBDisplay::screenInfo(screen)->displayString();
393   
394   if (! fork()) {
395       setsid();
396       int ret = putenv(const_cast<char *>(dstr.c_str()));
397       assert(ret != -1);
398       ret = execl("/bin/sh", "/bin/sh", "-c", bin.c_str(), NULL);
399       exit(ret);
400     }
401 #endif // __EMX__
402 }
403
404 }
405