Home | History | Annotate | Download | only in protocol
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "remoting/protocol/input_event_tracker.h"
      6 
      7 #include "base/logging.h"
      8 #include "remoting/proto/event.pb.h"
      9 
     10 namespace remoting {
     11 namespace protocol {
     12 
     13 InputEventTracker::InputEventTracker(InputStub* input_stub)
     14     : input_stub_(input_stub),
     15       mouse_button_state_(0) {
     16 }
     17 
     18 InputEventTracker::~InputEventTracker() {
     19 }
     20 
     21 bool InputEventTracker::IsKeyPressed(uint32 usb_keycode) const {
     22   return pressed_keys_.find(usb_keycode) != pressed_keys_.end();
     23 }
     24 
     25 int InputEventTracker::PressedKeyCount() const {
     26   return pressed_keys_.size();
     27 }
     28 
     29 void InputEventTracker::ReleaseAll() {
     30   std::set<uint32>::iterator i;
     31   for (i = pressed_keys_.begin(); i != pressed_keys_.end(); ++i) {
     32     KeyEvent event;
     33     event.set_pressed(false);
     34     event.set_usb_keycode(*i);
     35     input_stub_->InjectKeyEvent(event);
     36   }
     37   pressed_keys_.clear();
     38 
     39   for (int i = MouseEvent::BUTTON_UNDEFINED + 1;
     40        i < MouseEvent::BUTTON_MAX; ++i) {
     41     if (mouse_button_state_ & (1 << (i - 1))) {
     42       MouseEvent mouse;
     43 
     44       // TODO(wez): EventInjectors should cope with positionless events by
     45       // using the current cursor position, and we wouldn't set position here.
     46       mouse.set_x(mouse_pos_.x());
     47       mouse.set_y(mouse_pos_.y());
     48 
     49       mouse.set_button((MouseEvent::MouseButton)i);
     50       mouse.set_button_down(false);
     51       input_stub_->InjectMouseEvent(mouse);
     52     }
     53   }
     54   mouse_button_state_ = 0;
     55 }
     56 
     57 void InputEventTracker::InjectKeyEvent(const KeyEvent& event) {
     58   // We don't need to track the keyboard lock states of key down events.
     59   // Pressed keys will be released with |lock_states| set to 0.
     60   // The lock states of auto generated key up events don't matter as long as
     61   // we release all the pressed keys at blurring/disconnection time.
     62   if (event.has_pressed()) {
     63     if (event.has_usb_keycode()) {
     64       if (event.pressed()) {
     65         pressed_keys_.insert(event.usb_keycode());
     66       } else {
     67         pressed_keys_.erase(event.usb_keycode());
     68       }
     69     }
     70   }
     71   input_stub_->InjectKeyEvent(event);
     72 }
     73 
     74 void InputEventTracker::InjectMouseEvent(const MouseEvent& event) {
     75   if (event.has_x() && event.has_y()) {
     76     mouse_pos_ = webrtc::DesktopVector(event.x(), event.y());
     77   }
     78   if (event.has_button() && event.has_button_down()) {
     79     // Button values are defined in remoting/proto/event.proto.
     80     if (event.button() >= 1 && event.button() < MouseEvent::BUTTON_MAX) {
     81       uint32 button_change = 1 << (event.button() - 1);
     82       if (event.button_down()) {
     83         mouse_button_state_ |= button_change;
     84       } else {
     85         mouse_button_state_ &= ~button_change;
     86       }
     87     }
     88   }
     89   input_stub_->InjectMouseEvent(event);
     90 }
     91 
     92 }  // namespace protocol
     93 }  // namespace remoting
     94