• 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/shared_x_display.h"
12 
13 #include <X11/Xlib.h>
14 
15 #include <algorithm>
16 
17 #include "rtc_base/checks.h"
18 #include "rtc_base/logging.h"
19 
20 namespace webrtc {
21 
SharedXDisplay(Display * display)22 SharedXDisplay::SharedXDisplay(Display* display) : display_(display) {
23   RTC_DCHECK(display_);
24 }
25 
~SharedXDisplay()26 SharedXDisplay::~SharedXDisplay() {
27   RTC_DCHECK(event_handlers_.empty());
28   XCloseDisplay(display_);
29 }
30 
31 // static
Create(const std::string & display_name)32 rtc::scoped_refptr<SharedXDisplay> SharedXDisplay::Create(
33     const std::string& display_name) {
34   Display* display =
35       XOpenDisplay(display_name.empty() ? NULL : display_name.c_str());
36   if (!display) {
37     RTC_LOG(LS_ERROR) << "Unable to open display";
38     return NULL;
39   }
40   return new SharedXDisplay(display);
41 }
42 
43 // static
CreateDefault()44 rtc::scoped_refptr<SharedXDisplay> SharedXDisplay::CreateDefault() {
45   return Create(std::string());
46 }
47 
AddEventHandler(int type,XEventHandler * handler)48 void SharedXDisplay::AddEventHandler(int type, XEventHandler* handler) {
49   event_handlers_[type].push_back(handler);
50 }
51 
RemoveEventHandler(int type,XEventHandler * handler)52 void SharedXDisplay::RemoveEventHandler(int type, XEventHandler* handler) {
53   EventHandlersMap::iterator handlers = event_handlers_.find(type);
54   if (handlers == event_handlers_.end())
55     return;
56 
57   std::vector<XEventHandler*>::iterator new_end =
58       std::remove(handlers->second.begin(), handlers->second.end(), handler);
59   handlers->second.erase(new_end, handlers->second.end());
60 
61   // Check if no handlers left for this event.
62   if (handlers->second.empty())
63     event_handlers_.erase(handlers);
64 }
65 
ProcessPendingXEvents()66 void SharedXDisplay::ProcessPendingXEvents() {
67   // Hold reference to |this| to prevent it from being destroyed while
68   // processing events.
69   rtc::scoped_refptr<SharedXDisplay> self(this);
70 
71   // Find the number of events that are outstanding "now."  We don't just loop
72   // on XPending because we want to guarantee this terminates.
73   int events_to_process = XPending(display());
74   XEvent e;
75 
76   for (int i = 0; i < events_to_process; i++) {
77     XNextEvent(display(), &e);
78     EventHandlersMap::iterator handlers = event_handlers_.find(e.type);
79     if (handlers == event_handlers_.end())
80       continue;
81     for (std::vector<XEventHandler*>::iterator it = handlers->second.begin();
82          it != handlers->second.end(); ++it) {
83       if ((*it)->HandleXEvent(e))
84         break;
85     }
86   }
87 }
88 
89 }  // namespace webrtc
90