This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
/
HotKeyController.m
337 lines (296 loc) · 9.69 KB
/
HotKeyController.m
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// HotKeyController.m
//
// Modified by Gaurav Khanna on 8/17/10.
// SOURCE: http://github.com/sweetfm/SweetFM/blob/master/Source/HMediaKeys.m
// SOURCE: http://stackoverflow.com/questions/2969110/cgeventtapcreate-breaks-down-mysteriously-with-key-down-events
//
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "HotKeyController.h"
#import <IOKit/hidsystem/ev_keymap.h>
#import <sys/sysctl.h>
NSString* const MediaKeyPower = @"MediaKeyPower";
NSString* const MediaKeySoundMute = @"MediaKeySoundMute";
NSString* const MediaKeySoundUp = @"MediaKeySoundUp";
NSString* const MediaKeySoundDown = @"MediaKeySoundDown";
NSString* const MediaKeyPlayPauseNotification = @"MediaKeyPlayPauseNotification";
NSString* const MediaKeyFastNotification = @"MediaKeyFastNotification";
NSString* const MediaKeyRewindNotification = @"MediaKeyRewindNotification";
NSString* const MediaKeyNextNotification = @"MediaKeyNextNotification";
NSString* const MediaKeyPreviousNotification = @"MediaKeyPreviousNotification";
#ifndef kCGEventTapOptionDefault
#define kCGEventTapOptionDefault 0
#endif
#define NX_KEYSTATE_UP 0x0A
#define NX_KEYSTATE_DOWN 0x0B
@implementation HotKeyController
+ (HotKeyController*)sharedController
{
static HotKeyController *sharedHotKeyController = nil;
if (sharedHotKeyController == nil)
sharedHotKeyController = [[super allocWithZone:NULL] init];
return sharedHotKeyController;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [[self sharedController] retain];
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
//denotes an object that cannot be released
return NSUIntegerMax;
}
- (oneway void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
- (CFMachPortRef)eventPort
{
return m_eventPort;
}
- (void)sysPower: (BOOL)enable;
{
m_controlSysPower = enable;
}
- (BOOL)controlPower;
{
return m_controlSysPower;
}
- (void)sysVolume: (BOOL)enable;
{
m_controlSysVolume = enable;
}
- (BOOL)controlVolume;
{
return m_controlSysVolume;
}
- (void)setActive: (BOOL)active
{
m_active = active;
}
- (BOOL)getActive
{
return m_active;
}
- (BOOL)getDebuggerActive
{
// Technical Q&A QA1361
// returns true if the current process is being debugged (either
// running under the debugger or has a debugger attached post facto).
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;
// initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.
info.kp_proc.p_flag = 0;
// initialize mib, which tells sysctl the info we want, in this case
// we are looking for information about a specific process ID.
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
// Call sysctl.
size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);
// we are being debugged if the P_TRACED flag is set.
return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}
// WARNING: do not debugger breakpoint in this routine.
// It's a system level call back that taps ALL Events
// and you WILL lose all key control :)
static CGEventRef tapEventCallback2(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
HotKeyController *hot_key_controller = (HotKeyController*)refcon;
if (type == kCGEventTapDisabledByTimeout)
{
if ([hot_key_controller getActive])
CGEventTapEnable([hot_key_controller eventPort], TRUE);
return NULL;
}
if ((type != NX_SYSDEFINED) || (![hot_key_controller getActive]))
return event;
NSEvent *nsEvent = [NSEvent eventWithCGEvent:event];
if (!nsEvent || [nsEvent subtype] != 8)
return event;
int data = [nsEvent data1];
int keyCode = (data & 0xFFFF0000) >> 16;
int keyFlags = (data & 0xFFFF);
int keyState = (keyFlags & 0xFF00) >> 8;
BOOL keyIsRepeat = (keyFlags & 0x1) > 0;
if (keyIsRepeat)
return event;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
switch (keyCode)
{
case NX_POWER_KEY:
if ([hot_key_controller controlPower])
{
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeyPower object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
}
break;
case NX_KEYTYPE_MUTE:
if ([hot_key_controller controlVolume])
{
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeySoundMute object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
}
break;
case NX_KEYTYPE_SOUND_UP:
if ([hot_key_controller controlVolume])
{
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeySoundUp object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
}
break;
case NX_KEYTYPE_SOUND_DOWN:
if ([hot_key_controller controlVolume])
{
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeySoundDown object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
}
break;
case NX_KEYTYPE_PLAY:
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeyPlayPauseNotification object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
break;
case NX_KEYTYPE_FAST:
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeyFastNotification object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
break;
case NX_KEYTYPE_REWIND:
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeyRewindNotification object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
break;
case NX_KEYTYPE_NEXT:
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeyNextNotification object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
break;
case NX_KEYTYPE_PREVIOUS:
if (keyState == NX_KEYSTATE_DOWN)
[center postNotificationName:MediaKeyPreviousNotification object:(HotKeyController *)refcon];
if (keyState == NX_KEYSTATE_UP || keyState == NX_KEYSTATE_DOWN)
return NULL;
break;
}
return event;
}
static CGEventRef tapEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
CGEventRef ret = tapEventCallback2(proxy, type, event, refcon);
[pool drain];
return ret;
}
-(void)eventTapThread
{
m_runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, m_eventPort, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), m_runLoopSource, kCFRunLoopCommonModes);
// Enable the event tap.
CGEventTapEnable(m_eventPort, TRUE);
CFRunLoopRun();
[self setActive:NO];
// Disable the event tap.
if (m_eventPort)
CGEventTapEnable(m_eventPort, FALSE);
if (m_runLoopSource)
CFRelease(m_runLoopSource);
m_runLoopSource = NULL;
if (m_eventPort)
CFRelease(m_eventPort);
m_eventPort = NULL;
}
- (id)init
{
if (self = [super init])
{
m_active = NO;
m_eventPort = NULL;
m_runLoopSource = NULL;
m_controlSysPower = NO;
m_controlSysVolume = NO;
}
return self;
}
- (void)enableTap
{
if (![self getDebuggerActive] && ![self getActive] && floor(NSAppKitVersionNumber) >= 949)
{
// check runtime, we only allow this on 10.5+
m_eventPort = CGEventTapCreate(kCGSessionEventTap,
kCGHeadInsertEventTap, kCGEventTapOptionDefault,
CGEventMaskBit(NX_SYSDEFINED), tapEventCallback, self);
if (m_eventPort != NULL)
{
// Run this in a separate thread so that a slow app
// doesn't lag the event tap
[NSThread detachNewThreadSelector:@selector(eventTapThread) toTarget:self withObject:nil];
[self setActive:YES];
}
}
}
- (void)disableTap
{
if ([self getActive])
{
[self setActive:NO];
// Disable the event tap.
if (m_eventPort)
CGEventTapEnable(m_eventPort, FALSE);
if (m_runLoopSource)
CFRelease(m_runLoopSource);
m_runLoopSource = NULL;
if (m_eventPort)
CFRelease(m_eventPort);
m_eventPort = NULL;
}
}
@end