michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=8 et : michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_X11Util_h michael@0: #define mozilla_X11Util_h michael@0: michael@0: // Utilities common to all X clients, regardless of UI toolkit. michael@0: michael@0: #if defined(MOZ_WIDGET_GTK) michael@0: # include michael@0: # include michael@0: #elif defined(MOZ_WIDGET_QT) michael@0: #include "gfxQtPlatform.h" michael@0: #undef CursorShape michael@0: # include michael@0: #else michael@0: # error Unknown toolkit michael@0: #endif michael@0: michael@0: #include // for memset michael@0: #include "gfxCore.h" // for NS_GFX michael@0: #include "mozilla/Scoped.h" // for SCOPED_TEMPLATE michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * Return the default X Display created and used by the UI toolkit. michael@0: */ michael@0: inline Display* michael@0: DefaultXDisplay() michael@0: { michael@0: #if defined(MOZ_WIDGET_GTK) michael@0: return GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); michael@0: #elif defined(MOZ_WIDGET_QT) michael@0: return gfxQtPlatform::GetXDisplay(); michael@0: #endif michael@0: } michael@0: michael@0: /** michael@0: * Sets *aVisual to point to aDisplay's Visual struct corresponding to michael@0: * aVisualID, and *aDepth to its depth. When aVisualID is None, these are set michael@0: * to nullptr and 0 respectively. Both out-parameter pointers are assumed michael@0: * non-nullptr. michael@0: */ michael@0: void michael@0: FindVisualAndDepth(Display* aDisplay, VisualID aVisualID, michael@0: Visual** aVisual, int* aDepth); michael@0: michael@0: michael@0: /** michael@0: * Ensure that all X requests have been processed. michael@0: * michael@0: * This is similar to XSync, but doesn't need a round trip if the previous michael@0: * request was synchronous or if events have been received since the last michael@0: * request. Subsequent FinishX calls will be noops if there have been no michael@0: * intermediate requests. michael@0: */ michael@0: michael@0: void michael@0: FinishX(Display* aDisplay); michael@0: michael@0: /** michael@0: * Invoke XFree() on a pointer to memory allocated by Xlib (if the michael@0: * pointer is nonnull) when this class goes out of scope. michael@0: */ michael@0: template michael@0: struct ScopedXFreePtrTraits michael@0: { michael@0: typedef T *type; michael@0: static T *empty() { return nullptr; } michael@0: static void release(T *ptr) { if (ptr != nullptr) XFree(ptr); } michael@0: }; michael@0: SCOPED_TEMPLATE(ScopedXFree, ScopedXFreePtrTraits) michael@0: michael@0: /** michael@0: * On construction, set a graceful X error handler that doesn't crash the application and records X errors. michael@0: * On destruction, restore the X error handler to what it was before construction. michael@0: * michael@0: * The SyncAndGetError() method allows to know whether a X error occurred, optionally allows to get the full XErrorEvent, michael@0: * and resets the recorded X error state so that a single X error will be reported only once. michael@0: * michael@0: * Nesting is correctly handled: multiple nested ScopedXErrorHandler's don't interfere with each other's state. However, michael@0: * if SyncAndGetError is not called on the nested ScopedXErrorHandler, then any X errors caused by X calls made while the nested michael@0: * ScopedXErrorHandler was in place may then be caught by the other ScopedXErrorHandler. This is just a result of X being michael@0: * asynchronous and us not doing any implicit syncing: the only method in this class what causes syncing is SyncAndGetError(). michael@0: * michael@0: * This class is not thread-safe at all. It is assumed that only one thread is using any ScopedXErrorHandler's. Given that it's michael@0: * not used on Mac, it should be easy to make it thread-safe by using thread-local storage with __thread. michael@0: */ michael@0: class NS_GFX ScopedXErrorHandler michael@0: { michael@0: public: michael@0: // trivial wrapper around XErrorEvent, just adding ctor initializing by zero. michael@0: struct ErrorEvent michael@0: { michael@0: XErrorEvent mError; michael@0: michael@0: ErrorEvent() michael@0: { michael@0: memset(this, 0, sizeof(ErrorEvent)); michael@0: } michael@0: }; michael@0: michael@0: private: michael@0: michael@0: // this ScopedXErrorHandler's ErrorEvent object michael@0: ErrorEvent mXError; michael@0: michael@0: // static pointer for use by the error handler michael@0: static ErrorEvent* sXErrorPtr; michael@0: michael@0: // what to restore sXErrorPtr to on destruction michael@0: ErrorEvent* mOldXErrorPtr; michael@0: michael@0: // what to restore the error handler to on destruction michael@0: int (*mOldErrorHandler)(Display *, XErrorEvent *); michael@0: michael@0: public: michael@0: michael@0: static int michael@0: ErrorHandler(Display *, XErrorEvent *ev); michael@0: michael@0: ScopedXErrorHandler(); michael@0: michael@0: ~ScopedXErrorHandler(); michael@0: michael@0: /** \returns true if a X error occurred since the last time this method was called on this ScopedXErrorHandler object, michael@0: * or since the creation of this ScopedXErrorHandler object if this method was never called on it. michael@0: * michael@0: * \param ev this optional parameter, if set, will be filled with the XErrorEvent object. If multiple errors occurred, michael@0: * the first one will be returned. michael@0: */ michael@0: bool SyncAndGetError(Display *dpy, XErrorEvent *ev = nullptr); michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_X11Util_h