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