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#import "base/mac/foundation_util.h" 12#include "chrome/common/extensions/command.h" 13#include "content/public/browser/browser_thread.h" 14#include "ui/base/accelerators/accelerator.h" 15#include "ui/events/event.h" 16#import "ui/events/keycodes/keyboard_code_conversion_mac.h" 17 18using content::BrowserThread; 19using extensions::GlobalShortcutListenerMac; 20 21namespace { 22 23// The media keys subtype. No official docs found, but widely known. 24// http://lists.apple.com/archives/cocoa-dev/2007/Aug/msg00499.html 25const int kSystemDefinedEventMediaKeysSubtype = 8; 26 27ui::KeyboardCode MediaKeyCodeToKeyboardCode(int key_code) { 28 switch (key_code) { 29 case NX_KEYTYPE_PLAY: 30 return ui::VKEY_MEDIA_PLAY_PAUSE; 31 case NX_KEYTYPE_PREVIOUS: 32 case NX_KEYTYPE_REWIND: 33 return ui::VKEY_MEDIA_PREV_TRACK; 34 case NX_KEYTYPE_NEXT: 35 case NX_KEYTYPE_FAST: 36 return ui::VKEY_MEDIA_NEXT_TRACK; 37 } 38 return ui::VKEY_UNKNOWN; 39} 40 41} // namespace 42 43namespace extensions { 44 45// static 46GlobalShortcutListener* GlobalShortcutListener::GetInstance() { 47 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 48 static GlobalShortcutListenerMac* instance = 49 new GlobalShortcutListenerMac(); 50 return instance; 51} 52 53GlobalShortcutListenerMac::GlobalShortcutListenerMac() 54 : is_listening_(false), 55 hot_key_id_(0), 56 event_tap_(NULL), 57 event_tap_source_(NULL), 58 event_handler_(NULL) { 59 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 60} 61 62GlobalShortcutListenerMac::~GlobalShortcutListenerMac() { 63 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 64 65 // By this point, UnregisterAccelerator should have been called for all 66 // keyboard shortcuts. Still we should clean up. 67 if (is_listening_) 68 StopListening(); 69 70 // If keys are still registered, make sure we stop the tap. Again, this 71 // should never happen. 72 if (IsAnyMediaKeyRegistered()) 73 StopWatchingMediaKeys(); 74 75 if (IsAnyHotKeyRegistered()) 76 StopWatchingHotKeys(); 77} 78 79void GlobalShortcutListenerMac::StartListening() { 80 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 81 82 DCHECK(!accelerator_ids_.empty()); 83 DCHECK(!id_accelerators_.empty()); 84 DCHECK(!is_listening_); 85 86 is_listening_ = true; 87} 88 89void GlobalShortcutListenerMac::StopListening() { 90 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 91 92 DCHECK(accelerator_ids_.empty()); // Make sure the set is clean. 93 DCHECK(id_accelerators_.empty()); 94 DCHECK(is_listening_); 95 96 is_listening_ = false; 97} 98 99void GlobalShortcutListenerMac::OnHotKeyEvent(EventHotKeyID hot_key_id) { 100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 101 102 // This hot key should be registered. 103 DCHECK(id_accelerators_.find(hot_key_id.id) != id_accelerators_.end()); 104 // Look up the accelerator based on this hot key ID. 105 const ui::Accelerator& accelerator = id_accelerators_[hot_key_id.id]; 106 NotifyKeyPressed(accelerator); 107} 108 109bool GlobalShortcutListenerMac::OnMediaKeyEvent(int media_key_code) { 110 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 111 ui::KeyboardCode key_code = MediaKeyCodeToKeyboardCode(media_key_code); 112 // Create an accelerator corresponding to the keyCode. 113 ui::Accelerator accelerator(key_code, 0); 114 // Look for a match with a bound hot_key. 115 if (accelerator_ids_.find(accelerator) != accelerator_ids_.end()) { 116 // If matched, callback to the event handling system. 117 NotifyKeyPressed(accelerator); 118 return true; 119 } 120 return false; 121} 122 123bool GlobalShortcutListenerMac::RegisterAcceleratorImpl( 124 const ui::Accelerator& accelerator) { 125 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 126 DCHECK(accelerator_ids_.find(accelerator) == accelerator_ids_.end()); 127 128 if (Command::IsMediaKey(accelerator)) { 129 if (!IsAnyMediaKeyRegistered()) { 130 // If this is the first media key registered, start the event tap. 131 StartWatchingMediaKeys(); 132 } 133 } else { 134 // Register hot_key if they are non-media keyboard shortcuts. 135 if (!RegisterHotKey(accelerator, hot_key_id_)) 136 return false; 137 138 if (!IsAnyHotKeyRegistered()) { 139 StartWatchingHotKeys(); 140 } 141 } 142 143 // Store the hotkey-ID mappings we will need for lookup later. 144 id_accelerators_[hot_key_id_] = accelerator; 145 accelerator_ids_[accelerator] = hot_key_id_; 146 ++hot_key_id_; 147 return true; 148} 149 150void GlobalShortcutListenerMac::UnregisterAcceleratorImpl( 151 const ui::Accelerator& accelerator) { 152 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 153 DCHECK(accelerator_ids_.find(accelerator) != accelerator_ids_.end()); 154 155 // Unregister the hot_key if it's a keyboard shortcut. 156 if (!Command::IsMediaKey(accelerator)) 157 UnregisterHotKey(accelerator); 158 159 // Remove hot_key from the mappings. 160 KeyId key_id = accelerator_ids_[accelerator]; 161 id_accelerators_.erase(key_id); 162 accelerator_ids_.erase(accelerator); 163 164 if (Command::IsMediaKey(accelerator)) { 165 // If we unregistered a media key, and now no media keys are registered, 166 // stop the media key tap. 167 if (!IsAnyMediaKeyRegistered()) 168 StopWatchingMediaKeys(); 169 } else { 170 // If we unregistered a hot key, and no more hot keys are registered, remove 171 // the hot key handler. 172 if (!IsAnyHotKeyRegistered()) { 173 StopWatchingHotKeys(); 174 } 175 } 176} 177 178bool GlobalShortcutListenerMac::RegisterHotKey( 179 const ui::Accelerator& accelerator, KeyId hot_key_id) { 180 EventHotKeyID event_hot_key_id; 181 182 // Signature uniquely identifies the application that owns this hot_key. 183 event_hot_key_id.signature = base::mac::CreatorCodeForApplication(); 184 event_hot_key_id.id = hot_key_id; 185 186 // Translate ui::Accelerator modifiers to cmdKey, altKey, etc. 187 int modifiers = 0; 188 modifiers |= (accelerator.IsShiftDown() ? shiftKey : 0); 189 modifiers |= (accelerator.IsCtrlDown() ? controlKey : 0); 190 modifiers |= (accelerator.IsAltDown() ? optionKey : 0); 191 modifiers |= (accelerator.IsCmdDown() ? cmdKey : 0); 192 193 int key_code = ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(), 0, 194 NULL, NULL); 195 196 // Register the event hot key. 197 EventHotKeyRef hot_key_ref; 198 OSStatus status = RegisterEventHotKey(key_code, modifiers, event_hot_key_id, 199 GetApplicationEventTarget(), 0, &hot_key_ref); 200 if (status != noErr) 201 return false; 202 203 id_hot_key_refs_[hot_key_id] = hot_key_ref; 204 return true; 205} 206 207void GlobalShortcutListenerMac::UnregisterHotKey( 208 const ui::Accelerator& accelerator) { 209 // Ensure this accelerator is already registered. 210 DCHECK(accelerator_ids_.find(accelerator) != accelerator_ids_.end()); 211 // Get the ref corresponding to this accelerator. 212 KeyId key_id = accelerator_ids_[accelerator]; 213 EventHotKeyRef ref = id_hot_key_refs_[key_id]; 214 // Unregister the event hot key. 215 UnregisterEventHotKey(ref); 216 217 // Remove the event from the mapping. 218 id_hot_key_refs_.erase(key_id); 219} 220 221void GlobalShortcutListenerMac::StartWatchingMediaKeys() { 222 // Make sure there's no existing event tap. 223 DCHECK(event_tap_ == NULL); 224 DCHECK(event_tap_source_ == NULL); 225 226 // Add an event tap to intercept the system defined media key events. 227 event_tap_ = CGEventTapCreate(kCGSessionEventTap, 228 kCGHeadInsertEventTap, 229 kCGEventTapOptionDefault, 230 CGEventMaskBit(NX_SYSDEFINED), 231 EventTapCallback, 232 this); 233 if (event_tap_ == NULL) { 234 LOG(ERROR) << "Error: failed to create event tap."; 235 return; 236 } 237 238 event_tap_source_ = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, 239 event_tap_, 0); 240 if (event_tap_source_ == NULL) { 241 LOG(ERROR) << "Error: failed to create new run loop source."; 242 return; 243 } 244 245 CFRunLoopAddSource(CFRunLoopGetCurrent(), event_tap_source_, 246 kCFRunLoopCommonModes); 247} 248 249void GlobalShortcutListenerMac::StopWatchingMediaKeys() { 250 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), event_tap_source_, 251 kCFRunLoopCommonModes); 252 // Ensure both event tap and source are initialized. 253 DCHECK(event_tap_ != NULL); 254 DCHECK(event_tap_source_ != NULL); 255 256 // Invalidate the event tap. 257 CFMachPortInvalidate(event_tap_); 258 CFRelease(event_tap_); 259 event_tap_ = NULL; 260 261 // Release the event tap source. 262 CFRelease(event_tap_source_); 263 event_tap_source_ = NULL; 264} 265 266void GlobalShortcutListenerMac::StartWatchingHotKeys() { 267 DCHECK(!event_handler_); 268 EventHandlerUPP hot_key_function = NewEventHandlerUPP(HotKeyHandler); 269 EventTypeSpec event_type; 270 event_type.eventClass = kEventClassKeyboard; 271 event_type.eventKind = kEventHotKeyPressed; 272 InstallApplicationEventHandler( 273 hot_key_function, 1, &event_type, this, &event_handler_); 274} 275 276void GlobalShortcutListenerMac::StopWatchingHotKeys() { 277 DCHECK(event_handler_); 278 RemoveEventHandler(event_handler_); 279 event_handler_ = NULL; 280} 281 282bool GlobalShortcutListenerMac::IsAnyMediaKeyRegistered() { 283 // Iterate through registered accelerators, looking for media keys. 284 AcceleratorIdMap::iterator it; 285 for (it = accelerator_ids_.begin(); it != accelerator_ids_.end(); ++it) { 286 if (Command::IsMediaKey(it->first)) 287 return true; 288 } 289 return false; 290} 291 292bool GlobalShortcutListenerMac::IsAnyHotKeyRegistered() { 293 AcceleratorIdMap::iterator it; 294 for (it = accelerator_ids_.begin(); it != accelerator_ids_.end(); ++it) { 295 if (!Command::IsMediaKey(it->first)) 296 return true; 297 } 298 return false; 299} 300 301// Processed events should propagate if they aren't handled by any listeners. 302// For events that don't matter, this handler should return as quickly as 303// possible. 304// Returning event causes the event to propagate to other applications. 305// Returning NULL prevents the event from propagating. 306// static 307CGEventRef GlobalShortcutListenerMac::EventTapCallback( 308 CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon) { 309 GlobalShortcutListenerMac* shortcut_listener = 310 static_cast<GlobalShortcutListenerMac*>(refcon); 311 312 // Handle the timeout case by re-enabling the tap. 313 if (type == kCGEventTapDisabledByTimeout) { 314 CGEventTapEnable(shortcut_listener->event_tap_, TRUE); 315 return event; 316 } 317 318 // Convert the CGEvent to an NSEvent for access to the data1 field. 319 NSEvent* ns_event = [NSEvent eventWithCGEvent:event]; 320 if (ns_event == nil) { 321 return event; 322 } 323 324 // Ignore events that are not system defined media keys. 325 if (type != NX_SYSDEFINED || 326 [ns_event type] != NSSystemDefined || 327 [ns_event subtype] != kSystemDefinedEventMediaKeysSubtype) { 328 return event; 329 } 330 331 NSInteger data1 = [ns_event data1]; 332 // Ignore media keys that aren't previous, next and play/pause. 333 // Magical constants are from http://weblog.rogueamoeba.com/2007/09/29/ 334 int key_code = (data1 & 0xFFFF0000) >> 16; 335 if (key_code != NX_KEYTYPE_PLAY && key_code != NX_KEYTYPE_NEXT && 336 key_code != NX_KEYTYPE_PREVIOUS && key_code != NX_KEYTYPE_FAST && 337 key_code != NX_KEYTYPE_REWIND) { 338 return event; 339 } 340 341 int key_flags = data1 & 0x0000FFFF; 342 bool is_key_pressed = ((key_flags & 0xFF00) >> 8) == 0xA; 343 344 // If the key wasn't pressed (eg. was released), ignore this event. 345 if (!is_key_pressed) 346 return event; 347 348 // Now we have a media key that we care about. Send it to the caller. 349 bool was_handled = shortcut_listener->OnMediaKeyEvent(key_code); 350 351 // Prevent event from proagating to other apps if handled by Chrome. 352 if (was_handled) 353 return NULL; 354 355 // By default, pass the event through. 356 return event; 357} 358 359// static 360OSStatus GlobalShortcutListenerMac::HotKeyHandler( 361 EventHandlerCallRef next_handler, EventRef event, void* user_data) { 362 // Extract the hotkey from the event. 363 EventHotKeyID hot_key_id; 364 OSStatus result = GetEventParameter(event, kEventParamDirectObject, 365 typeEventHotKeyID, NULL, sizeof(hot_key_id), NULL, &hot_key_id); 366 if (result != noErr) 367 return result; 368 369 GlobalShortcutListenerMac* shortcut_listener = 370 static_cast<GlobalShortcutListenerMac*>(user_data); 371 shortcut_listener->OnHotKeyEvent(hot_key_id); 372 return noErr; 373} 374 375} // namespace extensions 376