normalizing_input_filter_mac.cc revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright 2013 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// NormalizingInputFilterMac is designed to solve the problem of missing keyup 6// events on Mac. 7// 8// PROBLEM 9// 10// On Mac if user presses CMD and then C key there is no keyup event generated 11// for C when user releases the C key before the CMD key. 12// The cause is that CMD + C triggers a system action and Chrome injects only a 13// keydown event for the C key. Safari shares the same behavior. 14// 15// SOLUTION 16// 17// When a keyup event for CMD key happens we will check all prior keydown 18// events received and inject corresponding keyup events artificially, with 19// the exception of: 20// 21// SHIFT, CONTROL, OPTION, LEFT CMD, RIGHT CMD and CAPS LOCK 22// 23// because they are reported by Chrome correctly. 24// 25// There are a couple cases that this solution doesn't work perfectly, one 26// of them leads to duplicated keyup events. 27// 28// User performs this sequence of actions: 29// 30// CMD DOWN, C DOWN, CMD UP, C UP 31// 32// In this case the algorithm will generate: 33// 34// CMD DOWN, C DOWN, C UP, CMD UP, C UP 35// 36// Because we artificially generate keyup events the C UP event is duplicated 37// as user releases the key after CMD key. This would not be a problem as the 38// receiver end will drop this duplicated keyup event. 39 40#include "remoting/client/plugin/normalizing_input_filter.h" 41 42#include <map> 43#include <vector> 44 45#include "base/logging.h" 46#include "remoting/proto/event.pb.h" 47 48namespace remoting { 49 50namespace { 51 52const unsigned int kUsbCapsLock = 0x070039; 53const unsigned int kUsbLeftControl = 0x0700e0; 54const unsigned int kUsbLeftShift = 0x0700e1; 55const unsigned int kUsbLeftOption = 0x0700e2; 56const unsigned int kUsbLeftCmd = 0x0700e3; 57const unsigned int kUsbRightControl = 0x0700e4; 58const unsigned int kUsbRightShift = 0x0700e5; 59const unsigned int kUsbRightOption = 0x0700e6; 60const unsigned int kUsbRightCmd = 0x0700e7; 61const unsigned int kUsbTab = 0x07002b; 62 63} // namespace 64 65class NormalizingInputFilterMac : public protocol::InputFilter { 66 public: 67 explicit NormalizingInputFilterMac(protocol::InputStub* input_stub); 68 virtual ~NormalizingInputFilterMac() {} 69 70 // InputFilter overrides. 71 virtual void InjectKeyEvent(const protocol::KeyEvent& event) OVERRIDE; 72 73 private: 74 // Generate keyup events for any keys pressed with CMD. 75 void GenerateKeyupEvents(); 76 77 // A map that stores pressed keycodes and the corresponding key event. 78 typedef std::map<int, protocol::KeyEvent> KeyPressedMap; 79 KeyPressedMap key_pressed_map_; 80 81 DISALLOW_COPY_AND_ASSIGN(NormalizingInputFilterMac); 82}; 83 84NormalizingInputFilterMac::NormalizingInputFilterMac( 85 protocol::InputStub* input_stub) 86 : protocol::InputFilter(input_stub) { 87} 88 89void NormalizingInputFilterMac::InjectKeyEvent(const protocol::KeyEvent& event) 90{ 91 DCHECK(event.has_usb_keycode()); 92 93 bool is_special_key = event.usb_keycode() == kUsbLeftControl || 94 event.usb_keycode() == kUsbLeftShift || 95 event.usb_keycode() == kUsbLeftOption || 96 event.usb_keycode() == kUsbRightControl || 97 event.usb_keycode() == kUsbRightShift || 98 event.usb_keycode() == kUsbRightOption || 99 event.usb_keycode() == kUsbTab; 100 101 bool is_cmd_key = event.usb_keycode() == kUsbLeftCmd || 102 event.usb_keycode() == kUsbRightCmd; 103 104 if (event.usb_keycode() == kUsbCapsLock) { 105 // Mac OS X generates keydown/keyup on lock-state transitions, rather than 106 // when the key is pressed & released, so fake keydown/keyup on each event. 107 protocol::KeyEvent newEvent(event); 108 109 newEvent.set_pressed(true); 110 InputFilter::InjectKeyEvent(newEvent); 111 newEvent.set_pressed(false); 112 InputFilter::InjectKeyEvent(newEvent); 113 114 return; 115 } else if (!is_cmd_key && !is_special_key) { 116 // Track keydown/keyup events for non-modifiers, so we can release them if 117 // necessary (see below). 118 if (event.pressed()) { 119 key_pressed_map_[event.usb_keycode()] = event; 120 } else { 121 key_pressed_map_.erase(event.usb_keycode()); 122 } 123 } 124 125 if (is_cmd_key && !event.pressed()) { 126 // Mac OS X will not generate release events for keys pressed while Cmd is 127 // pressed, so release all pressed keys when Cmd is released. 128 GenerateKeyupEvents(); 129 } 130 131 InputFilter::InjectKeyEvent(event); 132} 133 134void NormalizingInputFilterMac::GenerateKeyupEvents() { 135 for (KeyPressedMap::iterator i = key_pressed_map_.begin(); 136 i != key_pressed_map_.end(); ++i) { 137 // The generated key up event will have the same key code and lock states 138 // as the original key down event. 139 protocol::KeyEvent event = i->second; 140 event.set_pressed(false); 141 InputFilter::InjectKeyEvent(event); 142 } 143 144 // Clearing the map now that we have released all the pressed keys. 145 key_pressed_map_.clear(); 146} 147 148scoped_ptr<protocol::InputFilter> CreateNormalizingInputFilter( 149 protocol::InputStub* input_stub) { 150 return scoped_ptr<protocol::InputFilter>( 151 new NormalizingInputFilterMac(input_stub)); 152} 153 154} // namespace remoting 155