• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "modules/desktop_capture/linux/x11/shared_x_display.h"
12 
13 #include <X11/Xlib.h>
14 #include <X11/extensions/XTest.h>
15 
16 #include <algorithm>
17 
18 #include "absl/strings/string_view.h"
19 #include "rtc_base/checks.h"
20 #include "rtc_base/logging.h"
21 
22 namespace webrtc {
23 
SharedXDisplay(Display * display)24 SharedXDisplay::SharedXDisplay(Display* display) : display_(display) {
25   RTC_DCHECK(display_);
26 }
27 
~SharedXDisplay()28 SharedXDisplay::~SharedXDisplay() {
29   RTC_DCHECK(event_handlers_.empty());
30   XCloseDisplay(display_);
31 }
32 
33 // static
Create(absl::string_view display_name)34 rtc::scoped_refptr<SharedXDisplay> SharedXDisplay::Create(
35     absl::string_view display_name) {
36   Display* display = XOpenDisplay(
37       display_name.empty() ? NULL : std::string(display_name).c_str());
38   if (!display) {
39     RTC_LOG(LS_ERROR) << "Unable to open display";
40     return nullptr;
41   }
42   return rtc::scoped_refptr<SharedXDisplay>(new SharedXDisplay(display));
43 }
44 
45 // static
CreateDefault()46 rtc::scoped_refptr<SharedXDisplay> SharedXDisplay::CreateDefault() {
47   return Create(std::string());
48 }
49 
AddEventHandler(int type,XEventHandler * handler)50 void SharedXDisplay::AddEventHandler(int type, XEventHandler* handler) {
51   MutexLock lock(&mutex_);
52   event_handlers_[type].push_back(handler);
53 }
54 
RemoveEventHandler(int type,XEventHandler * handler)55 void SharedXDisplay::RemoveEventHandler(int type, XEventHandler* handler) {
56   MutexLock lock(&mutex_);
57   EventHandlersMap::iterator handlers = event_handlers_.find(type);
58   if (handlers == event_handlers_.end())
59     return;
60 
61   std::vector<XEventHandler*>::iterator new_end =
62       std::remove(handlers->second.begin(), handlers->second.end(), handler);
63   handlers->second.erase(new_end, handlers->second.end());
64 
65   // Check if no handlers left for this event.
66   if (handlers->second.empty())
67     event_handlers_.erase(handlers);
68 }
69 
ProcessPendingXEvents()70 void SharedXDisplay::ProcessPendingXEvents() {
71   // Hold reference to `this` to prevent it from being destroyed while
72   // processing events.
73   rtc::scoped_refptr<SharedXDisplay> self(this);
74 
75   // Protect access to `event_handlers_` after incrementing the refcount for
76   // `this` to ensure the instance is still valid when the lock is acquired.
77   MutexLock lock(&mutex_);
78 
79   // Find the number of events that are outstanding "now."  We don't just loop
80   // on XPending because we want to guarantee this terminates.
81   int events_to_process = XPending(display());
82   XEvent e;
83 
84   for (int i = 0; i < events_to_process; i++) {
85     XNextEvent(display(), &e);
86     EventHandlersMap::iterator handlers = event_handlers_.find(e.type);
87     if (handlers == event_handlers_.end())
88       continue;
89     for (std::vector<XEventHandler*>::iterator it = handlers->second.begin();
90          it != handlers->second.end(); ++it) {
91       if ((*it)->HandleXEvent(e))
92         break;
93     }
94   }
95 }
96 
IgnoreXServerGrabs()97 void SharedXDisplay::IgnoreXServerGrabs() {
98   int test_event_base = 0;
99   int test_error_base = 0;
100   int major = 0;
101   int minor = 0;
102   if (XTestQueryExtension(display(), &test_event_base, &test_error_base, &major,
103                           &minor)) {
104     XTestGrabControl(display(), true);
105   }
106 }
107 
108 }  // namespace webrtc
109