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