1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef mozilla_X11Util_h
9 #define mozilla_X11Util_h
11 // Utilities common to all X clients, regardless of UI toolkit.
13 #if defined(MOZ_WIDGET_GTK)
15 # include <gdk/gdkx.h>
16 #elif defined(MOZ_WIDGET_QT)
17 #include "gfxQtPlatform.h"
19 # include <X11/Xlib.h>
21 # error Unknown toolkit
24 #include "mozilla/Scoped.h"
32 * Return the default X Display created and used by the UI toolkit.
37 #if defined(MOZ_WIDGET_GTK)
38 return GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
39 #elif defined(MOZ_WIDGET_QT)
40 return gfxQtPlatform::GetXDisplay();
45 * Sets *aVisual to point to aDisplay's Visual struct corresponding to
46 * aVisualID, and *aDepth to its depth. When aVisualID is None, these are set
47 * to NULL and 0 respectively. Both out-parameter pointers are assumed
51 FindVisualAndDepth(Display
* aDisplay
, VisualID aVisualID
,
52 Visual
** aVisual
, int* aDepth
);
56 * Ensure that all X requests have been processed.
58 * This is similar to XSync, but doesn't need a round trip if the previous
59 * request was synchronous or if events have been received since the last
60 * request. Subsequent FinishX calls will be noops if there have been no
61 * intermediate requests.
65 FinishX(Display
* aDisplay
);
68 * Invoke XFree() on a pointer to memory allocated by Xlib (if the
69 * pointer is nonnull) when this class goes out of scope.
72 struct ScopedXFreePtrTraits
75 static T
*empty() { return NULL
; }
76 static void release(T
*ptr
) { if (ptr
!=NULL
) XFree(ptr
); }
78 SCOPED_TEMPLATE(ScopedXFree
, ScopedXFreePtrTraits
)
81 * On construction, set a graceful X error handler that doesn't crash the application and records X errors.
82 * On destruction, restore the X error handler to what it was before construction.
84 * The SyncAndGetError() method allows to know whether a X error occurred, optionally allows to get the full XErrorEvent,
85 * and resets the recorded X error state so that a single X error will be reported only once.
87 * Nesting is correctly handled: multiple nested ScopedXErrorHandler's don't interfere with each other's state. However,
88 * if SyncAndGetError is not called on the nested ScopedXErrorHandler, then any X errors caused by X calls made while the nested
89 * ScopedXErrorHandler was in place may then be caught by the other ScopedXErrorHandler. This is just a result of X being
90 * asynchronous and us not doing any implicit syncing: the only method in this class what causes syncing is SyncAndGetError().
92 * This class is not thread-safe at all. It is assumed that only one thread is using any ScopedXErrorHandler's. Given that it's
93 * not used on Mac, it should be easy to make it thread-safe by using thread-local storage with __thread.
95 class NS_GFX ScopedXErrorHandler
98 // trivial wrapper around XErrorEvent, just adding ctor initializing by zero.
105 memset(this, 0, sizeof(ErrorEvent
));
111 // this ScopedXErrorHandler's ErrorEvent object
114 // static pointer for use by the error handler
115 static ErrorEvent
* sXErrorPtr
;
117 // what to restore sXErrorPtr to on destruction
118 ErrorEvent
* mOldXErrorPtr
;
120 // what to restore the error handler to on destruction
121 int (*mOldErrorHandler
)(Display
*, XErrorEvent
*);
126 ErrorHandler(Display
*, XErrorEvent
*ev
);
128 ScopedXErrorHandler();
130 ~ScopedXErrorHandler();
132 /** \returns true if a X error occurred since the last time this method was called on this ScopedXErrorHandler object,
133 * or since the creation of this ScopedXErrorHandler object if this method was never called on it.
135 * \param ev this optional parameter, if set, will be filled with the XErrorEvent object. If multiple errors occurred,
136 * the first one will be returned.
138 bool SyncAndGetError(Display
*dpy
, XErrorEvent
*ev
= nullptr);
141 } // namespace mozilla
143 #endif // mozilla_X11Util_h