Bug 835381 - Update libnestegg to 38c83d9d4c0c5c84373aa285bd30094a12d6b6f6. r=kinetik
[gecko.git] / gfx / src / X11Util.h
blob8fd42f0dd9ca2383c885096a1e0b04802412d246
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
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)
14 # include <gdk/gdk.h>
15 # include <gdk/gdkx.h>
16 #elif defined(MOZ_WIDGET_QT)
17 #include "gfxQtPlatform.h"
18 #undef CursorShape
19 # include <X11/Xlib.h>
20 #else
21 # error Unknown toolkit
22 #endif
24 #include "mozilla/Scoped.h"
26 #include "gfxCore.h"
27 #include "nsDebug.h"
29 namespace mozilla {
31 /**
32 * Return the default X Display created and used by the UI toolkit.
34 inline Display*
35 DefaultXDisplay()
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();
41 #endif
44 /**
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
48 * non-NULL.
50 void
51 FindVisualAndDepth(Display* aDisplay, VisualID aVisualID,
52 Visual** aVisual, int* aDepth);
55 /**
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.
64 void
65 FinishX(Display* aDisplay);
67 /**
68 * Invoke XFree() on a pointer to memory allocated by Xlib (if the
69 * pointer is nonnull) when this class goes out of scope.
71 template <typename T>
72 struct ScopedXFreePtrTraits
74 typedef T *type;
75 static T *empty() { return NULL; }
76 static void release(T *ptr) { if (ptr!=NULL) XFree(ptr); }
78 SCOPED_TEMPLATE(ScopedXFree, ScopedXFreePtrTraits)
80 /**
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
97 public:
98 // trivial wrapper around XErrorEvent, just adding ctor initializing by zero.
99 struct ErrorEvent
101 XErrorEvent mError;
103 ErrorEvent()
105 memset(this, 0, sizeof(ErrorEvent));
109 private:
111 // this ScopedXErrorHandler's ErrorEvent object
112 ErrorEvent mXError;
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 *);
123 public:
125 static int
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