1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // BaseDisplay.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
30 #include <X11/Xatom.h>
31 #include <X11/Xutil.h>
32 #include <X11/keysym.h>
35 # include <X11/extensions/shape.h>
40 #endif // HAVE_FCNTL_H
44 #endif // HAVE_STDIO_H
48 #endif // HAVE_STDLIB_H
52 #endif // HAVE_STRING_H
55 # include <sys/types.h>
57 #endif // HAVE_UNISTD_H
59 #ifdef HAVE_SYS_SELECT_H
60 # include <sys/select.h>
61 #endif // HAVE_SYS_SELECT_H
65 #endif // HAVE_SIGNAL_H
69 # define SA_NODEFER SA_INTERRUPT
70 # else // !SA_INTERRUPT
71 # define SA_NODEFER (0)
72 # endif // SA_INTERRUPT
75 #ifdef HAVE_SYS_WAIT_H
76 # include <sys/types.h>
77 # include <sys/wait.h>
78 #endif // HAVE_SYS_WAIT_H
85 #include "BaseDisplay.hh"
91 // X error handler to handle any and all X errors while the application is
93 static bool internal_error = False;
94 static Window last_bad_window = None;
96 BaseDisplay *base_display;
99 static int handleXErrors(Display *d, XErrorEvent *e) {
102 XGetErrorText(d, e->error_code, errtxt, 128);
104 i18n(BaseDisplaySet, BaseDisplayXError,
105 "%s: X error: %s(%d) opcodes %d/%d\n resource 0x%lx\n"),
106 base_display->getApplicationName(), errtxt, e->error_code,
107 e->request_code, e->minor_code, e->resourceid);
109 static int handleXErrors(Display *, XErrorEvent *e) {
112 if (e->error_code == BadWindow) last_bad_window = e->resourceid;
113 if (internal_error) abort();
119 // signal handler to allow for proper and gentle shutdown
121 #ifndef HAVE_SIGACTION
122 static RETSIGTYPE signalhandler(int sig) {
123 #else // HAVE_SIGACTION
124 static void signalhandler(int sig) {
125 #endif // HAVE_SIGACTION
127 static int re_enter = 0;
132 waitpid(-1, &status, WNOHANG | WUNTRACED);
134 #ifndef HAVE_SIGACTION
135 // assume broken, braindead sysv signal semantics
136 signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
137 #endif // HAVE_SIGACTION
142 if (base_display->handleSignal(sig)) {
144 #ifndef HAVE_SIGACTION
145 // assume broken, braindead sysv signal semantics
146 signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
147 #endif // HAVE_SIGACTION
152 fprintf(stderr, i18n(BaseDisplaySet, BaseDisplaySignalCaught,
153 "%s: signal %d caught\n"),
154 base_display->getApplicationName(), sig);
156 if (! base_display->isStartup() && ! re_enter) {
157 internal_error = True;
160 fprintf(stderr, i18n(BaseDisplaySet, BaseDisplayShuttingDown,
162 base_display->shutdown();
165 if (sig != SIGTERM && sig != SIGINT) {
166 fprintf(stderr, i18n(BaseDisplaySet, BaseDisplayAborting,
167 "aborting... dumping core\n"));
178 BaseDisplay::BaseDisplay(const char *app_name, const char *dpy_name) {
179 application_name = app_name;
182 last_bad_window = None;
184 ::base_display = this;
186 #ifdef HAVE_SIGACTION
187 struct sigaction action;
189 action.sa_handler = signalhandler;
190 action.sa_mask = sigset_t();
191 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
193 sigaction(SIGPIPE, &action, NULL);
194 sigaction(SIGSEGV, &action, NULL);
195 sigaction(SIGFPE, &action, NULL);
196 sigaction(SIGTERM, &action, NULL);
197 sigaction(SIGINT, &action, NULL);
198 sigaction(SIGCHLD, &action, NULL);
199 sigaction(SIGHUP, &action, NULL);
200 sigaction(SIGUSR1, &action, NULL);
201 sigaction(SIGUSR2, &action, NULL);
202 #else // !HAVE_SIGACTION
203 signal(SIGPIPE, (RETSIGTYPE (*)(int)) signalhandler);
204 signal(SIGSEGV, (RETSIGTYPE (*)(int)) signalhandler);
205 signal(SIGFPE, (RETSIGTYPE (*)(int)) signalhandler);
206 signal(SIGTERM, (RETSIGTYPE (*)(int)) signalhandler);
207 signal(SIGINT, (RETSIGTYPE (*)(int)) signalhandler);
208 signal(SIGUSR1, (RETSIGTYPE (*)(int)) signalhandler);
209 signal(SIGUSR2, (RETSIGTYPE (*)(int)) signalhandler);
210 signal(SIGHUP, (RETSIGTYPE (*)(int)) signalhandler);
211 signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
212 #endif // HAVE_SIGACTION
214 if (! (display = XOpenDisplay(dpy_name))) {
216 i18n(BaseDisplaySet, BaseDisplayXConnectFail,
217 "BaseDisplay::BaseDisplay: connection to X server failed.\n"));
219 } else if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
221 i18n(BaseDisplaySet, BaseDisplayCloseOnExecFail,
222 "BaseDisplay::BaseDisplay: couldn't mark display connection "
223 "as close-on-exec\n"));
227 display_name = XDisplayName(dpy_name);
230 shape.extensions = XShapeQueryExtension(display, &shape.event_basep,
233 shape.extensions = False;
236 XSetErrorHandler((XErrorHandler) handleXErrors);
238 screenInfoList.reserve(ScreenCount(display));
239 for (int i = 0; i < ScreenCount(display); ++i)
240 screenInfoList.push_back(ScreenInfo(this, i));
243 NumLockMask = ScrollLockMask = 0;
245 const XModifierKeymap* const modmap = XGetModifierMapping(display);
246 if (modmap && modmap->max_keypermod > 0) {
247 const int mask_table[] = {
248 ShiftMask, LockMask, ControlMask, Mod1Mask,
249 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
251 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
252 modmap->max_keypermod;
253 // get the values of the keyboard lock modifiers
254 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
255 // since it doesn't need to be.
256 const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock);
257 const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock);
259 for (size_t cnt = 0; cnt < size; ++cnt) {
260 if (! modmap->modifiermap[cnt]) continue;
262 if (num_lock == modmap->modifiermap[cnt])
263 NumLockMask = mask_table[cnt / modmap->max_keypermod];
264 if (scroll_lock == modmap->modifiermap[cnt])
265 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
270 MaskList[1] = LockMask;
271 MaskList[2] = NumLockMask;
272 MaskList[3] = ScrollLockMask;
273 MaskList[4] = LockMask | NumLockMask;
274 MaskList[5] = NumLockMask | ScrollLockMask;
275 MaskList[6] = LockMask | ScrollLockMask;
276 MaskList[7] = LockMask | NumLockMask | ScrollLockMask;
277 MaskListLength = sizeof(MaskList) / sizeof(MaskList[0]);
279 if (modmap) XFreeModifiermap(const_cast<XModifierKeymap*>(modmap));
289 BaseDisplay::~BaseDisplay(void) {
292 XCloseDisplay(display);
296 void BaseDisplay::eventLoop(void) {
299 const int xfd = ConnectionNumber(display);
301 while (run_state == RUNNING && ! internal_error) {
302 if (XPending(display)) {
304 XNextEvent(display, &e);
306 if (last_bad_window != None && e.xany.window == last_bad_window)
309 last_bad_window = None;
313 timeval now, tm, *timeout = (timeval *) 0;
318 if (! timerList.empty()) {
319 const BTimer* const timer = timerList.top();
321 gettimeofday(&now, 0);
322 tm = timer->timeRemaining(now);
327 select(xfd + 1, &rfds, 0, 0, timeout);
329 // check for timer timeout
330 gettimeofday(&now, 0);
332 // there is a small chance for deadlock here:
333 // *IF* the timer list keeps getting refreshed *AND* the time between
334 // timer->start() and timer->shouldFire() is within the timer's period
335 // then the timer will keep firing. This should be VERY near impossible.
336 while (! timerList.empty()) {
337 BTimer *timer = timerList.top();
338 if (! timer->shouldFire(now))
343 timer->fireTimeout();
345 if (timer->isRecurring())
353 void BaseDisplay::addTimer(BTimer *timer) {
356 timerList.push(timer);
360 void BaseDisplay::removeTimer(BTimer *timer) {
361 timerList.release(timer);
366 * Grabs a button, but also grabs the button in every possible combination
367 * with the keyboard lock keys, so that they do not cancel out the event.
369 void BaseDisplay::grabButton(unsigned int button, unsigned int modifiers,
370 Window grab_window, bool owner_events,
371 unsigned int event_mask, int pointer_mode,
372 int keyboard_mode, Window confine_to,
373 Cursor cursor) const {
375 for (size_t cnt = 0; cnt < MaskListLength; ++cnt)
376 XGrabButton(display, button, modifiers | MaskList[cnt], grab_window,
377 owner_events, event_mask, pointer_mode, keyboard_mode,
380 XGrabButton(display, button, modifiers, grab_window,
381 owner_events, event_mask, pointer_mode, keyboard_mode,
387 * Releases the grab on a button, and ungrabs all possible combinations of the
388 * keyboard lock keys.
390 void BaseDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
391 Window grab_window) const {
393 for (size_t cnt = 0; cnt < MaskListLength; ++cnt)
394 XUngrabButton(display, button, modifiers | MaskList[cnt], grab_window);
396 XUngrabButton(display, button, modifiers, grab_window);
401 const ScreenInfo* BaseDisplay::getScreenInfo(unsigned int s) const {
402 if (s < screenInfoList.size())
403 return &screenInfoList[s];
404 return (const ScreenInfo*) 0;
408 BGCCache *BaseDisplay::gcCache(void) const
410 if (! gccache) gccache = new BGCCache(this);
415 ScreenInfo::ScreenInfo(BaseDisplay *d, unsigned int num) {
419 root_window = RootWindow(basedisplay->getXDisplay(), screen_number);
420 depth = DefaultDepth(basedisplay->getXDisplay(), screen_number);
422 rect.setSize(WidthOfScreen(ScreenOfDisplay(basedisplay->getXDisplay(),
424 HeightOfScreen(ScreenOfDisplay(basedisplay->getXDisplay(),
427 // search for a TrueColor Visual... if we can't find one... we will use the
428 // default visual for the screen
429 XVisualInfo vinfo_template, *vinfo_return;
432 vinfo_template.screen = screen_number;
433 vinfo_template.c_class = TrueColor;
435 visual = (Visual *) 0;
437 vinfo_return = XGetVisualInfo(basedisplay->getXDisplay(),
438 VisualScreenMask | VisualClassMask,
439 &vinfo_template, &vinfo_nitems);
440 if (vinfo_return && vinfo_nitems > 0) {
441 for (int i = 0; i < vinfo_nitems; i++) {
442 if (depth < (vinfo_return + i)->depth) {
443 depth = (vinfo_return + i)->depth;
444 visual = (vinfo_return + i)->visual;
452 colormap = XCreateColormap(basedisplay->getXDisplay(), root_window,
455 visual = DefaultVisual(basedisplay->getXDisplay(), screen_number);
456 colormap = DefaultColormap(basedisplay->getXDisplay(), screen_number);
459 // get the default display string and strip the screen number
460 string default_string = DisplayString(basedisplay->getXDisplay());
461 const string::size_type pos = default_string.rfind(".");
462 if (pos != string::npos)
463 default_string.resize(pos);
465 std::ostringstream formatter;
466 formatter << "DISPLAY=" << default_string << '.' << screen_number;
467 display_string = formatter.str();