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