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