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