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(); |
20 | 32 |
21 virtual void StartListening() OVERRIDE; | 33 virtual void StartListening() OVERRIDE; |
22 virtual void StopListening() OVERRIDE; | 34 virtual void StopListening() OVERRIDE; |
23 | 35 |
36 // Keyboard event callbacks. | |
37 bool OnKeyEvent(EventHotKeyID hot_key_id); | |
38 bool OnMediaKeyEvent(int key_code); | |
39 | |
24 private: | 40 private: |
25 friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>; | 41 friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>; |
26 | 42 |
27 GlobalShortcutListenerMac(); | 43 GlobalShortcutListenerMac(); |
28 | 44 |
29 // Register an |accelerator| with the particular |observer|. | 45 // Register an |accelerator| with the particular |observer|. |
30 virtual void RegisterAccelerator( | 46 virtual void RegisterAccelerator( |
31 const ui::Accelerator& accelerator, | 47 const ui::Accelerator& accelerator, |
32 GlobalShortcutListener::Observer* observer) OVERRIDE; | 48 GlobalShortcutListener::Observer* observer) OVERRIDE; |
33 // Unregister an |accelerator| with the particular |observer|. | 49 // Unregister an |accelerator| with the particular |observer|. |
34 virtual void UnregisterAccelerator( | 50 virtual void UnregisterAccelerator( |
35 const ui::Accelerator& accelerator, | 51 const ui::Accelerator& accelerator, |
36 GlobalShortcutListener::Observer* observer) OVERRIDE; | 52 GlobalShortcutListener::Observer* observer) OVERRIDE; |
37 | 53 |
54 // Mac-specific functions for registering hot keys with modifiers. | |
55 void RegisterHotKey(const ui::Accelerator& accelerator, int hot_key_id); | |
56 void UnregisterHotKey(const ui::Accelerator& accelerator); | |
57 | |
58 // Enable and disable the media key event tap. | |
59 void StartWatchingMediaKeys(); | |
60 void StopWatchingMediaKeys(); | |
61 | |
62 // Whether or not any media keys are currently registered. | |
63 bool IsAnyMediaKeyRegistered(); | |
64 | |
65 // The callback for when an event tap happens. | |
66 static CGEventRef EventTapCallback( | |
67 CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon); | |
68 | |
38 // Whether this object is listening for global shortcuts. | 69 // Whether this object is listening for global shortcuts. |
39 bool is_listening_; | 70 bool is_listening_; |
40 | 71 |
72 // The hotkey identifier for the next global shortcut that is added. | |
73 int hot_key_id_; | |
Mark Mentovai
2013/12/09 19:31:54
There are a bunch of different things in here that
smus
2013/12/09 23:37:06
Done.
| |
74 | |
75 // A map of all hotkeys (media keys and shortcuts) mapping to their | |
76 // corresponding hotkey IDs. For quickly finding if an accelerator is | |
77 // registered. | |
78 typedef std::map<ui::Accelerator, int> HotKeyIdMap; | |
79 HotKeyIdMap hot_key_ids_; | |
80 | |
81 // The inverse map for quickly looking up accelerators by hotkey id. | |
82 typedef std::map<int, ui::Accelerator> IdHotKeyMap; | |
83 IdHotKeyMap id_hot_keys_; | |
84 | |
85 // Keyboard shortcut IDs to hotkeys map for unregistration. | |
86 typedef std::map<int, EventHotKeyRef> IdHotKeyRefMap; | |
87 IdHotKeyRefMap id_hot_key_refs_; | |
88 | |
89 // Event tap for intercepting mac media keys. | |
90 CFMachPortRef event_tap_; | |
91 CFRunLoopSourceRef event_tap_source_; | |
92 | |
41 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); | 93 DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac); |
42 }; | 94 }; |
43 | 95 |
44 } // namespace extensions | 96 } // namespace extensions |
45 | 97 |
46 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ | 98 #endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_ |
OLD | NEW |