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 HotKeyId; |
Mark Mentovai
2013/12/11 15:01:51
Wait…to stay consistent in terminology, this is an
smus
2013/12/11 17:52:07
Renamed it to KeyId for brevity.
| |
38 typedef std::map<ui::Accelerator, HotKeyId> HotKeyIdMap; | |
39 typedef std::map<HotKeyId, ui::Accelerator> IdHotKeyMap; | |
40 typedef std::map<HotKeyId, 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, HotKeyId 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 // Whether or not any media keys are currently registered. | |
64 bool IsAnyMediaKeyRegistered(); | |
65 | |
66 // Whether or not any hot keys are currently registered. | |
67 bool IsAnyHotKeyRegistered(); | |
68 | |
69 // The callback for when an event tap happens. | |
70 static CGEventRef EventTapCallback( | |
71 CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon); | |
72 | |
73 // The callback for when a hot key event happens. | |
74 static OSStatus HotKeyHandler( | |
75 EventHandlerCallRef next_handler, EventRef event, void* user_data); | |
76 | |
38 // Whether this object is listening for global shortcuts. | 77 // Whether this object is listening for global shortcuts. |
39 bool is_listening_; | 78 bool is_listening_; |
40 | 79 |
80 // The hotkey identifier for the next global shortcut that is added. | |
81 HotKeyId hot_key_id_; | |
82 | |
83 // A map of all hotkeys (media keys and shortcuts) mapping to their | |
84 // corresponding hotkey IDs. For quickly finding if an accelerator is | |
85 // registered. | |
86 HotKeyIdMap hot_key_ids_; | |
87 | |
88 // The inverse map for quickly looking up accelerators by hotkey id. | |
89 IdHotKeyMap id_hot_keys_; | |
90 | |
91 // Keyboard shortcut IDs to hotkeys map for unregistration. | |
92 IdHotKeyRefMap id_hot_key_refs_; | |
93 | |
94 // Event tap for intercepting mac media keys. | |
95 CFMachPortRef event_tap_; | |
96 CFRunLoopSourceRef event_tap_source_; | |
97 | |
98 // Event handler for keyboard shortcut hot keys. | |
99 EventHandlerRef event_handler_; | |
100 | |
41 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); | 101 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); |
42 }; | 102 }; |
43 | 103 |
44 } // namespace extensions | 104 } // namespace extensions |
45 | 105 |
46 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ | 106 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ |
OLD | NEW |