Blame gfx/src/X11Util.h

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