• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "modules/desktop_capture/linux/x_error_trap.h"
12 
13 #include <assert.h>
14 #include <stddef.h>
15 
16 #if defined(TOOLKIT_GTK)
17 #include <gdk/gdk.h>
18 #endif  // !defined(TOOLKIT_GTK)
19 
20 namespace webrtc {
21 
22 namespace {
23 
24 #if !defined(TOOLKIT_GTK)
25 
26 // TODO(sergeyu): This code is not thread safe. Fix it. Bug 2202.
27 static bool g_xserver_error_trap_enabled = false;
28 static int g_last_xserver_error_code = 0;
29 
XServerErrorHandler(Display * display,XErrorEvent * error_event)30 int XServerErrorHandler(Display* display, XErrorEvent* error_event) {
31   assert(g_xserver_error_trap_enabled);
32   g_last_xserver_error_code = error_event->error_code;
33   return 0;
34 }
35 
36 #endif  // !defined(TOOLKIT_GTK)
37 
38 }  // namespace
39 
XErrorTrap(Display * display)40 XErrorTrap::XErrorTrap(Display* display)
41     : original_error_handler_(NULL), enabled_(true) {
42 #if defined(TOOLKIT_GTK)
43   gdk_error_trap_push();
44 #else   // !defined(TOOLKIT_GTK)
45   assert(!g_xserver_error_trap_enabled);
46   original_error_handler_ = XSetErrorHandler(&XServerErrorHandler);
47   g_xserver_error_trap_enabled = true;
48   g_last_xserver_error_code = 0;
49 #endif  // !defined(TOOLKIT_GTK)
50 }
51 
GetLastErrorAndDisable()52 int XErrorTrap::GetLastErrorAndDisable() {
53   enabled_ = false;
54 #if defined(TOOLKIT_GTK)
55   return gdk_error_trap_push();
56 #else   // !defined(TOOLKIT_GTK)
57   assert(g_xserver_error_trap_enabled);
58   XSetErrorHandler(original_error_handler_);
59   g_xserver_error_trap_enabled = false;
60   return g_last_xserver_error_code;
61 #endif  // !defined(TOOLKIT_GTK)
62 }
63 
~XErrorTrap()64 XErrorTrap::~XErrorTrap() {
65   if (enabled_)
66     GetLastErrorAndDisable();
67 }
68 
69 }  // namespace webrtc
70