-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHIDSupport_SDL.m
283 lines (227 loc) · 8.4 KB
/
HIDSupport_SDL.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
//
// HIDSupport_SDL.m
// CoreBreach
//
// Created by CoreCode on 08.11.11.
// Copyright 2011 - 2012 CoreCode. Licensed under the GPL License, see LICENSE.txt
//
#import "HIDSupport_SDL.h"
#include <strings.h>
HIDSupport *sharedhid = nil;
@implementation HIDSupport
@synthesize hidDevices, started;
+ (HIDSupport *)sharedInstance
{
if (sharedhid == nil)
fatal("HIDSupport not allocated");
return sharedhid;
}
- (void)dealloc
{
//NSLog(@"hid dealloc");
sharedhid = nil;
for (int i = 0; i < kNumActions; i++)
{
[actionRecs[i].deviceName release];
}
[hidDevices release];
[super dealloc];
}
- (IBAction)saveConfiguration:(id)inSender
{
for (int i = 0; i < kNumActions; i++)
{
if (actionRecs[i].deviceName)
$setdefault(actionRecs[i].deviceName, $stringf(@"HID_%i_deviceName", i));
else
$remdefault($stringf(@"HID_%i_deviceName", i));
$setdefaulti(actionRecs[i].actionIndex, $stringf(@"HID_%i_actionIndex", i));
$setdefaulti(actionRecs[i].actionType, $stringf(@"HID_%i_actionType", i));
}
}
- (IBAction)restoreConfiguration:(id)inSender
{
for (int i = 0; i < kNumActions; i++)
{
NSString *deviceName = $default($stringf(@"HID_%i_deviceName", i));
int actionType = $defaulti($stringf(@"HID_%i_actionType", i));
int actionIndex = $defaulti($stringf(@"HID_%i_actionIndex", i));
actionRecs[i].deviceIndex = -1;
for (NSDictionary *dev in hidDevices)
{
if ([[dev objectForKey:@"name"] isEqualToString:deviceName])
{
actionRecs[i].deviceName = [deviceName copy];
actionRecs[i].actionType = actionType;
actionRecs[i].actionIndex = actionIndex;
actionRecs[i].deviceIndex = [[dev objectForKey:@"index"] intValue];
}
}
}
}
- (BOOL)isButton:(int)item
{
if (actionRecs[item].actionType == SDL_JOYBUTTONDOWN)
return YES;
else
return NO;
}
- (NSString *)configItem:(int)item forDevice:(NSDictionary *)dev
{
assert(SDL_WasInit(SDL_INIT_JOYSTICK));
int devindex = [[dev objectForKey:@"index"] intValue];
int times = 0;
SDL_Event event;
SDL_Joystick *joystick = SDL_JoystickOpen(devindex);
while (times < 100)
{
while (SDL_PollEvent(&event))
{
switch( event.type )
{
case SDL_JOYBUTTONDOWN: /* Handle Joystick Button Presses */ // we save the type for buttons as SDL_JOYBUTTONDOWN
case SDL_JOYAXISMOTION: /* Handle Joystick Motion */
{
// NSLog(@"got event %i", event.type);
if (((event.type == SDL_JOYAXISMOTION) && (event.jaxis.which == devindex)) ||
((event.type == SDL_JOYBUTTONDOWN) && (event.jbutton.which == devindex)))
{
actionRecs[item].deviceName = [[dev objectForKey:@"name"] copy];
actionRecs[item].deviceIndex = devindex;
actionRecs[item].actionType = event.type;
actionRecs[item].actionIndex = (event.type == SDL_JOYAXISMOTION) ? event.jaxis.axis : event.jbutton.button;
SDL_JoystickClose(joystick);
return [self nameOfItem:item];
}
}
default:
break;
}
}
SDL_Delay(50);
}
SDL_JoystickClose(joystick);
return nil;
}
- (NSString *)nameOfItem:(int)item
{
if (actionRecs[item].deviceIndex >= 0 &&
(actionRecs[item].actionType == SDL_JOYBUTTONDOWN || actionRecs[item].actionType == SDL_JOYAXISMOTION))
{
return $stringf(@"%@ %i", (actionRecs[item].actionType == SDL_JOYBUTTONDOWN) ? @"Button" : @"Axis", actionRecs[item].actionIndex);
}
else
return nil;
}
- (double)valueOfItem:(int)item
{
return actionRecs[item].value;
}
- (BOOL)item:(uint8_t)item identicalToItem:(uint8_t)otherItem
{
return ((actionRecs[item].deviceIndex == actionRecs[otherItem].deviceIndex) &&
(actionRecs[item].actionIndex == actionRecs[otherItem].actionIndex) &&
(actionRecs[item].actionType == actionRecs[otherItem].actionType));
}
- (void)printItem:(int)item
{
NSLog(@"item number %i name %@ deviceName %@ deviceIndex %i actionType %i actionIndex %i", item, [self nameOfItem:item], actionRecs[item].deviceName, actionRecs[item].deviceIndex, actionRecs[item].actionType, actionRecs[item].actionIndex);
}
- (void)clearItem:(uint8_t)item
{
actionRecs[item].deviceName = nil;
actionRecs[item].deviceIndex = 0;
actionRecs[item].actionType = 0;
actionRecs[item].actionIndex = -1;
actionRecs[item].value = 0.0;
}
- (void)handleEvent:(SDL_Event)event
{
// NSLog(@"got handleevent %i", event.type);
switch( event.type )
{
case SDL_JOYBUTTONDOWN: /* Handle Joystick Button Presses */
case SDL_JOYBUTTONUP: /* Handle Joystick Button Presses */
case SDL_JOYAXISMOTION: /* Handle Joystick Motion */
{
int actionIndex = (event.type == SDL_JOYAXISMOTION) ? event.jaxis.axis : event.jbutton.button;
int deviceIndex = (event.type == SDL_JOYAXISMOTION) ? event.jaxis.which : event.jbutton.which;
int actionType = (event.type == SDL_JOYAXISMOTION) ? event.type : SDL_JOYBUTTONDOWN; // we save the type for buttons as SDL_JOYBUTTONDOWN
for (int i = 0; i < kNumActions; i++)
{
if ((actionRecs[i].actionIndex == actionIndex) && // same action
(actionRecs[i].deviceIndex == deviceIndex) && // same device
(actionRecs[i].actionType == actionType)) // same kind
{
if (event.type == SDL_JOYAXISMOTION)
actionRecs[i].value = ((double)event.jaxis.value / 65536.0) + 0.5;
else
actionRecs[i].value = event.jbutton.state;
}
}
}
break;
}
}
- (void)startHID
{
assert(hidDevices);
assert(SDL_WasInit(SDL_INIT_JOYSTICK));
started = YES;
SDL_JoystickEventState(SDL_ENABLE);
for (int i = 0; i < kNumActions; i++)
{
for (NSMutableDictionary *dict in hidDevices)
{
if ([[dict objectForKey:@"index"] intValue] == actionRecs[i].deviceIndex) // this device needs opening
{
if (![dict objectForKey:@"sdljoystick"]) // and it aint opened
{
SDL_Joystick *joystick = SDL_JoystickOpen(actionRecs[i].deviceIndex);
[dict setValue:[NSValue valueWithPointer:joystick] forKey:@"sdljoystick"];
}
}
}
}
}
- (void)stopHID
{
started = NO;
for (NSMutableDictionary *dict in hidDevices)
{
SDL_Joystick *joystick = (SDL_Joystick *)[[dict objectForKey:@"sdljoystick"] pointerValue];
SDL_JoystickClose(joystick);
[dict removeObjectForKey:@"sdljoystick"];
}
SDL_JoystickEventState(SDL_DISABLE);
}
- (id)init
{
if ((self = [super init]))
{
if (sharedhid)
fatal("HID inited twice");
sharedhid = self;
assert(SDL_WasInit(SDL_INIT_JOYSTICK));
NSMutableArray *tmpDevices = [[NSMutableArray alloc] init];
for(int i = 0; i < SDL_NumJoysticks(); i++ )
{
NSMutableDictionary *dev = [[NSMutableDictionary alloc] init];
[dev setObject:$stringf(@"%s", SDL_JoystickName(i)) forKey:@"name"];
[dev setObject:$numi(i) forKey:@"index"];
[tmpDevices addObject:dev];
[dev release];
// SDL_Joystick *joystick;
//
// joystick = SDL_JoystickOpen(0);
// printf("buttons axis etc %i %i %i", SDL_JoystickNumButtons(joystick), SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick));
//
// SDL_JoystickClose(joystick);
}
hidDevices = [[NSArray alloc] initWithArray:tmpDevices];
[tmpDevices release];
// NSLog(@"%@", [hidDevices description]);
}
return self;
}
@end