kdecore Library: kxerrorhandler.cpp Source File (kdecore Library)

| # kdecore Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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
    00032 KXErrorHandler** KXErrorHandler::handlers = NULL;
    00033 int KXErrorHandler::pos = 0;
    00034 int KXErrorHandler::size = 0;
    00035
    00036 KXErrorHandler::KXErrorHandler( Display* dpy )
    00037     :   user_handler1( NULL ),
    00038         user_handler2( NULL ),
    00039         old_handler( XSetErrorHandler( handler_wrapper )),
    00040         first_request( XNextRequest( dpy )),
    00041         display( dpy ),
    00042         was_error( false )
    00043     {
    00044     addHandler();
    00045     }
    00046
    00047 KXErrorHandler::KXErrorHandler( bool (*handler)( int request, int error_code, unsigned long resource_id ), Display* dpy )
    00048     :   user_handler1( handler ),
    00049         user_handler2( NULL ),
    00050         old_handler( XSetErrorHandler( handler_wrapper )),
    00051         first_request( XNextRequest( dpy )),
    00052         display( dpy ),
    00053         was_error( false )
    00054     {
    00055     addHandler();
    00056     }
    00057
    00058 KXErrorHandler::KXErrorHandler( int (*handler)( Display*, XErrorEvent* ), Display* dpy )
    00059     :   user_handler1( NULL ),
    00060         user_handler2( handler ),
    00061         old_handler( XSetErrorHandler( handler_wrapper )),
    00062         first_request( XNextRequest( dpy )),
    00063         display( dpy ),
    00064         was_error( false )
    00065     {
    00066     addHandler();
    00067     }
    00068
    00069 KXErrorHandler::~KXErrorHandler()
    00070     {
    00071     XSetErrorHandler( old_handler );
    00072     assert( this == handlers[ pos - 1 ] ); // destroy in reverse order
    00073     --pos;
    00074     }
    00075
    00076 void KXErrorHandler::addHandler()
    00077     {
    00078     if( size == pos )
    00079         {
    00080         size += 16;
    00081         handlers = static_cast< KXErrorHandler** >( realloc( handlers, size * sizeof( KXErrorHandler* )));
    00082         }
    00083     handlers[ pos++ ] = this;
    00084     }
    00085
    00086 bool KXErrorHandler::error( bool sync ) const
    00087     {
    00088     if( sync )
    00089         XSync( display, False );
    00090     return was_error;
    00091     }
    00092
    00093 int KXErrorHandler::handler_wrapper( Display* dpy, XErrorEvent* e )
    00094     {
    00095     --pos;
    00096     int ret = handlers[ pos ]->handle( dpy, e );
    00097     ++pos;
    00098     return ret;
    00099     }
    00100
    00101 int KXErrorHandler::handle( Display* dpy, XErrorEvent* e )
    00102     {
    00103     if( dpy == display
    00104         && e->serial - first_request < 1000000000 ) // e->serial > first_request, with wrapping
    00105         { // it's for us
    00106         //qDebug( "Handling: %p", static_cast< void* >( this ));
    00107         if( user_handler1 != NULL )
    00108             was_error |= user_handler1( e->request_code, e->error_code, e->resourceid );
    00109         else if( user_handler2 != NULL )
    00110             was_error |= ( user_handler2( dpy, e ) != 0 );
    00111         else // no handler set, simply set that there was an error
    00112             was_error = true;
    00113         return 0;
    00114         }
    00115     //qDebug( "Going deeper: %p", static_cast< void* >( this ));
    00116     return old_handler( dpy, e );
    00117     }
    00118
    00119 #endif

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:12 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003