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 | |
13 #include <map> | |
14 | |
8 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
9 #include "chrome/browser/extensions/global_shortcut_listener.h" | 16 #include "base/mac/scoped_nsobject.h" |
10 | 17 |
11 namespace extensions { | 18 namespace extensions { |
12 | 19 |
13 // Mac-specific implementation of the GlobalShortcutListener class that | 20 // Mac-specific implementation of the GlobalShortcutListener class that |
14 // listens for global shortcuts. Handles basic keyboard intercepting and | 21 // listens for global shortcuts. Handles basic keyboard intercepting and |
15 // forwards its output to the base class for processing. | 22 // forwards its output to the base class for processing. |
16 // TODO(finnur/smus): Implement this class. | 23 // |
24 // Uses an event tap for intercepting media keys (PlayPause, NextTrack, | |
25 // PreviousTrack). Also uses the Carbon RegisterEventHotKey API for binding to | |
26 // non-media key global keyboard shortcuts (eg. Command-Shift-1). | |
17 class GlobalShortcutListenerMac : public GlobalShortcutListener { | 27 class GlobalShortcutListenerMac : public GlobalShortcutListener { |
18 public: | 28 public: |
19 virtual ~GlobalShortcutListenerMac(); | 29 virtual ~GlobalShortcutListenerMac(); |
20 | 30 |
21 virtual void StartListening() OVERRIDE; | 31 virtual void StartListening() OVERRIDE; |
22 virtual void StopListening() OVERRIDE; | 32 virtual void StopListening() OVERRIDE; |
23 | 33 |
34 // Keyboard event callbacks. | |
35 bool OnKeyEvent(EventHotKeyID hot_key_id); | |
36 bool OnMediaKeyEvent(int key_code); | |
37 | |
24 private: | 38 private: |
25 friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>; | 39 friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>; |
26 | 40 |
27 GlobalShortcutListenerMac(); | 41 GlobalShortcutListenerMac(); |
28 | 42 |
29 // Register an |accelerator| with the particular |observer|. | 43 // Register an |accelerator| with the particular |observer|. |
30 virtual void RegisterAccelerator( | 44 virtual void RegisterAccelerator( |
31 const ui::Accelerator& accelerator, | 45 const ui::Accelerator& accelerator, |
32 GlobalShortcutListener::Observer* observer) OVERRIDE; | 46 GlobalShortcutListener::Observer* observer) OVERRIDE; |
33 // Unregister an |accelerator| with the particular |observer|. | 47 // Unregister an |accelerator| with the particular |observer|. |
34 virtual void UnregisterAccelerator( | 48 virtual void UnregisterAccelerator( |
35 const ui::Accelerator& accelerator, | 49 const ui::Accelerator& accelerator, |
36 GlobalShortcutListener::Observer* observer) OVERRIDE; | 50 GlobalShortcutListener::Observer* observer) OVERRIDE; |
37 | 51 |
52 // Mac-specific functions for registering a keyboard shortcut. | |
53 void RegisterHotKey(const ui::Accelerator& accelerator); | |
54 void UnregisterHotKey(const ui::Accelerator& accelerator); | |
55 | |
56 // Enable and disable the media key event tap. | |
57 void StartWatchingMediaKeys(); | |
58 void StopWatchingMediaKeys(); | |
59 | |
60 // Convert from mac media key code to the equivalent keyboard code. | |
61 ui::KeyboardCode MediaKeyCodeToKeyboardCode(int key_code); | |
Mark Mentovai
2013/11/26 15:32:46
This can be static. And private + static implies t
smus
2013/12/09 08:37:16
Done.
| |
62 | |
63 // Whether or not any media keys are currently registered. | |
64 bool AreMediaKeysRegistered(); | |
Mark Mentovai
2013/11/26 15:32:46
The comment is accurate but the name of the functi
smus
2013/12/09 08:37:16
Renamed to IsAnyMediaKeyRegistered.
| |
65 | |
66 // Re-enable the event tap if it was disabled by a timeout. | |
67 void EnableTap(); | |
68 | |
69 // The callback for when an event tap happens. | |
70 static CGEventRef EventTapCallback( | |
71 CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon); | |
72 | |
38 // Whether this object is listening for global shortcuts. | 73 // Whether this object is listening for global shortcuts. |
39 bool is_listening_; | 74 bool is_listening_; |
40 | 75 |
76 // A counter representing the current hotkey identifier. | |
Mark Mentovai
2013/11/26 15:32:46
What does “current” mean?
I looked at the impleme
smus
2013/12/09 08:37:16
Updated comment. What's the issue with a dumb coun
| |
77 int hot_key_id_ = 0; | |
Mark Mentovai
2013/11/26 15:32:46
Don’t initialize here, initialize in the construct
smus
2013/12/09 08:37:16
Done.
| |
78 | |
79 // A map of all hotkeys (media keys and shortcuts) mapping to their | |
80 // corresponding hotkey IDs. For quickly finding if an accelerator is | |
81 // registered. | |
82 typedef std::map<ui::Accelerator, int> HotKeyIdMap; | |
83 HotKeyIdMap hot_key_ids_; | |
84 | |
85 // The inverse map for quickly looking up accelerators by hotkey id. | |
86 typedef std::map<int, ui::Accelerator> IdHotKeyMap; | |
87 IdHotKeyMap id_hot_keys_; | |
88 | |
89 // Keyboard shortcut IDs to hotkeys map for unregistration. | |
90 typedef std::map<int, EventHotKeyRef> IdHotKeyRefMap; | |
91 IdHotKeyRefMap id_hot_key_refs_; | |
92 | |
93 // Event tap for intercepting mac media keys. | |
94 CFMachPortRef event_tap_; | |
95 CFRunLoopSourceRef event_tap_source_; | |
96 | |
41 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); | 97 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); |
42 }; | 98 }; |
43 | 99 |
44 } // namespace extensions | 100 } // namespace extensions |
45 | 101 |
46 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ | 102 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ |
OLD | NEW |