OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/desktop_capture/x11/shared_x_display.h" | 11 #include "webrtc/modules/desktop_capture/x11/shared_x_display.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 | 14 |
15 #include "webrtc/system_wrappers/interface/logging.h" | 15 #include "webrtc/system_wrappers/interface/logging.h" |
16 | 16 |
17 namespace webrtc { | 17 namespace webrtc { |
18 | 18 |
19 SharedXDisplay::SharedXDisplay(Display* display) | 19 SharedXDisplay::SharedXDisplay(Display* display) |
20 : display_(display) { | 20 : display_(display) { |
21 assert(display_); | 21 assert(display_); |
22 } | 22 } |
23 | 23 |
24 SharedXDisplay::~SharedXDisplay() { | 24 SharedXDisplay::~SharedXDisplay() { |
25 assert(event_handlers_.empty()); | 25 assert(event_handlers_.empty()); |
26 XCloseDisplay(display_); | 26 XCloseDisplay(display_); |
27 } | 27 } |
28 | 28 |
29 // static | 29 // static |
30 scoped_refptr<SharedXDisplay> SharedXDisplay::Create( | 30 rtc::scoped_refptr<SharedXDisplay> SharedXDisplay::Create( |
31 const std::string& display_name) { | 31 const std::string& display_name) { |
32 Display* display = | 32 Display* display = |
33 XOpenDisplay(display_name.empty() ? NULL : display_name.c_str()); | 33 XOpenDisplay(display_name.empty() ? NULL : display_name.c_str()); |
34 if (!display) { | 34 if (!display) { |
35 LOG(LS_ERROR) << "Unable to open display"; | 35 LOG(LS_ERROR) << "Unable to open display"; |
36 return NULL; | 36 return NULL; |
37 } | 37 } |
38 return new SharedXDisplay(display); | 38 return new SharedXDisplay(display); |
39 } | 39 } |
40 | 40 |
41 // static | 41 // static |
42 scoped_refptr<SharedXDisplay> SharedXDisplay::CreateDefault() { | 42 rtc::scoped_refptr<SharedXDisplay> SharedXDisplay::CreateDefault() { |
43 return Create(std::string()); | 43 return Create(std::string()); |
44 } | 44 } |
45 | 45 |
46 void SharedXDisplay::AddEventHandler(int type, XEventHandler* handler) { | 46 void SharedXDisplay::AddEventHandler(int type, XEventHandler* handler) { |
47 event_handlers_[type].push_back(handler); | 47 event_handlers_[type].push_back(handler); |
48 } | 48 } |
49 | 49 |
50 void SharedXDisplay::RemoveEventHandler(int type, XEventHandler* handler) { | 50 void SharedXDisplay::RemoveEventHandler(int type, XEventHandler* handler) { |
51 EventHandlersMap::iterator handlers = event_handlers_.find(type); | 51 EventHandlersMap::iterator handlers = event_handlers_.find(type); |
52 if (handlers == event_handlers_.end()) | 52 if (handlers == event_handlers_.end()) |
53 return; | 53 return; |
54 | 54 |
55 std::vector<XEventHandler*>::iterator new_end = | 55 std::vector<XEventHandler*>::iterator new_end = |
56 std::remove(handlers->second.begin(), handlers->second.end(), handler); | 56 std::remove(handlers->second.begin(), handlers->second.end(), handler); |
57 handlers->second.erase(new_end, handlers->second.end()); | 57 handlers->second.erase(new_end, handlers->second.end()); |
58 | 58 |
59 // Check if no handlers left for this event. | 59 // Check if no handlers left for this event. |
60 if (handlers->second.empty()) | 60 if (handlers->second.empty()) |
61 event_handlers_.erase(handlers); | 61 event_handlers_.erase(handlers); |
62 } | 62 } |
63 | 63 |
64 void SharedXDisplay::ProcessPendingXEvents() { | 64 void SharedXDisplay::ProcessPendingXEvents() { |
65 // Hold reference to |this| to prevent it from being destroyed while | 65 // Hold reference to |this| to prevent it from being destroyed while |
66 // processing events. | 66 // processing events. |
67 scoped_refptr<SharedXDisplay> self(this); | 67 rtc::scoped_refptr<SharedXDisplay> self(this); |
68 | 68 |
69 // Find the number of events that are outstanding "now." We don't just loop | 69 // Find the number of events that are outstanding "now." We don't just loop |
70 // on XPending because we want to guarantee this terminates. | 70 // on XPending because we want to guarantee this terminates. |
71 int events_to_process = XPending(display()); | 71 int events_to_process = XPending(display()); |
72 XEvent e; | 72 XEvent e; |
73 | 73 |
74 for (int i = 0; i < events_to_process; i++) { | 74 for (int i = 0; i < events_to_process; i++) { |
75 XNextEvent(display(), &e); | 75 XNextEvent(display(), &e); |
76 EventHandlersMap::iterator handlers = event_handlers_.find(e.type); | 76 EventHandlersMap::iterator handlers = event_handlers_.find(e.type); |
77 if (handlers == event_handlers_.end()) | 77 if (handlers == event_handlers_.end()) |
78 continue; | 78 continue; |
79 for (std::vector<XEventHandler*>::iterator it = handlers->second.begin(); | 79 for (std::vector<XEventHandler*>::iterator it = handlers->second.begin(); |
80 it != handlers->second.end(); ++it) { | 80 it != handlers->second.end(); ++it) { |
81 if ((*it)->HandleXEvent(e)) | 81 if ((*it)->HandleXEvent(e)) |
82 break; | 82 break; |
83 } | 83 } |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 } // namespace webrtc | 87 } // namespace webrtc |
OLD | NEW |