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#ifndef REMOTING_HOST_LINUX_X11_UTIL_H_
6#define REMOTING_HOST_LINUX_X11_UTIL_H_
7
8// Xlib.h defines XErrorEvent as an anonymous struct, so we can't forward-
9// declare it in this header. Since Xlib.h is not generally something you
10// should #include into arbitrary code, please refrain from #including this
11// header in another header.
12#include <X11/Xlib.h>
13
14#include "base/callback.h"
15
16namespace remoting {
17
18// Temporarily install an alternative handler for X errors. The default handler
19// exits the process, which is not what we want.
20//
21// Note that X error handlers are global, which means that this class is not
22// thread safe.
23class ScopedXErrorHandler {
24 public:
25  typedef base::Callback<void(Display*, XErrorEvent*)> Handler;
26
27  explicit ScopedXErrorHandler(const Handler& handler);
28  ~ScopedXErrorHandler();
29
30  // Return false if any X errors have been encountered in the scope of this
31  // handler.
32  bool ok() const { return ok_; }
33
34  // Basic handler that ignores X errors.
35  static Handler Ignore();
36
37 private:
38  static int HandleXErrors(Display* display, XErrorEvent* error);
39
40  Handler handler_;
41  int (*previous_handler_)(Display*, XErrorEvent*);
42  bool ok_;
43
44  DISALLOW_COPY_AND_ASSIGN(ScopedXErrorHandler);
45};
46
47
48// Grab/release the X server within a scope. This can help avoid race
49// conditions that would otherwise lead to X errors.
50class ScopedXGrabServer {
51 public:
52  ScopedXGrabServer(Display* display);
53  ~ScopedXGrabServer();
54
55 private:
56  Display* display_;
57
58  DISALLOW_COPY_AND_ASSIGN(ScopedXGrabServer);
59};
60
61}  // namespace remoting
62
63#endif  // REMOTING_HOST_LINUX_X11_UTIL_H_
64