OLD | NEW |
| (Empty) |
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 #include "remoting/client/plugin/normalizing_input_filter_mac.h" | |
6 | |
7 #include <map> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "remoting/proto/event.pb.h" | |
12 #include "remoting/protocol/usb_key_codes.h" | |
13 | |
14 namespace remoting { | |
15 | |
16 NormalizingInputFilterMac::NormalizingInputFilterMac( | |
17 protocol::InputStub* input_stub) | |
18 : protocol::InputFilter(input_stub) { | |
19 } | |
20 | |
21 NormalizingInputFilterMac::~NormalizingInputFilterMac() {} | |
22 | |
23 void NormalizingInputFilterMac::InjectKeyEvent(const protocol::KeyEvent& event) | |
24 { | |
25 DCHECK(event.has_usb_keycode()); | |
26 | |
27 bool is_special_key = event.usb_keycode() == kUsbLeftControl || | |
28 event.usb_keycode() == kUsbLeftShift || | |
29 event.usb_keycode() == kUsbLeftAlt || | |
30 event.usb_keycode() == kUsbRightControl || | |
31 event.usb_keycode() == kUsbRightShift || | |
32 event.usb_keycode() == kUsbRightAlt || | |
33 event.usb_keycode() == kUsbTab; | |
34 | |
35 bool is_cmd_key = event.usb_keycode() == kUsbLeftOs || | |
36 event.usb_keycode() == kUsbRightOs; | |
37 | |
38 if (event.usb_keycode() == kUsbCapsLock) { | |
39 // Mac OS X generates keydown/keyup on lock-state transitions, rather than | |
40 // when the key is pressed & released, so fake keydown/keyup on each event. | |
41 protocol::KeyEvent newEvent(event); | |
42 | |
43 newEvent.set_pressed(true); | |
44 InputFilter::InjectKeyEvent(newEvent); | |
45 newEvent.set_pressed(false); | |
46 InputFilter::InjectKeyEvent(newEvent); | |
47 | |
48 return; | |
49 } else if (!is_cmd_key && !is_special_key) { | |
50 // Track keydown/keyup events for non-modifiers, so we can release them if | |
51 // necessary (see below). | |
52 if (event.pressed()) { | |
53 key_pressed_map_[event.usb_keycode()] = event; | |
54 } else { | |
55 key_pressed_map_.erase(event.usb_keycode()); | |
56 } | |
57 } | |
58 | |
59 if (is_cmd_key && !event.pressed()) { | |
60 // Mac OS X will not generate release events for keys pressed while Cmd is | |
61 // pressed, so release all pressed keys when Cmd is released. | |
62 GenerateKeyupEvents(); | |
63 } | |
64 | |
65 InputFilter::InjectKeyEvent(event); | |
66 } | |
67 | |
68 void NormalizingInputFilterMac::GenerateKeyupEvents() { | |
69 for (KeyPressedMap::iterator i = key_pressed_map_.begin(); | |
70 i != key_pressed_map_.end(); ++i) { | |
71 // The generated key up event will have the same key code and lock states | |
72 // as the original key down event. | |
73 protocol::KeyEvent event = i->second; | |
74 event.set_pressed(false); | |
75 InputFilter::InjectKeyEvent(event); | |
76 } | |
77 | |
78 // Clearing the map now that we have released all the pressed keys. | |
79 key_pressed_map_.clear(); | |
80 } | |
81 | |
82 } // namespace remoting | |
OLD | NEW |