1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/logging.h"
6#include "ui/gfx/x/x11_error_tracker.h"
7
8#include "ui/gfx/x/x11_types.h"
9
10namespace {
11
12unsigned char g_x11_error_code = 0;
13static gfx::X11ErrorTracker* g_handler = NULL;
14
15int X11ErrorHandler(Display* display, XErrorEvent* error) {
16  g_x11_error_code = error->error_code;
17  return 0;
18}
19}
20
21namespace gfx {
22
23X11ErrorTracker::X11ErrorTracker() {
24  // This is a poor-man's check for incorrect usage. It disallows nested
25  // X11ErrorTracker instances on the same thread.
26  DCHECK(g_handler == NULL);
27  g_handler = this;
28  XSync(GetXDisplay(), False);
29  old_handler_ = XSetErrorHandler(X11ErrorHandler);
30  g_x11_error_code = 0;
31}
32
33X11ErrorTracker::~X11ErrorTracker() {
34  g_handler = NULL;
35  XSetErrorHandler(old_handler_);
36}
37
38bool X11ErrorTracker::FoundNewError() {
39  XSync(GetXDisplay(), False);
40  unsigned char error = g_x11_error_code;
41  g_x11_error_code = 0;
42  return error != 0;
43}
44
45}  // namespace gfx
46