Home | History | Annotate | Download | only in client
      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/client/key_event_mapper.h"
      6 
      7 #include "remoting/proto/event.pb.h"
      8 
      9 namespace remoting {
     10 
     11 KeyEventMapper::KeyEventMapper() {
     12 }
     13 
     14 KeyEventMapper::KeyEventMapper(InputStub* stub) : protocol::InputFilter(stub) {
     15 }
     16 
     17 KeyEventMapper::~KeyEventMapper() {
     18 }
     19 
     20 void KeyEventMapper::SetTrapCallback(KeyTrapCallback callback) {
     21   trap_callback = callback;
     22 }
     23 
     24 void KeyEventMapper::TrapKey(uint32 usb_keycode, bool trap_key) {
     25   if (trap_key) {
     26     trapped_keys.insert(usb_keycode);
     27   } else {
     28     trapped_keys.erase(usb_keycode);
     29   }
     30 }
     31 
     32 void KeyEventMapper::RemapKey(uint32 in_usb_keycode, uint32 out_usb_keycode) {
     33   if (in_usb_keycode == out_usb_keycode) {
     34     mapped_keys.erase(in_usb_keycode);
     35   } else {
     36     mapped_keys[in_usb_keycode] = out_usb_keycode;
     37   }
     38 }
     39 
     40 void KeyEventMapper::InjectKeyEvent(const protocol::KeyEvent& event) {
     41   if (event.has_usb_keycode()) {
     42     // Deliver trapped keys to the callback, not the next stub.
     43     if (!trap_callback.is_null() && event.has_pressed() &&
     44         (trapped_keys.find(event.usb_keycode()) != trapped_keys.end())) {
     45       trap_callback.Run(event);
     46       return;
     47     }
     48 
     49     // Re-map mapped keys to the new value before passing them on.
     50     std::map<uint32,uint32>::iterator mapped =
     51         mapped_keys.find(event.usb_keycode());
     52     if (mapped != mapped_keys.end()) {
     53       protocol::KeyEvent new_event(event);
     54       new_event.set_usb_keycode(mapped->second);
     55       InputFilter::InjectKeyEvent(new_event);
     56       return;
     57     }
     58   }
     59 
     60   InputFilter::InjectKeyEvent(event);
     61 }
     62 
     63 }  // namespace remoting
     64