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