Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / remoting / host / linux / x11_util.h
blob03d382141bdca2a06ff0ac1c9236ac25f50a0e46
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.
5 #ifndef REMOTING_HOST_LINUX_X11_UTIL_H_
6 #define REMOTING_HOST_LINUX_X11_UTIL_H_
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>
14 #include "base/callback.h"
16 namespace remoting {
18 // Temporarily install an alternative handler for X errors. The default handler
19 // exits the process, which is not what we want.
21 // Note that X error handlers are global, which means that this class is not
22 // thread safe.
23 class ScopedXErrorHandler {
24 public:
25 typedef base::Callback<void(Display*, XErrorEvent*)> Handler;
27 explicit ScopedXErrorHandler(const Handler& handler);
28 ~ScopedXErrorHandler();
30 // Return false if any X errors have been encountered in the scope of this
31 // handler.
32 bool ok() const { return ok_; }
34 // Basic handler that ignores X errors.
35 static Handler Ignore();
37 private:
38 static int HandleXErrors(Display* display, XErrorEvent* error);
40 Handler handler_;
41 int (*previous_handler_)(Display*, XErrorEvent*);
42 bool ok_;
44 DISALLOW_COPY_AND_ASSIGN(ScopedXErrorHandler);
48 // Grab/release the X server within a scope. This can help avoid race
49 // conditions that would otherwise lead to X errors.
50 class ScopedXGrabServer {
51 public:
52 ScopedXGrabServer(Display* display);
53 ~ScopedXGrabServer();
55 private:
56 Display* display_;
58 DISALLOW_COPY_AND_ASSIGN(ScopedXGrabServer);
61 } // namespace remoting
63 #endif // REMOTING_HOST_LINUX_X11_UTIL_H_