Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 "chrome/browser/extensions/global_shortcut_listener_mac.h" | |
| 6 | |
| 7 #include <ApplicationServices/ApplicationServices.h> | |
| 8 #import <Cocoa/Cocoa.h> | |
| 9 #include <IOKit/hidsystem/ev_keymap.h> | |
| 10 | |
| 11 #include "chrome/browser/extensions/api/commands/command_service.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "ui/base/accelerators/accelerator.h" | |
| 14 #include "ui/events/event.h" | |
| 15 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | |
| 16 | |
| 17 using content::BrowserThread; | |
| 18 using extensions::GlobalShortcutListenerMac; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // The media keys subtype. | |
| 23 const int kSystemDefinedEventMediaKeys = 8; | |
|
Mark Mentovai
2013/11/26 15:32:46
Can you describe how you found this number? Docume
smus
2013/12/09 08:37:16
Unfortunately I can't find official docs. I don't
| |
| 24 | |
| 25 OSStatus HotKeyHandler( | |
| 26 EventHandlerCallRef next_handler, EventRef event, void* user_data) { | |
| 27 DVLOG(0) << "HotKeyHandler fired with event: " << event; | |
| 28 // Extract the hotkey from the event. | |
| 29 EventHotKeyID hot_key_id; | |
| 30 int result = GetEventParameter(event, kEventParamDirectObject, | |
| 31 typeEventHotKeyID, NULL, sizeof(hot_key_id), NULL, &hot_key_id); | |
| 32 if (result != noErr) | |
| 33 return result; | |
| 34 | |
| 35 GlobalShortcutListenerMac* shortcut_listener = | |
| 36 static_cast<GlobalShortcutListenerMac*>(user_data); | |
| 37 shortcut_listener->OnKeyEvent(hot_key_id); | |
| 38 return noErr; | |
| 39 } | |
| 40 | |
| 41 static base::LazyInstance<extensions::GlobalShortcutListenerMac> g_instance = | |
| 42 LAZY_INSTANCE_INITIALIZER; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 namespace extensions { | |
| 47 | |
| 48 // static | |
| 49 GlobalShortcutListener* GlobalShortcutListener::GetInstance() { | |
| 50 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 51 return g_instance.Pointer(); | |
| 52 } | |
| 53 | |
| 54 GlobalShortcutListenerMac::GlobalShortcutListenerMac() | |
| 55 : is_listening_(false) { | |
| 56 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 57 } | |
| 58 | |
| 59 GlobalShortcutListenerMac::~GlobalShortcutListenerMac() { | |
| 60 if (is_listening_) | |
| 61 StopListening(); | |
| 62 | |
| 63 // If media keys are still registered, make sure we stop the tap. | |
| 64 if (AreMediaKeysRegistered()) | |
| 65 StopWatchingMediaKeys(); | |
| 66 } | |
| 67 | |
| 68 void GlobalShortcutListenerMac::StartListening() { | |
| 69 DCHECK(!is_listening_); // Don't start twice. | |
| 70 DCHECK(!hot_key_ids_.empty()); // Don't start if no hot key registered. | |
| 71 DCHECK(!id_hot_keys_.empty()); | |
| 72 is_listening_ = true; | |
| 73 } | |
| 74 | |
| 75 void GlobalShortcutListenerMac::StopListening() { | |
| 76 DCHECK(is_listening_); // No point if we are not already listening. | |
| 77 DCHECK(hot_key_ids_.empty()); // Make sure the set is clean. | |
| 78 DCHECK(id_hot_keys_.empty()); | |
| 79 is_listening_ = false; | |
| 80 } | |
| 81 | |
| 82 bool GlobalShortcutListenerMac::OnKeyEvent(EventHotKeyID hot_key_id) { | |
| 83 // Look up the accelerator based on this hot key ID. | |
| 84 DVLOG(0) << "OnKeyEvent! hot_key_id: " << hot_key_id.id; | |
| 85 const ui::Accelerator& accelerator = id_hot_keys_[hot_key_id.id]; | |
| 86 DVLOG(0) << "Key code: " << accelerator.key_code() | |
| 87 << " modifiers: " << accelerator.modifiers(); | |
| 88 NotifyKeyPressed(accelerator); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 bool GlobalShortcutListenerMac::OnMediaKeyEvent(int media_key_code) { | |
| 93 ui::KeyboardCode key_code = MediaKeyCodeToKeyboardCode(media_key_code); | |
| 94 DVLOG(0) << "OnMediaKeyEvent! keyCode: " << key_code; | |
| 95 // Create an accelerator corresponding to the keyCode. | |
| 96 ui::Accelerator accelerator(key_code, 0); | |
| 97 // Look for a match with a bound hot_key. | |
| 98 if (hot_key_ids_.find(accelerator) != hot_key_ids_.end()) { | |
| 99 // If matched, callback to the event handling system. | |
| 100 NotifyKeyPressed(accelerator); | |
| 101 return true; | |
| 102 } | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 void GlobalShortcutListenerMac::RegisterAccelerator( | |
| 107 const ui::Accelerator& accelerator, | |
| 108 GlobalShortcutListener::Observer* observer) { | |
| 109 DVLOG(0) << "Registered keyCode: " << accelerator.key_code() | |
| 110 << ", modifiers: " << accelerator.modifiers(); | |
| 111 | |
| 112 // If this is the first media key registered, start the event tap. | |
| 113 if (CommandService::IsMediaKey(accelerator) && !AreMediaKeysRegistered()) { | |
| 114 DVLOG(0) << "Registered the first media key, so starting event tap."; | |
| 115 StartWatchingMediaKeys(); | |
| 116 } | |
| 117 | |
| 118 // Register hot_key if they are non-media keyboard shortcuts. | |
| 119 if (!CommandService::IsMediaKey(accelerator)) | |
| 120 RegisterHotKey(accelerator); | |
| 121 | |
| 122 // Store the hotkey-ID mappings we will need for lookup later. | |
| 123 id_hot_keys_[hot_key_id_] = accelerator; | |
| 124 hot_key_ids_[accelerator] = hot_key_id_; | |
| 125 ++hot_key_id_; | |
| 126 GlobalShortcutListener::RegisterAccelerator(accelerator, observer); | |
| 127 } | |
| 128 | |
| 129 void GlobalShortcutListenerMac::UnregisterAccelerator( | |
| 130 const ui::Accelerator& accelerator, | |
| 131 GlobalShortcutListener::Observer* observer) { | |
| 132 // Unregister the hot_key if it's a keyboard shortcut. | |
| 133 if (!CommandService::IsMediaKey(accelerator)) | |
| 134 UnregisterHotKey(accelerator); | |
| 135 | |
| 136 // Remove hot_key from the mappings. | |
| 137 int id = hot_key_ids_[accelerator]; | |
| 138 id_hot_keys_.erase(id); | |
| 139 hot_key_ids_.erase(accelerator); | |
| 140 GlobalShortcutListener::UnregisterAccelerator(accelerator, observer); | |
| 141 | |
| 142 // If we unregistered a media key, and now if no media keys are registered, | |
| 143 // stop the media key tap. | |
| 144 if (CommandService::IsMediaKey(accelerator) && !AreMediaKeysRegistered()) { | |
| 145 DVLOG(0) << "Unregistered the last media key, stopping event tap."; | |
| 146 StopWatchingMediaKeys(); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void GlobalShortcutListenerMac::RegisterHotKey( | |
|
Mark Mentovai
2013/11/26 15:32:46
RegisterAccelerator, RegisterHotKey, StartWatching
smus
2013/12/09 08:37:16
Accelerator is the Chrome terminology, so stuck wi
| |
| 151 const ui::Accelerator& accelerator) { | |
| 152 DVLOG(0) << "Registering hot_key. Keycode: " << accelerator.key_code(); | |
| 153 EventHotKeyRef hot_key_ref; | |
| 154 EventHotKeyID event_hot_key_id; | |
| 155 EventHandlerUPP hot_key_function = NewEventHandlerUPP(HotKeyHandler); | |
| 156 | |
| 157 EventTypeSpec event_type; | |
| 158 event_type.eventClass = kEventClassKeyboard; | |
| 159 event_type.eventKind = kEventHotKeyPressed; | |
| 160 InstallApplicationEventHandler(hot_key_function, 1, &event_type, this, NULL); | |
|
Mark Mentovai
2013/11/26 15:32:46
You install this each time you pass through Regist
smus
2013/12/09 08:37:16
From http://oreilly.com/catalog/learncarbon/chapte
Mark Mentovai
2013/12/09 19:31:54
smus wrote:
smus
2013/12/09 23:37:06
Sorry, completely misunderstood the first time! No
| |
| 161 | |
| 162 // Signature uniquely identifies the application that owns this hot_key. | |
| 163 event_hot_key_id.signature = 'rimZ'; | |
|
Mark Mentovai
2013/11/26 15:32:46
You should get the bundle signature instead of har
smus
2013/12/09 08:37:16
Can you provide any pointer for doing this? I don'
| |
| 164 event_hot_key_id.id = hot_key_id_; | |
|
Mark Mentovai
2013/11/26 15:32:46
Perhaps the uses of hot_key_id_ here and on line 1
smus
2013/12/09 08:37:16
Done.
| |
| 165 | |
| 166 // Translate ui::Accelerator modifiers to cmdKey, altKey, etc. | |
| 167 int modifiers = 0; | |
| 168 modifiers += (accelerator.IsShiftDown() ? shiftKey : 0); | |
|
Mark Mentovai
2013/11/26 15:32:46
These are bits, right? Use |= instead of +=.
smus
2013/12/09 08:37:16
Done.
| |
| 169 modifiers += (accelerator.IsCtrlDown() ? controlKey : 0); | |
| 170 modifiers += (accelerator.IsAltDown() ? optionKey : 0); | |
| 171 modifiers += (accelerator.IsCmdDown() ? cmdKey : 0); | |
| 172 | |
| 173 unichar character; | |
| 174 unichar character_nomods; | |
| 175 int key_code = ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(), 0, | |
| 176 &character, &character_nomods); | |
| 177 DVLOG(0) << "RegisterHotKey. Code: " << key_code | |
| 178 << " modifier: " << modifiers; | |
| 179 | |
| 180 // Register the event hot key. | |
| 181 RegisterEventHotKey(key_code, modifiers, event_hot_key_id, | |
| 182 GetApplicationEventTarget(), 0, &hot_key_ref); | |
| 183 | |
| 184 // Note: hot_key_id_ will be incremented in the caller (RegisterAccelerator). | |
| 185 id_hot_key_refs_[hot_key_id_] = hot_key_ref; | |
| 186 } | |
| 187 | |
| 188 void GlobalShortcutListenerMac::UnregisterHotKey( | |
| 189 const ui::Accelerator& accelerator) { | |
| 190 // Get the ref corresponding to this accelerator. | |
| 191 int id = hot_key_ids_[accelerator]; | |
| 192 EventHotKeyRef ref = id_hot_key_refs_[id]; | |
| 193 // Unregister the event hot key. | |
| 194 UnregisterEventHotKey(ref); | |
| 195 | |
| 196 // Remove the event from the mapping. | |
| 197 id_hot_key_refs_.erase(id); | |
| 198 } | |
| 199 | |
| 200 void GlobalShortcutListenerMac::StartWatchingMediaKeys() { | |
| 201 DVLOG(0) << "StartWatchingMediaKeys"; | |
| 202 // Make sure there's no existing event tap. | |
| 203 DCHECK(event_tap_ == NULL); | |
|
Mark Mentovai
2013/11/26 15:32:46
It’s not necessarily going to be NULL because you
smus
2013/12/09 08:37:16
Done.
| |
| 204 | |
| 205 // Add an event tap to intercept the system defined media key events. | |
| 206 event_tap_ = CGEventTapCreate(kCGSessionEventTap, | |
| 207 kCGHeadInsertEventTap, | |
| 208 kCGEventTapOptionDefault, | |
| 209 CGEventMaskBit(NX_SYSDEFINED), | |
| 210 EventTapCallback, | |
| 211 this); | |
| 212 if (event_tap_ == NULL) { | |
| 213 LOG(ERROR) << "Error: failed to create event tap."; | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 event_tap_source_ = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, | |
| 218 event_tap_, 0); | |
| 219 if (event_tap_source_ == NULL) { | |
| 220 LOG(ERROR) << "Error: failed to create new run loop source."; | |
| 221 return; | |
| 222 } | |
| 223 | |
| 224 CFRunLoopAddSource(CFRunLoopGetCurrent(), event_tap_source_, | |
|
Mark Mentovai
2013/11/26 15:32:46
You never remove this source (in StopWatchingMedia
smus
2013/12/09 08:37:16
Oops, good point, thanks!
| |
| 225 kCFRunLoopCommonModes); | |
| 226 } | |
| 227 | |
| 228 void GlobalShortcutListenerMac::StopWatchingMediaKeys() { | |
| 229 DVLOG(0) << "StopWatchingMediaKeys"; | |
| 230 // Invalidate the event tap. | |
| 231 DCHECK(event_tap_ != NULL); | |
| 232 CFMachPortInvalidate(event_tap_); | |
| 233 CFRelease(event_tap_); | |
| 234 event_tap_ = NULL; | |
| 235 | |
| 236 // Release the event tap source. | |
| 237 DCHECK(event_tap_source_ != NULL); | |
| 238 CFRelease(event_tap_source_); | |
| 239 event_tap_source_ = NULL; | |
| 240 } | |
| 241 | |
| 242 ui::KeyboardCode GlobalShortcutListenerMac::MediaKeyCodeToKeyboardCode( | |
| 243 int key_code) { | |
| 244 switch (key_code) { | |
| 245 case NX_KEYTYPE_PLAY: | |
| 246 return ui::VKEY_MEDIA_PLAY_PAUSE; | |
| 247 case NX_KEYTYPE_PREVIOUS: | |
| 248 case NX_KEYTYPE_REWIND: | |
| 249 return ui::VKEY_MEDIA_PREV_TRACK; | |
| 250 case NX_KEYTYPE_NEXT: | |
| 251 case NX_KEYTYPE_FAST: | |
| 252 return ui::VKEY_MEDIA_NEXT_TRACK; | |
| 253 } | |
| 254 return ui::VKEY_UNKNOWN; | |
| 255 } | |
| 256 | |
| 257 bool GlobalShortcutListenerMac::AreMediaKeysRegistered() { | |
| 258 // Iterate through registered accelerators, looking for media keys. | |
| 259 HotKeyIdMap::iterator iter; | |
| 260 for (iter = hot_key_ids_.begin(); iter != hot_key_ids_.end(); ++iter) { | |
| 261 if (CommandService::IsMediaKey(iter->first)) | |
| 262 return true; | |
| 263 } | |
| 264 return false; | |
| 265 } | |
| 266 | |
| 267 void GlobalShortcutListenerMac::EnableTap() { | |
| 268 CGEventTapEnable(event_tap_, TRUE); | |
| 269 } | |
| 270 | |
| 271 // Processed events should propagate if they aren't handled by any listeners. | |
| 272 // For events that don't matter, this handler should return as quickly as | |
| 273 // possible. | |
| 274 // Returning event causes the event to propagate to other applications. | |
| 275 // Returning NULL prevents the event from propagating. | |
| 276 // static | |
| 277 CGEventRef GlobalShortcutListenerMac::EventTapCallback( | |
| 278 CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon) { | |
| 279 GlobalShortcutListenerMac* shortcut_listener = | |
| 280 static_cast<GlobalShortcutListenerMac*>(refcon); | |
| 281 | |
| 282 // Handle the timeout case by re-enabling the tap. | |
| 283 if (type == kCGEventTapDisabledByTimeout) { | |
| 284 shortcut_listener->EnableTap(); | |
|
Mark Mentovai
2013/11/26 15:32:46
EnableTap is a one-liner that is only used here. I
smus
2013/12/09 08:37:16
Done.
| |
| 285 return event; | |
| 286 } | |
| 287 | |
| 288 // TODO(smus): do some error handling since eventWithCGEvent can fail. | |
|
Mark Mentovai
2013/11/26 15:32:46
What kind of error handling? Why not do it?
smus
2013/12/09 08:37:16
Done.
| |
| 289 NSEvent* ns_event = [NSEvent eventWithCGEvent:event]; | |
|
Mark Mentovai
2013/11/26 15:32:46
Can you point (in a comment) to some documentation
smus
2013/12/09 08:37:16
Added a reference to http://weblog.rogueamoeba.com
| |
| 290 | |
| 291 // Ignore events that are not system defined media keys. | |
| 292 if (type != NX_SYSDEFINED || | |
| 293 [ns_event subtype] != kSystemDefinedEventMediaKeys) { | |
|
Mark Mentovai
2013/11/26 15:32:46
For safety, [ns_event type] should be NSSystemDefi
smus
2013/12/09 08:37:16
Done.
| |
| 294 return event; | |
| 295 } | |
| 296 | |
| 297 NSInteger data1 = [ns_event data1]; | |
| 298 // Ignore media keys that aren't previous, next and play/pause. | |
| 299 int key_code = ((data1 & 0xFFFF0000) >> 16); | |
| 300 if (key_code != NX_KEYTYPE_PLAY && key_code != NX_KEYTYPE_NEXT && | |
| 301 key_code != NX_KEYTYPE_PREVIOUS && key_code != NX_KEYTYPE_FAST && | |
| 302 key_code != NX_KEYTYPE_REWIND) { | |
| 303 return event; | |
| 304 } | |
| 305 | |
| 306 int key_flags = (data1 & 0x0000FFFF); | |
| 307 bool is_key_pressed = (((key_flags & 0xFF00) >> 8)) == 0xA; | |
|
Mark Mentovai
2013/11/26 15:32:46
More magic constants that we don’t control. Docume
smus
2013/12/09 08:37:16
See above.
| |
| 308 | |
| 309 // If the key wasn't pressed (eg. was released), ignore this event. | |
| 310 if (!is_key_pressed) | |
| 311 return event; | |
| 312 | |
| 313 // Now we have a media key that we care about. Send it to the caller. | |
| 314 bool was_handled = shortcut_listener->OnMediaKeyEvent(key_code); | |
| 315 | |
| 316 // Prevent event from proagating to other apps if handled by Chrome. | |
| 317 if (was_handled) | |
| 318 return NULL; | |
| 319 | |
| 320 // By default, pass the event through. | |
| 321 return event; | |
| 322 } | |
| 323 | |
| 324 } // namespace extensions | |
| OLD | NEW |