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