File: mouse_cursor_monitor_aura.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 3,097 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/chromeos/mouse_cursor_monitor_aura.h"

#include <optional>
#include <utility>

#include "ash/shell.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "remoting/host/chromeos/skia_bitmap_desktop_frame.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
#include "ui/aura/client/cursor_shape_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
#include "ui/gfx/geometry/point.h"

namespace {

// Creates an empty webrtc::MouseCursor. The caller is responsible for
// destroying the returned cursor.
webrtc::MouseCursor* CreateEmptyMouseCursor() {
  return new webrtc::MouseCursor(
      new webrtc::BasicDesktopFrame(webrtc::DesktopSize(0, 0)),
      webrtc::DesktopVector(0, 0));
}

}  // namespace

namespace remoting {

MouseCursorMonitorAura::MouseCursorMonitorAura()
    : callback_(nullptr), mode_(SHAPE_AND_POSITION) {}

void MouseCursorMonitorAura::Init(Callback* callback, Mode mode) {
  DCHECK(!callback_);
  DCHECK(callback);

  callback_ = callback;
  mode_ = mode;
}

void MouseCursorMonitorAura::Capture() {
  // Check if the cursor is different.
  gfx::NativeCursor cursor =
      ash::Shell::GetPrimaryRootWindow()->GetHost()->last_cursor();

  if (cursor != last_cursor_) {
    last_cursor_ = cursor;
    NotifyCursorChanged(cursor);
  }

  // Check if we need to update the location.
  if (mode_ == SHAPE_AND_POSITION) {
    gfx::Point position = aura::Env::GetInstance()->last_mouse_location();
    if (position != last_mouse_location_) {
      last_mouse_location_ = position;
      callback_->OnMouseCursorPosition(
          webrtc::DesktopVector(position.x(), position.y()));
    }
  }
}

void MouseCursorMonitorAura::NotifyCursorChanged(const ui::Cursor& cursor) {
  if (cursor.type() == ui::mojom::CursorType::kNone) {
    callback_->OnMouseCursor(CreateEmptyMouseCursor());
    return;
  }

  std::optional<ui::CursorData> cursor_data =
      aura::client::GetCursorShapeClient().GetCursorData(cursor);
  if (!cursor_data) {
    LOG(ERROR) << "Failed to load bitmap for cursor type: " << cursor.type();
    return;
  }

  const SkBitmap& cursor_bitmap = cursor_data->bitmaps[0];
  if (cursor_bitmap.drawsNothing()) {
    callback_->OnMouseCursor(CreateEmptyMouseCursor());
    return;
  }

  const gfx::Point& cursor_hotspot = cursor_data->hotspot;
  std::unique_ptr<webrtc::DesktopFrame> image(SkiaBitmapDesktopFrame::Create(
      std::make_unique<SkBitmap>(cursor_bitmap)));
  std::unique_ptr<webrtc::MouseCursor> cursor_shape(new webrtc::MouseCursor(
      image.release(),
      webrtc::DesktopVector(cursor_hotspot.x(), cursor_hotspot.y())));

  callback_->OnMouseCursor(cursor_shape.release());
}

}  // namespace remoting