Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/client/plugin/mac_key_event_processor.h" | 5 #include "remoting/client/plugin/mac_key_event_processor.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 namespace remoting { | 9 namespace remoting { |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 // A list of known keycodes. | 13 const unsigned int kUsbLeftControl = 0x0700e0; |
| 14 const int kShift = 16; | 14 const unsigned int kUsbLeftShift = 0x0700e1; |
| 15 const int kControl = 17; | 15 const unsigned int kUsbLeftOption = 0x0700e2; |
| 16 const int kOption = 18; | 16 const unsigned int kUsbLeftCmd = 0x0700e3; |
| 17 const int kCapsLock = 20; | 17 const unsigned int kUsbRightControl = 0x0700e4; |
| 18 const int kLeftCmd = 91; | 18 const unsigned int kUsbRightShift = 0x0700e5; |
| 19 const int kRightCmd = 93; | 19 const unsigned int kUsbRightOption = 0x0700e6; |
| 20 const unsigned int kUsbRightCmd = 0x0700e7; | |
| 21 const unsigned int kUsbTab = 0x07002b; | |
| 20 | 22 |
| 21 } // namespace | 23 } // namespace |
| 22 | 24 |
| 23 MacKeyEventProcessor::MacKeyEventProcessor(protocol::InputStub* input_stub) | 25 MacKeyEventProcessor::MacKeyEventProcessor(protocol::InputStub* input_stub) |
| 24 : protocol::InputFilter(input_stub) { | 26 : protocol::InputFilter(input_stub) { |
| 25 } | 27 } |
| 26 | 28 |
| 27 MacKeyEventProcessor::~MacKeyEventProcessor() { | 29 MacKeyEventProcessor::~MacKeyEventProcessor() { |
| 28 } | 30 } |
| 29 | 31 |
| 30 void MacKeyEventProcessor::InjectKeyEvent(const protocol::KeyEvent& event) { | 32 void MacKeyEventProcessor::InjectKeyEvent(const protocol::KeyEvent& event) { |
|
Wez
2012/05/22 00:17:08
nit: DCHECK(event.has_usb_keycode());
| |
| 31 if (event.pressed()) { | 33 bool special_key = event.usb_keycode() == kUsbLeftControl || |
|
Wez
2012/05/22 00:17:08
nit: is_special_key
| |
| 32 key_pressed_map_[event.keycode()] = event; | 34 event.usb_keycode() == kUsbLeftShift || |
| 33 } else { | 35 event.usb_keycode() == kUsbLeftOption || |
| 34 key_pressed_map_.erase(event.keycode()); | 36 event.usb_keycode() == kUsbRightControl || |
| 35 if (event.keycode() == kLeftCmd || event.keycode() == kRightCmd) | 37 event.usb_keycode() == kUsbRightShift || |
| 36 GenerateKeyupEvents(); | 38 event.usb_keycode() == kUsbRightOption || |
| 39 event.usb_keycode() == kUsbTab; | |
| 40 | |
| 41 bool cmd_key = event.usb_keycode() == kUsbLeftCmd || | |
|
Wez
2012/05/22 00:17:08
nit: is_cmd_key
| |
| 42 event.usb_keycode() == kUsbRightCmd; | |
| 43 | |
| 44 if (!cmd_key && !special_key) { | |
| 45 if (event.pressed()) { | |
| 46 key_pressed_map_[event.usb_keycode()] = event; | |
| 47 } else { | |
| 48 key_pressed_map_.erase(event.usb_keycode()); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 if (cmd_key && !event.pressed()) { | |
| 53 GenerateKeyupEvents(); | |
| 37 } | 54 } |
| 38 | 55 |
| 39 InputFilter::InjectKeyEvent(event); | 56 InputFilter::InjectKeyEvent(event); |
| 40 } | 57 } |
| 41 | 58 |
| 42 int MacKeyEventProcessor::NumberOfPressedKeys() const { | |
| 43 return key_pressed_map_.size(); | |
| 44 } | |
| 45 | |
| 46 void MacKeyEventProcessor::GenerateKeyupEvents() { | 59 void MacKeyEventProcessor::GenerateKeyupEvents() { |
| 47 // A list of key codes to be erased from |key_pressed_map_|. | 60 // A list of key codes to be erased from |key_pressed_map_|. |
| 48 typedef std::vector<int> KeycodeList; | 61 typedef std::vector<int> KeycodeList; |
| 49 KeycodeList keycodes; | 62 KeycodeList keycodes; |
| 50 | 63 |
| 51 for (KeyPressedMap::iterator i = key_pressed_map_.begin(); | 64 for (KeyPressedMap::iterator i = key_pressed_map_.begin(); |
| 52 i != key_pressed_map_.end(); ++i) { | 65 i != key_pressed_map_.end(); ++i) { |
| 53 const int keycode = i->first; | 66 const int keycode = i->first; |
| 54 | 67 |
| 55 if (keycode == kCapsLock || keycode == kOption || | |
| 56 keycode == kControl || keycode == kShift || | |
| 57 keycode == kLeftCmd || keycode == kRightCmd) { | |
| 58 continue; | |
| 59 } | |
| 60 | |
| 61 keycodes.push_back(keycode); | 68 keycodes.push_back(keycode); |
| 62 protocol::KeyEvent event = i->second; | 69 protocol::KeyEvent event = i->second; |
| 63 event.set_pressed(false); | 70 event.set_pressed(false); |
| 64 InputFilter::InjectKeyEvent(event); | 71 InputFilter::InjectKeyEvent(event); |
| 65 } | 72 } |
| 66 | 73 |
| 67 for (KeycodeList::iterator i = keycodes.begin(); i != keycodes.end(); ++i) { | 74 for (KeycodeList::iterator i = keycodes.begin(); i != keycodes.end(); ++i) { |
| 68 key_pressed_map_.erase(*i); | 75 key_pressed_map_.erase(*i); |
| 69 } | 76 } |
| 70 } | 77 } |
| 71 | 78 |
| 72 } // namespace remoting | 79 } // namespace remoting |
| OLD | NEW |