1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_X11Util_h
8 #define mozilla_X11Util_h
10 // Utilities common to all X clients, regardless of UI toolkit.
12 #if defined(MOZ_WIDGET_GTK)
14 # include <gdk/gdkx.h>
15 # include "mozilla/WidgetUtilsGtk.h"
16 # include "X11UndefineNone.h"
18 # error Unknown toolkit
21 #include <string.h> // for memset
22 #include "mozilla/Scoped.h" // for SCOPED_TEMPLATE
27 * Return the default X Display created and used by the UI toolkit.
29 inline Display
* DefaultXDisplay() {
30 #if defined(MOZ_WIDGET_GTK)
31 GdkDisplay
* gdkDisplay
= gdk_display_get_default();
32 if (mozilla::widget::GdkIsX11Display(gdkDisplay
)) {
33 return GDK_DISPLAY_XDISPLAY(gdkDisplay
);
40 * Sets *aVisual to point to aDisplay's Visual struct corresponding to
41 * aVisualID, and *aDepth to its depth. When aVisualID is None, these are set
42 * to nullptr and 0 respectively. Both out-parameter pointers are assumed
45 void FindVisualAndDepth(Display
* aDisplay
, VisualID aVisualID
, Visual
** aVisual
,
49 * Ensure that all X requests have been processed.
51 * This is similar to XSync, but doesn't need a round trip if the previous
52 * request was synchronous or if events have been received since the last
53 * request. Subsequent FinishX calls will be noops if there have been no
54 * intermediate requests.
57 void FinishX(Display
* aDisplay
);
60 * Invoke XFree() on a pointer to memory allocated by Xlib (if the
61 * pointer is nonnull) when this class goes out of scope.
64 struct ScopedXFreePtrTraits
{
66 static T
* empty() { return nullptr; }
67 static void release(T
* ptr
) {
68 if (ptr
!= nullptr) XFree(ptr
);
71 SCOPED_TEMPLATE(ScopedXFree
, ScopedXFreePtrTraits
)
74 * On construction, set a graceful X error handler that doesn't crash the
75 * application and records X errors. On destruction, restore the X error handler
76 * to what it was before construction.
78 * The SyncAndGetError() method allows to know whether a X error occurred,
79 * optionally allows to get the full XErrorEvent, and resets the recorded X
80 * error state so that a single X error will be reported only once.
82 * Nesting is correctly handled: multiple nested ScopedXErrorHandler's don't
83 * interfere with each other's state. However, if SyncAndGetError is not called
84 * on the nested ScopedXErrorHandler, then any X errors caused by X calls made
85 * while the nested ScopedXErrorHandler was in place may then be caught by the
86 * other ScopedXErrorHandler. This is just a result of X being asynchronous and
87 * us not doing any implicit syncing: the only method in this class what causes
88 * syncing is SyncAndGetError().
90 * This class is not thread-safe at all. It is assumed that only one thread is
91 * using any ScopedXErrorHandler's. Given that it's not used on Mac, it should
92 * be easy to make it thread-safe by using thread-local storage with __thread.
94 class ScopedXErrorHandler
{
96 // trivial wrapper around XErrorEvent, just adding ctor initializing by zero.
100 ErrorEvent() { memset(this, 0, sizeof(ErrorEvent
)); }
104 // this ScopedXErrorHandler's ErrorEvent object
107 // static pointer for use by the error handler
108 static ErrorEvent
* sXErrorPtr
;
110 // what to restore sXErrorPtr to on destruction
111 ErrorEvent
* mOldXErrorPtr
;
113 // what to restore the error handler to on destruction
114 int (*mOldErrorHandler
)(Display
*, XErrorEvent
*);
117 static int ErrorHandler(Display
*, XErrorEvent
* ev
);
120 * @param aAllowOffMainThread whether to warn if used off main thread
122 explicit ScopedXErrorHandler(bool aAllowOffMainThread
= false);
124 ~ScopedXErrorHandler();
126 /** \returns true if a X error occurred since the last time this method was
127 * called on this ScopedXErrorHandler object, or since the creation of this
128 * ScopedXErrorHandler object if this method was never called on it.
130 * \param ev this optional parameter, if set, will be filled with the
131 * XErrorEvent object. If multiple errors occurred, the first one will be
134 bool SyncAndGetError(Display
* dpy
, XErrorEvent
* ev
= nullptr);
137 class OffMainThreadScopedXErrorHandler
: public ScopedXErrorHandler
{
139 OffMainThreadScopedXErrorHandler() : ScopedXErrorHandler(true) {}
142 } // namespace mozilla
144 #endif // mozilla_X11Util_h