summaryrefslogtreecommitdiffstats
path: root/remoting/host/local_input_monitor_mac.mm
blob: c3955c62ae8c3000a2121087e7fab102720cc33e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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;
}