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