summaryrefslogtreecommitdiffstats
path: root/remoting/host/local_input_monitor_win.cc
blob: 7afb39231aad110d0550119956c853daa3129f63 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) 2012 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/local_input_monitor.h"

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/stringprintf.h"
#include "base/threading/non_thread_safe.h"
#include "base/win/wrapped_window_proc.h"
#include "remoting/host/mouse_move_observer.h"
#include "third_party/skia/include/core/SkPoint.h"

namespace remoting {

namespace {

const wchar_t kWindowClassFormat[] = L"Chromoting_LocalInputMonitorWin_%p";

// From the HID Usage Tables specification.
const USHORT kGenericDesktopPage = 1;
const USHORT kMouseUsage = 2;

class LocalInputMonitorWin : public base::NonThreadSafe,
                             public LocalInputMonitor {
 public:
  LocalInputMonitorWin();
  ~LocalInputMonitorWin();

  virtual void Start(MouseMoveObserver* mouse_move_observer,
                     const base::Closure& disconnect_callback) OVERRIDE;
  virtual void Stop() OVERRIDE;

 private:
  // Handles WM_CREATE messages.
  LRESULT OnCreate(HWND hwnd);

  // Handles WM_INPUT messages.
  LRESULT OnInput(HRAWINPUT input_handle);

  // Window procedure invoked by the OS to process window messages.
  static LRESULT CALLBACK WindowProc(HWND hwnd, UINT message,
                                     WPARAM wparam, LPARAM lparam);

  // Atom representing the input window class.
  ATOM atom_;

  // Instance of the module containing the window procedure.
  HINSTANCE instance_;

  // Handle of the input window.
  HWND window_;

  // Observer to dispatch mouse event notifications to.
  MouseMoveObserver* observer_;

  DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorWin);
};

LocalInputMonitorWin::LocalInputMonitorWin()
    : atom_(0),
      instance_(NULL),
      window_(NULL),
      observer_(NULL) {
}

LocalInputMonitorWin::~LocalInputMonitorWin() {
  DCHECK(CalledOnValidThread());
  DCHECK(!atom_);
  DCHECK(!instance_);
  DCHECK(!window_);
  DCHECK(!observer_);
}

void LocalInputMonitorWin::Start(MouseMoveObserver* mouse_move_observer,
                                 const base::Closure& disconnect_callback) {
  DCHECK(CalledOnValidThread());
  DCHECK(!observer_);
  DCHECK(mouse_move_observer);

  observer_ = mouse_move_observer;

  // Create a window for receiving raw input.
  string16 window_class_name = base::StringPrintf(kWindowClassFormat, this);
  WNDCLASSEX window_class;
  base::win::InitializeWindowClass(
      window_class_name.c_str(),
      &base::win::WrappedWindowProc<WindowProc>,
      0, 0, 0, NULL, NULL, NULL, NULL, NULL,
      &window_class);
  instance_ = window_class.hInstance;
  atom_ = RegisterClassEx(&window_class);
  if (atom_ == 0) {
    LOG_GETLASTERROR(ERROR)
        << "Failed to register the window class '" << window_class_name << "'";
    return;
  }

  window_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
                         instance_, this);
  if (window_ == NULL) {
    LOG_GETLASTERROR(ERROR) << "Failed to create the raw input window";
    return;
  }
}

void LocalInputMonitorWin::Stop() {
  DCHECK(CalledOnValidThread());
  DCHECK(observer_);

  if (window_ != NULL) {
    DestroyWindow(window_);
    window_ = NULL;
  }

  if (atom_ != 0) {
    UnregisterClass(MAKEINTATOM(atom_), instance_);
    atom_ = 0;
    instance_ = NULL;
  }

  observer_ = NULL;
}

LRESULT LocalInputMonitorWin::OnCreate(HWND hwnd) {
  DCHECK(CalledOnValidThread());

  // Register to receive raw mouse input.
  RAWINPUTDEVICE device = {0};
  device.dwFlags = RIDEV_INPUTSINK;
  device.usUsagePage = kGenericDesktopPage;
  device.usUsage = kMouseUsage;
  device.hwndTarget = hwnd;
  if (!RegisterRawInputDevices(&device, 1, sizeof(device))) {
    LOG_GETLASTERROR(ERROR) << "RegisterRawInputDevices() failed";
    return -1;
  }

  return 0;
}

LRESULT LocalInputMonitorWin::OnInput(HRAWINPUT input_handle) {
  DCHECK(CalledOnValidThread());

  // Get the size of the input record.
  UINT size = 0;
  UINT result = GetRawInputData(input_handle,
                                RID_INPUT,
                                NULL,
                                &size,
                                sizeof(RAWINPUTHEADER));
  if (result == -1) {
    LOG_GETLASTERROR(ERROR) << "GetRawInputData() failed";
    return 0;
  }

  // Retrieve the input record itself.
  scoped_array<uint8> buffer(new uint8[size]);
  RAWINPUT* input = reinterpret_cast<RAWINPUT*>(buffer.get());
  result = GetRawInputData(input_handle,
                           RID_INPUT,
                           buffer.get(),
                           &size,
                           sizeof(RAWINPUTHEADER));
  if (result == -1) {
    LOG_GETLASTERROR(ERROR) << "GetRawInputData() failed";
    return 0;
  }

  // Notify the observer about mouse events generated locally. Remote (injected)
  // mouse events do not specify a device handle (based on observed behavior).
  if (input->header.dwType == RIM_TYPEMOUSE &&
      input->header.hDevice != NULL) {
    POINT position;
    if (!GetCursorPos(&position)) {
      position.x = 0;
      position.y = 0;
    }

    observer_->OnLocalMouseMoved(SkIPoint::Make(position.x, position.y));
  }

  return DefRawInputProc(&input, 1, sizeof(RAWINPUTHEADER));
}

// static
LRESULT CALLBACK LocalInputMonitorWin::WindowProc(
    HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
  // Store |this| to the window's user data.
  if (message == WM_CREATE) {
    CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lparam);
    SetLastError(ERROR_SUCCESS);
    LONG_PTR result = SetWindowLongPtr(
        hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(cs->lpCreateParams));
    CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
  }

  LocalInputMonitorWin* self = reinterpret_cast<LocalInputMonitorWin*>(
      GetWindowLongPtr(hwnd, GWLP_USERDATA));
  switch (message) {
    case WM_CREATE:
      return self->OnCreate(hwnd);

    case WM_INPUT:
      return self->OnInput(reinterpret_cast<HRAWINPUT>(lparam));

    default:
      return DefWindowProc(hwnd, message, wparam, lparam);
  }
}

}  // namespace

scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create() {
  return scoped_ptr<LocalInputMonitor>(new LocalInputMonitorWin());
}

}  // namespace remoting