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/. */
6
7 #ifndef mozilla_X11Util_h
8 #define mozilla_X11Util_h
9
10 // Utilities common to all X clients, regardless of UI toolkit.
11
12 #if defined(MOZ_WIDGET_GTK)
13 # include <gdk/gdk.h>
14 # include <gdk/gdkx.h>
15 # include "mozilla/WidgetUtilsGtk.h"
16 # include "X11UndefineNone.h"
17 #else
18 # error Unknown toolkit
19 #endif
20
21 #include <string.h> // for memset
22 #include "mozilla/Scoped.h" // for SCOPED_TEMPLATE
23
24 namespace mozilla {
25
26 /**
27 * Return the default X Display created and used by the UI toolkit.
28 */
DefaultXDisplay()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);
34 }
35 #endif
36 return nullptr;
37 }
38
39 /**
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
43 * non-nullptr.
44 */
45 void FindVisualAndDepth(Display* aDisplay, VisualID aVisualID, Visual** aVisual,
46 int* aDepth);
47
48 /**
49 * Ensure that all X requests have been processed.
50 *
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.
55 */
56
57 void FinishX(Display* aDisplay);
58
59 /**
60 * Invoke XFree() on a pointer to memory allocated by Xlib (if the
61 * pointer is nonnull) when this class goes out of scope.
62 */
63 template <typename T>
64 struct ScopedXFreePtrTraits {
65 typedef T* type;
emptyScopedXFreePtrTraits66 static T* empty() { return nullptr; }
releaseScopedXFreePtrTraits67 static void release(T* ptr) {
68 if (ptr != nullptr) XFree(ptr);
69 }
70 };
SCOPED_TEMPLATE(ScopedXFree,ScopedXFreePtrTraits)71 SCOPED_TEMPLATE(ScopedXFree, ScopedXFreePtrTraits)
72
73 /**
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.
77 *
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.
81 *
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().
89 *
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.
93 */
94 class ScopedXErrorHandler {
95 public:
96 // trivial wrapper around XErrorEvent, just adding ctor initializing by zero.
97 struct ErrorEvent {
98 XErrorEvent mError;
99
100 ErrorEvent() { memset(this, 0, sizeof(ErrorEvent)); }
101 };
102
103 private:
104 // this ScopedXErrorHandler's ErrorEvent object
105 ErrorEvent mXError;
106
107 // static pointer for use by the error handler
108 static ErrorEvent* sXErrorPtr;
109
110 // what to restore sXErrorPtr to on destruction
111 ErrorEvent* mOldXErrorPtr;
112
113 // what to restore the error handler to on destruction
114 int (*mOldErrorHandler)(Display*, XErrorEvent*);
115
116 public:
117 static int ErrorHandler(Display*, XErrorEvent* ev);
118
119 /**
120 * @param aAllowOffMainThread whether to warn if used off main thread
121 */
122 explicit ScopedXErrorHandler(bool aAllowOffMainThread = false);
123
124 ~ScopedXErrorHandler();
125
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.
129 *
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
132 * returned.
133 */
134 bool SyncAndGetError(Display* dpy, XErrorEvent* ev = nullptr);
135 };
136
137 class OffMainThreadScopedXErrorHandler : public ScopedXErrorHandler {
138 public:
OffMainThreadScopedXErrorHandler()139 OffMainThreadScopedXErrorHandler() : ScopedXErrorHandler(true) {}
140 };
141
142 } // namespace mozilla
143
144 #endif // mozilla_X11Util_h
145