diff --git a/lib/kwinglobals.cpp b/lib/kwinglobals.cpp index 78a0887a9f..5c30fe4e48 100644 --- a/lib/kwinglobals.cpp +++ b/lib/kwinglobals.cpp @@ -23,6 +23,8 @@ along with this program. If not, see . #include #include +#include + #include #include @@ -65,70 +67,101 @@ int Extensions::render_version = 0; bool Extensions::has_glx = false; bool Extensions::has_sync = false; int Extensions::sync_event_base = 0; +// for fillExtensionsData() +const char* Extensions::data_extensions[ 32 ]; +int Extensions::data_nextensions; +int Extensions::data_opcodes[ 32 ]; +int Extensions::data_error_bases[ 32 ]; + +void Extensions::addData( const char* name ) + { + assert( data_nextensions < 32 ); + int opcode, event_base, error_base; + XQueryExtension( display(), name, &opcode, &event_base, &error_base ); + data_extensions[ data_nextensions ] = name; + data_opcodes[ data_nextensions ] = opcode; + data_error_bases[ data_nextensions ] = error_base; + ++data_nextensions; + } void Extensions::init() { - int dummy; + int event_base, error_base; + data_nextensions = 0; shape_version = 0; - if( XShapeQueryExtension( display(), &shape_event_base, &dummy )) + if( XShapeQueryExtension( display(), &shape_event_base, &error_base )) { int major, minor; if( XShapeQueryVersion( display(), &major, &minor )) + { shape_version = major * 0x10 + minor; + addData( "SHAPE" ); + } } #ifdef HAVE_XRANDR - has_randr = XRRQueryExtension( display(), &randr_event_base, &dummy ); + has_randr = XRRQueryExtension( display(), &randr_event_base, &error_base ); if( has_randr ) { int major, minor; XRRQueryVersion( display(), &major, &minor ); has_randr = ( major > 1 || ( major == 1 && minor >= 1 ) ); + addData( "RANDR" ); } #else has_randr = false; #endif #ifdef HAVE_XDAMAGE - has_damage = XDamageQueryExtension( display(), &damage_event_base, &dummy ); + has_damage = XDamageQueryExtension( display(), &damage_event_base, &error_base ); + if( has_damage ) + addData( "DAMAGE" ); #else has_damage = false; #endif composite_version = 0; #ifdef HAVE_XCOMPOSITE - if( XCompositeQueryExtension( display(), &dummy, &dummy )) + if( XCompositeQueryExtension( display(), &event_base, &error_base )) { int major = 0, minor = 0; XCompositeQueryVersion( display(), &major, &minor ); composite_version = major * 0x10 + minor; + addData( "Composite" ); } #endif fixes_version = 0; #ifdef HAVE_XFIXES - if( XFixesQueryExtension( display(), &dummy, &dummy )) + if( XFixesQueryExtension( display(), &event_base, &error_base )) { int major = 0, minor = 0; XFixesQueryVersion( display(), &major, &minor ); fixes_version = major * 0x10 + minor; + addData( "XFIXES" ); } #endif render_version = 0; #ifdef HAVE_XRENDER - if( XRenderQueryExtension( display(), &dummy, &dummy )) + if( XRenderQueryExtension( display(), &event_base, &error_base )) { int major = 0, minor = 0; XRenderQueryVersion( display(), &major, &minor ); render_version = major * 0x10 + minor; + addData( "RENDER" ); } #endif has_glx = false; #ifdef HAVE_OPENGL - has_glx = glXQueryExtension( display(), &dummy, &dummy ); + has_glx = glXQueryExtension( display(), &event_base, &error_base ); + if( has_glx ) + addData( "GLX" ); #endif #ifdef HAVE_XSYNC - if( XSyncQueryExtension( display(), &sync_event_base, &dummy )) + if( XSyncQueryExtension( display(), &sync_event_base, &error_base )) { int major = 0, minor = 0; if( XSyncInitialize( display(), &major, &minor )) + { has_sync = true; + addData( "SYNC" ); + } } #endif kDebug( 1212 ) << "Extensions: shape: 0x" << QString::number( shape_version, 16 ) @@ -137,6 +170,14 @@ void Extensions::init() << " fixes: 0x" << QString::number( fixes_version, 16 ) << endl; } +void Extensions::fillExtensionsData( const char**& extensions, int& nextensions, int*&opcodes, int*& error_bases ) + { + extensions = data_extensions; + nextensions = data_nextensions; + opcodes = data_opcodes; + error_bases = data_error_bases; + } + int Extensions::shapeNotifyEvent() { return shape_event_base + ShapeNotify; diff --git a/lib/kwinglobals.h b/lib/kwinglobals.h index 3990ccbf33..cf4923db42 100644 --- a/lib/kwinglobals.h +++ b/lib/kwinglobals.h @@ -128,7 +128,9 @@ class KWIN_EXPORT Extensions static bool glxAvailable() { return has_glx; } static bool syncAvailable() { return has_sync; } static int syncAlarmNotifyEvent(); + static void fillExtensionsData( const char**& extensions, int& nextensions, int*&majors, int*& error_bases ); private: + static void addData( const char* name ); static int shape_version; static int shape_event_base; static bool has_randr; @@ -141,6 +143,10 @@ class KWIN_EXPORT Extensions static bool has_glx; static bool has_sync; static int sync_event_base; + static const char* data_extensions[ 32 ]; + static int data_nextensions; + static int data_opcodes[ 32 ]; + static int data_error_bases[ 32 ]; }; } // namespace diff --git a/main.cpp b/main.cpp index 4e308a94e6..07f768fbfa 100644 --- a/main.cpp +++ b/main.cpp @@ -36,6 +36,7 @@ along with this program. If not, see . #include #include #include +#include #include #include @@ -72,10 +73,93 @@ static bool initting = false; // and -rdynamic in CXXFLAGS for kBacktrace() to work. static bool kwin_sync = false; +// this is copied from KXErrorHandler and modified to explicitly use known extensions +static QByteArray errorMessage( const XErrorEvent& event, Display* dpy ) + { // "Error: (), Request: (), Resource: " + QByteArray ret; + char tmp[ 256 ]; + char num[ 256 ]; + if( event.request_code < 128 ) // core request + { + XGetErrorText( dpy, event.error_code, tmp, 255 ); + if( char* paren = strchr( tmp, '(' )) // the explanation in parentheses just makes + *paren = '\0'; // it more verbose and is not really useful + // the various casts are to get overloads non-ambiguous :-/ + ret = QByteArray( "error: " ) + (const char*)tmp + '[' + QByteArray::number( event.error_code ) + ']'; + sprintf( num, "%d", event.request_code ); + XGetErrorDatabaseText( dpy, "XRequest", num, "", tmp, 256 ); + ret += QByteArray( ", request: " ) + (const char*)tmp + '[' + QByteArray::number( event.request_code ) + ']'; + if( event.resourceid != 0 ) + ret += QByteArray( ", resource: 0x" ) + QByteArray::number( (qlonglong)event.resourceid, 16 ); + } + else // extensions + { + // XGetErrorText() currently has a bug that makes it fail to find text + // for some errors (when error==error_base), also XGetErrorDatabaseText() + // requires the right extension name, so it is needed to get info about + // all extensions. However that is almost impossible: + // - Xlib itself has it, but in internal data. + // - Opening another X connection now can cause deadlock with server grabs. + // - Fetching it at startup means a bunch of roundtrips. + + // KWin here explicitly uses known extensions. + int nextensions; + const char** extensions; + int* majors; + int* error_bases; + Extensions::fillExtensionsData( extensions, nextensions, majors, error_bases ); + XGetErrorText( dpy, event.error_code, tmp, 255 ); + int index = -1; + int base = 0; + for( int i = 0; + i < nextensions; + ++i ) + if( error_bases[ i ] != 0 + && event.error_code >= error_bases[ i ] && ( index == -1 || error_bases[ i ] > base )) + { + index = i; + base = error_bases[ i ]; + } + if( tmp == QString::number( event.error_code )) // XGetErrorText() failed, + { // or it has a bug that causes not finding all errors, check ourselves + if( index != -1 ) + { + snprintf( num, 255, "%s.%d", extensions[ index ], event.error_code - base ); + XGetErrorDatabaseText( dpy, "XProtoError", num, "", tmp, 255 ); + } + else + strcpy( tmp, "" ); + } + if( char* paren = strchr( tmp, '(' )) + *paren = '\0'; + if( index != -1 ) + ret = QByteArray( "error: " ) + (const char*)tmp + '[' + (const char*)extensions[ index ] + + '+' + QByteArray::number( event.error_code - base ) + ']'; + else + ret = QByteArray( "error: " ) + (const char*)tmp + '[' + QByteArray::number( event.error_code ) + ']'; + tmp[ 0 ] = '\0'; + for( int i = 0; + i < nextensions; + ++i ) + if( majors[ i ] == event.request_code ) + { + snprintf( num, 255, "%s.%d", extensions[ i ], event.minor_code ); + XGetErrorDatabaseText( dpy, "XRequest", num, "", tmp, 255 ); + ret += QByteArray( ", request: " ) + (const char*)tmp + '[' + (const char*)extensions[ i ] + '+' + + QByteArray::number( event.minor_code ) + ']'; + } + if( tmp[ 0 ] == '\0' ) // not found??? + ret += QByteArray( ", request [" ) + QByteArray::number( event.request_code ) + ':' + + QByteArray::number( event.minor_code ) + ']'; + if( event.resourceid != 0 ) + ret += QByteArray( ", resource: 0x" ) + QByteArray::number( (qlonglong)event.resourceid, 16 ); + } + return ret; + } + static int x11ErrorHandler(Display *d, XErrorEvent *e) { - char msg[80], req[80], number[80]; bool ignore_badwindow = true; //maybe temporary if (initting && @@ -92,11 +176,8 @@ int x11ErrorHandler(Display *d, XErrorEvent *e) if (ignore_badwindow && (e->error_code == BadWindow || e->error_code == BadColor)) return 0; - XGetErrorText(d, e->error_code, msg, sizeof(msg)); - sprintf(number, "%d", e->request_code); - XGetErrorDatabaseText(d, "XRequest", number, "", req, sizeof(req)); - - fprintf(stderr, "kwin: %s(0x%lx): %s\n", req, e->resourceid, msg); +// fprintf( stderr, "kwin: X Error (%s)\n", KXErrorHandler::errorMessage( *e, d ).data()); + fprintf( stderr, "kwin: X Error (%s)\n", errorMessage( *e, d ).data()); if( kwin_sync ) kDebug() << kBacktrace();