Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6846)

Unified Diff: chrome/browser/extensions/global_shortcut_listener_mac.h

Issue 60353008: Mac global keybindings (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/global_shortcut_listener_mac.h
===================================================================
--- chrome/browser/extensions/global_shortcut_listener_mac.h (revision 239014)
+++ chrome/browser/extensions/global_shortcut_listener_mac.h (working copy)
@@ -5,26 +5,43 @@
#ifndef CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_
#define CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_
-#include "base/lazy_instance.h"
#include "chrome/browser/extensions/global_shortcut_listener.h"
+#include <Carbon/Carbon.h>
+#include <CoreFoundation/CoreFoundation.h>
+
+#include <map>
+
+#include "base/mac/scoped_nsobject.h"
+
namespace extensions {
// Mac-specific implementation of the GlobalShortcutListener class that
// listens for global shortcuts. Handles basic keyboard intercepting and
// forwards its output to the base class for processing.
-// TODO(finnur/smus): Implement this class.
+//
+// This class does two things:
+// 1. Intercepts media keys. Uses an event tap for intercepting media keys
+// (PlayPause, NextTrack, PreviousTrack).
+// 2. Binds keyboard shortcuts (hot keys). Carbon RegisterEventHotKey API for
+// binding to non-media key global hot keys (eg. Command-Shift-1).
class GlobalShortcutListenerMac : public GlobalShortcutListener {
public:
+ GlobalShortcutListenerMac();
virtual ~GlobalShortcutListenerMac();
virtual void StartListening() OVERRIDE;
virtual void StopListening() OVERRIDE;
private:
- friend struct base::DefaultLazyInstanceTraits<GlobalShortcutListenerMac>;
+ 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.
+ typedef std::map<ui::Accelerator, HotKeyId> HotKeyIdMap;
+ typedef std::map<HotKeyId, ui::Accelerator> IdHotKeyMap;
+ typedef std::map<HotKeyId, EventHotKeyRef> IdHotKeyRefMap;
- GlobalShortcutListenerMac();
+ // Keyboard event callbacks.
+ void OnHotKeyEvent(EventHotKeyID hot_key_id);
+ bool OnMediaKeyEvent(int key_code);
// Register an |accelerator| with the particular |observer|.
virtual void RegisterAccelerator(
@@ -35,9 +52,52 @@
const ui::Accelerator& accelerator,
GlobalShortcutListener::Observer* observer) OVERRIDE;
+ // Mac-specific functions for registering hot keys with modifiers.
+ void RegisterHotKey(const ui::Accelerator& accelerator, HotKeyId hot_key_id);
+ void UnregisterHotKey(const ui::Accelerator& accelerator);
+
+ // Enable and disable the media key event tap.
+ void StartWatchingMediaKeys();
+ void StopWatchingMediaKeys();
+
+ // Whether or not any media keys are currently registered.
+ bool IsAnyMediaKeyRegistered();
+
+ // Whether or not any hot keys are currently registered.
+ bool IsAnyHotKeyRegistered();
+
+ // The callback for when an event tap happens.
+ static CGEventRef EventTapCallback(
+ CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon);
+
+ // The callback for when a hot key event happens.
+ static OSStatus HotKeyHandler(
+ EventHandlerCallRef next_handler, EventRef event, void* user_data);
+
// Whether this object is listening for global shortcuts.
bool is_listening_;
+ // The hotkey identifier for the next global shortcut that is added.
+ HotKeyId hot_key_id_;
+
+ // A map of all hotkeys (media keys and shortcuts) mapping to their
+ // corresponding hotkey IDs. For quickly finding if an accelerator is
+ // registered.
+ HotKeyIdMap hot_key_ids_;
+
+ // The inverse map for quickly looking up accelerators by hotkey id.
+ IdHotKeyMap id_hot_keys_;
+
+ // Keyboard shortcut IDs to hotkeys map for unregistration.
+ IdHotKeyRefMap id_hot_key_refs_;
+
+ // Event tap for intercepting mac media keys.
+ CFMachPortRef event_tap_;
+ CFRunLoopSourceRef event_tap_source_;
+
+ // Event handler for keyboard shortcut hot keys.
+ EventHandlerRef event_handler_;
+
DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac);
};

Powered by Google App Engine
This is Rietveld 408576698