OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/chromeos/mouse_cursor_monitor_aura.h" | 5 #include "remoting/host/chromeos/mouse_cursor_monitor_aura.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "remoting/host/chromeos/skia_bitmap_desktop_frame.h" | 11 #include "remoting/host/chromeos/skia_bitmap_desktop_frame.h" |
12 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" | 12 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" |
13 #include "ui/aura/env.h" | 13 #include "ui/aura/env.h" |
14 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
15 #include "ui/aura/window_tree_host.h" | 15 #include "ui/aura/window_tree_host.h" |
16 #include "ui/base/cursor/cursors_aura.h" | 16 #include "ui/base/cursor/cursors_aura.h" |
17 | 17 |
| 18 namespace { |
| 19 |
| 20 // Creates an empty webrtc::MouseCursor. The caller is responsible for |
| 21 // destroying the returned cursor. |
| 22 webrtc::MouseCursor* CreateEmptyMouseCursor() { |
| 23 return new webrtc::MouseCursor( |
| 24 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(0, 0)), |
| 25 webrtc::DesktopVector(0, 0)); |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
18 namespace remoting { | 30 namespace remoting { |
19 | 31 |
20 MouseCursorMonitorAura::MouseCursorMonitorAura() | 32 MouseCursorMonitorAura::MouseCursorMonitorAura() |
21 : callback_(nullptr), | 33 : callback_(nullptr), |
22 mode_(SHAPE_AND_POSITION) { | 34 mode_(SHAPE_AND_POSITION) { |
23 } | 35 } |
24 | 36 |
25 void MouseCursorMonitorAura::Init(Callback* callback, Mode mode) { | 37 void MouseCursorMonitorAura::Init(Callback* callback, Mode mode) { |
26 DCHECK(!callback_); | 38 DCHECK(!callback_); |
27 DCHECK(callback); | 39 DCHECK(callback); |
28 | 40 |
29 callback_ = callback; | 41 callback_ = callback; |
30 mode_ = mode; | 42 mode_ = mode; |
31 } | 43 } |
32 | 44 |
33 void MouseCursorMonitorAura::Capture() { | 45 void MouseCursorMonitorAura::Capture() { |
34 // Check if the cursor is different. | 46 // Check if the cursor is different. |
35 gfx::NativeCursor cursor = | 47 gfx::NativeCursor cursor = |
36 ash::Shell::GetPrimaryRootWindow()->GetHost()->last_cursor(); | 48 ash::Shell::GetPrimaryRootWindow()->GetHost()->last_cursor(); |
37 | 49 |
38 if (cursor != last_cursor_) { | 50 if (cursor != last_cursor_) { |
| 51 last_cursor_ = cursor; |
39 NotifyCursorChanged(cursor); | 52 NotifyCursorChanged(cursor); |
40 } | 53 } |
41 | 54 |
42 // Check if we need to update the location. | 55 // Check if we need to update the location. |
43 if (mode_ == SHAPE_AND_POSITION) { | 56 if (mode_ == SHAPE_AND_POSITION) { |
44 gfx::Point position = aura::Env::GetInstance()->last_mouse_location(); | 57 gfx::Point position = aura::Env::GetInstance()->last_mouse_location(); |
45 if (position != last_mouse_location_) { | 58 if (position != last_mouse_location_) { |
46 last_mouse_location_ = position; | 59 last_mouse_location_ = position; |
47 callback_->OnMouseCursorPosition( | 60 callback_->OnMouseCursorPosition( |
48 INSIDE, webrtc::DesktopVector(position.x(), position.y())); | 61 INSIDE, webrtc::DesktopVector(position.x(), position.y())); |
49 } | 62 } |
50 } | 63 } |
51 } | 64 } |
52 | 65 |
53 void MouseCursorMonitorAura::NotifyCursorChanged(const ui::Cursor& cursor) { | 66 void MouseCursorMonitorAura::NotifyCursorChanged(const ui::Cursor& cursor) { |
54 scoped_ptr<SkBitmap> cursor_bitmap(new SkBitmap()); | 67 scoped_ptr<SkBitmap> cursor_bitmap(new SkBitmap()); |
55 gfx::Point cursor_hotspot; | 68 gfx::Point cursor_hotspot; |
| 69 |
| 70 if (cursor.native_type() == ui::kCursorNone) { |
| 71 callback_->OnMouseCursor(CreateEmptyMouseCursor()); |
| 72 return; |
| 73 } |
| 74 |
56 if (!ui::GetCursorBitmap(cursor, cursor_bitmap.get(), &cursor_hotspot)) { | 75 if (!ui::GetCursorBitmap(cursor, cursor_bitmap.get(), &cursor_hotspot)) { |
57 LOG(ERROR) << "Failed to load bitmap for cursor type:" | 76 LOG(ERROR) << "Failed to load bitmap for cursor type:" |
58 << cursor.native_type(); | 77 << cursor.native_type(); |
| 78 callback_->OnMouseCursor(CreateEmptyMouseCursor()); |
59 return; | 79 return; |
60 } | 80 } |
61 | 81 |
62 last_cursor_ = cursor; | |
63 | |
64 // There is a bug (crbug.com/436993) in aura::GetCursorBitmap() such that it | 82 // There is a bug (crbug.com/436993) in aura::GetCursorBitmap() such that it |
65 // it would return a scale-factor-100 bitmap with a scale-factor-200 hotspot. | 83 // it would return a scale-factor-100 bitmap with a scale-factor-200 hotspot. |
66 // This causes the hotspot to go out of range. As a result, we would need to | 84 // This causes the hotspot to go out of range. As a result, we would need to |
67 // manually downscale the hotspot. | 85 // manually downscale the hotspot. |
68 float scale_factor = cursor.device_scale_factor(); | 86 float scale_factor = cursor.device_scale_factor(); |
69 cursor_hotspot.SetPoint(cursor_hotspot.x() / scale_factor, | 87 cursor_hotspot.SetPoint(cursor_hotspot.x() / scale_factor, |
70 cursor_hotspot.y() / scale_factor); | 88 cursor_hotspot.y() / scale_factor); |
71 | 89 |
72 if (cursor_hotspot.x() >= cursor_bitmap->width() || | 90 if (cursor_hotspot.x() >= cursor_bitmap->width() || |
73 cursor_hotspot.y() >= cursor_bitmap->height()) { | 91 cursor_hotspot.y() >= cursor_bitmap->height()) { |
74 LOG(WARNING) << "Cursor hotspot is out of bounds for type: " | 92 LOG(WARNING) << "Cursor hotspot is out of bounds for type: " |
75 << cursor.native_type() << ". Setting to (0,0) instead"; | 93 << cursor.native_type() << ". Setting to (0,0) instead"; |
76 cursor_hotspot.SetPoint(0, 0); | 94 cursor_hotspot.SetPoint(0, 0); |
77 } | 95 } |
78 | 96 |
79 scoped_ptr<webrtc::DesktopFrame> image( | 97 scoped_ptr<webrtc::DesktopFrame> image( |
80 SkiaBitmapDesktopFrame::Create(cursor_bitmap.Pass())); | 98 SkiaBitmapDesktopFrame::Create(cursor_bitmap.Pass())); |
81 scoped_ptr<webrtc::MouseCursor> cursor_shape(new webrtc::MouseCursor( | 99 scoped_ptr<webrtc::MouseCursor> cursor_shape(new webrtc::MouseCursor( |
82 image.release(), | 100 image.release(), |
83 webrtc::DesktopVector(cursor_hotspot.x(), cursor_hotspot.y()))); | 101 webrtc::DesktopVector(cursor_hotspot.x(), cursor_hotspot.y()))); |
84 | 102 |
85 callback_->OnMouseCursor(cursor_shape.release()); | 103 callback_->OnMouseCursor(cursor_shape.release()); |
86 } | 104 } |
87 | 105 |
88 } // namespace remoting | 106 } // namespace remoting |
OLD | NEW |