X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=otk%2Fdisplay.cc;h=0f76a1cdbe667fed8a41dc3c9576e329c0a75ada;hb=00bc993775b916de76772bdc5e31618076194797;hp=459244283e4a99f59fa4aa50ca45346f5f0a802c;hpb=ace8c8896aa13a6fc4e489277cf9c96f49175322;p=chaz%2Fopenbox diff --git a/otk/display.cc b/otk/display.cc index 45924428..0f76a1cd 100644 --- a/otk/display.cc +++ b/otk/display.cc @@ -1,4 +1,4 @@ -// -*- mode: C++; indent-tabs-mode: nil; -*- +// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- #ifdef HAVE_CONFIG_H # include "../config.h" @@ -7,18 +7,27 @@ #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 XINERAMA +#include +#endif // XINERAMA + #ifdef HAVE_STDIO_H # include #endif // HAVE_STDIO_H -#ifdef HAVE_STDLIB_H -# include -#endif // HAVE_STDLIB_H - #ifdef HAVE_SIGNAL_H # include #endif // HAVE_SIGNAL_H @@ -39,23 +48,20 @@ extern "C" { namespace otk { -Display *OBDisplay::display = (Display*) 0; -bool OBDisplay::_shape = false; -int OBDisplay::_shape_event_basep; -bool OBDisplay::_xinerama = false; -int OBDisplay::_xinerama_event_basep; -unsigned int OBDisplay::_mask_list[8]; -OBDisplay::ScreenInfoList OBDisplay::_screenInfoList; -BGCCache *OBDisplay::_gccache = (BGCCache*) 0; +Display *display = (Display*) 0; - -int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e) +static int xerrorHandler(::Display *d, XErrorEvent *e) { #ifdef DEBUG char errtxt[128]; - XGetErrorText(d, e->error_code, errtxt, 128); - printf("X Error: %s\n", errtxt); + //if (e->error_code != BadWindow) + { + XGetErrorText(d, e->error_code, errtxt, 128); + printf("X Error: %s\n", errtxt); + if (e->error_code != BadWindow) + abort(); + } #else (void)d; (void)e; @@ -65,49 +71,66 @@ int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e) } -void OBDisplay::initialize(char *name) +Display::Display() + : _display(0), + _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), + _screenInfoList(), + _gccache((GCCache*) 0) { int junk; (void)junk; + display = this; + // Open the X display - if (!(display = XOpenDisplay(name))) { + if (!(_display = XOpenDisplay(NULL))) { 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")); +DISPLAY environment variable approriately.\n\n")); ::exit(1); } - if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) { + 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; - unsigned int NumLockMask = 0, ScrollLockMask = 0; - modmap = XGetModifierMapping(display); + modmap = XGetModifierMapping(_display); if (modmap && modmap->max_keypermod > 0) { const int mask_table[] = { ShiftMask, LockMask, ControlMask, Mod1Mask, @@ -118,16 +141,16 @@ line argument.\n\n")); // 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 (num_lock == modmap->modifiermap[cnt]) - NumLockMask = mask_table[cnt / modmap->max_keypermod]; + _num_lock_mask = mask_table[cnt / modmap->max_keypermod]; if (scroll_lock == modmap->modifiermap[cnt]) - ScrollLockMask = mask_table[cnt / modmap->max_keypermod]; + _scroll_lock_mask = mask_table[cnt / modmap->max_keypermod]; } } @@ -135,31 +158,64 @@ line argument.\n\n")); _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; + _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; // Get information on all the screens which are available. - _screenInfoList.reserve(ScreenCount(display)); - for (int i = 0; i < ScreenCount(display); ++i) + _screenInfoList.reserve(ScreenCount(_display)); + for (int i = 0; i < ScreenCount(_display); ++i) _screenInfoList.push_back(ScreenInfo(i)); - _gccache = new BGCCache(_screenInfoList.size()); + _gccache = new GCCache(_screenInfoList.size()); } -void OBDisplay::destroy() +Display::~Display() { delete _gccache; - XCloseDisplay(display); + while (_grab_count > 0) + ungrab(); + XCloseDisplay(_display); +} + + +const ScreenInfo* Display::screenInfo(int snum) +{ + assert(snum >= 0); + assert(snum < static_cast(_screenInfoList.size())); + return &_screenInfoList[snum]; +} + + +const ScreenInfo* Display::findScreen(Window root) +{ + ScreenInfoList::iterator it, end = _screenInfoList.end(); + for (it = _screenInfoList.begin(); it != end; ++it) + if (it->rootWindow() == root) + return &(*it); + return 0; } +void Display::grab() +{ + if (_grab_count == 0) + XGrabServer(_display); + _grab_count++; +} +void Display::ungrab() +{ + if (_grab_count == 0) return; + _grab_count--; + if (_grab_count == 0) + XUngrabServer(_display); +} @@ -174,15 +230,16 @@ void OBDisplay::destroy() * 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); } @@ -192,12 +249,32 @@ 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 Display::grabKey(unsigned int keycode, unsigned int modifiers, + Window grab_window, bool owner_events, + int pointer_mode, int keyboard_mode, + bool allow_scroll_lock) const +{ + unsigned int length = (allow_scroll_lock) ? 8 / 2: + 8; + for (size_t cnt = 0; cnt < length; ++cnt) + XGrabKey(_display, keycode, modifiers | _mask_list[cnt], + grab_window, owner_events, pointer_mode, keyboard_mode); +} + +void Display::ungrabKey(unsigned int keycode, unsigned int modifiers, + Window grab_window) const +{ + for (size_t cnt = 0; cnt < 8; ++cnt) + XUngrabKey(_display, keycode, modifiers | _mask_list[cnt], + grab_window); +} }