OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "content/browser/cocoa/system_hotkey_map.h" | 5 #import "content/browser/cocoa/system_hotkey_map.h" |
6 | 6 |
7 #pragma mark - NSDictionary Helper Functions | 7 #pragma mark - NSDictionary Helper Functions |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 } | 22 } |
23 | 23 |
24 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) { | 24 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) { |
25 return ObjectForKey(dict, key, [NSArray class]); | 25 return ObjectForKey(dict, key, [NSArray class]); |
26 } | 26 } |
27 | 27 |
28 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) { | 28 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) { |
29 return ObjectForKey(dict, key, [NSNumber class]); | 29 return ObjectForKey(dict, key, [NSNumber class]); |
30 } | 30 } |
31 | 31 |
| 32 NSString* StringForKey(NSDictionary* dict, NSString* key) { |
| 33 return ObjectForKey(dict, key, [NSString class]); |
| 34 } |
| 35 |
32 } // namespace | 36 } // namespace |
33 | 37 |
34 #pragma mark - SystemHotkey | 38 #pragma mark - SystemHotkey |
35 | 39 |
36 namespace content { | 40 namespace content { |
37 | 41 |
38 struct SystemHotkey { | 42 struct SystemHotkey { |
39 int key_code; | 43 unsigned short key_code; |
40 int modifiers; | 44 NSUInteger modifiers; |
41 }; | 45 }; |
42 | 46 |
43 #pragma mark - SystemHotkeyMap | 47 #pragma mark - SystemHotkeyMap |
44 | 48 |
45 SystemHotkeyMap::SystemHotkeyMap() { | 49 SystemHotkeyMap::SystemHotkeyMap() { |
46 } | 50 } |
47 SystemHotkeyMap::~SystemHotkeyMap() { | 51 SystemHotkeyMap::~SystemHotkeyMap() { |
48 } | 52 } |
49 | 53 |
50 NSDictionary* SystemHotkeyMap::DictionaryFromData(NSData* data) { | 54 NSDictionary* SystemHotkeyMap::DictionaryFromData(NSData* data) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 continue; | 90 continue; |
87 | 91 |
88 NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled"); | 92 NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled"); |
89 if (!enabled || enabled.boolValue == NO) | 93 if (!enabled || enabled.boolValue == NO) |
90 continue; | 94 continue; |
91 | 95 |
92 NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value"); | 96 NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value"); |
93 if (!value) | 97 if (!value) |
94 continue; | 98 continue; |
95 | 99 |
| 100 NSString* type = StringForKey(value, @"type"); |
| 101 if (!type || ![type isEqualToString:@"standard"]) |
| 102 continue; |
| 103 |
96 NSArray* parameters = ArrayForKey(value, @"parameters"); | 104 NSArray* parameters = ArrayForKey(value, @"parameters"); |
97 if (!parameters || [parameters count] != 3) | 105 if (!parameters || [parameters count] != 3) |
98 continue; | 106 continue; |
99 | 107 |
100 NSNumber* key_code = [parameters objectAtIndex:1]; | 108 NSNumber* key_code = [parameters objectAtIndex:1]; |
101 if (![key_code isKindOfClass:[NSNumber class]]) | 109 if (![key_code isKindOfClass:[NSNumber class]]) |
102 continue; | 110 continue; |
103 | 111 |
104 NSNumber* modifiers = [parameters objectAtIndex:2]; | 112 NSNumber* modifiers = [parameters objectAtIndex:2]; |
105 if (![modifiers isKindOfClass:[NSNumber class]]) | 113 if (![modifiers isKindOfClass:[NSNumber class]]) |
106 continue; | 114 continue; |
107 | 115 |
108 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); | 116 ReserveHotkey(key_code.unsignedShortValue, |
| 117 modifiers.unsignedIntegerValue, |
| 118 hotkey_system_effect); |
109 } | 119 } |
110 | 120 |
111 return true; | 121 return true; |
112 } | 122 } |
113 | 123 |
114 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const { | 124 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const { |
115 NSUInteger modifiers = | 125 NSUInteger modifiers = |
116 NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask; | 126 NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask; |
117 return IsHotkeyReserved(event.keyCode, event.modifierFlags & modifiers); | 127 return IsHotkeyReserved(event.keyCode, event.modifierFlags & modifiers); |
118 } | 128 } |
119 | 129 |
120 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) const { | 130 bool SystemHotkeyMap::IsHotkeyReserved(unsigned short key_code, |
| 131 NSUInteger modifiers) const { |
121 std::vector<SystemHotkey>::const_iterator it; | 132 std::vector<SystemHotkey>::const_iterator it; |
122 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { | 133 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { |
123 if (it->key_code == key_code && it->modifiers == modifiers) | 134 if (it->key_code == key_code && it->modifiers == modifiers) |
124 return true; | 135 return true; |
125 } | 136 } |
126 return false; | 137 return false; |
127 } | 138 } |
128 | 139 |
129 void SystemHotkeyMap::ReserveHotkey(int key_code, | 140 void SystemHotkeyMap::ReserveHotkey(unsigned short key_code, |
130 int modifiers, | 141 NSUInteger modifiers, |
131 NSString* system_effect) { | 142 NSString* system_effect) { |
132 ReserveHotkey(key_code, modifiers); | 143 ReserveHotkey(key_code, modifiers); |
133 | 144 |
134 // If a hotkey exists for toggling through the windows of an application, then | 145 // If a hotkey exists for toggling through the windows of an application, then |
135 // adding shift to that hotkey toggles through the windows backwards. | 146 // adding shift to that hotkey toggles through the windows backwards. |
136 if ([system_effect isEqualToString:@"27"]) | 147 if ([system_effect isEqualToString:@"27"]) |
137 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); | 148 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); |
138 } | 149 } |
139 | 150 |
140 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { | 151 void SystemHotkeyMap::ReserveHotkey(unsigned short key_code, |
| 152 NSUInteger modifiers) { |
| 153 // Hotkeys require at least one of control, command, or alternate keys to be |
| 154 // down. |
| 155 NSUInteger required_modifiers = |
| 156 NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask; |
| 157 if ((modifiers & required_modifiers) == 0) |
| 158 return; |
| 159 |
141 SystemHotkey hotkey; | 160 SystemHotkey hotkey; |
142 hotkey.key_code = key_code; | 161 hotkey.key_code = key_code; |
143 hotkey.modifiers = modifiers; | 162 hotkey.modifiers = modifiers; |
144 system_hotkeys_.push_back(hotkey); | 163 system_hotkeys_.push_back(hotkey); |
145 } | 164 } |
146 | 165 |
147 } // namespace content | 166 } // namespace content |
OLD | NEW |