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