shared_x_display.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef MODULES_DESKTOP_CAPTURE_LINUX_SHARED_X_DISPLAY_H_
  11. #define MODULES_DESKTOP_CAPTURE_LINUX_SHARED_X_DISPLAY_H_
  12. #include <map>
  13. #include <string>
  14. #include <vector>
  15. #include "api/ref_counted_base.h"
  16. #include "api/scoped_refptr.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/system/rtc_export.h"
  19. // Including Xlib.h will involve evil defines (Bool, Status, True, False), which
  20. // easily conflict with other headers.
  21. typedef struct _XDisplay Display;
  22. typedef union _XEvent XEvent;
  23. namespace webrtc {
  24. // A ref-counted object to store XDisplay connection.
  25. class RTC_EXPORT SharedXDisplay : public rtc::RefCountedBase {
  26. public:
  27. class XEventHandler {
  28. public:
  29. virtual ~XEventHandler() {}
  30. // Processes XEvent. Returns true if the event has been handled.
  31. virtual bool HandleXEvent(const XEvent& event) = 0;
  32. };
  33. // Takes ownership of |display|.
  34. explicit SharedXDisplay(Display* display);
  35. // Creates a new X11 Display for the |display_name|. NULL is returned if X11
  36. // connection failed. Equivalent to CreateDefault() when |display_name| is
  37. // empty.
  38. static rtc::scoped_refptr<SharedXDisplay> Create(
  39. const std::string& display_name);
  40. // Creates X11 Display connection for the default display (e.g. specified in
  41. // DISPLAY). NULL is returned if X11 connection failed.
  42. static rtc::scoped_refptr<SharedXDisplay> CreateDefault();
  43. Display* display() { return display_; }
  44. // Adds a new event |handler| for XEvent's of |type|.
  45. void AddEventHandler(int type, XEventHandler* handler);
  46. // Removes event |handler| added using |AddEventHandler|. Doesn't do anything
  47. // if |handler| is not registered.
  48. void RemoveEventHandler(int type, XEventHandler* handler);
  49. // Processes pending XEvents, calling corresponding event handlers.
  50. void ProcessPendingXEvents();
  51. void IgnoreXServerGrabs();
  52. protected:
  53. ~SharedXDisplay() override;
  54. private:
  55. typedef std::map<int, std::vector<XEventHandler*> > EventHandlersMap;
  56. Display* display_;
  57. EventHandlersMap event_handlers_;
  58. RTC_DISALLOW_COPY_AND_ASSIGN(SharedXDisplay);
  59. };
  60. } // namespace webrtc
  61. #endif // MODULES_DESKTOP_CAPTURE_LINUX_SHARED_X_DISPLAY_H_