OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import "content/browser/cocoa/system_hotkey_map.h" | |
6 | |
7 #pragma mark - NSDictionary Helper Functions | |
8 | |
9 namespace { | |
10 | |
11 // All 4 following functions return nil if the object doesn't exist, or isn't of | |
12 // the right class. | |
13 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) { | |
14 id object = [dict objectForKey:key]; | |
15 if (![object isKindOfClass:aClass]) | |
16 return nil; | |
17 return object; | |
18 } | |
19 | |
20 NSDictionary* DictionaryForKey(NSDictionary* dict, NSString* key) { | |
21 return ObjectForKey(dict, key, [NSDictionary class]); | |
22 } | |
23 | |
24 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) { | |
25 return ObjectForKey(dict, key, [NSArray class]); | |
26 } | |
27 | |
28 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) { | |
29 return ObjectForKey(dict, key, [NSNumber class]); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 #pragma mark - SystemHotkey | |
35 | |
36 namespace content { | |
37 | |
38 struct SystemHotkey { | |
39 int key_code; | |
40 int modifiers; | |
41 }; | |
42 | |
43 #pragma mark - SystemHotkeyMap | |
44 | |
45 SystemHotkeyMap::SystemHotkeyMap() { | |
46 } | |
47 SystemHotkeyMap::~SystemHotkeyMap() { | |
48 } | |
49 | |
50 NSDictionary* SystemHotkeyMap::DictionaryFromData(NSData* data) { | |
51 if (!data) | |
52 return nil; | |
53 | |
54 NSError* error = nil; | |
55 NSPropertyListFormat format; | |
56 NSDictionary* dictionary = | |
57 [NSPropertyListSerialization propertyListWithData:data | |
58 options:0 | |
59 format:&format | |
60 error:&error]; | |
61 | |
62 if (![dictionary isKindOfClass:[NSDictionary class]]) | |
63 return nil; | |
64 | |
65 return dictionary; | |
66 } | |
67 | |
68 bool SystemHotkeyMap::ParseDictionary(NSDictionary* dictionary) { | |
69 system_hotkeys_.clear(); | |
70 | |
71 if (!dictionary) | |
72 return false; | |
73 | |
74 NSDictionary* hotkey_dictionaries = | |
75 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys"); | |
76 if (!hotkey_dictionaries) | |
77 return false; | |
78 | |
79 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) { | |
80 if (![hotkey_system_effect isKindOfClass:[NSString class]]) | |
81 continue; | |
82 | |
83 NSDictionary* hotkey_dictionary = | |
84 [hotkey_dictionaries objectForKey:hotkey_system_effect]; | |
85 if (![hotkey_dictionary isKindOfClass:[NSDictionary class]]) | |
86 continue; | |
87 | |
88 NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled"); | |
89 if (!enabled || enabled.boolValue == NO) | |
90 continue; | |
91 | |
92 NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value"); | |
93 if (!value) | |
94 continue; | |
95 | |
96 NSArray* parameters = ArrayForKey(value, @"parameters"); | |
97 if (!parameters || [parameters count] != 3) | |
98 continue; | |
99 | |
100 NSNumber* key_code = [parameters objectAtIndex:1]; | |
101 if (![key_code isKindOfClass:[NSNumber class]]) | |
102 continue; | |
103 | |
104 NSNumber* modifiers = [parameters objectAtIndex:2]; | |
105 if (![modifiers isKindOfClass:[NSNumber class]]) | |
106 continue; | |
107 | |
108 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); | |
109 } | |
110 | |
111 return true; | |
112 } | |
113 | |
114 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const { | |
115 NSUInteger modifiers = | |
116 NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask; | |
117 return IsHotkeyReserved(event.keyCode, event.modifierFlags & modifiers); | |
118 } | |
119 | |
120 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) const { | |
121 std::vector<SystemHotkey>::const_iterator it; | |
122 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { | |
123 if (it->key_code == key_code && it->modifiers == modifiers) | |
124 return true; | |
125 } | |
126 return false; | |
127 } | |
128 | |
129 void SystemHotkeyMap::ReserveHotkey(int key_code, | |
130 int modifiers, | |
131 NSString* system_effect) { | |
132 ReserveHotkey(key_code, modifiers); | |
133 | |
134 // If a hotkey exists for toggling through the windows of an application, then | |
135 // adding shift to that hotkey toggles through the windows backwards. | |
136 if ([system_effect isEqualToString:@"27"]) | |
137 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); | |
138 } | |
139 | |
140 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { | |
141 SystemHotkey hotkey; | |
142 hotkey.key_code = key_code; | |
143 hotkey.modifiers = modifiers; | |
144 system_hotkeys_.push_back(hotkey); | |
145 } | |
146 | |
147 } // namespace content | |
OLD | NEW |