]> Dogcows Code - chaz/openbox/blob - src/openbox.cc
order things to work correctly for now, with the python loading and the the config...
[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 all the screens
135 _focused_screen = 0;
136
137 for (int i = 0, max = ScreenCount(**otk::display); i < max; ++i) {
138 Screen *screen;
139 // in single mode skip the screens we don't want to manage
140 if (_single && i != DefaultScreen(**otk::display)) {
141 _screens.push_back(0);
142 continue;
143 }
144 // try manage the screen
145 screen = new Screen(i);
146 if (screen->managed()) {
147 _screens.push_back(screen);
148 if (!_focused_screen) // set this to the first screen managed
149 _focused_screen = screen;
150 } else {
151 delete screen;
152 _screens.push_back(0);
153 }
154 }
155
156 assert(_focused_screen);
157
158 if (_screens.empty()) {
159 printf(_("No screens were found without a window manager. Exiting.\n"));
160 ::exit(1);
161 }
162
163 ScreenList::iterator it, end = _screens.end();
164
165 // run the user's script or the system defaults if that fails
166 bool pyerr, doretry;
167 do {
168 // initialize scripting
169 python_init(argv[0]);
170
171 // load all of the screens' configs here so we have a theme set if
172 // we decide to show the dialog below
173 for (it = _screens.begin(); it != end; ++it)
174 (*it)->config().load(); // load the defaults from config.py
175
176 pyerr = doretry = false;
177
178 // reset all the python stuff
179 _bindings->removeAllKeys();
180 _bindings->removeAllButtons();
181 _bindings->removeAllEvents();
182
183 int ret = python_exec(_scriptfilepath.c_str());
184 if (ret == 2)
185 pyerr = true;
186
187 if (ret) {
188 // reset all the python stuff
189 _bindings->removeAllKeys();
190 _bindings->removeAllButtons();
191 _bindings->removeAllEvents();
192
193 if (python_exec(SCRIPTDIR"/defaults.py")) // system default bahaviors
194 pyerr = true;
195 }
196
197 if (pyerr) {
198 std::string msg;
199 msg += _("An error occured while executing the python scripts.");
200 msg += "\n\n";
201 msg += _("See the exact error message in Openbox's output for details.");
202 otk::MessageDialog dia(this, _("Python Error"), msg);
203 otk::DialogButton ok(_("Okay"), true);
204 otk::DialogButton retry(_("Retry"));
205 dia.addButton(ok);
206 dia.addButton(retry);
207 dia.show();
208 dia.focus();
209 const otk::DialogButton &res = dia.run();
210 if (res == retry) {
211 doretry = true;
212 python_destroy(); // kill all the python modules so they reinit right
213 }
214 }
215 } while (pyerr && doretry);
216
217 for (it = _screens.begin(); it != end; ++it) {
218 (*it)->config().load(); // load the config as the scripts may change it
219 (*it)->manageExisting();
220 }
221
222 // grab any keys set up before the screens existed
223 //_bindings->grabKeys(true);
224
225 // set up input focus
226 setFocusedClient(0);
227
228 _state = State_Normal; // done starting
229 }
230
231
232 Openbox::~Openbox()
233 {
234 _state = State_Exiting; // time to kill everything
235
236 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
237
238 delete _bindings;
239 delete _actions;
240
241 python_destroy();
242
243 XSetInputFocus(**otk::display, PointerRoot, RevertToNone,
244 CurrentTime);
245 XSync(**otk::display, false);
246
247 otk::destroy();
248 }
249
250
251 void Openbox::parseCommandLine(int argc, char **argv)
252 {
253 bool err = false;
254
255 for (int i = 1; i < argc; ++i) {
256 std::string arg(argv[i]);
257
258 if (arg == "-rc") {
259 if (++i >= argc)
260 err = true;
261 else
262 _rcfilepath = argv[i];
263 } else if (arg == "-menu") {
264 if (++i >= argc)
265 err = true;
266 else
267 _menufilepath = argv[i];
268 } else if (arg == "-script") {
269 if (++i >= argc)
270 err = true;
271 else
272 _scriptfilepath = argv[i];
273 } else if (arg == "-sync") {
274 _sync = true;
275 } else if (arg == "-single") {
276 _single = true;
277 } else if (arg == "-version") {
278 showVersion();
279 ::exit(0);
280 } else if (arg == "-help") {
281 showHelp();
282 ::exit(0);
283 } else
284 err = true;
285
286 if (err) {
287 showHelp();
288 exit(1);
289 }
290 }
291 }
292
293
294 void Openbox::showVersion()
295 {
296 printf(_("Openbox - version %s\n"), VERSION);
297 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
298 }
299
300
301 void Openbox::showHelp()
302 {
303 showVersion(); // show the version string and copyright
304
305 // print program usage and command line options
306 printf(_("Usage: %s [OPTIONS...]\n\
307 Options:\n\
308 -display <string> use display connection.\n\
309 -single run on a single screen (default is to run every one).\n\
310 -rc <string> use alternate resource file.\n\
311 -menu <string> use alternate menu file.\n\
312 -script <string> use alternate startup script file.\n\
313 -sync run in synchronous mode (for debugging X errors).\n\
314 -version display version and exit.\n\
315 -help display this help text and exit.\n\n"), _argv[0]);
316
317 printf(_("Compile time options:\n\
318 Debugging: %s\n\
319 Shape: %s\n\
320 Xinerama: %s\n\
321 Xkb: %s\n"),
322 #ifdef DEBUG
323 _("yes"),
324 #else // !DEBUG
325 _("no"),
326 #endif // DEBUG
327
328 #ifdef SHAPE
329 _("yes"),
330 #else // !SHAPE
331 _("no"),
332 #endif // SHAPE
333
334 #ifdef XINERAMA
335 _("yes"),
336 #else // !XINERAMA
337 _("no"),
338 #endif // XINERAMA
339
340 #ifdef XKB
341 _("yes")
342 #else // !XKB
343 _("no")
344 #endif // XKB
345 );
346 }
347
348
349 void Openbox::eventLoop()
350 {
351 while (true) {
352 dispatchEvents(); // from otk::EventDispatcher
353 XFlush(**otk::display); // flush here before we go wait for timers
354 // don't wait if we're to shutdown
355 if (_shutdown) break;
356 otk::Timer::dispatchTimers(!_sync); // wait if not in sync mode
357 }
358 }
359
360
361 void Openbox::addClient(Window window, Client *client)
362 {
363 _clients[window] = client;
364 }
365
366
367 void Openbox::removeClient(Window window)
368 {
369 _clients.erase(window);
370 }
371
372
373 Client *Openbox::findClient(Window window)
374 {
375 /*
376 NOTE: we dont use _clients[] to find the value because that will insert
377 a new null into the hash, which really sucks when we want to clean up the
378 hash at shutdown!
379 */
380 ClientMap::iterator it = _clients.find(window);
381 if (it != _clients.end())
382 return it->second;
383 else
384 return (Client*) 0;
385 }
386
387
388 void Openbox::setFocusedClient(Client *c)
389 {
390 // sometimes this is called with the already-focused window, this is
391 // important for the python scripts to work (eg, c = 0 twice). don't just
392 // return if _focused_client == c
393
394 assert(_focused_screen);
395
396 // uninstall the old colormap
397 if (_focused_client)
398 _focused_client->installColormap(false);
399 else
400 _focused_screen->installColormap(false);
401
402 _focused_client = c;
403 if (c) {
404 _focused_screen = _screens[c->screen()];
405
406 // install the client's colormap
407 c->installColormap(true);
408 } else {
409 XSetInputFocus(**otk::display, _focused_screen->focuswindow(),
410 RevertToNone, CurrentTime);
411
412 // install the root window colormap
413 _focused_screen->installColormap(true);
414 }
415 // set the NET_ACTIVE_WINDOW hint for all screens
416 ScreenList::iterator it, end = _screens.end();
417 for (it = _screens.begin(); it != end; ++it) {
418 int num = (*it)->number();
419 Window root = otk::display->screenInfo(num)->rootWindow();
420 otk::Property::set(root, otk::Property::atoms.net_active_window,
421 otk::Property::atoms.window,
422 (c && _focused_screen == *it) ? c->window() : None);
423 }
424
425 // call the python Focus callbacks
426 EventData data(_focused_screen->number(), c, EventAction::Focus, 0);
427 _bindings->fireEvent(&data);
428 }
429
430 }
431
This page took 0.053368 seconds and 5 git commands to generate.