summaryrefslogtreecommitdiffstats
path: root/remoting/host/event_executor_win.cc
blob: 4c928166b93b0b6d3a3018034556c990ab45687f (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
// Copyright (c) 2010 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/event_executor_win.h"

#include <windows.h>

#include "app/keyboard_codes.h"
#include "base/message_loop.h"
#include "base/stl_util-inl.h"
#include "remoting/host/capturer.h"
#include "remoting/proto/event.pb.h"

namespace remoting {

using protocol::MouseEvent;
using protocol::KeyEvent;

EventExecutorWin::EventExecutorWin(
    MessageLoop* message_loop, Capturer* capturer)
    : message_loop_(message_loop),
      capturer_(capturer) {
}

EventExecutorWin::~EventExecutorWin() {
}

void EventExecutorWin::InjectKeyEvent(const KeyEvent* event, Task* done) {
  if (MessageLoop::current() != message_loop_) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(this, &EventExecutorWin::InjectKeyEvent,
                          event, done));
    return;
  }
  HandleKey(event);
  done->Run();
  delete done;
}

void EventExecutorWin::InjectMouseEvent(const MouseEvent* event, Task* done) {
  if (MessageLoop::current() != message_loop_) {
    message_loop_->PostTask(
        FROM_HERE,
        NewRunnableMethod(this, &EventExecutorWin::InjectMouseEvent,
                          event, done));
    return;
  }
  HandleMouse(event);
  done->Run();
  delete done;
}

void EventExecutorWin::HandleKey(const KeyEvent* event) {
  int key = event->keycode();
  bool down = event->pressed();

  // Calculate scan code from virtual key.
  HKL hkl = GetKeyboardLayout(0);
  int scan_code = MapVirtualKeyEx(key, MAPVK_VK_TO_VSC_EX, hkl);

  INPUT input;
  input.type = INPUT_KEYBOARD;
  input.ki.time = 0;
  input.ki.wVk = key;
  input.ki.wScan = scan_code;

  // Flag to mark extended 'e0' key scancodes. Without this, the left and
  // right windows keys will not be handled properly (on US keyboard).
  if ((scan_code & 0xFF00) == 0xE000) {
    input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
  }

  // Flag to mark keyup events. Default is keydown.
  if (!down) {
    input.ki.dwFlags |= KEYEVENTF_KEYUP;
  }

  SendInput(1, &input, sizeof(INPUT));
}

protocol::InputStub* CreateEventExecutor(MessageLoop* message_loop,
                                         Capturer* capturer) {
  return new EventExecutorWin(message_loop, capturer);
}

void EventExecutorWin::HandleMouse(const MouseEvent* event) {
  // TODO(garykac) Collapse mouse (x,y) and button events into a single
  // input event when possible.
  if (event->has_x() && event->has_y()) {
    int x = event->x();
    int y = event->y();

    INPUT input;
    input.type = INPUT_MOUSE;
    input.mi.time = 0;
    input.mi.dx = static_cast<int>((x * 65535) / capturer_->width());
    input.mi.dy = static_cast<int>((y * 65535) / capturer_->height());
    input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
    SendInput(1, &input, sizeof(INPUT));
  }

  if (event->has_wheel_offset_x() && event->has_wheel_offset_y()) {
    INPUT wheel;
    wheel.type = INPUT_MOUSE;
    wheel.mi.time = 0;

    int dx = event->wheel_offset_x();
    int dy = event->wheel_offset_y();

    if (dx != 0) {
      wheel.mi.mouseData = dx;
      wheel.mi.dwFlags = MOUSEEVENTF_HWHEEL;
      SendInput(1, &wheel, sizeof(INPUT));
    }
    if (dy != 0) {
      wheel.mi.mouseData = dy;
      wheel.mi.dwFlags = MOUSEEVENTF_WHEEL;
      SendInput(1, &wheel, sizeof(INPUT));
    }
  }

  if (event->has_button() && event->has_button_down()) {
    INPUT button_event;
    button_event.type = INPUT_MOUSE;
    button_event.mi.time = 0;
    button_event.mi.dx = 0;
    button_event.mi.dy = 0;

    MouseEvent::MouseButton button = event->button();
    bool down = event->button_down();
    if (button == MouseEvent::BUTTON_LEFT) {
      button_event.mi.dwFlags =
          down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
    } else if (button == MouseEvent::BUTTON_MIDDLE) {
      button_event.mi.dwFlags =
          down ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
    } else if (button == MouseEvent::BUTTON_RIGHT) {
      button_event.mi.dwFlags =
          down ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
    } else {
      button_event.mi.dwFlags =
          down ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
    }

    SendInput(1, &button_event, sizeof(INPUT));
  }
}

}  // namespace remoting