-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.c
190 lines (160 loc) · 6.23 KB
/
controller.c
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
#include <IOKit/hid/IOHIDValue.h>
#include <IOKit/hid/IOHIDManager.h>
#include <dispatch/dispatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "controller.h"
#include "core.h"
#include "errors.h"
typedef struct Controller {
Core_t core;
} Controller;
void sendControl(Core_t core, ControlEvent e) {
send(core, (Event){.type = E_CONTROL, .whenOccurred = now(), .control = e});
}
#define BUTTON_ID_SQUARE 2
#define BUTTON_ID_CROSS 3
#define BUTTON_ID_CIRCLE 4
#define BUTTON_ID_TRIANGLE 5
#define BUTTON_ID_R1 7
#define BUTTON_ID_DPAD 20
void controllerInputCallback( void* context, IOReturn result, void* sender, IOHIDValueRef value ) {
Controller *c = (Controller*)context;
Core_t core = c->core;
IOHIDElementRef element = IOHIDValueGetElement(value);
IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
const int int_value = IOHIDValueGetIntegerValue(value);
if (cookie == BUTTON_ID_SQUARE) {
if (int_value == 1) {
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_LED_TOGGLE}); // only send down
}
else {
// don't send up
}
}
else if (cookie == BUTTON_ID_TRIANGLE) {
if (int_value == 1) {
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_MODE_TOGGLE}); // only send down
}
else {
// don't send up
}
}
else if (cookie == BUTTON_ID_CIRCLE) {
if (int_value == 1) {
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_SENTRY_MODE_TOGGLE}); // only send down
}
else {
// don't send up
}
}
else if (cookie == BUTTON_ID_R1) {
if (int_value == 1) {
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_RELOAD}); // only send down
}
else {
// don't send up
}
}
// else if (cookie > 0 && cookie < 10) { // square
// printf("%u\n", cookie);
// }
else if (cookie == BUTTON_ID_CROSS) {
if (int_value == 1) {
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_FIRE_BEGIN});
}
else {
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_FIRE_END});
}
}
else if (cookie == BUTTON_ID_DPAD) {
switch (int_value) {
case 0:
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_MOVEMENT, .movement = MOVE_UP});
break;
case 4:
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_MOVEMENT, .movement = MOVE_DOWN});
break;
case 6:
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_MOVEMENT, .movement = MOVE_LEFT});
break;
case 2:
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_MOVEMENT, .movement = MOVE_RIGHT});
break;
case 8:
sendControl(core, (ControlEvent){.type = CONTROL_TYPE_MOVEMENT, .movement = MOVE_NONE});
break;
}
}
}
int kHidPageDesktop = kHIDPage_GenericDesktop;
int kHidUsageGamepad = kHIDUsage_GD_GamePad;
int kHidUsageJoystick = kHIDUsage_GD_Joystick;
int kHidUsageController = kHIDUsage_GD_MultiAxisController;
static void attachCallback(void *context, IOReturn r, void *hidManager, IOHIDDeviceRef device) {
Controller *c = (Controller*)context;
IOHIDDeviceOpen(device, kIOHIDOptionsTypeNone);
IOHIDDeviceRegisterInputValueCallback(device, controllerInputCallback, c);
IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
}
void* controllerThread(void *arg) {
Controller *c = (Controller*)arg;
printf("thread starting\n");
IOHIDManagerRef hidManager = IOHIDManagerCreate( kCFAllocatorDefault, kIOHIDOptionsTypeNone );
CFStringRef keys[2];
keys[0] = CFSTR(kIOHIDDeviceUsagePageKey);
keys[1] = CFSTR(kIOHIDDeviceUsageKey);
CFDictionaryRef dictionaries[3];
CFNumberRef values[2];
values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &kHidPageDesktop);
values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &kHidUsageJoystick);
dictionaries[0] = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFRelease(values[0]);
CFRelease(values[1]);
values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &kHidPageDesktop);
values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &kHidUsageGamepad);
dictionaries[1] = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFRelease(values[0]);
CFRelease(values[1]);
values[0] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &kHidPageDesktop);
values[1] = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &kHidUsageController);
dictionaries[2] = CFDictionaryCreate(kCFAllocatorDefault, (const void **)keys, (const void **)values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFRelease(values[0]);
CFRelease(values[1]);
CFArrayRef dictionariesRef = CFArrayCreate(kCFAllocatorDefault,
(const void **)dictionaries, 3, &kCFTypeArrayCallBacks);
CFRelease(dictionaries[0]);
CFRelease(dictionaries[1]);
CFRelease(dictionaries[2]);
IOHIDManagerSetDeviceMatchingMultiple(hidManager, dictionariesRef);
IOHIDManagerRegisterDeviceMatchingCallback(hidManager, attachCallback, c);
IOHIDManagerScheduleWithRunLoop( hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
IOHIDManagerOpen( hidManager, kIOHIDOptionsTypeNone );
printf("thread spinning\n");
CFRunLoopRun(); // spins
printf("thread ending\n");
return NULL;
}
Controller* controllerInit(Core_t core) {
Controller *c = malloc(sizeof(Controller));
if (c == NULL) {
explode("failed to malloc");
}
c->core = core;
return c;
}
void controllerStart(Controller *c) {
pthread_t threadId;
printf("before thread\n");
pthread_create(&threadId, NULL, controllerThread, c);
printf("after thread\n");
}
// https://stackoverflow.com/questions/1363787/is-it-safe-to-call-cfrunloopstop-from-another-thread
void controllerStop(Controller_t c) {
// TODO: create an additional source for the run loop that can be used to trigger run loop termination code in a callback
}
//void printControl(char *name, char *value) {
// printf("{\"type\": \"control\", \"name\": \"%s\", \"value\": \"%s\"}\n", name, value);
// fflush(stdout);
//}