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