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