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.
5 #include "remoting/host/single_window_input_injector.h"
7 #include <ApplicationServices/ApplicationServices.h>
8 #include <Carbon/Carbon.h>
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"
17 using protocol::ClipboardEvent
;
18 using protocol::KeyEvent
;
19 using protocol::TextEvent
;
20 using protocol::MouseEvent
;
22 class SingleWindowInputInjectorMac
: public SingleWindowInputInjector
{
24 SingleWindowInputInjectorMac(
25 webrtc::WindowId window_id
,
26 scoped_ptr
<InputInjector
> input_injector
);
27 ~SingleWindowInputInjectorMac() override
;
29 // InputInjector interface.
30 void Start(scoped_ptr
<protocol::ClipboardStub
> client_clipboard
) override
;
31 void InjectKeyEvent(const KeyEvent
& event
) override
;
32 void InjectTextEvent(const TextEvent
& event
) override
;
33 void InjectMouseEvent(const MouseEvent
& event
) override
;
34 void InjectClipboardEvent(const ClipboardEvent
& event
) override
;
37 CGRect
FindCGRectOfWindow();
39 CGWindowID window_id_
;
40 scoped_ptr
<InputInjector
> input_injector_
;
42 DISALLOW_COPY_AND_ASSIGN(SingleWindowInputInjectorMac
);
45 SingleWindowInputInjectorMac::SingleWindowInputInjectorMac(
46 webrtc::WindowId window_id
,
47 scoped_ptr
<InputInjector
> input_injector
)
48 : window_id_(static_cast<CGWindowID
>(window_id
)),
49 input_injector_(input_injector
.Pass()) {
52 SingleWindowInputInjectorMac::~SingleWindowInputInjectorMac() {
55 void SingleWindowInputInjectorMac::Start(
56 scoped_ptr
<protocol::ClipboardStub
> client_clipboard
) {
57 input_injector_
->Start(client_clipboard
.Pass());
60 void SingleWindowInputInjectorMac::InjectKeyEvent(const KeyEvent
& event
) {
61 input_injector_
->InjectKeyEvent(event
);
64 void SingleWindowInputInjectorMac::InjectTextEvent(const TextEvent
& event
) {
65 input_injector_
->InjectTextEvent(event
);
68 void SingleWindowInputInjectorMac::InjectMouseEvent(const MouseEvent
& event
) {
69 if (event
.has_x() && event
.has_y()) {
70 CGRect window_rect
= FindCGRectOfWindow();
71 if (CGRectIsNull(window_rect
)) {
72 LOG(ERROR
) << "Window rect is null, so forwarding unmodified MouseEvent";
73 input_injector_
->InjectMouseEvent(event
);
77 webrtc::MacDesktopConfiguration desktop_config
=
78 webrtc::MacDesktopConfiguration::GetCurrent(
79 webrtc::MacDesktopConfiguration::TopLeftOrigin
);
81 // Create a vector that has the origin of the window.
82 webrtc::DesktopVector
window_pos(window_rect
.origin
.x
,
83 window_rect
.origin
.y
);
85 // The underlying InputInjector expects coordinates relative to the
86 // top-left of the top-left-most monitor, so translate the window origin
87 // to that coordinate scheme.
89 webrtc::DesktopVector(desktop_config
.pixel_bounds
.left(),
90 desktop_config
.pixel_bounds
.top()));
92 // We must make sure we are taking into account the fact that when we
93 // find the window on the host it returns its coordinates in Density
94 // Independent coordinates. We have to convert to Density Dependent
95 // because InputInjector assumes Density Dependent coordinates in the
97 window_pos
.set(window_pos
.x() * desktop_config
.dip_to_pixel_scale
,
98 window_pos
.y() * desktop_config
.dip_to_pixel_scale
);
100 // Create a new event with coordinates that are in respect to the window.
101 MouseEvent
modified_event(event
);
102 modified_event
.set_x(event
.x() + window_pos
.x());
103 modified_event
.set_y(event
.y() + window_pos
.y());
104 input_injector_
->InjectMouseEvent(modified_event
);
106 input_injector_
->InjectMouseEvent(event
);
110 void SingleWindowInputInjectorMac::InjectClipboardEvent(
111 const ClipboardEvent
& event
) {
112 input_injector_
->InjectClipboardEvent(event
);
115 // This method finds the rectangle of the window we are streaming using
116 // |window_id_|. The InputInjector can then use this rectangle
117 // to translate the input event to coordinates of the window rather
119 CGRect
SingleWindowInputInjectorMac::FindCGRectOfWindow() {
121 CGWindowID ids
[1] = {window_id_
};
122 base::ScopedCFTypeRef
<CFArrayRef
> window_id_array(
123 CFArrayCreate(NULL
, reinterpret_cast<const void **>(&ids
), 1, NULL
));
125 base::ScopedCFTypeRef
<CFArrayRef
> window_array(
126 CGWindowListCreateDescriptionFromArray(window_id_array
));
128 if (window_array
== NULL
|| CFArrayGetCount(window_array
) == 0) {
129 // Could not find the window. It might have been closed.
130 LOG(ERROR
) << "Specified window to stream not found for id: "
135 // We don't use ScopedCFTypeRef for |window_array| because the
136 // CFDictionaryRef returned by CFArrayGetValueAtIndex is owned by
137 // window_array. The same is true of the |bounds|.
138 CFDictionaryRef window
=
139 base::mac::CFCast
<CFDictionaryRef
>(
140 CFArrayGetValueAtIndex(window_array
, 0));
142 if (CFDictionaryContainsKey(window
, kCGWindowBounds
)) {
143 CFDictionaryRef bounds
=
144 base::mac::GetValueFromDictionary
<CFDictionaryRef
>(
145 window
, kCGWindowBounds
);
148 if (CGRectMakeWithDictionaryRepresentation(bounds
, &rect
)) {
157 scoped_ptr
<InputInjector
> SingleWindowInputInjector::CreateForWindow(
158 webrtc::WindowId window_id
,
159 scoped_ptr
<InputInjector
> input_injector
) {
160 scoped_ptr
<SingleWindowInputInjectorMac
> injector(
161 new SingleWindowInputInjectorMac(window_id
, input_injector
.Pass()));
162 return injector
.Pass();
165 } // namespace remoting