diff options
author | dmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-02 15:31:40 +0000 |
---|---|---|
committer | dmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-02 15:31:40 +0000 |
commit | eccefee0229e222c0130dca22412b4dccfa3f181 (patch) | |
tree | 959ca4d2d1c15bd29b821a6af45ed1c433779fb1 /remoting/host/local_input_monitor_mac.mm | |
parent | 91ef7b0311e0bea576d5b935b2cd378e04c517ca (diff) | |
download | chromium_src-eccefee0229e222c0130dca22412b4dccfa3f181.zip chromium_src-eccefee0229e222c0130dca22412b4dccfa3f181.tar.gz chromium_src-eccefee0229e222c0130dca22412b4dccfa3f181.tar.bz2 |
Add support for hotkey to quit chromoting.
BUG=NONE
TEST=Start up chromoting session, try hitting "shift-esc" on host to quit the session.
Review URL: http://codereview.chromium.org/7466041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95084 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/host/local_input_monitor_mac.mm')
-rw-r--r-- | remoting/host/local_input_monitor_mac.mm | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/remoting/host/local_input_monitor_mac.mm b/remoting/host/local_input_monitor_mac.mm new file mode 100644 index 0000000..c3955c6 --- /dev/null +++ b/remoting/host/local_input_monitor_mac.mm @@ -0,0 +1,101 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "remoting/host/local_input_monitor.h" + +#import <AppKit/AppKit.h> + +#include "base/compiler_specific.h" +#include "base/logging.h" +#include "remoting/host/chromoting_host.h" +#import "third_party/GTM/AppKit/GTMCarbonEvent.h" + +// Esc Key Code is 53. +// http://boredzo.org/blog/wp-content/uploads/2007/05/IMTx-virtual-keycodes.pdf +static const NSUInteger kEscKeyCode = 53; + +@interface HotKeyMonitor : NSObject { + @private + GTMCarbonHotKey* hot_key_; + remoting::ChromotingHost* host_; +} + +// Called when the hotKey is hit. +- (void)hotKeyHit:(GTMCarbonHotKey*)hotKey; + +// Must be called when the HotKeyMonitor is no longer to be used. +// Similar to NSTimer in that more than a simple release is required. +- (void)invalidate; + +@end + +@implementation HotKeyMonitor + +- (id)initWithHost:(remoting::ChromotingHost*)host { + if ((self = [super init])) { + host_ = host; + GTMCarbonEventDispatcherHandler* handler = + [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler]; + hot_key_ = [handler registerHotKey:kEscKeyCode + modifiers:NSShiftKeyMask + target:self + action:@selector(hotKeyHit:) + userInfo:nil + whenPressed:YES]; + if (!hot_key_) { + [self release]; + return nil; + } + } + return self; +} + +- (void)hotKeyHit:(GTMCarbonHotKey*)hotKey { + host_->Shutdown(NULL); +} + +- (void)invalidate { + GTMCarbonEventDispatcherHandler* handler = + [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler]; + [handler unregisterHotKey:hot_key_]; +} + +@end + +namespace { + +class LocalInputMonitorMac : public remoting::LocalInputMonitor { + public: + LocalInputMonitorMac() : hot_key_monitor_(NULL) {} + virtual ~LocalInputMonitorMac(); + virtual void Start(remoting::ChromotingHost* host) OVERRIDE; + virtual void Stop() OVERRIDE; + + private: + HotKeyMonitor* hot_key_monitor_; + + DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorMac); +}; + +} // namespace + +LocalInputMonitorMac::~LocalInputMonitorMac() { + Stop(); +} + +void LocalInputMonitorMac::Start(remoting::ChromotingHost* host) { + CHECK(!hot_key_monitor_); + hot_key_monitor_ = [[HotKeyMonitor alloc] initWithHost:host]; + CHECK(hot_key_monitor_); +} + +void LocalInputMonitorMac::Stop() { + [hot_key_monitor_ invalidate]; + [hot_key_monitor_ release]; + hot_key_monitor_ = nil; +} + +remoting::LocalInputMonitor* remoting::LocalInputMonitor::Create() { + return new LocalInputMonitorMac; +} |