]> icculus.org git repositories - mikachu/openbox.git/blob - src/openbox.cc
use the new non-static display
[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 "openbox.hh"
8 #include "client.hh"
9 #include "screen.hh"
10 #include "actions.hh"
11 #include "bindings.hh"
12 #include "python.hh"
13 #include "otk/property.hh"
14 #include "otk/assassin.hh"
15 #include "otk/util.hh"
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 = (Openbox *) 0;
54
55
56 void Openbox::signalHandler(int signal)
57 {
58   switch (signal) {
59   case SIGUSR1:
60     printf("Caught SIGUSR1 signal. Restarting.\n");
61     openbox->restart();
62     break;
63
64   case SIGHUP:
65   case SIGINT:
66   case SIGTERM:
67   case SIGPIPE:
68     printf("Caught signal %d. Exiting.\n", signal);
69     openbox->shutdown();
70     break;
71
72   case SIGFPE:
73   case SIGSEGV:
74     printf("Caught signal %d. Aborting and dumping core.\n", signal);
75     abort();
76   }
77 }
78
79
80 Openbox::Openbox(int argc, char **argv)
81   : otk::EventDispatcher(),
82     otk::EventHandler(),
83     _display()
84 {
85   struct sigaction action;
86
87   _state = State_Starting; // initializing everything
88
89   openbox = 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   XSynchronize(**otk::display, _sync);
103   
104   // set up the signal handler
105   action.sa_handler = Openbox::signalHandler;
106   action.sa_mask = sigset_t();
107   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
108   sigaction(SIGUSR1, &action, (struct sigaction *) 0);
109   sigaction(SIGPIPE, &action, (struct sigaction *) 0);
110   sigaction(SIGSEGV, &action, (struct sigaction *) 0);
111   sigaction(SIGFPE, &action, (struct sigaction *) 0);
112   sigaction(SIGTERM, &action, (struct sigaction *) 0);
113   sigaction(SIGINT, &action, (struct sigaction *) 0);
114   sigaction(SIGHUP, &action, (struct sigaction *) 0);
115
116   _property = new otk::Property();
117   _actions = new Actions();
118   _bindings = new Bindings();
119
120   setMasterHandler(_actions); // set as the master event handler
121
122   // create the mouse cursors we'll use
123   _cursors.session = XCreateFontCursor(**otk::display, XC_left_ptr);
124   _cursors.move = XCreateFontCursor(**otk::display, XC_fleur);
125   _cursors.ll_angle = XCreateFontCursor(**otk::display, XC_ll_angle);
126   _cursors.lr_angle = XCreateFontCursor(**otk::display, XC_lr_angle);
127   _cursors.ul_angle = XCreateFontCursor(**otk::display, XC_ul_angle);
128   _cursors.ur_angle = XCreateFontCursor(**otk::display, XC_ur_angle);
129
130   // initialize scripting
131   python_init(argv[0]);
132   
133   // load config values
134   python_exec(SCRIPTDIR"/config.py"); // load openbox config values
135   // run all of the python scripts
136   python_exec(SCRIPTDIR"/builtins.py"); // builtin callbacks
137   // run the user's script or the system defaults if that fails
138   if (!python_exec(_scriptfilepath.c_str()))
139     python_exec(SCRIPTDIR"/defaults.py"); // system default bahaviors
140
141   // initialize all the screens
142   Screen *screen;
143   int i = _single ? DefaultScreen(**otk::display) : 0;
144   int max = _single ? i + 1 : ScreenCount(**otk::display);
145   for (; i < max; ++i) {
146     screen = new Screen(i);
147     if (screen->managed())
148       _screens.push_back(screen);
149     else
150       delete screen;
151   }
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::display, PointerRoot, RevertToNone,
189                  CurrentTime);
190   XSync(**otk::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::display->destroy();
196
197   if (_restart) {
198     if (!_restart_prog.empty()) {
199       const std::string &dstr =
200         otk::display->screenInfo(first_screen)->displayString();
201       otk::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 == "-single") {
243       _single = true;
244     } else if (arg == "-version") {
245       showVersion();
246       ::exit(0);
247     } else if (arg == "-help") {
248       showHelp();
249       ::exit(0);
250     } else
251       err = true;
252
253     if (err) {
254       showHelp();
255       exit(1);
256     }
257   }
258 }
259
260
261 void Openbox::showVersion()
262 {
263   printf(_("Openbox - version %s\n"), VERSION);
264   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
265 }
266
267
268 void Openbox::showHelp()
269 {
270   showVersion(); // show the version string and copyright
271
272   // print program usage and command line options
273   printf(_("Usage: %s [OPTIONS...]\n\
274   Options:\n\
275   -display <string>  use display connection.\n\
276   -single            run on a single screen (default is to run every one).\n\
277   -rc <string>       use alternate resource file.\n\
278   -menu <string>     use alternate menu file.\n\
279   -script <string>   use alternate startup script file.\n\
280   -sync              run in synchronous mode (for debugging).\n\
281   -version           display version and exit.\n\
282   -help              display this help text and exit.\n\n"), _argv[0]);
283
284   printf(_("Compile time options:\n\
285   Debugging: %s\n\
286   Shape:     %s\n\
287   Xinerama:  %s\n\
288   Xkb:       %s\n"),
289 #ifdef    DEBUG
290          _("yes"),
291 #else // !DEBUG
292          _("no"),
293 #endif // DEBUG
294
295 #ifdef    SHAPE
296          _("yes"),
297 #else // !SHAPE
298          _("no"),
299 #endif // SHAPE
300
301 #ifdef    XINERAMA
302          _("yes"),
303 #else // !XINERAMA
304          _("no"),
305 #endif // XINERAMA
306
307 #ifdef    XKB
308          _("yes")
309 #else // !XKB
310          _("no")
311 #endif // XKB
312     );
313 }
314
315
316 void Openbox::eventLoop()
317 {
318   while (true) {
319     dispatchEvents(); // from otk::EventDispatcher
320     XFlush(**otk::display); // flush here before we go wait for timers
321     // don't wait if we're to shutdown
322     if (_shutdown) break;
323     _timermanager.fire(!_sync); // wait if not in sync mode
324   }
325 }
326
327
328 void Openbox::addClient(Window window, Client *client)
329 {
330   _clients[window] = client;
331 }
332
333
334 void Openbox::removeClient(Window window)
335 {
336   _clients.erase(window);
337 }
338
339
340 Client *Openbox::findClient(Window window)
341 {
342   /*
343     NOTE: we dont use _clients[] to find the value because that will insert
344     a new null into the hash, which really sucks when we want to clean up the
345     hash at shutdown!
346   */
347   ClientMap::iterator it = _clients.find(window);
348   if (it != _clients.end())
349     return it->second;
350   else
351     return (Client*) 0;
352 }
353
354
355 void Openbox::setFocusedClient(Client *c)
356 {
357   _focused_client = c;
358   if (c) {
359     _focused_screen = _screens[c->screen()];
360   } else {
361     assert(_focused_screen);
362     XSetInputFocus(**otk::display, _focused_screen->focuswindow(),
363                    RevertToNone, CurrentTime);
364   }
365   // set the NET_ACTIVE_WINDOW hint for all screens
366   ScreenList::iterator it, end = _screens.end();
367   for (it = _screens.begin(); it != end; ++it) {
368     int num = (*it)->number();
369     Window root = otk::display->screenInfo(num)->rootWindow();
370     _property->set(root, otk::Property::net_active_window,
371                    otk::Property::Atom_Window,
372                    (c && _focused_screen == *it) ? c->window() : None);
373   }
374
375   // call the python Focus callbacks
376   EventData data(_focused_screen->number(), c, EventFocus, 0);
377   _bindings->fireEvent(&data);
378 }
379
380 void Openbox::execute(int screen, const std::string &bin)
381 {
382   if (screen >= ScreenCount(**otk::display))
383     screen = 0;
384   otk::bexec(bin, otk::display->screenInfo(screen)->displayString());
385 }
386
387 }
388