OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/host/single_window_input_injector.h" | |
6 | |
7 #include <ApplicationServices/ApplicationServices.h> | |
8 #include <Carbon/Carbon.h> | |
9 | |
10 #include "base/mac/foundation_util.h" | |
11 #include "base/mac/scoped_cftyperef.h" | |
12 #include "remoting/proto/event.pb.h" | |
13 #include "third_party/webrtc/modules/desktop_capture/mac/desktop_configuration.h " | |
14 | |
15 namespace remoting { | |
16 | |
17 using protocol::ClipboardEvent; | |
18 using protocol::KeyEvent; | |
19 using protocol::TextEvent; | |
20 using protocol::MouseEvent; | |
21 | |
22 // Implements InputInjector to direct input events to a specific window. | |
23 // Clipboard, Key, and Text Events will all still be Global. MouseEvents | |
24 // will be local to the window. | |
Wez
2014/08/07 23:14:33
This comment really just duplicates what is descri
ronakvora do not use
2014/08/08 17:07:14
Removed.
| |
25 class SingleWindowInputInjectorMac : public SingleWindowInputInjector { | |
26 public: | |
27 SingleWindowInputInjectorMac( | |
28 webrtc::WindowId window_id, | |
29 scoped_ptr<InputInjector> input_injector); | |
30 virtual ~SingleWindowInputInjectorMac(); | |
31 | |
32 // InputInjector interface. | |
33 virtual void Start( | |
34 scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; | |
35 virtual void InjectKeyEvent(const KeyEvent& event) OVERRIDE; | |
36 virtual void InjectTextEvent(const TextEvent& event) OVERRIDE; | |
37 virtual void InjectMouseEvent(const MouseEvent& event) OVERRIDE; | |
38 virtual void InjectClipboardEvent(const ClipboardEvent& event) OVERRIDE; | |
39 | |
40 private: | |
41 CGRect FindCGRectOfWindow(); | |
42 | |
43 CGWindowID window_id_; | |
44 scoped_ptr<InputInjector> input_injector_; | |
45 | |
46 DISALLOW_COPY_AND_ASSIGN(SingleWindowInputInjectorMac); | |
47 }; | |
48 | |
49 SingleWindowInputInjectorMac::SingleWindowInputInjectorMac( | |
50 webrtc::WindowId window_id, | |
51 scoped_ptr<InputInjector> input_injector) | |
52 : window_id_(static_cast<CGWindowID>(window_id)), | |
53 input_injector_(input_injector.Pass()) { | |
54 } | |
55 | |
56 SingleWindowInputInjectorMac::~SingleWindowInputInjectorMac() { | |
57 } | |
58 | |
59 void SingleWindowInputInjectorMac::Start( | |
60 scoped_ptr<protocol::ClipboardStub> client_clipboard) { | |
61 input_injector_->Start(client_clipboard.Pass()); | |
62 } | |
63 | |
64 void SingleWindowInputInjectorMac::InjectKeyEvent(const KeyEvent& event) { | |
65 input_injector_->InjectKeyEvent(event); | |
66 } | |
67 | |
68 void SingleWindowInputInjectorMac::InjectTextEvent(const TextEvent& event) { | |
69 input_injector_->InjectTextEvent(event); | |
70 } | |
71 | |
72 void SingleWindowInputInjectorMac::InjectMouseEvent(const MouseEvent& event) { | |
73 if (event.has_x() && event.has_y()) { | |
74 CGRect windowRect = FindCGRectOfWindow(); | |
75 if (CGRectIsNull(windowRect)) { | |
76 LOG(ERROR) << "Window rect is null, so forwarding unmodified MouseEvent"; | |
77 input_injector_->InjectMouseEvent(event); | |
78 return; | |
79 } | |
80 | |
81 webrtc::MacDesktopConfiguration desktop_config = | |
82 webrtc::MacDesktopConfiguration::GetCurrent( | |
83 webrtc::MacDesktopConfiguration::TopLeftOrigin); | |
84 | |
85 // Create a vector that has the origin of the window. | |
86 webrtc::DesktopVector window_mouse_pos(windowRect.origin.x, | |
87 windowRect.origin.y); | |
88 | |
89 // The underlying InputInjector expects coordinates relative to the | |
90 // top-left of the top-left-most monitor, so translate the window origin | |
91 // to that coordinate scheme. | |
92 window_mouse_pos.subtract( | |
93 webrtc::DesktopVector(desktop_config.pixel_bounds.left(), | |
94 desktop_config.pixel_bounds.top())); | |
95 | |
96 // We must make sure we are taking into account the fact that when we | |
97 // find the window on the host it returns its coordinates in Density | |
98 // Independent coordinates. We have to convert to Density Dependent | |
99 // because InputInjector assumes Density Dependent coordinates in the | |
100 // MouseEvent. | |
101 window_mouse_pos.set(window_mouse_pos.x() * | |
102 desktop_config.dip_to_pixel_scale, | |
103 window_mouse_pos.y() * | |
104 desktop_config.dip_to_pixel_scale); | |
105 | |
106 // Create a new event with coordinates that are in respect to the window. | |
107 MouseEvent modified_event(event); | |
108 modified_event.set_x(event.x() + window_mouse_pos.x()); | |
109 modified_event.set_y(event.y() + window_mouse_pos.y()); | |
110 input_injector_->InjectMouseEvent(modified_event); | |
111 } else { | |
112 input_injector_->InjectMouseEvent(event); | |
113 } | |
114 } | |
115 | |
116 void SingleWindowInputInjectorMac::InjectClipboardEvent( | |
117 const ClipboardEvent& event) { | |
118 input_injector_->InjectClipboardEvent(event); | |
119 } | |
120 | |
121 // This method is used if we are capturing a window instead of a screen. | |
Wez
2014/08/07 23:14:33
We're always doing that if this class is used, so
ronakvora do not use
2014/08/08 17:07:13
Yeah. Removed.
| |
122 // It finds the rectangle of the window we are streaming using | |
123 // |window_id_|. The InputInjector can then use this rectangle | |
124 // to translate the input event to coordinates of the window rather | |
125 // than the screen. | |
126 CGRect SingleWindowInputInjectorMac::FindCGRectOfWindow() { | |
127 CGRect rect; | |
128 CGWindowID ids[1] = {window_id_}; | |
129 base::ScopedCFTypeRef<CFArrayRef> window_id_array( | |
130 CFArrayCreate(NULL, reinterpret_cast<const void **>(&ids), 1, NULL)); | |
131 | |
132 base::ScopedCFTypeRef<CFArrayRef> window_array( | |
133 CGWindowListCreateDescriptionFromArray(window_id_array)); | |
134 | |
135 if (window_array == NULL || CFArrayGetCount(window_array) == 0) { | |
136 // Could not find the window. It might have been closed. | |
137 LOG(ERROR) << "Specified window to stream not found for id: " | |
138 << window_id_; | |
139 return CGRectNull; | |
140 } | |
141 | |
142 // We don't use ScopedCFTypeRef for |window| because the | |
143 // CFDictionaryRef returned by CFArrayGetValueAtIndex is owned by | |
144 // window_array. The same is true of the |bounds|. | |
Wez
2014/08/07 23:14:33
nit: |window_array|
ronakvora do not use
2014/08/08 17:07:13
Done.
| |
145 CFDictionaryRef window = | |
146 base::mac::CFCast<CFDictionaryRef>( | |
147 CFArrayGetValueAtIndex(window_array, 0)); | |
148 | |
149 if (CFDictionaryContainsKey(window, kCGWindowBounds)) { | |
150 CFDictionaryRef bounds = | |
151 base::mac::GetValueFromDictionary<CFDictionaryRef>( | |
152 window, kCGWindowBounds); | |
153 | |
154 if (bounds) { | |
155 bool success = CGRectMakeWithDictionaryRepresentation(bounds, &rect); | |
156 if (success) { | |
Wez
2014/08/07 23:14:33
nit: if (CGRectMakeWith...()) { ?
ronakvora do not use
2014/08/08 17:07:14
Done.
| |
157 return rect; | |
158 } | |
159 } | |
160 } | |
161 | |
162 return CGRectNull; | |
163 } | |
164 | |
165 scoped_ptr<InputInjector> SingleWindowInputInjector::Create( | |
166 webrtc::WindowId window_id, | |
167 scoped_ptr<InputInjector> input_injector) { | |
168 scoped_ptr<SingleWindowInputInjectorMac> injector( | |
169 new SingleWindowInputInjectorMac(window_id, input_injector.Pass())); | |
170 return injector.PassAs<InputInjector>(); | |
171 } | |
172 | |
173 | |
174 | |
175 } // namespace remoting | |
OLD | NEW |