• 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 "webrtc/modules/desktop_capture/x11/x_error_trap.h"
12 
13 #include <assert.h>
14 
15 #if defined(TOOLKIT_GTK)
16 #include <gdk/gdk.h>
17 #endif  // !defined(TOOLKIT_GTK)
18 
19 namespace webrtc {
20 
21 namespace {
22 
23 #if !defined(TOOLKIT_GTK)
24 
25 // TODO(sergeyu): This code is not thread safe. Fix it. Bug 2202.
26 static bool g_xserver_error_trap_enabled = false;
27 static int g_last_xserver_error_code = 0;
28 
XServerErrorHandler(Display * display,XErrorEvent * error_event)29 int XServerErrorHandler(Display* display, XErrorEvent* error_event) {
30   assert(g_xserver_error_trap_enabled);
31   g_last_xserver_error_code = error_event->error_code;
32   return 0;
33 }
34 
35 #endif  // !defined(TOOLKIT_GTK)
36 
37 }  // namespace
38 
XErrorTrap(Display * display)39 XErrorTrap::XErrorTrap(Display* display)
40     : original_error_handler_(NULL),
41       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