1// Copyright (c) 2012 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 "remoting/host/linux/x11_util.h"
6
7#include "base/bind.h"
8
9namespace remoting {
10
11static ScopedXErrorHandler* g_handler = NULL;
12
13ScopedXErrorHandler::ScopedXErrorHandler(const Handler& handler):
14    handler_(handler),
15    ok_(true) {
16  // This is a poor-man's check for incorrect usage. It doesn't handle the case
17  // where a mix of ScopedXErrorHandler and raw XSetErrorHandler calls are used,
18  // and it disallows nested ScopedXErrorHandlers on the same thread, despite
19  // these being perfectly safe.
20  DCHECK(g_handler == NULL);
21  g_handler = this;
22  previous_handler_ = XSetErrorHandler(HandleXErrors);
23}
24
25ScopedXErrorHandler::~ScopedXErrorHandler() {
26  g_handler = NULL;
27  XSetErrorHandler(previous_handler_);
28}
29
30namespace {
31void IgnoreXErrors(Display* display, XErrorEvent* error) {}
32}  // namespace
33
34// Static
35ScopedXErrorHandler::Handler ScopedXErrorHandler::Ignore() {
36  return base::Bind(IgnoreXErrors);
37}
38
39int ScopedXErrorHandler::HandleXErrors(Display* display, XErrorEvent* error) {
40  DCHECK(g_handler != NULL);
41  g_handler->ok_ = false;
42  g_handler->handler_.Run(display, error);
43  return 0;
44}
45
46
47ScopedXGrabServer::ScopedXGrabServer(Display* display)
48    : display_(display) {
49  XGrabServer(display_);
50}
51
52ScopedXGrabServer::~ScopedXGrabServer() {
53  XUngrabServer(display_);
54  XFlush(display_);
55}
56
57}  // namespace remoting
58