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