]> icculus.org git repositories - mikachu/openbox.git/blob - src/openbox.cc
handle mouse clicks in the python scripts now
[mikachu/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
112   parseCommandLine(argc, argv);
113
114   // TEMPORARY: using the xrdb rc3
115   _config.setFile(_rcfilepath);
116   if (!_config.load()) {
117     printf("failed to load rc file %s\n", _config.file().c_str());
118     ::exit(2);
119   }
120   std::string s;
121   _config.getValue("session.styleFile", s);
122   _config.setFile(s);
123   if (!_config.load()) {
124     printf("failed to load style %s\n", _config.file().c_str());
125     ::exit(2);
126   }
127
128   // open the X display (and gets some info about it, and its screens)
129   otk::OBDisplay::initialize(_displayreq);
130   assert(otk::OBDisplay::display);
131     
132   // set up the signal handler
133   action.sa_handler = Openbox::signalHandler;
134   action.sa_mask = sigset_t();
135   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
136   sigaction(SIGPIPE, &action, (struct sigaction *) 0);
137   sigaction(SIGSEGV, &action, (struct sigaction *) 0);
138   sigaction(SIGFPE, &action, (struct sigaction *) 0);
139   sigaction(SIGTERM, &action, (struct sigaction *) 0);
140   sigaction(SIGINT, &action, (struct sigaction *) 0);
141   sigaction(SIGHUP, &action, (struct sigaction *) 0);
142
143   _property = new otk::OBProperty();
144
145   _actions = new OBActions();
146
147   setMasterHandler(_actions); // set as the master event handler
148
149   // create the mouse cursors we'll use
150   _cursors.session = XCreateFontCursor(otk::OBDisplay::display, XC_left_ptr);
151   _cursors.move = XCreateFontCursor(otk::OBDisplay::display, XC_fleur);
152   _cursors.ll_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ll_angle);
153   _cursors.lr_angle = XCreateFontCursor(otk::OBDisplay::display, XC_lr_angle);
154   _cursors.ul_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ul_angle);
155   _cursors.ur_angle = XCreateFontCursor(otk::OBDisplay::display, XC_ur_angle);
156
157   // start up python and run the user's startup script
158   Py_SetProgramName(argv[0]);
159   Py_Initialize();
160   init_otk();
161   init_openbox();
162   PyRun_SimpleString("from _otk import *; from _openbox import *;");
163   PyRun_SimpleString("openbox = Openbox_instance()");
164
165   runPython(SCRIPTDIR"/clientmotion.py"); // moving and resizing clients
166   runPython(SCRIPTDIR"/clicks.py"); // titlebar/root clicks and dblclicks
167   runPython(_scriptfilepath.c_str());
168  
169   // initialize all the screens
170   OBScreen *screen;
171   screen = new OBScreen(0, _config);
172   if (screen->managed()) {
173     _screens.push_back(screen);
174     _screens[0]->manageExisting();
175     // XXX: "change to" the first workspace on the screen to initialize stuff
176   } else
177     delete screen;
178
179   if (_screens.empty()) {
180     printf(_("No screens were found without a window manager. Exiting.\n"));
181     ::exit(1);
182   }
183
184   _state = State_Normal; // done starting
185 }
186
187
188 Openbox::~Openbox()
189 {
190   _state = State_Exiting; // time to kill everything
191
192   std::for_each(_screens.begin(), _screens.end(), otk::PointerAssassin());
193   
194   // close the X display
195   otk::OBDisplay::destroy();
196 }
197
198
199 void Openbox::parseCommandLine(int argc, char **argv)
200 {
201   bool err = false;
202
203   for (int i = 1; i < argc; ++i) {
204     std::string arg(argv[i]);
205
206     if (arg == "-display") {
207       if (++i >= argc)
208         err = true;
209       else
210         _displayreq = argv[i];
211     } else if (arg == "-rc") {
212       if (++i >= argc)
213         err = true;
214       else
215         _rcfilepath = argv[i];
216     } else if (arg == "-menu") {
217       if (++i >= argc)
218         err = true;
219       else
220         _menufilepath = argv[i];
221     } else if (arg == "-script") {
222       if (++i >= argc)
223         err = true;
224       else
225         _scriptfilepath = argv[i];
226     } else if (arg == "-version") {
227       showVersion();
228       ::exit(0);
229     } else if (arg == "-help") {
230       showHelp();
231       ::exit(0);
232     } else
233       err = true;
234
235     if (err) {
236       showHelp();
237       exit(1);
238     }
239   }
240 }
241
242
243 void Openbox::showVersion()
244 {
245   printf(_("Openbox - version %s\n"), OPENBOX_VERSION);
246   printf("    (c) 2002 - 2002 Ben Jansens\n\n");
247 }
248
249
250 void Openbox::showHelp()
251 {
252   showVersion(); // show the version string and copyright
253
254   // print program usage and command line options
255   printf(_("Usage: %s [OPTIONS...]\n\
256   Options:\n\
257   -display <string>  use display connection.\n\
258   -rc <string>       use alternate resource file.\n\
259   -menu <string>     use alternate menu file.\n\
260   -script <string>   use alternate startup script file.\n\
261   -version           display version and exit.\n\
262   -help              display this help text and exit.\n\n"), _argv0);
263
264   printf(_("Compile time options:\n\
265   Debugging: %s\n\
266   Shape:     %s\n\
267   Xinerama:  %s\n"),
268 #ifdef    DEBUG
269          _("yes"),
270 #else // !DEBUG
271          _("no"),
272 #endif // DEBUG
273
274 #ifdef    SHAPE
275          _("yes"),
276 #else // !SHAPE
277          _("no"),
278 #endif // SHAPE
279
280 #ifdef    XINERAMA
281          _("yes")
282 #else // !XINERAMA
283          _("no")
284 #endif // XINERAMA
285     );
286 }
287
288
289 void Openbox::eventLoop()
290 {
291   while (!_doshutdown) {
292     dispatchEvents(); // from OtkEventDispatcher
293     _timermanager.fire();
294   }
295 }
296
297
298 void Openbox::addClient(Window window, OBClient *client)
299 {
300   _clients[window] = client;
301 }
302
303
304 void Openbox::removeClient(Window window)
305 {
306   _clients.erase(window);
307 }
308
309
310 OBClient *Openbox::findClient(Window window)
311 {
312   /*
313     NOTE: we dont use _clients[] to find the value because that will insert
314     a new null into the hash, which really sucks when we want to clean up the
315     hash at shutdown!
316   */
317   ClientMap::iterator it = _clients.find(window);
318   if (it != _clients.end())
319     return it->second;
320   else
321     return (OBClient*) 0;
322 }
323
324 }
325