Chromium Code Reviews| Index: remoting/host/single_window_input_injector_mac.mm |
| diff --git a/remoting/host/single_window_input_injector_mac.mm b/remoting/host/single_window_input_injector_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..67618a086d2c383d9223e58e527d806a62e1a92b |
| --- /dev/null |
| +++ b/remoting/host/single_window_input_injector_mac.mm |
| @@ -0,0 +1,160 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
Lambros
2014/08/05 22:47:24
Does this need to be a .mm file? It doesn't seem t
ronakvora do not use
2014/08/06 20:56:16
Guess not! I just saw that every mac file was .mm
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/host/single_window_input_injector.h" |
| + |
| +#include <ApplicationServices/ApplicationServices.h> |
| +#include <Carbon/Carbon.h> |
| + |
| +#include "base/mac/scoped_cftyperef.h" |
| +#include "remoting/proto/internal.pb.h" |
| +#include "third_party/webrtc/modules/desktop_capture/mac/desktop_configuration.h" |
| + |
| +namespace remoting { |
| + |
| +using protocol::ClipboardEvent; |
| +using protocol::KeyEvent; |
| +using protocol::TextEvent; |
| +using protocol::MouseEvent; |
| + |
| +// A class to generate events to a window on Mac. |
| +class SingleWindowInputInjectorMac : public SingleWindowInputInjector { |
| + public: |
| + SingleWindowInputInjectorMac( |
| + webrtc::WindowId window_id, |
| + scoped_ptr<InputInjector> input_injector); |
| + |
| + virtual ~SingleWindowInputInjectorMac(); |
| + |
| + // InputInjector interface. |
| + virtual void Start( |
| + scoped_ptr<protocol::ClipboardStub> client_clipboard) OVERRIDE; |
| + virtual void InjectKeyEvent(const KeyEvent& event) OVERRIDE; |
| + virtual void InjectTextEvent(const TextEvent& event) OVERRIDE; |
| + virtual void InjectMouseEvent(const MouseEvent& event) OVERRIDE; |
| + virtual void InjectClipboardEvent(const ClipboardEvent& event) OVERRIDE; |
| + |
| + private: |
| + CGRect FindCGRectOfWindow(); |
| + |
| + CGWindowID window_id_; |
| + scoped_ptr<InputInjector> input_injector_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SingleWindowInputInjectorMac); |
| +}; |
| + |
| +SingleWindowInputInjectorMac::SingleWindowInputInjectorMac( |
| + webrtc::WindowId window_id, |
| + scoped_ptr<InputInjector> input_injector): |
|
Lambros
2014/08/05 22:47:24
nit: Move ':' to next line, and format like this:
ronakvora do not use
2014/08/06 20:56:16
Done.
|
| + window_id_((CGWindowID)window_id), |
|
Lambros
2014/08/05 22:47:24
nit: static_cast<CGWindowID>(window_id)
(see forma
ronakvora do not use
2014/08/06 20:56:16
Done.
|
| + input_injector_(input_injector.Pass()) { |
| +} |
| + |
| +SingleWindowInputInjectorMac::~SingleWindowInputInjectorMac() { |
| +} |
| + |
| +void SingleWindowInputInjectorMac::Start( |
| + scoped_ptr<protocol::ClipboardStub> client_clipboard) { |
| + input_injector_->Start(client_clipboard.Pass()); |
| +} |
| + |
| +void SingleWindowInputInjectorMac::InjectKeyEvent(const KeyEvent& event) { |
| + input_injector_->InjectKeyEvent(event); |
| +} |
| + |
| +void SingleWindowInputInjectorMac::InjectTextEvent(const TextEvent& event) { |
| + input_injector_->InjectTextEvent(event); |
| +} |
| + |
| +void SingleWindowInputInjectorMac::InjectMouseEvent(const MouseEvent& event) { |
| + if (event.has_x() && event.has_y()) { |
| + CGRect windowRect = FindCGRectOfWindow(); |
| + if (CGRectIsNull(windowRect)) { |
| + LOG(ERROR) << "Failing silently and just forwarding event"; |
| + input_injector_->InjectMouseEvent(event); |
| + return; |
| + } |
| + webrtc::MacDesktopConfiguration desktop_config = |
| + webrtc::MacDesktopConfiguration::GetCurrent( |
| + webrtc::MacDesktopConfiguration::TopLeftOrigin); |
| + |
| + // Create a vector that has the origin of the window. |
| + webrtc::DesktopVector mouse_pos(CGRectGetMinX(windowRect), |
| + CGRectGetMinY(windowRect)); |
| + |
| + // Subtract coordinates that are already added in for multiple |
| + // desktops. They get added in already in InputInjector. |
| + mouse_pos.subtract( |
| + webrtc::DesktopVector(desktop_config.pixel_bounds.left(), |
| + desktop_config.pixel_bounds.top())); |
| + |
| + // Create a new event with coordinates that are in respect to the window. |
| + MouseEvent modified_event(event); |
| + modified_event.set_x( |
| + event.x() + mouse_pos.x() * desktop_config.dip_to_pixel_scale); |
| + modified_event.set_y( |
| + event.y() + mouse_pos.y() * desktop_config.dip_to_pixel_scale); |
| + input_injector_->InjectMouseEvent(modified_event); |
| + } else { |
| + input_injector_->InjectMouseEvent(event); |
| + } |
| +} |
| + |
| +void SingleWindowInputInjectorMac::InjectClipboardEvent( |
| + const ClipboardEvent& event) { |
| + input_injector_->InjectClipboardEvent(event); |
| +} |
| + |
| +// This method is used if we are capturing a window instead of a screen. |
| +// It finds the rectangle of the window we are streaming using |
| +// |window_id_|. The InputInjector can then use this rectangle |
| +// to translate the input event to coordinates of the window rather |
| +// than the screen. |
| +CGRect SingleWindowInputInjectorMac::FindCGRectOfWindow() { |
| + CGRect rect; |
| + CGWindowID ids[1] = {window_id_}; |
| + base::ScopedCFTypeRef<CFArrayRef> window_id_array( |
| + CFArrayCreate(NULL, reinterpret_cast<const void **>(&ids), 1, NULL)); |
| + |
| + base::ScopedCFTypeRef<CFArrayRef> window_array( |
| + CGWindowListCreateDescriptionFromArray(window_id_array)); |
| + |
| + if (window_array == NULL || CFArrayGetCount(window_array) == 0) { |
|
Lambros
2014/08/05 22:47:23
I think you can do 'window_array.is_null()' ?
ronakvora do not use
2014/08/06 20:56:16
I don't think ScopedCFTypeRef has this method impl
|
| + // Could not find the window. It might have been closed. |
| + LOG(ERROR) << "Specified window to stream not found for id: " |
| + << window_id_; |
| + return CGRectNull; |
| + } |
| + |
| + CFDictionaryRef window = |
|
Lambros
2014/08/05 22:47:23
To answer your question, the raw CFDictonaryRef is
ronakvora do not use
2014/08/06 20:56:16
Acknowledged.
|
| + reinterpret_cast<CFDictionaryRef>( |
|
Lambros
2014/08/05 22:47:23
nit: Use CFCast<> from base/mac/foundation_util.h
ronakvora do not use
2014/08/06 20:56:16
Done.
|
| + CFArrayGetValueAtIndex(window_array, 0)); |
| + |
| + if (CFDictionaryContainsKey(window, kCGWindowBounds)) { |
|
Lambros
2014/08/05 22:47:24
nit: Use GetValueFromDictionary() from base/mac/fo
ronakvora do not use
2014/08/06 20:56:16
Done.
|
| + CFDictionaryRef bounds = |
|
Lambros
2014/08/05 22:47:23
Same here. This ref is owned by the |window| dicti
ronakvora do not use
2014/08/06 20:56:16
Acknowledged.
|
| + reinterpret_cast<CFDictionaryRef>( |
| + CFDictionaryGetValue(window, kCGWindowBounds)); |
| + if (bounds) { |
| + CGRectMakeWithDictionaryRepresentation(bounds, &rect); |
|
Lambros
2014/08/05 22:47:24
Check that CGRectMakeWithDictionaryRepresentation
ronakvora do not use
2014/08/06 20:56:16
Done.
|
| + } else { |
| + return CGRectNull; |
| + } |
| + } else { |
| + return CGRectNull; |
| + } |
| + |
| + return rect; |
| +} |
| + |
| +scoped_ptr<InputInjector> SingleWindowInputInjector::Create( |
| + webrtc::WindowId window_id, |
| + scoped_ptr<InputInjector> input_injector) { |
| + scoped_ptr<SingleWindowInputInjectorMac>injector( |
|
Lambros
2014/08/05 22:47:24
nit: space between '>' and injector
ronakvora do not use
2014/08/06 20:56:16
Done.
|
| + new SingleWindowInputInjectorMac(window_id, input_injector.Pass())); |
| + return injector.PassAs<InputInjector>(); |
| +} |
| + |
| + |
| + |
| +} // namespace remoting |