-
Notifications
You must be signed in to change notification settings - Fork 1
/
macmediakeys.mm
206 lines (179 loc) · 6.6 KB
/
macmediakeys.mm
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
#include "macmediakeys.h"
#include <exception>
#include <QWidget>
#include <QDebug>
#include <QTimerEvent>
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
#import <IOKit/hidsystem/ev_keymap.h>
#define NX_KEYSTATE_DOWN 0x0A
#define NX_KEYSTATE_UP 0x0B
static CFMachPortRef _eventPort;
static CFRunLoopSourceRef _runLoopSource;
CGEventRef tapEventCallback(CGEventTapProxy /*proxy*/, CGEventType type, CGEventRef event, void *refcon) {
MacMediaKeys *me = reinterpret_cast<MacMediaKeys*>(refcon);
if(type == kCGEventTapDisabledByTimeout)
CGEventTapEnable(_eventPort, TRUE);
if(type != NX_SYSDEFINED)
return event;
NSEvent *nsEvent = [NSEvent eventWithCGEvent:event];
if ([nsEvent subtype] != 8)
return event;
NSInteger 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;
switch (keyCode) {
case NX_KEYTYPE_PLAY:
me->onPlayPauseKey(keyState);
break;
case NX_KEYTYPE_FAST:
me->onForwardKey(keyState);
break;
case NX_KEYTYPE_REWIND:
me->onBackwardKey(keyState);
break;
default:
return event;
}
return nullptr;
}
static MacMediaKeys *ms_instance = nullptr;
MacMediaKeys::MacMediaKeys(QObject *parent):
QObject(parent),
long_press_timeout(500), long_press_cycle(500),
backward_state(NotPressed), backward_timer_id(0),
forward_state(NotPressed), forward_timer_id(0)
{
if (ms_instance) {
qFatal("MacMediaKeys instance is already allocated");
}
ms_instance = this;
NSDictionary *options = @{(id) kAXTrustedCheckOptionPrompt : @YES};
AXIsProcessTrustedWithOptions((CFDictionaryRef) options);
_eventPort = CGEventTapCreate(
kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
CGEventMaskBit(NX_SYSDEFINED),
tapEventCallback,
reinterpret_cast<void *>(this)
);
if (_eventPort == nullptr) {
qWarning("MacMediaKeys: Event Tap could not be created");
return;
}
_runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, _eventPort, 0);
if (_runLoopSource == nullptr) {
qWarning("MacMediaKeys: Run Loop Source could not be created");
return;
}
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
if (runLoop == nullptr) {
qWarning("MacMediaKeys: Couldn't get current threads Run Loop");
return;
}
CFRunLoopAddSource(runLoop, _runLoopSource, kCFRunLoopCommonModes);
}
MacMediaKeys::~MacMediaKeys() {
if (_eventPort) CFRelease(_eventPort);
if (_runLoopSource) CFRelease(_runLoopSource);
if (ms_instance == this) ms_instance = nullptr;
}
void MacMediaKeys::timerEvent(QTimerEvent *event)
{
if (event->timerId() == backward_timer_id) {
if (backward_state == NotPressed) {
qCritical("timerEvent(): programming error, backward button state is NotPressed");
} else if (backward_state == Pressed) {
backward_state = PressedEmmiting;
killTimer(backward_timer_id);
backward_timer_id = startTimer(long_press_cycle);
Q_ASSERT(backward_timer_id > 0);
emit seekBackward();
} else if (backward_state == PressedEmmiting) {
emit seekBackward();
}
} else if (event->timerId() == forward_timer_id) {
if (forward_state == NotPressed) {
qCritical("timerEvent(): programming error, forward button state is NotPressed");
} else if (forward_state == Pressed) {
forward_state = PressedEmmiting;
killTimer(forward_timer_id);
forward_timer_id = startTimer(long_press_cycle);
Q_ASSERT(forward_timer_id > 0);
emit seekForward();
} else if (forward_state == PressedEmmiting) {
emit seekForward();
}
} else {
qCritical("timerEvent(): programming error, unknown timer id fired %i", event->timerId());
}
}
void MacMediaKeys::onBackwardKey(int keystate)
{
if (backward_state == NotPressed) {
if (keystate == NX_KEYSTATE_DOWN) {
backward_timer_id = startTimer(long_press_timeout);
Q_ASSERT(backward_timer_id > 0);
backward_state = Pressed;
} else if (keystate == NX_KEYSTATE_UP) {
qCritical("onBackwardKey(): programming error, state is NotPressed, but UP state recieved");
}
} else if (backward_state == Pressed) {
if (keystate == NX_KEYSTATE_DOWN) {
qCritical("onBackwardKey(): programming error, state is Pressed, but DOWN state recieved");
} else if (keystate == NX_KEYSTATE_UP) {
killTimer(backward_timer_id);
backward_timer_id = 0;
backward_state = NotPressed;
emit backward();
}
} else if (backward_state == PressedEmmiting) {
if (keystate == NX_KEYSTATE_DOWN) {
qCritical("onBackwardKey(): programming error, state is PressedEmmiting, but DOWN state recieved");
} else if (keystate == NX_KEYSTATE_UP) {
killTimer(backward_timer_id);
backward_timer_id = 0;
backward_state = NotPressed;
}
}
}
void MacMediaKeys::onPlayPauseKey(int keystate)
{
if (keystate == NX_KEYSTATE_UP) {
emit playPause();
}
}
void MacMediaKeys::onForwardKey(int keystate)
{
if (forward_state == NotPressed) {
if (keystate == NX_KEYSTATE_DOWN) {
forward_timer_id = startTimer(long_press_timeout);
Q_ASSERT(forward_timer_id > 0);
forward_state = Pressed;
} else if (keystate == NX_KEYSTATE_UP) {
qCritical("onForwardKey(): programming error, state is NotPressed, but UP state recieved");
}
} else if (forward_state == Pressed) {
if (keystate == NX_KEYSTATE_DOWN) {
qCritical("onForwardKey(): programming error, state is Pressed, but DOWN state recieved");
} else if (keystate == NX_KEYSTATE_UP) {
killTimer(forward_timer_id);
forward_timer_id = 0;
forward_state = NotPressed;
emit forward();
}
} else if (forward_state == PressedEmmiting) {
if (keystate == NX_KEYSTATE_DOWN) {
qCritical("onForwardKey(): programming error, state is PressedEmmiting, but DOWN state recieved");
} else if (keystate == NX_KEYSTATE_UP) {
killTimer(forward_timer_id);
forward_timer_id = 0;
forward_state = NotPressed;
}
}
}