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