]> icculus.org git repositories - dana/openbox.git/blob - src/openbox.cc
nicer output
[dana/openbox.git] / 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   OBBindings::StringVect v;
153   v.push_back("C-x");
154   v.push_back("C-y");
155   v.push_back("v");
156   _bindings->add(v, 1);
157   v.clear();
158 //  v.push_back("C-x");
159 //  v.push_back("C-z");
160   v.push_back("a");
161   _bindings->add(v, 2);
162
163   printf("CHAINS:\n");
164   _bindings->display();
165   ::exit(0);
166
167   setMasterHandler(_actions); // set as the master event handler
168
169   // create the mouse cursors we'll use
170   _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
171   _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
172   _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
173   _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
174   _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
175   _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
176
177   // start up python and run the user's startup script
178   Py_SetProgramName(argv[0]);
179   Py_Initialize();
180   init_otk();
181   init_openbox();
182   PyRun_SimpleString("from _otk import *; from _openbox import *;");
183   PyRun_SimpleString("openbox = Openbox_instance()");
184
185   runPython(SCRIPTDIR"/clientmotion.py"); // moving and resizing clients
186   runPython(SCRIPTDIR"/clicks.py"); // titlebar/root clicks and dblclicks
187   runPython(_scriptfilepath.c_str());
188  
189   // initialize all the screens
190   OBScreen *screen;
191   screen = new OBScreen(0, _config);
192   if (screen->managed()) {
193     _screens.push_back(screen);
194     _screens[0]->manageExisting();
195     // XXX: "change to" the first workspace on the screen to initialize stuff
196   } else
197     delete screen;
198
199   if (_screens.empty()) {
200     printf(_("No screens were found without a window manager. Exiting.\n"));
201     ::exit(1);
202   }
203
204   // set up input focus
205   _focused_screen = _screens[0];
206   setFocusedClient(0);
207   
208   _state = State_Normal; // done starting
209 }
210
211
212 Openbox::~Openbox()
213 {
214   _state = State_Exiting; // time to kill everything
215
216   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
217
218   delete _bindings;
219   delete _actions;
220   delete _property;
221   
222   // close the X display
223   otk::OBDisplay::destroy();
224 }
225
226
227 void Openbox::parseCommandLine(int argc, char **argv)
228 {
229   bool err = false;
230
231   for (int i = 1; i < argc; ++i) {
232     std::string arg(argv[i]);
233
234     if (arg == "-display") {
235       if (++i >= argc)
236         err = true;
237       else
238         _displayreq = argv[i];
239     } else if (arg == "-rc") {
240       if (++i >= argc)
241         err = true;
242       else
243         _rcfilepath = argv[i];
244     } else if (arg == "-menu") {
245       if (++i >= argc)
246         err = true;
247       else
248         _menufilepath = argv[i];
249     } else if (arg == "-script") {
250       if (++i >= argc)
251         err = true;
252       else
253         _scriptfilepath = argv[i];
254     } else if (arg == "-sync") {
255       _sync = 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"), OPENBOX_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   -display <string>  use display connection.\n\
288   -rc <string>       use alternate resource file.\n\
289   -menu <string>     use alternate menu file.\n\
290   -script <string>   use alternate startup script file.\n\
291   -version           display version and exit.\n\
292   -help              display this help text and exit.\n\n"), _argv0);
293
294   printf(_("Compile time options:\n\
295   Debugging: %s\n\
296   Shape:     %s\n\
297   Xinerama:  %s\n"),
298 #ifdef    DEBUG
299          _("yes"),
300 #else // !DEBUG
301          _("no"),
302 #endif // DEBUG
303
304 #ifdef    SHAPE
305          _("yes"),
306 #else // !SHAPE
307          _("no"),
308 #endif // SHAPE
309
310 #ifdef    XINERAMA
311          _("yes")
312 #else // !XINERAMA
313          _("no")
314 #endif // XINERAMA
315     );
316 }
317
318
319 void Openbox::eventLoop()
320 {
321   while (!_doshutdown) {
322     dispatchEvents(); // from OtkEventDispatcher
323     XFlush(otk::OBDisplay::display); // flush here before we go wait for timers
324     _timermanager.fire();
325   }
326 }
327
328
329 void Openbox::addClient(Window window, OBClient *client)
330 {
331   _clients[window] = client;
332 }
333
334
335 void Openbox::removeClient(Window window)
336 {
337   _clients.erase(window);
338 }
339
340
341 OBClient *Openbox::findClient(Window window)
342 {
343   /*
344     NOTE: we dont use _clients[] to find the value because that will insert
345     a new null into the hash, which really sucks when we want to clean up the
346     hash at shutdown!
347   */
348   ClientMap::iterator it = _clients.find(window);
349   if (it != _clients.end())
350     return it->second;
351   else
352     return (OBClient*) 0;
353 }
354
355
356 void Openbox::setFocusedClient(OBClient *c)
357 {
358   _focused_client = c;
359   if (c) {
360     _focused_screen = _screens[c->screen()];
361   } else {
362     assert(_focused_screen);
363     XSetInputFocus(otk::OBDisplay::display, _focused_screen->focuswindow(),
364                    RevertToNone, CurrentTime);
365   }
366 }
367
368 }
369