]> Dogcows Code - chaz/openbox/blob - src/openbox.cc
create some globals for use in the python scripts
[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 "../version.h"
8 #include "openbox.hh"
9 #include "client.hh"
10 #include "screen.hh"
11 #include "actions.hh"
12 #include "bindings.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 <Python.h>
47
48 // The initializer in openbox_wrap.cc
49 extern void init_openbox(void);
50 // The initializer in otk_wrap.cc
51 extern void init_otk(void);
52
53 #include "gettext.h"
54 #define _(str) gettext(str)
55 }
56
57 #include <algorithm>
58
59 namespace ob {
60
61 Openbox *Openbox::instance = (Openbox *) 0;
62
63
64 void Openbox::signalHandler(int signal)
65 {
66 switch (signal) {
67 case SIGHUP:
68 // XXX: Do something with HUP? Really shouldn't, we get this when X shuts
69 // down and hangs-up on us.
70
71 case SIGINT:
72 case SIGTERM:
73 case SIGPIPE:
74 printf("Caught signal %d. Exiting.\n", signal);
75 instance->shutdown();
76
77 break;
78 case SIGFPE:
79 case SIGSEGV:
80 printf("Caught signal %d. Aborting and dumping core.\n", signal);
81 abort();
82 }
83 }
84
85
86 static void runPython(const char *s) {
87 FILE *rcpyfd = fopen(s, "r");
88 if (!rcpyfd) {
89 printf("failed to load python file %s\n", s);
90 } else {
91 PyRun_SimpleFile(rcpyfd, const_cast<char*>(s));
92 fclose(rcpyfd);
93 }
94 }
95
96
97 Openbox::Openbox(int argc, char **argv)
98 : otk::OtkEventDispatcher(),
99 otk::OtkEventHandler()
100 {
101 struct sigaction action;
102
103 _state = State_Starting; // initializing everything
104
105 Openbox::instance = this;
106
107 _displayreq = (char*) 0;
108 _argv0 = argv[0];
109 _doshutdown = false;
110 _rcfilepath = otk::expandTilde("~/.openbox/rc3");
111 _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
112 _focused_client = 0;
113 _sync = false;
114
115 parseCommandLine(argc, argv);
116
117 // TEMPORARY: using the xrdb rc3
118 _config.setFile(_rcfilepath);
119 if (!_config.load()) {
120 printf("failed to load rc file %s\n", _config.file().c_str());
121 ::exit(2);
122 }
123 std::string s;
124 _config.getValue("session.styleFile", s);
125 _config.setFile(s);
126 if (!_config.load()) {
127 printf("failed to load style %s\n", _config.file().c_str());
128 ::exit(2);
129 }
130
131 // open the X display (and gets some info about it, and its screens)
132 otk::OBDisplay::initialize(_displayreq);
133 assert(otk::OBDisplay::display);
134
135 XSynchronize(otk::OBDisplay::display, _sync);
136
137 // set up the signal handler
138 action.sa_handler = Openbox::signalHandler;
139 action.sa_mask = sigset_t();
140 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
141 sigaction(SIGPIPE, &action, (struct sigaction *) 0);
142 sigaction(SIGSEGV, &action, (struct sigaction *) 0);
143 sigaction(SIGFPE, &action, (struct sigaction *) 0);
144 sigaction(SIGTERM, &action, (struct sigaction *) 0);
145 sigaction(SIGINT, &action, (struct sigaction *) 0);
146 sigaction(SIGHUP, &action, (struct sigaction *) 0);
147
148 _property = new otk::OBProperty();
149 _actions = new OBActions();
150 _bindings = new OBBindings();
151
152 setMasterHandler(_actions); // set as the master event handler
153
154 // create the mouse cursors we'll use
155 _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
156 _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
157 _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
158 _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
159 _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
160 _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
161
162 // initialize all the screens
163 OBScreen *screen;
164 screen = new OBScreen(0, _config);
165 if (screen->managed()) {
166 _screens.push_back(screen);
167 // XXX: "change to" the first workspace on the screen to initialize stuff
168 } else
169 delete screen;
170
171 if (_screens.empty()) {
172 printf(_("No screens were found without a window manager. Exiting.\n"));
173 ::exit(1);
174 }
175
176 // start up python and run the user's startup script
177 Py_SetProgramName(argv[0]);
178 Py_Initialize();
179 init_otk();
180 init_openbox();
181 PyRun_SimpleString("from _otk import *; from _openbox import *;");
182
183 runPython(SCRIPTDIR"/globals.py"); // create/set global vars
184 runPython(SCRIPTDIR"/clientmotion.py"); // moving and resizing clients
185 runPython(SCRIPTDIR"/clicks.py"); // titlebar/root clicks and dblclicks
186 runPython(_scriptfilepath.c_str());
187
188 ScreenList::iterator it, end = _screens.end();
189 for (it = _screens.begin(); it != end; ++it)
190 (*it)->manageExisting();
191
192 // grab any keys set up before the screens existed
193 _bindings->grabKeys(true);
194
195 // set up input focus
196 _focused_screen = _screens[0];
197 setFocusedClient(0);
198
199 _state = State_Normal; // done starting
200 }
201
202
203 Openbox::~Openbox()
204 {
205 _state = State_Exiting; // time to kill everything
206
207 delete _bindings;
208 delete _actions;
209
210 std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
211
212 delete _property;
213
214 // close the X display
215 otk::OBDisplay::destroy();
216 }
217
218
219 void Openbox::parseCommandLine(int argc, char **argv)
220 {
221 bool err = false;
222
223 for (int i = 1; i < argc; ++i) {
224 std::string arg(argv[i]);
225
226 if (arg == "-display") {
227 if (++i >= argc)
228 err = true;
229 else
230 _displayreq = argv[i];
231 } else if (arg == "-rc") {
232 if (++i >= argc)
233 err = true;
234 else
235 _rcfilepath = argv[i];
236 } else if (arg == "-menu") {
237 if (++i >= argc)
238 err = true;
239 else
240 _menufilepath = argv[i];
241 } else if (arg == "-script") {
242 if (++i >= argc)
243 err = true;
244 else
245 _scriptfilepath = argv[i];
246 } else if (arg == "-sync") {
247 _sync = 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"), OPENBOX_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 -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 -version display version and exit.\n\
284 -help display this help text and exit.\n\n"), _argv0);
285
286 printf(_("Compile time options:\n\
287 Debugging: %s\n\
288 Shape: %s\n\
289 Xinerama: %s\n"),
290 #ifdef DEBUG
291 _("yes"),
292 #else // !DEBUG
293 _("no"),
294 #endif // DEBUG
295
296 #ifdef SHAPE
297 _("yes"),
298 #else // !SHAPE
299 _("no"),
300 #endif // SHAPE
301
302 #ifdef XINERAMA
303 _("yes")
304 #else // !XINERAMA
305 _("no")
306 #endif // XINERAMA
307 );
308 }
309
310
311 void Openbox::eventLoop()
312 {
313 while (!_doshutdown) {
314 dispatchEvents(); // from OtkEventDispatcher
315 XFlush(otk::OBDisplay::display); // flush here before we go wait for timers
316 _timermanager.fire();
317 }
318 }
319
320
321 void Openbox::addClient(Window window, OBClient *client)
322 {
323 _clients[window] = client;
324 }
325
326
327 void Openbox::removeClient(Window window)
328 {
329 _clients.erase(window);
330 }
331
332
333 OBClient *Openbox::findClient(Window window)
334 {
335 /*
336 NOTE: we dont use _clients[] to find the value because that will insert
337 a new null into the hash, which really sucks when we want to clean up the
338 hash at shutdown!
339 */
340 ClientMap::iterator it = _clients.find(window);
341 if (it != _clients.end())
342 return it->second;
343 else
344 return (OBClient*) 0;
345 }
346
347
348 void Openbox::setFocusedClient(OBClient *c)
349 {
350 _focused_client = c;
351 if (c) {
352 _focused_screen = _screens[c->screen()];
353 } else {
354 assert(_focused_screen);
355 XSetInputFocus(otk::OBDisplay::display, _focused_screen->focuswindow(),
356 RevertToNone, CurrentTime);
357 }
358 }
359
360 }
361
This page took 0.048547 seconds and 5 git commands to generate.