]> Dogcows Code - chaz/openbox/blob - src/openbox.cc
add cvsignores
[chaz/openbox] / src / openbox.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "openbox.hh"
6 #include "client.hh"
7 #include "screen.hh"
8 #include "actions.hh"
9 #include "bindings.hh"
10 #include "python.hh"
11 #include "otk/property.hh"
12 #include "otk/assassin.hh"
13 #include "otk/property.hh"
14 #include "otk/util.hh"
15 #include "otk/rendercolor.hh"
16 #include "otk/renderstyle.hh"
17 #include "otk/messagedialog.hh"
18
19 extern "C" {
20 #include <X11/cursorfont.h>
21
22 #ifdef HAVE_SIGNAL_H
23 # include <signal.h>
24 #endif // HAVE_SIGNAL_H
25
26 #ifdef HAVE_FCNTL_H
27 # include <fcntl.h>
28 #endif // HAVE_FCNTL_H
29
30 #ifdef HAVE_SYS_WAIT_H
31 # include <sys/wait.h>
32 #endif // HAVE_SYS_WAIT_H
33
34 #include "gettext.h"
35 #define _(str) gettext(str)
36 }
37
38 #include <algorithm>
39 #include <cstdio>
40 #include <cstdlib>
41
42 namespace otk {
43 extern void initialize();
44 extern void destroy();
45 }
46
47 namespace ob {
48
49 Openbox *openbox = (Openbox *) 0;
50
51
52 void Openbox::signalHandler(int signal)
53 {
54 switch (signal) {
55 case SIGUSR1:
56 printf("Caught SIGUSR1 signal. Restarting.\n");
57 openbox->restart();
58 break;
59
60 case SIGCHLD:
61 wait(NULL);
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 {
84 struct sigaction action;
85
86 _state = State_Starting; // initializing everything
87
88 openbox = this;
89
90 _argv = argv;
91 _shutdown = false;
92 _restart = false;
93 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
94 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
95 _focused_client = 0;
96 _sync = false;
97 _single = false;
98 _remote = false;
99
100 parseCommandLine(argc, argv);
101
102 otk::initialize();
103
104 XSynchronize(**otk::display, _sync);
105
106 // set up the signal handler
107 action.sa_handler = Openbox::signalHandler;
108 action.sa_mask = sigset_t();
109 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
110 sigaction(SIGUSR1, &action, (struct sigaction *) 0);
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 sigaction(SIGCHLD, &action, (struct sigaction *) 0);
118
119 // anything that died while we were restarting won't give us a SIGCHLD
120 while (waitpid(-1, NULL, WNOHANG) > 0);
121
122 _actions = new Actions();
123 _bindings = new Bindings();
124
125 setMasterHandler(_actions); // set as the master event handler
126
127 // create the mouse cursors we'll use
128 _cursors.session = XCreateFontCursor(**otk::display, XC_left_ptr);
129 _cursors.move = XCreateFontCursor(**otk::display, XC_fleur);
130 _cursors.ll_angle = XCreateFontCursor(**otk::display, XC_ll_angle);
131 _cursors.lr_angle = XCreateFontCursor(**otk::display, XC_lr_angle);
132 _cursors.ul_angle = XCreateFontCursor(**otk::display, XC_ul_angle);
133 _cursors.ur_angle = XCreateFontCursor(**otk::display, XC_ur_angle);
134
135 // initialize all the screens
136 _focused_screen = 0;
137 _managed_count = 0;
138
139 for (int i = 0, max = ScreenCount(**otk::display); i < max; ++i) {
140 Screen *screen;
141 // in single mode skip the screens we don't want to manage
142 if (_single && i != DefaultScreen(**otk::display)) {
143 _screens.push_back(0);
144 continue;
145 }
146 // try manage the screen
147 screen = new Screen(i);
148 if (screen->managed()) {
149 _screens.push_back(screen);
150 if (!_focused_screen) // set this to the first screen managed
151 _focused_screen = screen;
152 _managed_count++;
153 } else {
154 delete screen;
155 _screens.push_back(0);
156 }
157 }
158
159 if (!_managed_count) {
160 printf(_("No screens were found without a window manager. Exiting.\n"));
161 ::exit(1);
162 }
163
164 assert(_focused_screen);
165
166 // initialize scripting
167 python_init(argv[0]);
168
169 // load the theme XXX TEMP SHIT
170 otk::RenderStyle::setStyle(0, "");
171
172 int ret = python_exec(_scriptfilepath.c_str());
173 if (ret == 2) {
174 std::string msg;
175 msg += _("An error occured while executing the python scripts.");
176 msg += "\n\n";
177 msg += _("See the exact error message in Openbox's output for details.");
178 otk::MessageDialog dia(this, _("Python Error"), msg);
179 otk::DialogButton ok(_("Okay"), true);
180 otk::DialogButton retry(_("Restart"));
181 dia.addButton(ok);
182 dia.addButton(retry);
183 dia.show();
184 dia.focus();
185 const otk::DialogButton &res = dia.run();
186 if (res == retry) {
187 _restart = _shutdown = true;
188 return;
189 }
190 }
191
192 if (ret)
193 python_exec(SCRIPTDIR"/defaults.py"); // system default bahaviors
194
195 ScreenList::iterator it, end = _screens.end();
196 for (it = _screens.begin(); it != end; ++it)
197 if (*it) (*it)->manageExisting();
198
199 // grab any keys set up before the screens existed
200 //_bindings->grabKeys(true);
201
202 // set up input focus
203 setFocusedClient(0);
204
205 _state = State_Normal; // done starting
206 }
207
208
209 Openbox::~Openbox()
210 {
211 _state = State_Exiting; // time to kill everything
212
213 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
214
215 delete _bindings;
216 delete _actions;
217
218 python_destroy();
219
220 XSetInputFocus(**otk::display, PointerRoot, RevertToNone,
221 CurrentTime);
222 XSync(**otk::display, false);
223
224 otk::destroy();
225 }
226
227
228 void Openbox::parseCommandLine(int argc, char **argv)
229 {
230 bool err = false;
231
232 for (int i = 1; i < argc; ++i) {
233 std::string arg(argv[i]);
234
235 if (arg == "-rc") {
236 if (++i >= argc)
237 err = true;
238 else
239 _rcfilepath = argv[i];
240 } else if (arg == "-menu") {
241 if (++i >= argc)
242 err = true;
243 else
244 _menufilepath = argv[i];
245 } else if (arg == "-script") {
246 if (++i >= argc)
247 err = true;
248 else
249 _scriptfilepath = argv[i];
250 } else if (arg == "-sync") {
251 _sync = true;
252 } else if (arg == "-single") {
253 _single = true;
254 } else if (arg == "-remote") {
255 _remote = true;
256 } else if (arg == "-version") {
257 showVersion();
258 ::exit(0);
259 } else if (arg == "-help") {
260 showHelp();
261 ::exit(0);
262 } else
263 err = true;
264
265 if (err) {
266 showHelp();
267 exit(1);
268 }
269 }
270 }
271
272
273 void Openbox::showVersion()
274 {
275 printf(_("Openbox - version %s\n"), VERSION);
276 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
277 }
278
279
280 void Openbox::showHelp()
281 {
282 showVersion(); // show the version string and copyright
283
284 // print program usage and command line options
285 printf(_("Usage: %s [OPTIONS...]\n\
286 Options:\n\
287 -remote optimize for a remote (low bandwidth) connection to the\n\
288 display/Xserver.\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 X errors).\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(false); // from otk::EventDispatcher
333 // XFlush(**otk::display); // flush here before we go wait for timers
334 // .. the XPending() should have done this last
335 // already, it does a flush when it returns 0
336 // don't wait if we're to shutdown
337 if (_shutdown) break;
338 otk::Timer::dispatchTimers(!_sync); // wait if not in sync mode
339 }
340 }
341
342
343 void Openbox::addClient(Window window, Client *client)
344 {
345 _clients[window] = client;
346 }
347
348
349 void Openbox::removeClient(Window window)
350 {
351 _clients.erase(window);
352 }
353
354
355 Client *Openbox::findClient(Window window)
356 {
357 /*
358 NOTE: we dont use _clients[] to find the value because that will insert
359 a new null into the hash, which really sucks when we want to clean up the
360 hash at shutdown!
361 */
362 ClientMap::iterator it = _clients.find(window);
363 if (it != _clients.end())
364 return it->second;
365 else
366 return (Client*) 0;
367 }
368
369
370 void Openbox::setFocusedClient(Client *c)
371 {
372 // sometimes this is called with the already-focused window, this is
373 // important for the python scripts to work (eg, c = 0 twice). don't just
374 // return if _focused_client == c
375
376 assert(_focused_screen);
377
378 // uninstall the old colormap
379 if (_focused_client)
380 _focused_client->installColormap(false);
381 else
382 _focused_screen->installColormap(false);
383
384 _focused_client = c;
385 if (c) {
386 _focused_screen = _screens[c->screen()];
387
388 // install the client's colormap
389 c->installColormap(true);
390 } else {
391 XSetInputFocus(**otk::display, _focused_screen->focuswindow(),
392 RevertToNone, CurrentTime);
393
394 // install the root window colormap
395 _focused_screen->installColormap(true);
396 }
397 // set the NET_ACTIVE_WINDOW hint for all screens
398 ScreenList::iterator it, end = _screens.end();
399 for (it = _screens.begin(); it != end; ++it) {
400 int num = (*it)->number();
401 Window root = otk::display->screenInfo(num)->rootWindow();
402 otk::Property::set(root, otk::Property::atoms.net_active_window,
403 otk::Property::atoms.window,
404 (c && _focused_screen == *it) ? c->window() : None);
405 }
406
407 // call the python Focus callbacks
408 EventData data(_focused_screen->number(), c, EventAction::Focus, 0);
409 _bindings->fireEvent(&data);
410 }
411
412 }
413
This page took 0.049495 seconds and 4 git commands to generate.