aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/platform/OSXMediaKeySimulator.mm
blob: efc4251bb54a493684cb281894d2676afd97ca59 (plain) (blame)
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
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2016 Symless.
 *
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file COPYING that should have accompanied this file.
 *
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#import "platform/OSXMediaKeySimulator.h"

#import <Cocoa/Cocoa.h>

int convertKeyIDToNXKeyType(KeyID id)
{
	// hidsystem/ev_keymap.h
	// NX_KEYTYPE_SOUND_UP			0
	// NX_KEYTYPE_SOUND_DOWN		1
	// NX_KEYTYPE_BRIGHTNESS_UP		2
	// NX_KEYTYPE_BRIGHTNESS_DOWN	3
	// NX_KEYTYPE_MUTE				7
	// NX_KEYTYPE_EJECT				14
	// NX_KEYTYPE_PLAY				16
	// NX_KEYTYPE_NEXT				17
	// NX_KEYTYPE_PREVIOUS			18
	// NX_KEYTYPE_FAST				19
	// NX_KEYTYPE_REWIND			20

	int type = -1;
	switch (id) {
	case kKeyAudioUp:
		type = 0;
		break;
	case kKeyAudioDown:
		type = 1;
		break;
	case kKeyBrightnessUp:
		type = 2;
		break;
	case kKeyBrightnessDown:
		type = 3;
		break;
	case kKeyAudioMute:
		type = 7;
		break;
	case kKeyEject:
		type = 14;
		break;
	case kKeyAudioPlay:
		type = 16;
		break;
	case kKeyAudioNext:
		type = 17;
		break;
	case kKeyAudioPrev:
		type = 18;
		break;
	default:
		break;
	}
	
	return type;
}

bool
fakeNativeMediaKey(KeyID id)
{
	
	NSEvent* downRef = [NSEvent otherEventWithType:NSSystemDefined
					location: NSMakePoint(0, 0) modifierFlags:0xa00
					timestamp:0 windowNumber:0 context:0 subtype:8
					data1:(convertKeyIDToNXKeyType(id) << 16) | ((0xa) << 8)
					data2:-1];
	CGEventRef downEvent = [downRef CGEvent];
	
	NSEvent* upRef = [NSEvent otherEventWithType:NSSystemDefined
					location: NSMakePoint(0, 0) modifierFlags:0xa00
					timestamp:0 windowNumber:0 context:0 subtype:8
					data1:(convertKeyIDToNXKeyType(id) << 16) | ((0xb) << 8)
					data2:-1];
	CGEventRef upEvent = [upRef CGEvent];
	
    CGEventPost(kCGHIDEventTap, downEvent);
    CGEventPost(kCGHIDEventTap, upEvent);
	
	return true;
}