summaryrefslogtreecommitdiffstats
path: root/remoting/host/chromeos/mouse_cursor_monitor_aura.cc
blob: ed7a0e0b38887a30e791906377d5b96427b15961 (plain)
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
101
102
103
104
105
106
107
108
// Copyright 2014 The Chromium Authors. All rights reserved.
// 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 <utility>

#include "ash/shell.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/location.h"
#include "remoting/host/chromeos/skia_bitmap_desktop_frame.h"
#include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/cursor/cursors_aura.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(
          INSIDE, webrtc::DesktopVector(position.x(), position.y()));
    }
  }
}

void MouseCursorMonitorAura::NotifyCursorChanged(const ui::Cursor& cursor) {
  scoped_ptr<SkBitmap> cursor_bitmap(new SkBitmap());
  gfx::Point cursor_hotspot;

  if (cursor.native_type() == ui::kCursorNone) {
    callback_->OnMouseCursor(CreateEmptyMouseCursor());
    return;
  }

  if (!ui::GetCursorBitmap(cursor, cursor_bitmap.get(), &cursor_hotspot)) {
    LOG(ERROR) << "Failed to load bitmap for cursor type:"
               << cursor.native_type();
    callback_->OnMouseCursor(CreateEmptyMouseCursor());
    return;
  }

  // There is a bug (crbug.com/436993) in aura::GetCursorBitmap() such that it
  // it would return a scale-factor-100 bitmap with a scale-factor-200 hotspot.
  // This causes the hotspot to go out of range.  As a result, we would need to
  // manually downscale the hotspot.
  float scale_factor = cursor.device_scale_factor();
  cursor_hotspot.SetPoint(cursor_hotspot.x() / scale_factor,
                          cursor_hotspot.y() / scale_factor);

  if (cursor_hotspot.x() >= cursor_bitmap->width() ||
      cursor_hotspot.y() >= cursor_bitmap->height()) {
    LOG(WARNING) << "Cursor hotspot is out of bounds for type: "
                 << cursor.native_type() << ".  Setting to (0,0) instead";
    cursor_hotspot.SetPoint(0, 0);
  }

  scoped_ptr<webrtc::DesktopFrame> image(
      SkiaBitmapDesktopFrame::Create(std::move(cursor_bitmap)));
  scoped_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