Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ |
| 7 | 7 |
| 8 #include "chrome/browser/extensions/global_shortcut_listener.h" | |
| 9 | |
| 10 #include <Carbon/Carbon.h> | |
| 11 #include <CoreFoundation/CoreFoundation.h> | |
| 12 #include <map> | |
| 13 | |
| 8 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 9 #include "chrome/browser/extensions/global_shortcut_listener.h" | 15 #include "base/mac/scoped_nsobject.h" |
| 10 | 16 |
| 11 namespace extensions { | 17 namespace extensions { |
| 12 | 18 |
| 13 // Mac-specific implementation of the GlobalShortcutListener class that | 19 // Mac-specific implementation of the GlobalShortcutListener class that |
| 14 // listens for global shortcuts. Handles basic keyboard intercepting and | 20 // listens for global shortcuts. Handles basic keyboard intercepting and |
| 15 // forwards its output to the base class for processing. | 21 // forwards its output to the base class for processing. |
| 16 // TODO(finnur/smus): Implement this class. | 22 // |
| 23 // Uses an event tap for intercepting media keys (PlayPause, NextTrack, | |
| 24 // PreviousTrack). Also uses the Carbon RegisterEventHotKey API for binding to | |
| 25 // non-media key global keyboard shortcuts (eg. Command-Shift-1). | |
| 17 class GlobalShortcutListenerMac : public GlobalShortcutListener { | 26 class GlobalShortcutListenerMac : public GlobalShortcutListener { |
| 18 public: | 27 public: |
| 19 virtual ~GlobalShortcutListenerMac(); | 28 virtual ~GlobalShortcutListenerMac(); |
| 20 | 29 |
| 21 virtual void StartListening() OVERRIDE; | 30 virtual void StartListening() OVERRIDE; |
| 22 virtual void StopListening() OVERRIDE; | 31 virtual void StopListening() OVERRIDE; |
| 23 | 32 |
| 33 // Keyboard event callbacks. | |
| 34 bool OnKeyEvent(EventHotKeyID hotKeyID); | |
| 35 bool OnMediaKeyEvent(int key_code); | |
| 36 | |
| 24 private: | 37 private: |
| 25 friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>; | 38 friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>; |
| 26 | 39 |
| 27 GlobalShortcutListenerMac(); | 40 GlobalShortcutListenerMac(); |
| 28 | 41 |
| 29 // Register an |accelerator| with the particular |observer|. | 42 // Register an |accelerator| with the particular |observer|. |
| 30 virtual void RegisterAccelerator( | 43 virtual void RegisterAccelerator( |
| 31 const ui::Accelerator& accelerator, | 44 const ui::Accelerator& accelerator, |
| 32 GlobalShortcutListener::Observer* observer) OVERRIDE; | 45 GlobalShortcutListener::Observer* observer) OVERRIDE; |
| 33 // Unregister an |accelerator| with the particular |observer|. | 46 // Unregister an |accelerator| with the particular |observer|. |
| 34 virtual void UnregisterAccelerator( | 47 virtual void UnregisterAccelerator( |
| 35 const ui::Accelerator& accelerator, | 48 const ui::Accelerator& accelerator, |
| 36 GlobalShortcutListener::Observer* observer) OVERRIDE; | 49 GlobalShortcutListener::Observer* observer) OVERRIDE; |
| 37 | 50 |
| 51 // Mac-specific functions for registering a keyboard shortcut. | |
| 52 void RegisterHotKey(const ui::Accelerator& accelerator); | |
| 53 void UnregisterHotKey(const ui::Accelerator& accelerator); | |
| 54 | |
| 55 // Enable and disable the media key event tap. | |
| 56 void StartWatchingMediaKeys(); | |
| 57 void StopWatchingMediaKeys(); | |
| 58 | |
| 59 // Convert from mac media key code to the equivalent keyboard code. | |
| 60 ui::KeyboardCode MediaKeyCodeToKeyboardCode(int keyCode); | |
|
Robert Sesek
2013/11/22 16:49:59
naming: key_code
smus
2013/11/22 20:48:09
Done.
| |
| 61 | |
| 62 // Determine if a key is a media key or not. | |
| 63 // TODO(smus): Use the one in CommandService. | |
|
Robert Sesek
2013/11/22 16:49:59
What is "the one" referring to?
Finnur
2013/11/22 17:52:53
Neo.
smus
2013/11/22 20:48:09
No longer relevant. Switched to CommandService::Is
| |
| 64 bool IsMediaKey(const ui::Accelerator& accelerator); | |
| 65 | |
| 66 // Whether or not any media keys are currently registered. | |
| 67 bool AreMediaKeysRegistered(); | |
| 68 | |
| 69 // Re-enable the event tap if it was disabled by a timeout. | |
| 70 void EnableTap(); | |
| 71 | |
| 72 // The callback for when an event tap happens. | |
| 73 static CGEventRef EventTapCallback( | |
| 74 CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon); | |
| 75 | |
| 38 // Whether this object is listening for global shortcuts. | 76 // Whether this object is listening for global shortcuts. |
| 39 bool is_listening_; | 77 bool is_listening_; |
| 40 | 78 |
| 79 // A counter representing the current hotkey identifier. | |
| 80 int hotkey_id_ = 0; | |
| 81 | |
| 82 // A map of all hotkeys (media keys and shortcuts) mapping to their | |
| 83 // corresponding hotkey IDs. For quickly finding if an accelerator is | |
| 84 // registered. | |
| 85 typedef std::map<ui::Accelerator, int> HotKeyIdMap; | |
| 86 HotKeyIdMap hotkey_ids_; | |
| 87 | |
| 88 // The inverse map for quickly looking up accelerators by hotkey id. | |
| 89 typedef std::map<int, ui::Accelerator> IdHotKeyMap; | |
| 90 IdHotKeyMap id_hotkeys_; | |
| 91 | |
| 92 // Keyboard shortcut IDs to hotkeys map for unregistration. | |
| 93 typedef std::map<int, EventHotKeyRef> IdHotKeyRefMap; | |
| 94 IdHotKeyRefMap id_hotkey_refs_; | |
| 95 | |
| 96 // Event tap for intercepting mac media keys. | |
| 97 CFMachPortRef event_tap_; | |
| 98 CFRunLoopSourceRef event_tap_source_; | |
| 99 | |
| 41 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); | 100 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); |
| 42 }; | 101 }; |
| 43 | 102 |
| 44 } // namespace extensions | 103 } // namespace extensions |
| 45 | 104 |
| 46 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ | 105 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ |
| OLD | NEW |