[go: nahoru, domu]

blob: 14e2ea68aac43b6a9ad28a5e2fcc2685bdfb8f73 [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2014 The Chromium Authors
kelvinp95f548402014-11-13 21:42:202// 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
sergeyu1417e0132015-12-23 19:01:227#include <utility>
8
kelvinpc41dfa2a2014-12-08 02:45:539#include "ash/shell.h"
Avi Drissman135261e2023-01-11 22:43:1510#include "base/functional/bind.h"
11#include "base/functional/callback.h"
kelvinpc41dfa2a2014-12-08 02:45:5312#include "base/location.h"
13#include "remoting/host/chromeos/skia_bitmap_desktop_frame.h"
Henrique Ferreirofea50802022-10-04 22:22:1714#include "third_party/abseil-cpp/absl/types/optional.h"
15#include "third_party/skia/include/core/SkBitmap.h"
kelvinpc41dfa2a2014-12-08 02:45:5316#include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
Henrique Ferreirofea50802022-10-04 22:22:1717#include "ui/aura/client/cursor_shape_client.h"
kelvinpc41dfa2a2014-12-08 02:45:5318#include "ui/aura/env.h"
19#include "ui/aura/window.h"
20#include "ui/aura/window_tree_host.h"
Henrique Ferreirofea50802022-10-04 22:22:1721#include "ui/base/cursor/cursor.h"
Henrique Ferreiroe09c0efd2020-05-07 14:29:4622#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
Henrique Ferreirofea50802022-10-04 22:22:1723#include "ui/gfx/geometry/point.h"
kelvinp95f548402014-11-13 21:42:2024
kelvinpefa63e02015-01-06 23:50:0725namespace {
26
27// Creates an empty webrtc::MouseCursor. The caller is responsible for
28// destroying the returned cursor.
29webrtc::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
kelvinp95f548402014-11-13 21:42:2037namespace remoting {
38
kelvinpc41dfa2a2014-12-08 02:45:5339MouseCursorMonitorAura::MouseCursorMonitorAura()
Joe Downinge53b2a32023-01-12 22:48:3740 : callback_(nullptr), mode_(SHAPE_AND_POSITION) {}
kelvinp95f548402014-11-13 21:42:2041
kelvinp95f548402014-11-13 21:42:2042void MouseCursorMonitorAura::Init(Callback* callback, Mode mode) {
43 DCHECK(!callback_);
44 DCHECK(callback);
45
46 callback_ = callback;
47 mode_ = mode;
kelvinp95f548402014-11-13 21:42:2048}
49
50void MouseCursorMonitorAura::Capture() {
kelvinpc41dfa2a2014-12-08 02:45:5351 // Check if the cursor is different.
52 gfx::NativeCursor cursor =
53 ash::Shell::GetPrimaryRootWindow()->GetHost()->last_cursor();
54
55 if (cursor != last_cursor_) {
kelvinpefa63e02015-01-06 23:50:0756 last_cursor_ = cursor;
kelvinpc41dfa2a2014-12-08 02:45:5357 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 Walchd07d5722020-01-23 23:12:1966 webrtc::DesktopVector(position.x(), position.y()));
kelvinpc41dfa2a2014-12-08 02:45:5367 }
68 }
69}
70
71void MouseCursorMonitorAura::NotifyCursorChanged(const ui::Cursor& cursor) {
Henrique Ferreiro1631155022020-03-05 00:42:1472 if (cursor.type() == ui::mojom::CursorType::kNone) {
kelvinpefa63e02015-01-06 23:50:0773 callback_->OnMouseCursor(CreateEmptyMouseCursor());
kelvinpc41dfa2a2014-12-08 02:45:5374 return;
75 }
76
Henrique Ferreirofea50802022-10-04 22:22:1777 absl::optional<ui::CursorData> cursor_data =
Henrique Ferreiroe53ca7a2023-04-06 21:29:5778 aura::client::GetCursorShapeClient().GetCursorData(cursor);
Henrique Ferreirofea50802022-10-04 22:22:1779 if (!cursor_data) {
80 LOG(ERROR) << "Failed to load bitmap for cursor type: " << cursor.type();
81 return;
82 }
Weidong Guoa8f56452018-04-14 01:22:2283
Henrique Ferreirofea50802022-10-04 22:22:1784 const SkBitmap& cursor_bitmap = cursor_data->bitmaps[0];
85 if (cursor_bitmap.drawsNothing()) {
kelvinpefa63e02015-01-06 23:50:0786 callback_->OnMouseCursor(CreateEmptyMouseCursor());
87 return;
88 }
kelvinpc41dfa2a2014-12-08 02:45:5389
Henrique Ferreirofea50802022-10-04 22:22:1790 const gfx::Point& cursor_hotspot = cursor_data->hotspot;
91 std::unique_ptr<webrtc::DesktopFrame> image(SkiaBitmapDesktopFrame::Create(
92 std::make_unique<SkBitmap>(cursor_bitmap)));
dcheng0765c492016-04-06 22:41:5393 std::unique_ptr<webrtc::MouseCursor> cursor_shape(new webrtc::MouseCursor(
kelvinpc41dfa2a2014-12-08 02:45:5394 image.release(),
95 webrtc::DesktopVector(cursor_hotspot.x(), cursor_hotspot.y())));
96
97 callback_->OnMouseCursor(cursor_shape.release());
kelvinp95f548402014-11-13 21:42:2098}
99
100} // namespace remoting