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 "base/lazy_instance.h" | |
9 #include "chrome/browser/extensions/global_shortcut_listener.h" | 8 #include "chrome/browser/extensions/global_shortcut_listener.h" |
10 | 9 |
| 10 #include <Carbon/Carbon.h> |
| 11 #include <CoreFoundation/CoreFoundation.h> |
| 12 |
| 13 #include <map> |
| 14 |
| 15 #include "base/mac/scoped_nsobject.h" |
| 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 // This class does two things: |
| 24 // 1. Intercepts media keys. Uses an event tap for intercepting media keys |
| 25 // (PlayPause, NextTrack, PreviousTrack). |
| 26 // 2. Binds keyboard shortcuts (hot keys). Carbon RegisterEventHotKey API for |
| 27 // binding to non-media key global hot keys (eg. Command-Shift-1). |
17 class GlobalShortcutListenerMac : public GlobalShortcutListener { | 28 class GlobalShortcutListenerMac : public GlobalShortcutListener { |
18 public: | 29 public: |
| 30 GlobalShortcutListenerMac(); |
19 virtual ~GlobalShortcutListenerMac(); | 31 virtual ~GlobalShortcutListenerMac(); |
20 | 32 |
21 virtual void StartListening() OVERRIDE; | 33 virtual void StartListening() OVERRIDE; |
22 virtual void StopListening() OVERRIDE; | 34 virtual void StopListening() OVERRIDE; |
23 | 35 |
24 private: | 36 private: |
25 friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>; | 37 typedef int KeyId; |
| 38 typedef std::map<ui::Accelerator, KeyId> AcceleratorIdMap; |
| 39 typedef std::map<KeyId, ui::Accelerator> IdAcceleratorMap; |
| 40 typedef std::map<KeyId, EventHotKeyRef> IdHotKeyRefMap; |
26 | 41 |
27 GlobalShortcutListenerMac(); | 42 // Keyboard event callbacks. |
| 43 void OnHotKeyEvent(EventHotKeyID hot_key_id); |
| 44 bool OnMediaKeyEvent(int key_code); |
28 | 45 |
29 // Register an |accelerator| with the particular |observer|. | 46 // Register an |accelerator| with the particular |observer|. |
30 virtual void RegisterAccelerator( | 47 virtual void RegisterAccelerator( |
31 const ui::Accelerator& accelerator, | 48 const ui::Accelerator& accelerator, |
32 GlobalShortcutListener::Observer* observer) OVERRIDE; | 49 GlobalShortcutListener::Observer* observer) OVERRIDE; |
33 // Unregister an |accelerator| with the particular |observer|. | 50 // Unregister an |accelerator| with the particular |observer|. |
34 virtual void UnregisterAccelerator( | 51 virtual void UnregisterAccelerator( |
35 const ui::Accelerator& accelerator, | 52 const ui::Accelerator& accelerator, |
36 GlobalShortcutListener::Observer* observer) OVERRIDE; | 53 GlobalShortcutListener::Observer* observer) OVERRIDE; |
37 | 54 |
| 55 // Mac-specific functions for registering hot keys with modifiers. |
| 56 void RegisterHotKey(const ui::Accelerator& accelerator, KeyId hot_key_id); |
| 57 void UnregisterHotKey(const ui::Accelerator& accelerator); |
| 58 |
| 59 // Enable and disable the media key event tap. |
| 60 void StartWatchingMediaKeys(); |
| 61 void StopWatchingMediaKeys(); |
| 62 |
| 63 // Enable and disable the hot key event handler. |
| 64 void StartWatchingHotKeys(); |
| 65 void StopWatchingHotKeys(); |
| 66 |
| 67 // Whether or not any media keys are currently registered. |
| 68 bool IsAnyMediaKeyRegistered(); |
| 69 |
| 70 // Whether or not any hot keys are currently registered. |
| 71 bool IsAnyHotKeyRegistered(); |
| 72 |
| 73 // The callback for when an event tap happens. |
| 74 static CGEventRef EventTapCallback( |
| 75 CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon); |
| 76 |
| 77 // The callback for when a hot key event happens. |
| 78 static OSStatus HotKeyHandler( |
| 79 EventHandlerCallRef next_handler, EventRef event, void* user_data); |
| 80 |
38 // Whether this object is listening for global shortcuts. | 81 // Whether this object is listening for global shortcuts. |
39 bool is_listening_; | 82 bool is_listening_; |
40 | 83 |
| 84 // The hotkey identifier for the next global shortcut that is added. |
| 85 KeyId hot_key_id_; |
| 86 |
| 87 // A map of all hotkeys (media keys and shortcuts) mapping to their |
| 88 // corresponding hotkey IDs. For quickly finding if an accelerator is |
| 89 // registered. |
| 90 AcceleratorIdMap accelerator_ids_; |
| 91 |
| 92 // The inverse map for quickly looking up accelerators by hotkey id. |
| 93 IdAcceleratorMap id_accelerators_; |
| 94 |
| 95 // Keyboard shortcut IDs to hotkeys map for unregistration. |
| 96 IdHotKeyRefMap id_hot_key_refs_; |
| 97 |
| 98 // Event tap for intercepting mac media keys. |
| 99 CFMachPortRef event_tap_; |
| 100 CFRunLoopSourceRef event_tap_source_; |
| 101 |
| 102 // Event handler for keyboard shortcut hot keys. |
| 103 EventHandlerRef event_handler_; |
| 104 |
41 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); | 105 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); |
42 }; | 106 }; |
43 | 107 |
44 } // namespace extensions | 108 } // namespace extensions |
45 | 109 |
46 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ | 110 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ |
OLD | NEW |