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