]>
Dogcows Code - chaz/openbox/blob - src/openbox.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
11 #include "bindings.hh"
13 #include "otk/property.hh"
14 #include "otk/display.hh"
15 #include "otk/assassin.hh"
16 #include "otk/util.hh"
19 #include <X11/cursorfont.h>
23 #endif // HAVE_STDIO_H
27 #endif // HAVE_STDLIB_H
31 #endif // HAVE_SIGNAL_H
35 #endif // HAVE_FCNTL_H
38 # include <sys/types.h>
40 #endif // HAVE_UNISTD_H
42 #ifdef HAVE_SYS_SELECT_H
43 # include <sys/select.h>
44 #endif // HAVE_SYS_SELECT_H
47 #define _(str) gettext(str)
54 Openbox
*Openbox::instance
= (Openbox
*) 0;
57 void Openbox::signalHandler(int signal
)
61 printf("Caught SIGUSR1 signal. Restarting.\n");
69 printf("Caught signal %d. Exiting.\n", signal
);
75 printf("Caught signal %d. Aborting and dumping core.\n", signal
);
81 Openbox::Openbox(int argc
, char **argv
)
82 : otk::EventDispatcher(),
85 struct sigaction action
;
87 _state
= State_Starting
; // initializing everything
89 Openbox::instance
= this;
91 _displayreq
= (char*) 0;
95 _rcfilepath
= otk::expandTilde("~/.openbox/rc3");
96 _scriptfilepath
= otk::expandTilde("~/.openbox/user.py");
100 parseCommandLine(argc
, argv
);
102 // open the X display (and gets some info about it, and its screens)
103 otk::Display::initialize(_displayreq
);
104 assert(otk::Display::display
);
106 XSynchronize(otk::Display::display
, _sync
);
108 // set up the signal handler
109 action
.sa_handler
= Openbox::signalHandler
;
110 action
.sa_mask
= sigset_t();
111 action
.sa_flags
= SA_NOCLDSTOP
| SA_NODEFER
;
112 sigaction(SIGUSR1
, &action
, (struct sigaction
*) 0);
113 sigaction(SIGPIPE
, &action
, (struct sigaction
*) 0);
114 sigaction(SIGSEGV
, &action
, (struct sigaction
*) 0);
115 sigaction(SIGFPE
, &action
, (struct sigaction
*) 0);
116 sigaction(SIGTERM
, &action
, (struct sigaction
*) 0);
117 sigaction(SIGINT
, &action
, (struct sigaction
*) 0);
118 sigaction(SIGHUP
, &action
, (struct sigaction
*) 0);
120 _property
= new otk::Property();
121 _actions
= new Actions();
122 _bindings
= new Bindings();
124 setMasterHandler(_actions
); // set as the master event handler
126 // create the mouse cursors we'll use
127 _cursors
.session
= XCreateFontCursor(otk::Display::display
, XC_left_ptr
);
128 _cursors
.move
= XCreateFontCursor(otk::Display::display
, XC_fleur
);
129 _cursors
.ll_angle
= XCreateFontCursor(otk::Display::display
, XC_ll_angle
);
130 _cursors
.lr_angle
= XCreateFontCursor(otk::Display::display
, XC_lr_angle
);
131 _cursors
.ul_angle
= XCreateFontCursor(otk::Display::display
, XC_ul_angle
);
132 _cursors
.ur_angle
= XCreateFontCursor(otk::Display::display
, XC_ur_angle
);
134 // initialize scripting
135 python_init(argv
[0]);
137 // load config values
138 python_exec(SCRIPTDIR
"/config.py"); // load openbox config values
139 // run all of the python scripts
140 python_exec(SCRIPTDIR
"/builtins.py"); // builtin callbacks
141 // run the user's script or the system defaults if that fails
142 if (!python_exec(_scriptfilepath
.c_str()))
143 python_exec(SCRIPTDIR
"/defaults.py"); // system default bahaviors
145 // initialize all the screens
147 int i
= _single
? DefaultScreen(otk::Display::display
) : 0;
148 int max
= _single
? i
+ 1 : ScreenCount(otk::Display::display
);
149 for (; i
< max
; ++i
) {
150 screen
= new Screen(i
);
151 if (screen
->managed())
152 _screens
.push_back(screen
);
157 if (_screens
.empty()) {
158 printf(_("No screens were found without a window manager. Exiting.\n"));
162 ScreenList::iterator it
, end
= _screens
.end();
163 for (it
= _screens
.begin(); it
!= end
; ++it
) {
164 (*it
)->manageExisting();
167 // grab any keys set up before the screens existed
168 _bindings
->grabKeys(true);
170 // set up input focus
171 _focused_screen
= _screens
[0];
174 _state
= State_Normal
; // done starting
180 _state
= State_Exiting
; // time to kill everything
182 int first_screen
= _screens
.front()->number();
184 std::for_each(_screens
.begin(), _screens
.end(), otk::PointerAssassin());
192 XSetInputFocus(otk::Display::display
, PointerRoot
, RevertToNone
,
194 XSync(otk::Display::display
, false);
196 // this tends to block.. i honestly am not sure why. causing an x error in
197 // the shutdown process unblocks it. blackbox simply did a ::exit(0), so
198 // all im gunna do is the same.
199 //otk::Display::destroy();
202 if (!_restart_prog
.empty()) {
203 const std::string
&dstr
=
204 otk::Display::screenInfo(first_screen
)->displayString();
205 otk::putenv(const_cast<char *>(dstr
.c_str()));
206 execlp(_restart_prog
.c_str(), _restart_prog
.c_str(), NULL
);
207 perror(_restart_prog
.c_str());
210 // fall back in case the above execlp doesn't work
211 execvp(_argv
[0], _argv
);
212 execvp(otk::basename(_argv
[0]).c_str(), _argv
);
217 void Openbox::parseCommandLine(int argc
, char **argv
)
221 for (int i
= 1; i
< argc
; ++i
) {
222 std::string
arg(argv
[i
]);
224 if (arg
== "-display") {
228 _displayreq
= argv
[i
];
229 } else if (arg
== "-rc") {
233 _rcfilepath
= argv
[i
];
234 } else if (arg
== "-menu") {
238 _menufilepath
= argv
[i
];
239 } else if (arg
== "-script") {
243 _scriptfilepath
= argv
[i
];
244 } else if (arg
== "-sync") {
246 } else if (arg
== "-single") {
248 } else if (arg
== "-version") {
251 } else if (arg
== "-help") {
265 void Openbox::showVersion()
267 printf(_("Openbox - version %s\n"), VERSION
);
268 printf(" (c) 2002 - 2002 Ben Jansens\n\n");
272 void Openbox::showHelp()
274 showVersion(); // show the version string and copyright
276 // print program usage and command line options
277 printf(_("Usage: %s [OPTIONS...]\n\
279 -display <string> use display connection.\n\
280 -single run on a single screen (default is to run every one).\n\
281 -rc <string> use alternate resource file.\n\
282 -menu <string> use alternate menu file.\n\
283 -script <string> use alternate startup script file.\n\
284 -sync run in synchronous mode (for debugging).\n\
285 -version display version and exit.\n\
286 -help display this help text and exit.\n\n"), _argv
[0]);
288 printf(_("Compile time options:\n\
320 void Openbox::eventLoop()
323 _timermanager
.fire(!_sync
); // wait if not in sync mode
324 dispatchEvents(); // from otk::EventDispatcher
325 XFlush(otk::Display::display
); // flush here before we go wait for timers
330 void Openbox::addClient(Window window
, Client
*client
)
332 _clients
[window
] = client
;
336 void Openbox::removeClient(Window window
)
338 _clients
.erase(window
);
342 Client
*Openbox::findClient(Window window
)
345 NOTE: we dont use _clients[] to find the value because that will insert
346 a new null into the hash, which really sucks when we want to clean up the
349 ClientMap::iterator it
= _clients
.find(window
);
350 if (it
!= _clients
.end())
357 void Openbox::setFocusedClient(Client
*c
)
361 _focused_screen
= _screens
[c
->screen()];
363 assert(_focused_screen
);
364 XSetInputFocus(otk::Display::display
, _focused_screen
->focuswindow(),
365 RevertToNone
, CurrentTime
);
367 // set the NET_ACTIVE_WINDOW hint for all screens
368 ScreenList::iterator it
, end
= _screens
.end();
369 for (it
= _screens
.begin(); it
!= end
; ++it
) {
370 int num
= (*it
)->number();
371 Window root
= otk::Display::screenInfo(num
)->rootWindow();
372 _property
->set(root
, otk::Property::net_active_window
,
373 otk::Property::Atom_Window
,
374 (c
&& _focused_screen
== *it
) ? c
->window() : None
);
377 // call the python Focus callbacks
378 EventData
data(_focused_screen
->number(), c
, EventFocus
, 0);
379 Openbox::instance
->bindings()->fireEvent(&data
);
382 void Openbox::execute(int screen
, const std::string
&bin
)
384 if (screen
>= ScreenCount(otk::Display::display
))
386 otk::bexec(bin
, otk::Display::screenInfo(screen
)->displayString());
This page took 0.052708 seconds and 4 git commands to generate.