]> icculus.org git repositories - dana/openbox.git/blob - src/openbox.cc
new code for bindings/callbacks. much sexier. now passes python classes back to the...
[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 "python.hh"
14 #include "otk/property.hh"
15 #include "otk/display.hh"
16 #include "otk/assassin.hh"
17 #include "otk/util.hh" // TEMPORARY
18
19 extern "C" {
20 #include <X11/cursorfont.h>
21
22 #ifdef    HAVE_STDIO_H
23 #  include <stdio.h>
24 #endif // HAVE_STDIO_H
25
26 #ifdef    HAVE_STDLIB_H
27 #  include <stdlib.h>
28 #endif // HAVE_STDLIB_H
29
30 #ifdef    HAVE_SIGNAL_H
31 #  include <signal.h>
32 #endif // HAVE_SIGNAL_H
33
34 #ifdef    HAVE_FCNTL_H
35 #  include <fcntl.h>
36 #endif // HAVE_FCNTL_H
37
38 #ifdef    HAVE_UNISTD_H
39 #  include <sys/types.h>
40 #  include <unistd.h>
41 #endif // HAVE_UNISTD_H
42
43 #ifdef    HAVE_SYS_SELECT_H
44 #  include <sys/select.h>
45 #endif // HAVE_SYS_SELECT_H
46
47 #include "gettext.h"
48 #define _(str) gettext(str)
49 }
50
51 #include <algorithm>
52
53 namespace ob {
54
55 Openbox *Openbox::instance  = (Openbox *) 0;
56
57
58 void Openbox::signalHandler(int signal)
59 {
60   switch (signal) {
61   case SIGHUP:
62     // XXX: Do something with HUP? Really shouldn't, we get this when X shuts
63     //      down and hangs-up on us.
64     
65   case SIGINT:
66   case SIGTERM:
67   case SIGPIPE:
68     printf("Caught signal %d. Exiting.\n", signal);
69     instance->shutdown();
70
71     break;
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::OtkEventDispatcher(),
82     otk::OtkEventHandler()
83 {
84   struct sigaction action;
85
86   _state = State_Starting; // initializing everything
87
88   Openbox::instance = this;
89
90   _displayreq = (char*) 0;
91   _argv0 = argv[0];
92   _doshutdown = false;
93   _rcfilepath = otk::expandTilde("~/.openbox/rc3");
94   _scriptfilepath = otk::expandTilde("~/.openbox/user.py");
95   _focused_client = 0;
96   _sync = false;
97
98   parseCommandLine(argc, argv);
99
100   // open the X display (and gets some info about it, and its screens)
101   otk::OBDisplay::initialize(_displayreq);
102   assert(otk::OBDisplay::display);
103
104   XSynchronize(otk::OBDisplay::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(SIGPIPE, &action, (struct sigaction *) 0);
111   sigaction(SIGSEGV, &action, (struct sigaction *) 0);
112   sigaction(SIGFPE, &action, (struct sigaction *) 0);
113   sigaction(SIGTERM, &action, (struct sigaction *) 0);
114   sigaction(SIGINT, &action, (struct sigaction *) 0);
115   sigaction(SIGHUP, &action, (struct sigaction *) 0);
116
117   _property = new otk::OBProperty();
118   _actions = new OBActions();
119   _bindings = new OBBindings();
120
121   setMasterHandler(_actions); // set as the master event handler
122
123   // create the mouse cursors we'll use
124   _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
125   _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
126   _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
127   _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
128   _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
129   _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
130
131   // initialize scripting
132   python_init(argv[0]);
133   
134   // load config values
135   python_exec(SCRIPTDIR"/config.py"); // load openbox config values
136
137   // initialize all the screens
138   OBScreen *screen;
139   screen = new OBScreen(0);
140   if (screen->managed()) {
141     _screens.push_back(screen);
142     // XXX: "change to" the first workspace on the screen to initialize stuff
143   } else
144     delete screen;
145
146   if (_screens.empty()) {
147     printf(_("No screens were found without a window manager. Exiting.\n"));
148     ::exit(1);
149   }
150
151   // run all of the python scripts, including the user's
152   python_exec(SCRIPTDIR"/globals.py"); // create/set global vars
153   python_exec(SCRIPTDIR"/clientmotion.py"); // moving and resizing clients
154   python_exec(SCRIPTDIR"/clicks.py"); // titlebar/root clicks and dblclicks
155   python_exec(_scriptfilepath.c_str());
156
157   ScreenList::iterator it, end = _screens.end();
158   for (it = _screens.begin(); it != end; ++it) {
159     (*it)->manageExisting();
160   }
161  
162   // grab any keys set up before the screens existed
163   _bindings->grabKeys(true);
164
165   // set up input focus
166   _focused_screen = _screens[0];
167   setFocusedClient(0);
168   
169   _state = State_Normal; // done starting
170 }
171
172
173 Openbox::~Openbox()
174 {
175   _state = State_Exiting; // time to kill everything
176
177   delete _bindings;
178   delete _actions;
179   
180   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
181
182   delete _property;
183
184   python_destroy();
185   
186   // close the X display
187   otk::OBDisplay::destroy();
188 }
189
190
191 void Openbox::parseCommandLine(int argc, char **argv)
192 {
193   bool err = false;
194
195   for (int i = 1; i < argc; ++i) {
196     std::string arg(argv[i]);
197
198     if (arg == "-display") {
199       if (++i >= argc)
200         err = true;
201       else
202         _displayreq = argv[i];
203     } else if (arg == "-rc") {
204       if (++i >= argc)
205         err = true;
206       else
207         _rcfilepath = argv[i];
208     } else if (arg == "-menu") {
209       if (++i >= argc)
210         err = true;
211       else
212         _menufilepath = argv[i];
213     } else if (arg == "-script") {
214       if (++i >= argc)
215         err = true;
216       else
217         _scriptfilepath = argv[i];
218     } else if (arg == "-sync") {
219       _sync = true;
220     } else if (arg == "-version") {
221       showVersion();
222       ::exit(0);
223     } else if (arg == "-help") {
224       showHelp();
225       ::exit(0);
226     } else
227       err = true;
228
229     if (err) {
230       showHelp();
231       exit(1);
232     }
233   }
234 }
235
236
237 void Openbox::showVersion()
238 {
239   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
240   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
241 }
242
243
244 void Openbox::showHelp()
245 {
246   showVersion(); // show the version string and copyright
247
248   // print program usage and command line options
249   printf(_("Usage: %s [OPTIONS...]\n\
250   Options:\n\
251   -display <string>  use display connection.\n\
252   -rc <string>       use alternate resource file.\n\
253   -menu <string>     use alternate menu file.\n\
254   -script <string>   use alternate startup script file.\n\
255   -version           display version and exit.\n\
256   -help              display this help text and exit.\n\n"), _argv0);
257
258   printf(_("Compile time options:\n\
259   Debugging: %s\n\
260   Shape:     %s\n\
261   Xinerama:  %s\n"),
262 #ifdef    DEBUG
263          _("yes"),
264 #else // !DEBUG
265          _("no"),
266 #endif // DEBUG
267
268 #ifdef    SHAPE
269          _("yes"),
270 #else // !SHAPE
271          _("no"),
272 #endif // SHAPE
273
274 #ifdef    XINERAMA
275          _("yes")
276 #else // !XINERAMA
277          _("no")
278 #endif // XINERAMA
279     );
280 }
281
282
283 void Openbox::eventLoop()
284 {
285   while (!_doshutdown) {
286     dispatchEvents(); // from OtkEventDispatcher
287     XFlush(otk::OBDisplay::display); // flush here before we go wait for timers
288     _timermanager.fire();
289   }
290 }
291
292
293 void Openbox::addClient(Window window, OBClient *client)
294 {
295   _clients[window] = client;
296 }
297
298
299 void Openbox::removeClient(Window window)
300 {
301   _clients.erase(window);
302 }
303
304
305 OBClient *Openbox::findClient(Window window)
306 {
307   /*
308     NOTE: we dont use _clients[] to find the value because that will insert
309     a new null into the hash, which really sucks when we want to clean up the
310     hash at shutdown!
311   */
312   ClientMap::iterator it = _clients.find(window);
313   if (it != _clients.end())
314     return it->second;
315   else
316     return (OBClient*) 0;
317 }
318
319
320 void Openbox::setFocusedClient(OBClient *c)
321 {
322   _focused_client = c;
323   if (c) {
324     _focused_screen = _screens[c->screen()];
325   } else {
326     assert(_focused_screen);
327     XSetInputFocus(otk::OBDisplay::display, _focused_screen->focuswindow(),
328                    RevertToNone, CurrentTime);
329   }
330 }
331
332 }
333