File indexing completed on 2024-04-21 14:56:00

0001 /*
0002 
0003   Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org>
0004 
0005   Permission is hereby granted, free of charge, to any person obtaining a
0006   copy of this software and associated documentation files (the "Software"),
0007   to deal in the Software without restriction, including without limitation
0008   the rights to use, copy, modify, merge, publish, distribute, sublicense,
0009   and/or sell copies of the Software, and to permit persons to whom the
0010   Software is furnished to do so, subject to the following conditions:
0011 
0012   The above copyright notice and this permission notice shall be included in
0013   all copies or substantial portions of the Software.
0014 
0015   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0020   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0021   DEALINGS IN THE SOFTWARE.
0022 
0023 */
0024 
0025 #ifndef KXERRORHANDLER_H
0026 #define KXERRORHANDLER_H
0027 
0028 #include <QWidget>
0029 #include <QWidgetList>
0030 
0031 #include <kdelibs4support_export.h>
0032 #include <X11/Xlib.h>
0033 
0034 class KXErrorHandlerPrivate;
0035 /**
0036  * This class simplifies handling of X errors. It shouldn't be necessary to use
0037  * with Qt classes, as the toolkit should handle X errors itself, so this
0038  * class will be mainly used with direct Xlib usage, and some lowlevel classes
0039  * like NETWinInfo.
0040  *
0041  * The usual usage is to create a KXErrorHandler instance right before starting
0042  * operations that might cause X errors, and checking if there was an error
0043  * by calling error() after the operations are finished. The handlers
0044  * may be nested, but must be destroyed in reverse order they were created.
0045  *
0046  * There's no need to do X sync before creating an instance, every instance
0047  * will handle only errors for request issued after the instance was created.
0048  * Errors for older requests will be passed to previous error handler.
0049  * When checking for error by calling error() at the end, it is necessary
0050  * to sync with X, to catch all errors that were caused by requests issued
0051  * before the call to error(). This can be done by passing true to error()
0052  * to cause explicit XSync(), however, if the last X request needed a roundtrip
0053  * (e.g. XGetWindowAttributes(), XGetGeometry(), etc.), it is not required
0054  * to do an explicit sync.
0055  *
0056  * @author Lubos Lunak <l.lunak@kde.org>
0057  * @short Handler for X errors
0058  */
0059 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KXErrorHandler
0060 {
0061 public:
0062     /** This function simply wraps QX11Info::display(), to make sure the public interface doesn't require QtX11Extras */
0063     static Display *display();
0064     /**
0065      * Creates error handler that will set error flag after encountering
0066      * any X error.
0067      */
0068     KDELIBS4SUPPORT_DEPRECATED explicit KXErrorHandler(Display *dpy = display());
0069     /**
0070      * This constructor takes pointer to a function whose prototype matches
0071      * the one that's used with the XSetErrorHandler() Xlib function.
0072      * NOTE: For the error flag to be set, the function must return a non-zero
0073      * value.
0074      */
0075     KDELIBS4SUPPORT_DEPRECATED explicit KXErrorHandler(int (*handler)(Display *, XErrorEvent *), Display *dpy = display());
0076     /**
0077      * This constructor takes pointer to a function that will get request number,
0078      * error code number and resource id of the failed request, as provided
0079      * by XErrorEvent. If the function returns true, the error flag will be set.
0080      * @deprecated Use the variant with XErrorEvent.
0081      */
0082 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0083     KDELIBS4SUPPORT_DEPRECATED explicit KXErrorHandler(bool (*handler)(int request, int error_code, unsigned long resource_id), Display *dpy = display()) KDELIBS4SUPPORT_DEPRECATED;
0084 #endif
0085     /**
0086      * This function returns true if the error flag is set (i.e. no custom handler
0087      * function was used and there was any error, or the custom handler indicated
0088      * an error by its return value).
0089      *
0090      * @param sync if true, an explicit XSync() will be done. Not necessary
0091      *             when the last X request required a roundtrip.
0092      */
0093     bool error(bool sync) const;
0094     /**
0095      * This function returns the error event for the first X error that occurred.
0096      * The return value is useful only if error() returned true.
0097      * @since 4.0.1
0098      */
0099     XErrorEvent errorEvent() const;
0100     /**
0101      * Returns error message for the given error. The error message is not translated,
0102      * as it is meant for debugging.
0103      * @since 4.0.1
0104      */
0105     static QByteArray errorMessage(const XErrorEvent &e, Display *dpy = display());
0106     ~KXErrorHandler();
0107 private:
0108     void addHandler();
0109     int handle(Display *dpy, XErrorEvent *e);
0110     bool (*user_handler1)(int request, int error_code, unsigned long resource_id);
0111     int (*user_handler2)(Display *, XErrorEvent *);
0112     int (*old_handler)(Display *, XErrorEvent *);
0113     static int handler_wrapper(Display *, XErrorEvent *);
0114     static KXErrorHandler **handlers;
0115     static int pos;
0116     static int size;
0117     Q_DISABLE_COPY(KXErrorHandler)
0118     KXErrorHandlerPrivate *const d;
0119 };
0120 
0121 #endif