Bumping manifests a=b2g-bump
[gecko.git] / gfx / src / X11Util.cpp
blobdd1f025d4d962223fb29700f6aa7a4bfbbddc597
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 #include "X11Util.h"
9 #include "nsDebug.h" // for NS_ASSERTION, etc
10 #include "MainThreadUtils.h" // for NS_IsMainThread
12 namespace mozilla {
14 void
15 FindVisualAndDepth(Display* aDisplay, VisualID aVisualID,
16 Visual** aVisual, int* aDepth)
18 const Screen* screen = DefaultScreenOfDisplay(aDisplay);
20 for (int d = 0; d < screen->ndepths; d++) {
21 Depth *d_info = &screen->depths[d];
22 for (int v = 0; v < d_info->nvisuals; v++) {
23 Visual* visual = &d_info->visuals[v];
24 if (visual->visualid == aVisualID) {
25 *aVisual = visual;
26 *aDepth = d_info->depth;
27 return;
32 NS_ASSERTION(aVisualID == None, "VisualID not on Screen.");
33 *aVisual = nullptr;
34 *aDepth = 0;
35 return;
38 void
39 FinishX(Display* aDisplay)
41 unsigned long lastRequest = NextRequest(aDisplay) - 1;
42 if (lastRequest == LastKnownRequestProcessed(aDisplay))
43 return;
45 XSync(aDisplay, False);
48 ScopedXErrorHandler::ErrorEvent* ScopedXErrorHandler::sXErrorPtr;
50 int
51 ScopedXErrorHandler::ErrorHandler(Display *, XErrorEvent *ev)
53 // only record the error if no error was previously recorded.
54 // this means that in case of multiple errors, it's the first error that we report.
55 if (!sXErrorPtr->mError.error_code)
56 sXErrorPtr->mError = *ev;
57 return 0;
60 ScopedXErrorHandler::ScopedXErrorHandler()
62 // Off main thread usage is not safe in general, but OMTC GL layers uses this
63 // with the main thread blocked, which makes it safe.
64 NS_WARN_IF_FALSE(NS_IsMainThread(), "ScopedXErrorHandler being called off main thread, may cause issues");
65 // let sXErrorPtr point to this object's mXError object, but don't reset this mXError object!
66 // think of the case of nested ScopedXErrorHandler's.
67 mOldXErrorPtr = sXErrorPtr;
68 sXErrorPtr = &mXError;
69 mOldErrorHandler = XSetErrorHandler(ErrorHandler);
72 ScopedXErrorHandler::~ScopedXErrorHandler()
74 sXErrorPtr = mOldXErrorPtr;
75 XSetErrorHandler(mOldErrorHandler);
78 bool
79 ScopedXErrorHandler::SyncAndGetError(Display *dpy, XErrorEvent *ev)
81 FinishX(dpy);
83 bool retval = mXError.mError.error_code != 0;
84 if (ev)
85 *ev = mXError.mError;
86 mXError = ErrorEvent(); // reset
87 return retval;
90 } // namespace mozilla