1/*
2
3 Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org>
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
22
23*/
24
25#ifndef KXERRORHANDLER_H
26#define KXERRORHANDLER_H
27
28#include <QtGui/QWidget>
29#include <QtGui/QWidgetList>
30
31#ifdef Q_WS_X11
32
33#include <kdeui_export.h>
34#include <QtGui/QX11Info>
35#include <X11/Xlib.h>
36
37class KXErrorHandlerPrivate;
38/**
39 * This class simplifies handling of X errors. It shouldn't be necessary to use
40 * with Qt classes, as the toolkit should handle X errors itself, so this
41 * class will be mainly used with direct Xlib usage, and some lowlevel classes
42 * like NETWinInfo.
43 *
44 * The usual usage is to create a KXErrorHandler instance right before starting
45 * operations that might cause X errors, and checking if there was an error
46 * by calling error() after the operations are finished. The handlers
47 * may be nested, but must be destroyed in reverse order they were created.
48 *
49 * There's no need to do X sync before creating an instance, every instance
50 * will handle only errors for request issued after the instance was created.
51 * Errors for older requests will be passed to previous error handler.
52 * When checking for error by calling error() at the end, it is necessary
53 * to sync with X, to catch all errors that were caused by requests issued
54 * before the call to error(). This can be done by passing true to error()
55 * to cause explicit XSync(), however, if the last X request needed a roundtrip
56 * (e.g. XGetWindowAttributes(), XGetGeometry(), etc.), it is not required
57 * to do an explicit sync.
58 *
59 * @author Lubos Lunak <l.lunak@kde.org>
60 * @short Handler for X errors
61 */
62class KDEUI_EXPORT KXErrorHandler
63 {
64 public:
65 /**
66 * Creates error handler that will set error flag after encountering
67 * any X error.
68 */
69 explicit KXErrorHandler( Display* dpy = QX11Info::display());
70 /**
71 * This constructor takes pointer to a function whose prototype matches
72 * the one that's used with the XSetErrorHandler() Xlib function.
73 * NOTE: For the error flag to be set, the function must return a non-zero
74 * value.
75 */
76 explicit KXErrorHandler( int (*handler)( Display*, XErrorEvent* ), Display* dpy = QX11Info::display());
77 /**
78 * This constructor takes pointer to a function that will get request number,
79 * error code number and resource id of the failed request, as provided
80 * by XErrorEvent. If the function returns true, the error flag will be set.
81 * @deprecated Use the variant with XErrorEvent.
82 */
83#ifndef KDE_NO_DEPRECATED
84 explicit KXErrorHandler( bool (*handler)( int request, int error_code, unsigned long resource_id ), Display* dpy = QX11Info::display()) KDE_DEPRECATED;
85#endif
86 /**
87 * This function returns true if the error flag is set (i.e. no custom handler
88 * function was used and there was any error, or the custom handler indicated
89 * an error by its return value).
90 *
91 * @param sync if true, an explicit XSync() will be done. Not necessary
92 * when the last X request required a roundtrip.
93 */
94 bool error( bool sync ) const;
95 /**
96 * This function returns the error event for the first X error that occurred.
97 * The return value is useful only if error() returned true.
98 * @since 4.0.1
99 */
100 XErrorEvent errorEvent() const;
101 /**
102 * Returns error message for the given error. The error message is not translated,
103 * as it is meant for debugging.
104 * @since 4.0.1
105 */
106 static QByteArray errorMessage( const XErrorEvent& e, Display* dpy = QX11Info::display());
107 ~KXErrorHandler();
108 private:
109 void addHandler();
110 int handle( Display* dpy, XErrorEvent* e );
111 bool (*user_handler1)( int request, int error_code, unsigned long resource_id );
112 int (*user_handler2)( Display*, XErrorEvent* );
113 int (*old_handler)( Display*, XErrorEvent* );
114 static int handler_wrapper( Display*, XErrorEvent* );
115 static KXErrorHandler** handlers;
116 static int pos;
117 static int size;
118 Q_DISABLE_COPY( KXErrorHandler )
119 KXErrorHandlerPrivate * const d;
120 };
121
122#endif // Q_WS_X11
123
124#endif
125