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/. */
7 
8 #include "X11Util.h"
9 #include "nsDebug.h"          // for NS_ASSERTION, etc
10 #include "MainThreadUtils.h"  // for NS_IsMainThread
11 
12 namespace mozilla {
13 
FindVisualAndDepth(Display * aDisplay,VisualID aVisualID,Visual ** aVisual,int * aDepth)14 void FindVisualAndDepth(Display* aDisplay, VisualID aVisualID, Visual** aVisual,
15                         int* aDepth) {
16   const Screen* screen = DefaultScreenOfDisplay(aDisplay);
17 
18   for (int d = 0; d < screen->ndepths; d++) {
19     Depth* d_info = &screen->depths[d];
20     for (int v = 0; v < d_info->nvisuals; v++) {
21       Visual* visual = &d_info->visuals[v];
22       if (visual->visualid == aVisualID) {
23         *aVisual = visual;
24         *aDepth = d_info->depth;
25         return;
26       }
27     }
28   }
29 
30   NS_ASSERTION(aVisualID == X11None, "VisualID not on Screen.");
31   *aVisual = nullptr;
32   *aDepth = 0;
33 }
34 
FinishX(Display * aDisplay)35 void FinishX(Display* aDisplay) {
36   unsigned long lastRequest = NextRequest(aDisplay) - 1;
37   if (lastRequest == LastKnownRequestProcessed(aDisplay)) return;
38 
39   XSync(aDisplay, X11False);
40 }
41 
42 ScopedXErrorHandler::ErrorEvent* ScopedXErrorHandler::sXErrorPtr;
43 
ErrorHandler(Display *,XErrorEvent * ev)44 int ScopedXErrorHandler::ErrorHandler(Display*, XErrorEvent* ev) {
45   // only record the error if no error was previously recorded.
46   // this means that in case of multiple errors, it's the first error that we
47   // report.
48   if (!sXErrorPtr->mError.error_code) sXErrorPtr->mError = *ev;
49   return 0;
50 }
51 
ScopedXErrorHandler(bool aAllowOffMainThread)52 ScopedXErrorHandler::ScopedXErrorHandler(bool aAllowOffMainThread) {
53   if (!aAllowOffMainThread) {
54     // Off main thread usage is not safe in general, but OMTC GL layers uses
55     // this with the main thread blocked, which makes it safe.
56     NS_WARNING_ASSERTION(
57         NS_IsMainThread(),
58         "ScopedXErrorHandler being called off main thread, may cause issues");
59   }
60   // let sXErrorPtr point to this object's mXError object, but don't reset this
61   // mXError object! think of the case of nested ScopedXErrorHandler's.
62   mOldXErrorPtr = sXErrorPtr;
63   sXErrorPtr = &mXError;
64   mOldErrorHandler = XSetErrorHandler(ErrorHandler);
65 }
66 
~ScopedXErrorHandler()67 ScopedXErrorHandler::~ScopedXErrorHandler() {
68   sXErrorPtr = mOldXErrorPtr;
69   XSetErrorHandler(mOldErrorHandler);
70 }
71 
SyncAndGetError(Display * dpy,XErrorEvent * ev)72 bool ScopedXErrorHandler::SyncAndGetError(Display* dpy, XErrorEvent* ev) {
73   FinishX(dpy);
74 
75   bool retval = mXError.mError.error_code != 0;
76   if (ev) *ev = mXError.mError;
77   mXError = ErrorEvent();  // reset
78   return retval;
79 }
80 
81 }  // namespace mozilla
82