]> Dogcows Code - chaz/openbox/blob - src/openbox.cc
7cee8a9aa578f261fef566f261f8603336a26686
[chaz/openbox] / 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 "../version.h"
8 #include "openbox.hh"
9 #include "client.hh"
10 #include "screen.hh"
11 #include "actions.hh"
12 #include "bindings.hh"
13 #include "python.hh"
14 #include "otk/property.hh"
15 #include "otk/display.hh"
16 #include "otk/assassin.hh"
17 #include "otk/util.hh" // TEMPORARY
18
19 extern "C" {
20 #include <X11/cursorfont.h>
21
22 #ifdef HAVE_STDIO_H
23 # include <stdio.h>
24 #endif // HAVE_STDIO_H
25
26 #ifdef HAVE_STDLIB_H
27 # include <stdlib.h>
28 #endif // HAVE_STDLIB_H
29
30 #ifdef HAVE_SIGNAL_H
31 # include <signal.h>
32 #endif // HAVE_SIGNAL_H
33
34 #ifdef HAVE_FCNTL_H
35 # include <fcntl.h>
36 #endif // HAVE_FCNTL_H
37
38 #ifdef HAVE_UNISTD_H
39 # include <sys/types.h>
40 # include <unistd.h>
41 #endif // HAVE_UNISTD_H
42
43 #ifdef HAVE_SYS_SELECT_H
44 # include <sys/select.h>
45 #endif // HAVE_SYS_SELECT_H
46
47 #include "gettext.h"
48 #define _(str) gettext(str)
49 }
50
51 #include <algorithm>
52
53 namespace ob {
54
55 Openbox *Openbox::instance = (Openbox *) 0;
56
57
58 void Openbox::signalHandler(int signal)
59 {
60 switch (signal) {
61 case SIGHUP:
62 printf("Caught HUP signal. Restarting.\n");
63 instance->restart();
64 break;
65
66 case SIGINT:
67 case SIGTERM:
68 case SIGPIPE:
69 printf("Caught signal %d. Exiting.\n", signal);
70 instance->shutdown();
71 break;
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::OtkEventDispatcher(),
82 otk::OtkEventHandler()
83 {
84 struct sigaction action;
85
86 _state = State_Starting; // initializing everything
87
88 Openbox::instance = this;
89
90 _displayreq = (char*) 0;
91 _argv = argv;
92 _shutdown = false;
93 _restart = false;
94 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
95 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
96 _focused_client = 0;
97 _sync = false;
98
99 parseCommandLine(argc, argv);
100
101 // open the X display (and gets some info about it, and its screens)
102 otk::OBDisplay::initialize(_displayreq);
103 assert(otk::OBDisplay::display);
104
105 XSynchronize(otk::OBDisplay::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(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
118 _property = new otk::OBProperty();
119 _actions = new OBActions();
120 _bindings = new OBBindings();
121
122 setMasterHandler(_actions); // set as the master event handler
123
124 // create the mouse cursors we'll use
125 _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
126 _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
127 _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
128 _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
129 _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
130 _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::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 // run the user's script
140 python_exec(_scriptfilepath.c_str());
141
142 // initialize all the screens
143 OBScreen *screen;
144 screen = new OBScreen(0);
145 if (screen->managed()) {
146 _screens.push_back(screen);
147 // XXX: "change to" the first workspace on the screen to initialize stuff
148 } else
149 delete screen;
150
151 if (_screens.empty()) {
152 printf(_("No screens were found without a window manager. Exiting.\n"));
153 ::exit(1);
154 }
155
156 ScreenList::iterator it, end = _screens.end();
157 for (it = _screens.begin(); it != end; ++it) {
158 (*it)->manageExisting();
159 }
160
161 // grab any keys set up before the screens existed
162 _bindings->grabKeys(true);
163
164 // set up input focus
165 _focused_screen = _screens[0];
166 setFocusedClient(0);
167
168 _state = State_Normal; // done starting
169 }
170
171
172 Openbox::~Openbox()
173 {
174 _state = State_Exiting; // time to kill everything
175
176 int first_screen = _screens.front()->number();
177
178 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
179
180 delete _bindings;
181 delete _actions;
182 delete _property;
183
184 python_destroy();
185
186 XSetInputFocus(otk::OBDisplay::display, PointerRoot, RevertToNone,
187 CurrentTime);
188 XSync(otk::OBDisplay::display, false);
189
190 // this tends to block.. i honestly am not sure why. causing an x error in
191 // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
192 // all im gunna do is the same.
193 //otk::OBDisplay::destroy();
194
195 if (_restart) {
196 if (!_restart_prog.empty()) {
197 const std::string &dstr =
198 otk::OBDisplay::screenInfo(first_screen)->displayString();
199 putenv(const_cast<char *>(dstr.c_str()));
200 execlp(_restart_prog.c_str(), _restart_prog.c_str(), NULL);
201 perror(_restart_prog.c_str());
202 }
203
204 // fall back in case the above execlp doesn't work
205 execvp(_argv[0], _argv);
206 execvp(otk::basename(_argv[0]).c_str(), _argv);
207 }
208 }
209
210
211 void Openbox::parseCommandLine(int argc, char **argv)
212 {
213 bool err = false;
214
215 for (int i = 1; i < argc; ++i) {
216 std::string arg(argv[i]);
217
218 if (arg == "-display") {
219 if (++i >= argc)
220 err = true;
221 else
222 _displayreq = argv[i];
223 } else if (arg == "-rc") {
224 if (++i >= argc)
225 err = true;
226 else
227 _rcfilepath = argv[i];
228 } else if (arg == "-menu") {
229 if (++i >= argc)
230 err = true;
231 else
232 _menufilepath = argv[i];
233 } else if (arg == "-script") {
234 if (++i >= argc)
235 err = true;
236 else
237 _scriptfilepath = argv[i];
238 } else if (arg == "-sync") {
239 _sync = true;
240 } else if (arg == "-version") {
241 showVersion();
242 ::exit(0);
243 } else if (arg == "-help") {
244 showHelp();
245 ::exit(0);
246 } else
247 err = true;
248
249 if (err) {
250 showHelp();
251 exit(1);
252 }
253 }
254 }
255
256
257 void Openbox::showVersion()
258 {
259 printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
260 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
261 }
262
263
264 void Openbox::showHelp()
265 {
266 showVersion(); // show the version string and copyright
267
268 // print program usage and command line options
269 printf(_("Usage: %s [OPTIONS...]\n\
270 Options:\n\
271 -display <string> use display connection.\n\
272 -rc <string> use alternate resource file.\n\
273 -menu <string> use alternate menu file.\n\
274 -script <string> use alternate startup script file.\n\
275 -version display version and exit.\n\
276 -help display this help text and exit.\n\n"), _argv[0]);
277
278 printf(_("Compile time options:\n\
279 Debugging: %s\n\
280 Shape: %s\n\
281 Xinerama: %s\n"),
282 #ifdef DEBUG
283 _("yes"),
284 #else // !DEBUG
285 _("no"),
286 #endif // DEBUG
287
288 #ifdef SHAPE
289 _("yes"),
290 #else // !SHAPE
291 _("no"),
292 #endif // SHAPE
293
294 #ifdef XINERAMA
295 _("yes")
296 #else // !XINERAMA
297 _("no")
298 #endif // XINERAMA
299 );
300 }
301
302
303 void Openbox::eventLoop()
304 {
305 while (!_shutdown) {
306 _timermanager.fire();
307 dispatchEvents(); // from OtkEventDispatcher
308 XFlush(otk::OBDisplay::display); // flush here before we go wait for timers
309 }
310 }
311
312
313 void Openbox::addClient(Window window, OBClient *client)
314 {
315 _clients[window] = client;
316 }
317
318
319 void Openbox::removeClient(Window window)
320 {
321 _clients.erase(window);
322 }
323
324
325 OBClient *Openbox::findClient(Window window)
326 {
327 /*
328 NOTE: we dont use _clients[] to find the value because that will insert
329 a new null into the hash, which really sucks when we want to clean up the
330 hash at shutdown!
331 */
332 ClientMap::iterator it = _clients.find(window);
333 if (it != _clients.end())
334 return it->second;
335 else
336 return (OBClient*) 0;
337 }
338
339
340 void Openbox::setFocusedClient(OBClient *c)
341 {
342 _focused_client = c;
343 if (c) {
344 _focused_screen = _screens[c->screen()];
345 } else {
346 assert(_focused_screen);
347 XSetInputFocus(otk::OBDisplay::display, _focused_screen->focuswindow(),
348 RevertToNone, CurrentTime);
349 }
350 // set the NET_ACTIVE_WINDOW hint for all screens
351 ScreenList::iterator it, end = _screens.end();
352 for (it = _screens.begin(); it != end; ++it) {
353 int num = (*it)->number();
354 Window root = otk::OBDisplay::screenInfo(num)->rootWindow();
355 _property->set(root, otk::OBProperty::net_active_window,
356 otk::OBProperty::Atom_Window,
357 (c && _focused_screen == *it) ? c->window() : None);
358 }
359 }
360
361 void Openbox::execute(int screen, const std::string &bin)
362 {
363 #ifdef __EMX__
364 // XXX: whats this for? windows?
365 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", bin.c_str(), NULL);
366 #else // __EMX__
367 if (screen >= ScreenCount(otk::OBDisplay::display))
368 screen = 0;
369 const std::string &dstr =
370 otk::OBDisplay::screenInfo(screen)->displayString();
371
372 if (! fork()) {
373 setsid();
374 int ret = putenv(const_cast<char *>(dstr.c_str()));
375 assert(ret != -1);
376 ret = execl("/bin/sh", "/bin/sh", "-c", bin.c_str(), NULL);
377 exit(ret);
378 }
379 #endif // __EMX__
380 }
381
382 }
383
This page took 0.049522 seconds and 4 git commands to generate.