gfx/src/X11Util.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/src/X11Util.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=8 et :
     1.6 + */
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#include "X11Util.h"
    1.12 +#include "nsDebug.h"                    // for NS_ASSERTION, etc
    1.13 +
    1.14 +namespace mozilla {
    1.15 +
    1.16 +void
    1.17 +FindVisualAndDepth(Display* aDisplay, VisualID aVisualID,
    1.18 +                   Visual** aVisual, int* aDepth)
    1.19 +{
    1.20 +    const Screen* screen = DefaultScreenOfDisplay(aDisplay);
    1.21 +
    1.22 +    for (int d = 0; d < screen->ndepths; d++) {
    1.23 +        Depth *d_info = &screen->depths[d];
    1.24 +        for (int v = 0; v < d_info->nvisuals; v++) {
    1.25 +            Visual* visual = &d_info->visuals[v];
    1.26 +            if (visual->visualid == aVisualID) {
    1.27 +                *aVisual = visual;
    1.28 +                *aDepth = d_info->depth;
    1.29 +                return;
    1.30 +            }
    1.31 +        }
    1.32 +    }
    1.33 +
    1.34 +    NS_ASSERTION(aVisualID == None, "VisualID not on Screen.");
    1.35 +    *aVisual = nullptr;
    1.36 +    *aDepth = 0;
    1.37 +    return;
    1.38 +}
    1.39 +
    1.40 +void
    1.41 +FinishX(Display* aDisplay)
    1.42 +{
    1.43 +  unsigned long lastRequest = NextRequest(aDisplay) - 1;
    1.44 +  if (lastRequest == LastKnownRequestProcessed(aDisplay))
    1.45 +    return;
    1.46 +
    1.47 +  XSync(aDisplay, False);
    1.48 +}
    1.49 +
    1.50 +ScopedXErrorHandler::ErrorEvent* ScopedXErrorHandler::sXErrorPtr;
    1.51 +
    1.52 +int
    1.53 +ScopedXErrorHandler::ErrorHandler(Display *, XErrorEvent *ev)
    1.54 +{
    1.55 +    // only record the error if no error was previously recorded.
    1.56 +    // this means that in case of multiple errors, it's the first error that we report.
    1.57 +    if (!sXErrorPtr->mError.error_code)
    1.58 +      sXErrorPtr->mError = *ev;
    1.59 +    return 0;
    1.60 +}
    1.61 +
    1.62 +ScopedXErrorHandler::ScopedXErrorHandler()
    1.63 +{
    1.64 +    // let sXErrorPtr point to this object's mXError object, but don't reset this mXError object!
    1.65 +    // think of the case of nested ScopedXErrorHandler's.
    1.66 +    mOldXErrorPtr = sXErrorPtr;
    1.67 +    sXErrorPtr = &mXError;
    1.68 +    mOldErrorHandler = XSetErrorHandler(ErrorHandler);
    1.69 +}
    1.70 +
    1.71 +ScopedXErrorHandler::~ScopedXErrorHandler()
    1.72 +{
    1.73 +    sXErrorPtr = mOldXErrorPtr;
    1.74 +    XSetErrorHandler(mOldErrorHandler);
    1.75 +}
    1.76 +
    1.77 +bool
    1.78 +ScopedXErrorHandler::SyncAndGetError(Display *dpy, XErrorEvent *ev)
    1.79 +{
    1.80 +    FinishX(dpy);
    1.81 +
    1.82 +    bool retval = mXError.mError.error_code != 0;
    1.83 +    if (ev)
    1.84 +        *ev = mXError.mError;
    1.85 +    mXError = ErrorEvent(); // reset
    1.86 +    return retval;
    1.87 +}
    1.88 +
    1.89 +} // namespace mozilla

mercurial