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