summaryrefslogtreecommitdiffstats
path: root/remoting/client/input_handler.cc
blob: 48beddedd523414cad69cfb65da912424c3d306d (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
// Copyright (c) 2011 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/client/input_handler.h"

#include "remoting/client/chromoting_view.h"
#include "remoting/proto/event.pb.h"
#include "remoting/protocol/connection_to_host.h"
#include "remoting/protocol/input_stub.h"

namespace remoting {

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

InputHandler::InputHandler(ClientContext* context,
                           protocol::ConnectionToHost* connection,
                           ChromotingView* view)
    : context_(context),
      connection_(connection),
      view_(view) {
}

InputHandler::~InputHandler() {
}

void InputHandler::SendKeyEvent(bool press, int keycode) {
  protocol::InputStub* stub = connection_->input_stub();
  if (stub) {
    if (press) {
      pressed_keys_.insert(keycode);
    } else {
      pressed_keys_.erase(keycode);
    }

    KeyEvent event;
    event.set_keycode(keycode);
    event.set_pressed(press);
    stub->InjectKeyEvent(event);
  }
}

void InputHandler::SendMouseMoveEvent(int x, int y) {
  protocol::InputStub* stub = connection_->input_stub();
  if (stub) {
    MouseEvent event;
    event.set_x(x);
    event.set_y(y);
    stub->InjectMouseEvent(event);
  }
}

void InputHandler::SendMouseButtonEvent(bool button_down,
                                        MouseEvent::MouseButton button) {
  protocol::InputStub* stub = connection_->input_stub();
  if (stub) {
    MouseEvent event;
    event.set_button(button);
    event.set_button_down(button_down);
    stub->InjectMouseEvent(event);
  }
}

void InputHandler::SendMouseWheelEvent(int dx, int dy) {
  protocol::InputStub* stub = connection_->input_stub();
  if (stub) {
    MouseEvent event;
    event.set_wheel_offset_x(dx);
    event.set_wheel_offset_y(dy);
    stub->InjectMouseEvent(event);
  }
}

void InputHandler::ReleaseAllKeys() {
  std::set<int> pressed_keys_copy = pressed_keys_;
  std::set<int>::iterator i;
  for (i = pressed_keys_copy.begin(); i != pressed_keys_copy.end(); ++i) {
    SendKeyEvent(false, *i);
  }
  pressed_keys_.clear();
}

}  // namespace remoting