xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/linux/x11/x_error_trap.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_DESKTOP_CAPTURE_LINUX_X11_X_ERROR_TRAP_H_
12 #define MODULES_DESKTOP_CAPTURE_LINUX_X11_X_ERROR_TRAP_H_
13 
14 #include <X11/Xlib.h>
15 
16 #include "rtc_base/synchronization/mutex.h"
17 
18 namespace webrtc {
19 
20 // Helper class that registers an X Window error handler. Caller can use
21 // GetLastErrorAndDisable() to get the last error that was caught, if any.
22 class XErrorTrap {
23  public:
24   explicit XErrorTrap(Display* display);
25 
26   XErrorTrap(const XErrorTrap&) = delete;
27   XErrorTrap& operator=(const XErrorTrap&) = delete;
28 
29   ~XErrorTrap();
30 
31   // Returns the last error if one was caught, otherwise 0. Also unregisters the
32   // error handler and replaces it with `original_error_handler_`.
33   int GetLastErrorAndDisable();
34 
35  private:
36   MutexLock mutex_lock_;
37   XErrorHandler original_error_handler_ = nullptr;
38 };
39 
40 }  // namespace webrtc
41 
42 #endif  // MODULES_DESKTOP_CAPTURE_LINUX_X11_X_ERROR_TRAP_H_
43