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 // This class does two things: |
| 25 // 1. Intercepts media keys. Uses an event tap for intercepting media keys |
| 26 // (PlayPause, NextTrack, PreviousTrack). |
| 27 // 2. Binds keyboard shortcuts (hot keys). Carbon RegisterEventHotKey API for |
| 28 // binding to non-media key global hot keys (eg. Command-Shift-1). |
17 class GlobalShortcutListenerMac : public GlobalShortcutListener { | 29 class GlobalShortcutListenerMac : public GlobalShortcutListener { |
18 public: | 30 public: |
19 virtual ~GlobalShortcutListenerMac(); | 31 virtual ~GlobalShortcutListenerMac(); |
| 32 GlobalShortcutListenerMac(); |
20 | 33 |
21 virtual void StartListening() OVERRIDE; | 34 virtual void StartListening() OVERRIDE; |
22 virtual void StopListening() OVERRIDE; | 35 virtual void StopListening() OVERRIDE; |
23 | 36 |
| 37 // Keyboard event callbacks. |
| 38 void OnKeyEvent(EventHotKeyID hot_key_id); |
| 39 bool OnMediaKeyEvent(int key_code); |
| 40 |
24 private: | 41 private: |
25 friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>; | |
26 | |
27 GlobalShortcutListenerMac(); | |
28 | |
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 hot keys with modifiers. |
| 52 void RegisterHotKey(const ui::Accelerator& accelerator, int hot_key_id); |
| 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 // Whether or not any media keys are currently registered. |
| 60 bool IsAnyMediaKeyRegistered(); |
| 61 |
| 62 // The callback for when an event tap happens. |
| 63 static CGEventRef EventTapCallback( |
| 64 CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon); |
| 65 |
38 // Whether this object is listening for global shortcuts. | 66 // Whether this object is listening for global shortcuts. |
39 bool is_listening_; | 67 bool is_listening_; |
40 | 68 |
| 69 // The hotkey identifier for the next global shortcut that is added. |
| 70 typedef int HotKeyId; |
| 71 HotKeyId hot_key_id_; |
| 72 |
| 73 // A map of all hotkeys (media keys and shortcuts) mapping to their |
| 74 // corresponding hotkey IDs. For quickly finding if an accelerator is |
| 75 // registered. |
| 76 typedef std::map<ui::Accelerator, HotKeyId> HotKeyIdMap; |
| 77 HotKeyIdMap hot_key_ids_; |
| 78 |
| 79 // The inverse map for quickly looking up accelerators by hotkey id. |
| 80 typedef std::map<HotKeyId, ui::Accelerator> IdHotKeyMap; |
| 81 IdHotKeyMap id_hot_keys_; |
| 82 |
| 83 // Keyboard shortcut IDs to hotkeys map for unregistration. |
| 84 typedef std::map<HotKeyId, EventHotKeyRef> IdHotKeyRefMap; |
| 85 IdHotKeyRefMap id_hot_key_refs_; |
| 86 |
| 87 // Event tap for intercepting mac media keys. |
| 88 CFMachPortRef event_tap_; |
| 89 CFRunLoopSourceRef event_tap_source_; |
| 90 |
| 91 // Event handler for keyboard shortcut hot keys. |
| 92 EventHandlerRef event_handler_; |
| 93 |
41 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); | 94 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); |
42 }; | 95 }; |
43 | 96 |
44 } // namespace extensions | 97 } // namespace extensions |
45 | 98 |
46 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ | 99 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ |
OLD | NEW |