kxerrorhandler.cpp
00001 /* 00002 00003 Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org> 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a 00006 copy of this software and associated documentation files (the "Software"), 00007 to deal in the Software without restriction, including without limitation 00008 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00009 and/or sell copies of the Software, and to permit persons to whom the 00010 Software is furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00018 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00020 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00021 DEALINGS IN THE SOFTWARE. 00022 00023 */ 00024 00025 #include <qwidget.h> 00026 #ifdef Q_WS_X11 //FIXME 00027 00028 #include "kxerrorhandler.h" 00029 #include <assert.h> 00030 #include <stdlib.h> 00031 #include <netwm_def.h> 00032 00033 KXErrorHandler** KXErrorHandler::handlers = NULL; 00034 int KXErrorHandler::pos = 0; 00035 int KXErrorHandler::size = 0; 00036 00037 KXErrorHandler::KXErrorHandler( Display* dpy ) 00038 : user_handler1( NULL ), 00039 user_handler2( NULL ), 00040 old_handler( XSetErrorHandler( handler_wrapper )), 00041 first_request( XNextRequest( dpy )), 00042 display( dpy ), 00043 was_error( false ) 00044 { 00045 addHandler(); 00046 } 00047 00048 KXErrorHandler::KXErrorHandler( bool (*handler)( int request, int error_code, unsigned long resource_id ), Display* dpy ) 00049 : user_handler1( handler ), 00050 user_handler2( NULL ), 00051 old_handler( XSetErrorHandler( handler_wrapper )), 00052 first_request( XNextRequest( dpy )), 00053 display( dpy ), 00054 was_error( false ) 00055 { 00056 addHandler(); 00057 } 00058 00059 KXErrorHandler::KXErrorHandler( int (*handler)( Display*, XErrorEvent* ), Display* dpy ) 00060 : user_handler1( NULL ), 00061 user_handler2( handler ), 00062 old_handler( XSetErrorHandler( handler_wrapper )), 00063 first_request( XNextRequest( dpy )), 00064 display( dpy ), 00065 was_error( false ) 00066 { 00067 addHandler(); 00068 } 00069 00070 KXErrorHandler::~KXErrorHandler() 00071 { 00072 XSetErrorHandler( old_handler ); 00073 assert( this == handlers[ pos - 1 ] ); // destroy in reverse order 00074 --pos; 00075 } 00076 00077 void KXErrorHandler::addHandler() 00078 { 00079 if( size == pos ) 00080 { 00081 size += 16; 00082 handlers = static_cast< KXErrorHandler** >( realloc( handlers, size * sizeof( KXErrorHandler* ))); 00083 } 00084 handlers[ pos++ ] = this; 00085 } 00086 00087 bool KXErrorHandler::error( bool sync ) const 00088 { 00089 if( sync ) 00090 XSync( display, False ); 00091 return was_error; 00092 } 00093 00094 int KXErrorHandler::handler_wrapper( Display* dpy, XErrorEvent* e ) 00095 { 00096 --pos; 00097 int ret = handlers[ pos ]->handle( dpy, e ); 00098 ++pos; 00099 return ret; 00100 } 00101 00102 int KXErrorHandler::handle( Display* dpy, XErrorEvent* e ) 00103 { 00104 if( dpy == display 00105 // e->serial >= first_request , compare like X timestamps to handle wrapping 00106 && NET::timestampCompare( e->serial, first_request ) >= 0 ) 00107 { // it's for us 00108 //qDebug( "Handling: %p", static_cast< void* >( this )); 00109 if( user_handler1 != NULL && user_handler1( e->request_code, e->error_code, e->resourceid )) 00110 was_error = true; 00111 if( user_handler2 != NULL && user_handler2( dpy, e ) != 0 ) 00112 was_error = true; 00113 else // no handler set, simply set that there was an error 00114 was_error = true; 00115 return 0; 00116 } 00117 //qDebug( "Going deeper: %p", static_cast< void* >( this )); 00118 return old_handler( dpy, e ); 00119 } 00120 00121 #endif