Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
kelvinp | 95f54840 | 2014-11-13 21:42:20 | [diff] [blame] | 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/chromeos/mouse_cursor_monitor_aura.h" |
| 6 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 9 | #include "ash/shell.h" |
Avi Drissman | 135261e | 2023-01-11 22:43:15 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
| 11 | #include "base/functional/callback.h" |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 12 | #include "base/location.h" |
| 13 | #include "remoting/host/chromeos/skia_bitmap_desktop_frame.h" |
Henrique Ferreiro | fea5080 | 2022-10-04 22:22:17 | [diff] [blame] | 14 | #include "third_party/abseil-cpp/absl/types/optional.h" |
| 15 | #include "third_party/skia/include/core/SkBitmap.h" |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 16 | #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" |
Henrique Ferreiro | fea5080 | 2022-10-04 22:22:17 | [diff] [blame] | 17 | #include "ui/aura/client/cursor_shape_client.h" |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 18 | #include "ui/aura/env.h" |
| 19 | #include "ui/aura/window.h" |
| 20 | #include "ui/aura/window_tree_host.h" |
Henrique Ferreiro | fea5080 | 2022-10-04 22:22:17 | [diff] [blame] | 21 | #include "ui/base/cursor/cursor.h" |
Henrique Ferreiro | e09c0efd | 2020-05-07 14:29:46 | [diff] [blame] | 22 | #include "ui/base/cursor/mojom/cursor_type.mojom-shared.h" |
Henrique Ferreiro | fea5080 | 2022-10-04 22:22:17 | [diff] [blame] | 23 | #include "ui/gfx/geometry/point.h" |
kelvinp | 95f54840 | 2014-11-13 21:42:20 | [diff] [blame] | 24 | |
kelvinp | efa63e0 | 2015-01-06 23:50:07 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // Creates an empty webrtc::MouseCursor. The caller is responsible for |
| 28 | // destroying the returned cursor. |
| 29 | webrtc::MouseCursor* CreateEmptyMouseCursor() { |
| 30 | return new webrtc::MouseCursor( |
| 31 | new webrtc::BasicDesktopFrame(webrtc::DesktopSize(0, 0)), |
| 32 | webrtc::DesktopVector(0, 0)); |
| 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
kelvinp | 95f54840 | 2014-11-13 21:42:20 | [diff] [blame] | 37 | namespace remoting { |
| 38 | |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 39 | MouseCursorMonitorAura::MouseCursorMonitorAura() |
Joe Downing | e53b2a3 | 2023-01-12 22:48:37 | [diff] [blame] | 40 | : callback_(nullptr), mode_(SHAPE_AND_POSITION) {} |
kelvinp | 95f54840 | 2014-11-13 21:42:20 | [diff] [blame] | 41 | |
kelvinp | 95f54840 | 2014-11-13 21:42:20 | [diff] [blame] | 42 | void MouseCursorMonitorAura::Init(Callback* callback, Mode mode) { |
| 43 | DCHECK(!callback_); |
| 44 | DCHECK(callback); |
| 45 | |
| 46 | callback_ = callback; |
| 47 | mode_ = mode; |
kelvinp | 95f54840 | 2014-11-13 21:42:20 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void MouseCursorMonitorAura::Capture() { |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 51 | // Check if the cursor is different. |
| 52 | gfx::NativeCursor cursor = |
| 53 | ash::Shell::GetPrimaryRootWindow()->GetHost()->last_cursor(); |
| 54 | |
| 55 | if (cursor != last_cursor_) { |
kelvinp | efa63e0 | 2015-01-06 23:50:07 | [diff] [blame] | 56 | last_cursor_ = cursor; |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 57 | NotifyCursorChanged(cursor); |
| 58 | } |
| 59 | |
| 60 | // Check if we need to update the location. |
| 61 | if (mode_ == SHAPE_AND_POSITION) { |
| 62 | gfx::Point position = aura::Env::GetInstance()->last_mouse_location(); |
| 63 | if (position != last_mouse_location_) { |
| 64 | last_mouse_location_ = position; |
| 65 | callback_->OnMouseCursorPosition( |
Jamie Walch | d07d572 | 2020-01-23 23:12:19 | [diff] [blame] | 66 | webrtc::DesktopVector(position.x(), position.y())); |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void MouseCursorMonitorAura::NotifyCursorChanged(const ui::Cursor& cursor) { |
Henrique Ferreiro | 163115502 | 2020-03-05 00:42:14 | [diff] [blame] | 72 | if (cursor.type() == ui::mojom::CursorType::kNone) { |
kelvinp | efa63e0 | 2015-01-06 23:50:07 | [diff] [blame] | 73 | callback_->OnMouseCursor(CreateEmptyMouseCursor()); |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
Henrique Ferreiro | fea5080 | 2022-10-04 22:22:17 | [diff] [blame] | 77 | absl::optional<ui::CursorData> cursor_data = |
Henrique Ferreiro | e53ca7a | 2023-04-06 21:29:57 | [diff] [blame] | 78 | aura::client::GetCursorShapeClient().GetCursorData(cursor); |
Henrique Ferreiro | fea5080 | 2022-10-04 22:22:17 | [diff] [blame] | 79 | if (!cursor_data) { |
| 80 | LOG(ERROR) << "Failed to load bitmap for cursor type: " << cursor.type(); |
| 81 | return; |
| 82 | } |
Weidong Guo | a8f5645 | 2018-04-14 01:22:22 | [diff] [blame] | 83 | |
Henrique Ferreiro | fea5080 | 2022-10-04 22:22:17 | [diff] [blame] | 84 | const SkBitmap& cursor_bitmap = cursor_data->bitmaps[0]; |
| 85 | if (cursor_bitmap.drawsNothing()) { |
kelvinp | efa63e0 | 2015-01-06 23:50:07 | [diff] [blame] | 86 | callback_->OnMouseCursor(CreateEmptyMouseCursor()); |
| 87 | return; |
| 88 | } |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 89 | |
Henrique Ferreiro | fea5080 | 2022-10-04 22:22:17 | [diff] [blame] | 90 | const gfx::Point& cursor_hotspot = cursor_data->hotspot; |
| 91 | std::unique_ptr<webrtc::DesktopFrame> image(SkiaBitmapDesktopFrame::Create( |
| 92 | std::make_unique<SkBitmap>(cursor_bitmap))); |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 93 | std::unique_ptr<webrtc::MouseCursor> cursor_shape(new webrtc::MouseCursor( |
kelvinp | c41dfa2a | 2014-12-08 02:45:53 | [diff] [blame] | 94 | image.release(), |
| 95 | webrtc::DesktopVector(cursor_hotspot.x(), cursor_hotspot.y()))); |
| 96 | |
| 97 | callback_->OnMouseCursor(cursor_shape.release()); |
kelvinp | 95f54840 | 2014-11-13 21:42:20 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace remoting |