Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / host / single_window_input_injector_mac.cc
blob333f684058d130e041d4304a227006562c893b71
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"
15 namespace remoting {
17 using protocol::ClipboardEvent;
18 using protocol::KeyEvent;
19 using protocol::TextEvent;
20 using protocol::MouseEvent;
21 using protocol::TouchEvent;
23 class SingleWindowInputInjectorMac : public SingleWindowInputInjector {
24 public:
25 SingleWindowInputInjectorMac(
26 webrtc::WindowId window_id,
27 scoped_ptr<InputInjector> input_injector);
28 ~SingleWindowInputInjectorMac() override;
30 // InputInjector interface.
31 void Start(scoped_ptr<protocol::ClipboardStub> client_clipboard) override;
32 void InjectKeyEvent(const KeyEvent& event) override;
33 void InjectTextEvent(const TextEvent& event) override;
34 void InjectMouseEvent(const MouseEvent& event) override;
35 void InjectTouchEvent(const TouchEvent& event) override;
36 void InjectClipboardEvent(const ClipboardEvent& event) override;
38 private:
39 CGRect FindCGRectOfWindow();
41 CGWindowID window_id_;
42 scoped_ptr<InputInjector> input_injector_;
44 DISALLOW_COPY_AND_ASSIGN(SingleWindowInputInjectorMac);
47 SingleWindowInputInjectorMac::SingleWindowInputInjectorMac(
48 webrtc::WindowId window_id,
49 scoped_ptr<InputInjector> input_injector)
50 : window_id_(static_cast<CGWindowID>(window_id)),
51 input_injector_(input_injector.Pass()) {
54 SingleWindowInputInjectorMac::~SingleWindowInputInjectorMac() {
57 void SingleWindowInputInjectorMac::Start(
58 scoped_ptr<protocol::ClipboardStub> client_clipboard) {
59 input_injector_->Start(client_clipboard.Pass());
62 void SingleWindowInputInjectorMac::InjectKeyEvent(const KeyEvent& event) {
63 input_injector_->InjectKeyEvent(event);
66 void SingleWindowInputInjectorMac::InjectTextEvent(const TextEvent& event) {
67 input_injector_->InjectTextEvent(event);
70 void SingleWindowInputInjectorMac::InjectMouseEvent(const MouseEvent& event) {
71 if (event.has_x() && event.has_y()) {
72 CGRect window_rect = FindCGRectOfWindow();
73 if (CGRectIsNull(window_rect)) {
74 LOG(ERROR) << "Window rect is null, so forwarding unmodified MouseEvent";
75 input_injector_->InjectMouseEvent(event);
76 return;
79 webrtc::MacDesktopConfiguration desktop_config =
80 webrtc::MacDesktopConfiguration::GetCurrent(
81 webrtc::MacDesktopConfiguration::TopLeftOrigin);
83 // Create a vector that has the origin of the window.
84 webrtc::DesktopVector window_pos(window_rect.origin.x,
85 window_rect.origin.y);
87 // The underlying InputInjector expects coordinates relative to the
88 // top-left of the top-left-most monitor, so translate the window origin
89 // to that coordinate scheme.
90 window_pos.subtract(
91 webrtc::DesktopVector(desktop_config.pixel_bounds.left(),
92 desktop_config.pixel_bounds.top()));
94 // We must make sure we are taking into account the fact that when we
95 // find the window on the host it returns its coordinates in Density
96 // Independent coordinates. We have to convert to Density Dependent
97 // because InputInjector assumes Density Dependent coordinates in the
98 // MouseEvent.
99 window_pos.set(window_pos.x() * desktop_config.dip_to_pixel_scale,
100 window_pos.y() * desktop_config.dip_to_pixel_scale);
102 // Create a new event with coordinates that are in respect to the window.
103 MouseEvent modified_event(event);
104 modified_event.set_x(event.x() + window_pos.x());
105 modified_event.set_y(event.y() + window_pos.y());
106 input_injector_->InjectMouseEvent(modified_event);
107 } else {
108 input_injector_->InjectMouseEvent(event);
112 void SingleWindowInputInjectorMac::InjectTouchEvent(const TouchEvent& event) {
113 NOTIMPLEMENTED();
116 void SingleWindowInputInjectorMac::InjectClipboardEvent(
117 const ClipboardEvent& event) {
118 input_injector_->InjectClipboardEvent(event);
121 // This method finds the rectangle of the window we are streaming using
122 // |window_id_|. The InputInjector can then use this rectangle
123 // to translate the input event to coordinates of the window rather
124 // than the screen.
125 CGRect SingleWindowInputInjectorMac::FindCGRectOfWindow() {
126 CGRect rect;
127 CGWindowID ids[1] = {window_id_};
128 base::ScopedCFTypeRef<CFArrayRef> window_id_array(
129 CFArrayCreate(nullptr, reinterpret_cast<const void**>(&ids), 1, nullptr));
131 base::ScopedCFTypeRef<CFArrayRef> window_array(
132 CGWindowListCreateDescriptionFromArray(window_id_array));
134 if (window_array == nullptr || CFArrayGetCount(window_array) == 0) {
135 // Could not find the window. It might have been closed.
136 LOG(ERROR) << "Specified window to stream not found for id: "
137 << window_id_;
138 return CGRectNull;
141 // We don't use ScopedCFTypeRef for |window_array| because the
142 // CFDictionaryRef returned by CFArrayGetValueAtIndex is owned by
143 // window_array. The same is true of the |bounds|.
144 CFDictionaryRef window =
145 base::mac::CFCast<CFDictionaryRef>(
146 CFArrayGetValueAtIndex(window_array, 0));
148 if (CFDictionaryContainsKey(window, kCGWindowBounds)) {
149 CFDictionaryRef bounds =
150 base::mac::GetValueFromDictionary<CFDictionaryRef>(
151 window, kCGWindowBounds);
153 if (bounds) {
154 if (CGRectMakeWithDictionaryRepresentation(bounds, &rect)) {
155 return rect;
160 return CGRectNull;
163 scoped_ptr<InputInjector> SingleWindowInputInjector::CreateForWindow(
164 webrtc::WindowId window_id,
165 scoped_ptr<InputInjector> input_injector) {
166 scoped_ptr<SingleWindowInputInjectorMac> injector(
167 new SingleWindowInputInjectorMac(window_id, input_injector.Pass()));
168 return injector.Pass();
171 } // namespace remoting