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