X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=otk%2Fdisplay.cc;h=d4580a0e4e4d8430b4ba6d8301a9b6851a73c843;hb=648c55b829e09c66222a9bbf08d10434622feae2;hp=32674be9736787564ebb6799f33976a325741aca;hpb=a2de94e91e1c0e7775c97745ae11d14e5c5f5659;p=chaz%2Fopenbox diff --git a/otk/display.cc b/otk/display.cc index 32674be9..d4580a0e 100644 --- a/otk/display.cc +++ b/otk/display.cc @@ -1,27 +1,24 @@ // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- -#ifdef HAVE_CONFIG_H -# include "../config.h" -#endif +#include "config.h" #include "display.hh" -#include "screeninfo.hh" -#include "gccache.hh" +#include "util.hh" extern "C" { #include +#ifdef XKB +#include +#endif // XKB + #ifdef SHAPE #include #endif // SHAPE -#ifdef HAVE_STDIO_H -# include -#endif // HAVE_STDIO_H - -#ifdef HAVE_STDLIB_H -# include -#endif // HAVE_STDLIB_H +#ifdef XINERAMA +#include +#endif // XINERAMA #ifdef HAVE_SIGNAL_H # include @@ -36,172 +33,209 @@ extern "C" { # include #endif // HAVE_UNISTD_H -#include "gettext.h" +#include "../src/gettext.h" #define _(str) gettext(str) } -namespace otk { +#include +namespace otk { -Display *OBDisplay::display = (Display*) 0; -bool OBDisplay::_shape = false; -int OBDisplay::_shape_event_basep = 0; -bool OBDisplay::_xinerama = false; -int OBDisplay::_xinerama_event_basep = 0; -unsigned int OBDisplay::_mask_list[8]; -unsigned int OBDisplay::_scrollLockMask = 0; -unsigned int OBDisplay::_numLockMask = 0; -OBDisplay::ScreenInfoList OBDisplay::_screenInfoList; -BGCCache *OBDisplay::_gccache = (BGCCache*) 0; -int OBDisplay::_grab_count = 0; +Display *display = (Display*) 0; -int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e) +static int xerrorHandler(::Display *d, XErrorEvent *e) { + if (!display->ignoreErrors()) { #ifdef DEBUG - char errtxt[128]; - - //if (e->error_code != BadWindow) - { - XGetErrorText(d, e->error_code, errtxt, 128); - printf("X Error: %s\n", errtxt); -// if (e->error_code != BadWindow) -// abort(); - } + char errtxt[128]; + + //if (e->error_code != BadWindow) + { + XGetErrorText(d, e->error_code, errtxt, 127); + printf("X Error: %s\n", errtxt); + if (e->error_code != BadWindow) + abort(); + } #else - (void)d; - (void)e; + (void)d; + (void)e; #endif - + } return false; } -void OBDisplay::initialize(char *name) +Display::Display(::Display *d) + : _display(d), + _xkb(false), + _xkb_event_basep(0), + _shape(false), + _shape_event_basep(0), + _xinerama(false), + _xinerama_event_basep(0), + _mask_list(), + _num_lock_mask(0), + _scroll_lock_mask(0), + _grab_count(0) { int junk; (void)junk; - // Open the X display - if (!(display = XOpenDisplay(name))) { - printf(_("Unable to open connection to the X server. Please set the \n\ -DISPLAY environment variable approriately, or use the '-display' command \n\ -line argument.\n\n")); - ::exit(1); - } - if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) { + assert(_display); + + display = this; + + if (fcntl(ConnectionNumber(_display), F_SETFD, 1) == -1) { printf(_("Couldn't mark display connection as close-on-exec.\n\n")); ::exit(1); } - + if (!XSupportsLocale()) + printf(_("X server does not support locale.\n")); + if (!XSetLocaleModifiers("")) + printf(_("Cannot set locale modifiers for the X server.\n")); + // set our error handler for X errors XSetErrorHandler(xerrorHandler); // set the DISPLAY environment variable for any lauched children, to the // display we're using, so they open in the right place. - // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display); - if (putenv(const_cast((std::string("DISPLAY=") + - DisplayString(display)).c_str()))) { - printf(_("warning: couldn't set environment variable 'DISPLAY'\n")); - perror("putenv()"); - } + putenv(std::string("DISPLAY=") + DisplayString(_display)); // find the availability of X extensions we like to use +#ifdef XKB + _xkb = XkbQueryExtension(_display, &junk, &_xkb_event_basep, &junk, NULL, + NULL); +#endif + #ifdef SHAPE - _shape = XShapeQueryExtension(display, &_shape_event_basep, &junk); + _shape = XShapeQueryExtension(_display, &_shape_event_basep, &junk); #endif #ifdef XINERAMA - _xinerama = XineramaQueryExtension(display, &_xinerama_event_basep, &junk); + _xinerama = XineramaQueryExtension(_display, &_xinerama_event_basep, &junk); #endif // XINERAMA // get lock masks that are defined by the display (not constant) - XModifierKeymap *modmap; - - modmap = XGetModifierMapping(display); - if (modmap && modmap->max_keypermod > 0) { + _modmap = XGetModifierMapping(_display); + assert(_modmap); + if (_modmap && _modmap->max_keypermod > 0) { const int mask_table[] = { ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask }; const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) * - modmap->max_keypermod; + _modmap->max_keypermod; // get the values of the keyboard lock modifiers // Note: Caps lock is not retrieved the same way as Scroll and Num lock // since it doesn't need to be. - const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock); - const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock); + const KeyCode num_lock = XKeysymToKeycode(_display, XK_Num_Lock); + const KeyCode scroll_lock = XKeysymToKeycode(_display, XK_Scroll_Lock); for (size_t cnt = 0; cnt < size; ++cnt) { - if (! modmap->modifiermap[cnt]) continue; + if (! _modmap->modifiermap[cnt]) continue; - if (num_lock == modmap->modifiermap[cnt]) - _numLockMask = mask_table[cnt / modmap->max_keypermod]; - if (scroll_lock == modmap->modifiermap[cnt]) - _scrollLockMask = mask_table[cnt / modmap->max_keypermod]; + if (num_lock == _modmap->modifiermap[cnt]) + _num_lock_mask = mask_table[cnt / _modmap->max_keypermod]; + if (scroll_lock == _modmap->modifiermap[cnt]) + _scroll_lock_mask = mask_table[cnt / _modmap->max_keypermod]; } } - if (modmap) XFreeModifiermap(modmap); - _mask_list[0] = 0; _mask_list[1] = LockMask; - _mask_list[2] = _numLockMask; - _mask_list[3] = LockMask | _numLockMask; - _mask_list[4] = _scrollLockMask; - _mask_list[5] = _scrollLockMask | LockMask; - _mask_list[6] = _scrollLockMask | _numLockMask; - _mask_list[7] = _scrollLockMask | LockMask | _numLockMask; - - // Get information on all the screens which are available. - _screenInfoList.reserve(ScreenCount(display)); - for (int i = 0; i < ScreenCount(display); ++i) - _screenInfoList.push_back(ScreenInfo(i)); - - _gccache = new BGCCache(_screenInfoList.size()); + _mask_list[2] = _num_lock_mask; + _mask_list[3] = LockMask | _num_lock_mask; + _mask_list[4] = _scroll_lock_mask; + _mask_list[5] = _scroll_lock_mask | LockMask; + _mask_list[6] = _scroll_lock_mask | _num_lock_mask; + _mask_list[7] = _scroll_lock_mask | LockMask | _num_lock_mask; + + /* + If the default depth is at least 8 we will use that, + otherwise we try to find the largest TrueColor visual. + Preference is given to 24 bit over larger depths if 24 bit is an option. + */ + + int screen = DefaultScreen(_display); + _depth = DefaultDepth(_display, screen); + _visual = DefaultVisual(_display, screen); + _colormap = DefaultColormap(_display, screen); + + if (_depth < 8) { + // search for a TrueColor Visual... if we can't find one... + // we will use the default visual for the screen + XVisualInfo vinfo_template, *vinfo_return; + int vinfo_nitems; + int best = -1; + + vinfo_template.screen = screen; + vinfo_template.c_class = TrueColor; + + vinfo_return = XGetVisualInfo(_display, + VisualScreenMask | VisualClassMask, + &vinfo_template, &vinfo_nitems); + if (vinfo_return) { + int max_depth = 1; + for (int i = 0; i < vinfo_nitems; ++i) { + if (vinfo_return[i].depth > max_depth) { + if (max_depth == 24 && vinfo_return[i].depth > 24) + break; // prefer 24 bit over 32 + max_depth = vinfo_return[i].depth; + best = i; + } + } + if (max_depth < _depth) best = -1; + } + + if (best != -1) { + _depth = vinfo_return[best].depth; + _visual = vinfo_return[best].visual; + _colormap = XCreateColormap(_display, RootWindow(_display, screen), + _visual, AllocNone); + } + + XFree(vinfo_return); + } } -void OBDisplay::destroy() +Display::~Display() { - delete _gccache; while (_grab_count > 0) ungrab(); - XCloseDisplay(display); -} - -const ScreenInfo* OBDisplay::screenInfo(int snum) { - assert(snum >= 0); - assert(snum < static_cast(_screenInfoList.size())); - return &_screenInfoList[snum]; + XFreeModifiermap(_modmap); + + XCloseDisplay(_display); } -const ScreenInfo* OBDisplay::findScreen(Window root) +void Display::setIgnoreErrors(bool t) { - ScreenInfoList::iterator it, end = _screenInfoList.end(); - for (it = _screenInfoList.begin(); it != end; ++it) - if (it->rootWindow() == root) - return &(*it); - return 0; + // sync up so that anything already sent is/isn't ignored! + XSync(_display, false); + _ignore_errors = t; } - -void OBDisplay::grab() +void Display::grab() { - if (_grab_count == 0) - XGrabServer(display); + if (_grab_count == 0) { + XGrabServer(_display); + XSync(_display, false); // make sure it kicks in + } _grab_count++; } -void OBDisplay::ungrab() +void Display::ungrab() { if (_grab_count == 0) return; _grab_count--; - if (_grab_count == 0) - XUngrabServer(display); + if (_grab_count == 0) { + XUngrabServer(_display); + XFlush(_display); // ungrab as soon as possible + } } @@ -217,15 +251,16 @@ void OBDisplay::ungrab() * if allow_scroll_lock is true then only the top half of the lock mask * table is used and scroll lock is ignored. This value defaults to false. */ -void OBDisplay::grabButton(unsigned int button, unsigned int modifiers, +void Display::grabButton(unsigned int button, unsigned int modifiers, Window grab_window, bool owner_events, unsigned int event_mask, int pointer_mode, int keyboard_mode, Window confine_to, - Cursor cursor, bool allow_scroll_lock) { + Cursor cursor, bool allow_scroll_lock) const +{ unsigned int length = (allow_scroll_lock) ? 8 / 2: 8; for (size_t cnt = 0; cnt < length; ++cnt) - XGrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt], + XGrabButton(_display, button, modifiers | _mask_list[cnt], grab_window, owner_events, event_mask, pointer_mode, keyboard_mode, confine_to, cursor); } @@ -235,31 +270,37 @@ void OBDisplay::grabButton(unsigned int button, unsigned int modifiers, * Releases the grab on a button, and ungrabs all possible combinations of the * keyboard lock keys. */ -void OBDisplay::ungrabButton(unsigned int button, unsigned int modifiers, - Window grab_window) { +void Display::ungrabButton(unsigned int button, unsigned int modifiers, + Window grab_window) const +{ for (size_t cnt = 0; cnt < 8; ++cnt) - XUngrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt], + XUngrabButton(_display, button, modifiers | _mask_list[cnt], grab_window); } -void OBDisplay::grabKey(unsigned int keycode, unsigned int modifiers, +void Display::grabKey(unsigned int keycode, unsigned int modifiers, Window grab_window, bool owner_events, int pointer_mode, int keyboard_mode, - bool allow_scroll_lock) + bool allow_scroll_lock) const { unsigned int length = (allow_scroll_lock) ? 8 / 2: 8; for (size_t cnt = 0; cnt < length; ++cnt) - XGrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt], + XGrabKey(_display, keycode, modifiers | _mask_list[cnt], grab_window, owner_events, pointer_mode, keyboard_mode); } -void OBDisplay::ungrabKey(unsigned int keycode, unsigned int modifiers, - Window grab_window) +void Display::ungrabKey(unsigned int keycode, unsigned int modifiers, + Window grab_window) const { for (size_t cnt = 0; cnt < 8; ++cnt) - XUngrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt], + XUngrabKey(_display, keycode, modifiers | _mask_list[cnt], grab_window); } +void Display::ungrabAllKeys(Window grab_window) const +{ + XUngrabKey(_display, AnyKey, AnyModifier, grab_window); +} + }