ObjFW  Diff

Differences From Artifact [bcc812f365]:

To Artifact [d527831ab5]:


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
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







-
+

+



-
+
+










-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
+
+
+
-
-
-

-
+
-
-
-
+
-
-

-
+



-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-
+
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
+
-
-
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+













-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-

-
+







-
+







-
+








#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#import "OFEvdevGameController.h"
#import "HIDEvdevGameController.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFileManager.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFSet.h"
#import "HIDGameControllerAxis.h"
#import "HIDGameControllerButton.h"

#include <sys/ioctl.h>
#include <linux/input.h>

#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"

/*
 * Controllers with tested correct mapping:
 *
 *   Microsoft X-Box 360 pad [045E:028E]
 *   Joy-Con (L) [057E:2006]
 *   Joy-Con (R) [057E:2007]
 *   N64 Controller [057E:2019]
@interface HIDEvdevGameControllerAxis: HIDGameControllerAxis
 *   Sony Interactive Entertainment DualSense Wireless Controller [054C:0CE6]
 *   8BitDo Pro 2 Wired Controller [2DC8:3106]
 *   Stadia2SZY-0d6c [18D1:9400]
 *   Wireless Controller [054C:09CC]
 */

{
static const uint16_t vendorIDMicrosoft = 0x045E;
static const uint16_t vendorIDNintendo = 0x057E;
static const uint16_t vendorIDSony = 0x054C;
static const uint16_t vendorIDGoogle = 0x18D1;

@public
/* Microsoft controllers */
static const uint16_t productIDXbox360 = 0x028E;

/* Nintendo controllers */
	int32_t _minValue, _maxValue;
}
@end
static const uint16_t productIDLeftJoyCon = 0x2006;
static const uint16_t productIDRightJoyCon = 0x2007;
static const uint16_t productIDN64Controller = 0x2019;

/* Sony controllers */
@implementation HIDEvdevGameControllerAxis
static const uint16_t productIDDualSense = 0x0CE6;
static const uint16_t productIDDualShock4 = 0x09CC;

@end
/* Google controllers */
static const uint16_t productIDStadia = 0x9400;

static const uint16_t buttons[] = {
static const uint16_t buttonIDs[] = {
	BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TL2,
	BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_THUMBL, BTN_THUMBR,
	BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT,
	BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2
	BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, BTN_TRIGGER_HAPPY3,
	BTN_TRIGGER_HAPPY4, BTN_TRIGGER_HAPPY5, BTN_TRIGGER_HAPPY6,
	BTN_TRIGGER_HAPPY7, BTN_TRIGGER_HAPPY8, BTN_TRIGGER_HAPPY9,
	BTN_TRIGGER_HAPPY10, BTN_TRIGGER_HAPPY11, BTN_TRIGGER_HAPPY12,
	BTN_TRIGGER_HAPPY13, BTN_TRIGGER_HAPPY14, BTN_TRIGGER_HAPPY15,
	BTN_TRIGGER_HAPPY16, BTN_TRIGGER_HAPPY17, BTN_TRIGGER_HAPPY18,
	BTN_TRIGGER_HAPPY19, BTN_TRIGGER_HAPPY20, BTN_TRIGGER_HAPPY21,
	BTN_TRIGGER_HAPPY22, BTN_TRIGGER_HAPPY23, BTN_TRIGGER_HAPPY24,
	BTN_TRIGGER_HAPPY25, BTN_TRIGGER_HAPPY26, BTN_TRIGGER_HAPPY27,
	BTN_TRIGGER_HAPPY28, BTN_TRIGGER_HAPPY29, BTN_TRIGGER_HAPPY30,
	BTN_TRIGGER_HAPPY31, BTN_TRIGGER_HAPPY32, BTN_TRIGGER_HAPPY33,
	BTN_TRIGGER_HAPPY34, BTN_TRIGGER_HAPPY35, BTN_TRIGGER_HAPPY36,
	BTN_TRIGGER_HAPPY37, BTN_TRIGGER_HAPPY38, BTN_TRIGGER_HAPPY39,
	BTN_TRIGGER_HAPPY40
};
static const uint16_t axisIDs[] = {
	ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ, ABS_THROTTLE, ABS_RUDDER,
	ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X,
	ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y, ABS_HAT3X, ABS_HAT3Y
};

static OFGameControllerButton
buttonToName(uint16_t button, uint16_t vendorID, uint16_t productID)
static OFString *
buttonToName(uint16_t button)
{
	if (vendorID == vendorIDNintendo &&
	    productID == productIDLeftJoyCon) {
		switch (button) {
		case BTN_DPAD_RIGHT:
			return OFGameControllerNorthButton;
		case BTN_DPAD_LEFT:
			return OFGameControllerSouthButton;
		case BTN_DPAD_UP:
			return OFGameControllerWestButton;
		case BTN_DPAD_DOWN:
			return OFGameControllerEastButton;
		case BTN_Z:
			return OFGameControllerCaptureButton;
		case BTN_TR:
			return @"SL";
		case BTN_TR2:
			return @"SR";
	switch (button) {
	case BTN_A:
		return @"A";
	case BTN_B:
		return @"B";
	case BTN_C:
		return @"C";
	case BTN_X:
		return @"X";
	case BTN_Y:
		return @"Y";
	case BTN_Z:
		return @"Z";
	case BTN_TL:
		return @"TL";
	case BTN_TR:
		return @"TR";
	case BTN_TL2:
		return @"TL2";
	case BTN_TR2:
		return @"TR2";
		}
	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDRightJoyCon) {
		switch (button) {
		case BTN_WEST:
			return OFGameControllerNorthButton;
		case BTN_EAST:
			return OFGameControllerSouthButton;
		case BTN_SOUTH:
			return OFGameControllerWestButton;
		case BTN_NORTH:
			return OFGameControllerEastButton;
		case BTN_TL:
			return @"SL";
		case BTN_TL2:
			return @"SR";
	case BTN_SELECT:
		return @"Select";
	case BTN_START:
		return @"Start";
	case BTN_MODE:
		return @"Mode";
	case BTN_THUMBL:
		return @"Thumb L";
	case BTN_THUMBR:
		return @"Thumb R";
	case BTN_DPAD_UP:
		return @"DPad Up";
	case BTN_DPAD_DOWN:
		return @"DPad Down";
	case BTN_DPAD_LEFT:
		return @"DPad Left";
	case BTN_DPAD_RIGHT:
		return @"DPad Right";
	case BTN_TRIGGER_HAPPY1:
		return @"Trigger Happy 1";
	case BTN_TRIGGER_HAPPY2:
		return @"Trigger Happy 2";
		}
	} else if (vendorID == vendorIDNintendo &&
	    productID == productIDN64Controller) {
		switch (button) {
		case BTN_B:
			return OFGameControllerWestButton;
		case BTN_SELECT:
			return @"_C-Pad Up";
		case BTN_X:
			return @"_C-Pad Down";
		case BTN_Y:
			return @"_C-Pad Left";
		case BTN_C:
			return @"_C-Pad Right";
		case BTN_Z:
			return OFGameControllerCaptureButton;
	case BTN_TRIGGER_HAPPY3:
		return @"Trigger Happy 3";
	case BTN_TRIGGER_HAPPY4:
		return @"Trigger Happy 4";
	case BTN_TRIGGER_HAPPY5:
		return @"Trigger Happy 5";
	case BTN_TRIGGER_HAPPY6:
		return @"Trigger Happy 6";
	case BTN_TRIGGER_HAPPY7:
		return @"Trigger Happy 7";
	case BTN_TRIGGER_HAPPY8:
		return @"Trigger Happy 8";
	case BTN_TRIGGER_HAPPY9:
		return @"Trigger Happy 9";
	case BTN_TRIGGER_HAPPY10:
		return @"Trigger Happy 10";
	case BTN_TRIGGER_HAPPY11:
		return @"Trigger Happy 11";
	case BTN_TRIGGER_HAPPY12:
		return @"Trigger Happy 12";
	case BTN_TRIGGER_HAPPY13:
		return @"Trigger Happy 13";
		}
	} else if (vendorID == vendorIDSony &&
	    (productID == productIDDualSense ||
	    productID == productIDDualShock4)) {
		switch (button) {
		case BTN_NORTH:
			return OFGameControllerNorthButton;
		case BTN_WEST:
			return OFGameControllerWestButton;
	case BTN_TRIGGER_HAPPY14:
		return @"Trigger Happy 14";
	case BTN_TRIGGER_HAPPY15:
		return @"Trigger Happy 15";
	case BTN_TRIGGER_HAPPY16:
		return @"Trigger Happy 16";
	case BTN_TRIGGER_HAPPY17:
		return @"Trigger Happy 17";
	case BTN_TRIGGER_HAPPY18:
		return @"Trigger Happy 18";
		}
	} else if (vendorID == vendorIDGoogle && productID == productIDStadia) {
		switch (button) {
		case BTN_TRIGGER_HAPPY1:
			return @"Assistant";
		case BTN_TRIGGER_HAPPY2:
			return OFGameControllerCaptureButton;
		}
	}

	switch (button) {
	case BTN_Y:
		return OFGameControllerNorthButton;
	case BTN_A:
		return OFGameControllerSouthButton;
	case BTN_X:
		return OFGameControllerWestButton;
	case BTN_B:
		return OFGameControllerEastButton;
	case BTN_TL2:
		return OFGameControllerLeftTriggerButton;
	case BTN_TR2:
		return OFGameControllerRightTriggerButton;
	case BTN_TL:
		return OFGameControllerLeftShoulderButton;
	case BTN_TR:
	case BTN_TRIGGER_HAPPY19:
		return @"Trigger Happy 19";
	case BTN_TRIGGER_HAPPY20:
		return @"Trigger Happy 20";
	case BTN_TRIGGER_HAPPY21:
		return @"Trigger Happy 21";
	case BTN_TRIGGER_HAPPY22:
		return @"Trigger Happy 22";
	case BTN_TRIGGER_HAPPY23:
		return @"Trigger Happy 23";
	case BTN_TRIGGER_HAPPY24:
		return @"Trigger Happy 24";
	case BTN_TRIGGER_HAPPY25:
		return @"Trigger Happy 25";
	case BTN_TRIGGER_HAPPY26:
		return @"Trigger Happy 26";
	case BTN_TRIGGER_HAPPY27:
		return @"Trigger Happy 27";
	case BTN_TRIGGER_HAPPY28:
		return @"Trigger Happy 28";
	case BTN_TRIGGER_HAPPY29:
		return @"Trigger Happy 29";
	case BTN_TRIGGER_HAPPY30:
		return @"Trigger Happy 30";
	case BTN_TRIGGER_HAPPY31:
		return @"Trigger Happy 31";
	case BTN_TRIGGER_HAPPY32:
		return @"Trigger Happy 32";
	case BTN_TRIGGER_HAPPY33:
		return @"Trigger Happy 33";
	case BTN_TRIGGER_HAPPY34:
		return @"Trigger Happy 34";
	case BTN_TRIGGER_HAPPY35:
		return @"Trigger Happy 35";
	case BTN_TRIGGER_HAPPY36:
		return @"Trigger Happy 36";
	case BTN_TRIGGER_HAPPY37:
		return @"Trigger Happy 37";
	case BTN_TRIGGER_HAPPY38:
		return @"Trigger Happy 38";
	case BTN_TRIGGER_HAPPY39:
		return @"Trigger Happy 39";
	case BTN_TRIGGER_HAPPY40:
		return @"Trigger Happy 40";
	default:
		return nil;
	}
}

static OFString *
axisToName(uint16_t axis)
{
	switch (axis) {
	case ABS_X:
		return @"X";
	case ABS_Y:
		return @"Y";
	case ABS_Z:
		return @"Z";
	case ABS_RX:
		return @"RX";
	case ABS_RY:
		return @"RY";
	case ABS_RZ:
		return @"RZ";
	case ABS_THROTTLE:
		return @"Throttle";
	case ABS_RUDDER:
		return OFGameControllerRightShoulderButton;
	case BTN_THUMBL:
		return OFGameControllerLeftStickButton;
	case BTN_THUMBR:
		return OFGameControllerRightStickButton;
	case BTN_DPAD_UP:
		return OFGameControllerDPadUpButton;
		return @"Rudder";
	case BTN_DPAD_DOWN:
		return OFGameControllerDPadDownButton;
	case BTN_DPAD_LEFT:
		return OFGameControllerDPadLeftButton;
	case BTN_DPAD_RIGHT:
		return OFGameControllerDPadRightButton;
	case BTN_START:
		return OFGameControllerStartButton;
	case ABS_WHEEL:
		return @"Wheel";
	case BTN_SELECT:
		return OFGameControllerSelectButton;
	case BTN_MODE:
	case ABS_GAS:
		return OFGameControllerHomeButton;
	}

	return nil;
		return @"Gas";
	case ABS_BRAKE:
		return @"Brake";
	case ABS_HAT0X:
		return @"HAT0X";
	case ABS_HAT0Y:
		return @"HAT0Y";
	case ABS_HAT1X:
		return @"HAT1X";
	case ABS_HAT1Y:
		return @"HAT1Y";
	case ABS_HAT2X:
		return @"HAT2X";
	case ABS_HAT2Y:
		return @"HAT2Y";
	case ABS_HAT3X:
		return @"HAT3X";
	case ABS_HAT3Y:
		return @"HAT3Y";
	default:
		return nil;
	}
}

static float
scale(float value, float min, float max)
{
	if (value < min)
		value = min;
	if (value > max)
		value = max;

	return ((value - min) / (max - min) * 2) - 1;
}

static bool
emulateRightAnalogStick(uint16_t vendorID, uint16_t productID,
    OFMutableSet *pressedButtons, OFPoint *rightAnalogStickPosition)
{
	if (vendorID == vendorIDNintendo &&
	    productID == productIDN64Controller) {
		if ([pressedButtons containsObject: @"_C-Pad Left"] &&
		    [pressedButtons containsObject: @"_C-Pad Right"])
			rightAnalogStickPosition->x = -0.f;
		else if ([pressedButtons containsObject: @"_C-Pad Left"])
			rightAnalogStickPosition->x = -1.f;
		else if ([pressedButtons containsObject: @"_C-Pad Right"])
			rightAnalogStickPosition->x = 1.f;
		else
			rightAnalogStickPosition->x = 0.f;

		if ([pressedButtons containsObject: @"_C-Pad Up"] &&
		    [pressedButtons containsObject: @"_C-Pad Down"])
			rightAnalogStickPosition->y = -0.f;
		else if ([pressedButtons containsObject: @"_C-Pad Up"])
			rightAnalogStickPosition->y = -1.f;
		else if ([pressedButtons containsObject: @"_C-Pad Down"])
			rightAnalogStickPosition->y = 1.f;
		else
			rightAnalogStickPosition->y = 0.f;

		return true;
	}

	return false;
}

@implementation OFEvdevGameController
@synthesize name = _name, buttons = _buttons;
@implementation HIDEvdevGameController
@synthesize name = _name, buttons = _buttons, axes = _axes;
@synthesize hasLeftAnalogStick = _hasLeftAnalogStick;
@synthesize hasRightAnalogStick = _hasRightAnalogStick;
@synthesize rightAnalogStickPosition = _rightAnalogStickPosition;

+ (OFArray OF_GENERIC(OFGameController *) *)controllers
+ (OFArray OF_GENERIC(HIDGameController *) *)controllers
{
	OFMutableArray *controllers = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();

	for (OFString *device in [[OFFileManager defaultManager]
	    contentsOfDirectoryAtPath: @"/dev/input"]) {
		OFString *path;
		OFGameController *controller;
		HIDGameController *controller;

		if (![device hasPrefix: @"event"])
			continue;

		path = [@"/dev/input" stringByAppendingPathComponent: device];

		@try {
			controller = [[[OFEvdevGameController alloc]
			controller = [[[HIDEvdevGameController alloc]
			    of_initWithPath: path] autorelease];
		} @catch (OFOpenItemFailedException *e) {
			if (e.errNo == EACCES)
				continue;

			@throw e;
		} @catch (OFInvalidArgumentException *e) {
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
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
338
339
340







-
-
-
-


+










+
+
+
-
+
+
+


-
+








- (instancetype)of_initWithPath: (OFString *)path
{
	self = [super init];

	@try {
		OFStringEncoding encoding = [OFLocale encoding];
		unsigned long evBits[OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    EV_MAX) / OF_ULONG_BIT] = { 0 };
		unsigned long absBits[OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    ABS_MAX) / OF_ULONG_BIT] = { 0 };
		struct input_id inputID;
		char name[128];
		OFMutableDictionary *buttons, *axes;

		_path = [path copy];

		if ((_fd = open([_path cStringWithEncoding: encoding],
		    O_RDONLY | O_NONBLOCK)) == -1)
			@throw [OFOpenItemFailedException
			    exceptionWithPath: _path
					 mode: @"r"
					errNo: errno];

		_evBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    EV_MAX) / OF_ULONG_BIT, sizeof(unsigned long));

		if (ioctl(_fd, EVIOCGBIT(0, sizeof(evBits)), evBits) == -1)
		if (ioctl(_fd, EVIOCGBIT(0, OFRoundUpToPowerOf2(
		    OF_ULONG_BIT, EV_MAX) / OF_ULONG_BIT *
		    sizeof(unsigned long)), _evBits) == -1)
			@throw [OFInitializationFailedException exception];

		if (!OFBitSetIsSet(evBits, EV_KEY))
		if (!OFBitSetIsSet(_evBits, EV_KEY))
			@throw [OFInvalidArgumentException exception];

		_keyBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(OF_ULONG_BIT,
		    KEY_MAX) / OF_ULONG_BIT, sizeof(unsigned long));

		if (ioctl(_fd, EVIOCGBIT(EV_KEY, OFRoundUpToPowerOf2(
		    OF_ULONG_BIT, KEY_MAX) / OF_ULONG_BIT *
326
327
328
329
330
331
332

333
334


335
336
337
338







339
340
341
342
343
344
345







346


347
348
349








350
351
352
353
354
355




356
357
358

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386


387
388
389
390

391
392
393
394

395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412

413
414
415
416

417
418
419

420
421
422


423
424
425
426
427
428
429
430




431
432
433

434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451

452

453
454
455
456

457
458
459
460
461
462
463
353
354
355
356
357
358
359
360


361
362
363



364
365
366
367
368
369
370
371






372
373
374
375
376
377
378
379
380
381



382
383
384
385
386
387
388
389
390
391
392



393
394
395
396



397




























398
399




400




401







402










403




404



405



406
407








408
409
410
411
412


413



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434

435
436
437
438
439
440
441
442







+
-
-
+
+

-
-
-
+
+
+
+
+
+
+

-
-
-
-
-
-
+
+
+
+
+
+
+

+
+
-
-
-
+
+
+
+
+
+
+
+



-
-
-
+
+
+
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-

-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
+
-
-
-
+
+
-
-
-
-
-
-
-
-
+
+
+
+

-
-
+
-
-
-















+

+



-
+








		if (ioctl(_fd, EVIOCGNAME(sizeof(name)), name) == -1)
			@throw [OFInitializationFailedException exception];

		_name = [[OFString alloc] initWithCString: name
						 encoding: encoding];

		buttons = [[OFMutableDictionary alloc] init];
		_buttons = [[OFMutableSet alloc] init];
		for (size_t i = 0; i < sizeof(buttons) / sizeof(*buttons);
		_buttons = buttons;
		for (size_t i = 0; i < sizeof(buttonIDs) / sizeof(*buttonIDs);
		    i++) {
			if (OFBitSetIsSet(_keyBits, buttons[i])) {
				OFGameControllerButton button = buttonToName(
				    buttons[i], _vendorID, _productID);
			if (OFBitSetIsSet(_keyBits, buttonIDs[i])) {
				OFString *buttonName;
				HIDGameControllerButton *button;

				buttonName = buttonToName(buttonIDs[i]);
				if (buttonName == nil)
					continue;

				if (button != nil && ![button hasPrefix: @"_"])
					[_buttons addObject: button];
			}
		}

		_pressedButtons = [[OFMutableSet alloc] init];
				button = [[[HIDGameControllerButton alloc]
				    initWithName: buttonName] autorelease];

				[buttons setObject: button forKey: buttonName];
			}
		}
		[buttons makeImmutable];

		axes = [[OFMutableDictionary alloc] init];
		_axes = axes;
		if (OFBitSetIsSet(evBits, EV_ABS)) {
			if (ioctl(_fd, EVIOCGBIT(EV_ABS, sizeof(absBits)),
			    absBits) == -1)
		if (OFBitSetIsSet(_evBits, EV_ABS)) {
			_absBits = OFAllocZeroedMemory(OFRoundUpToPowerOf2(
			    OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT,
			    sizeof(unsigned long));

			if (ioctl(_fd, EVIOCGBIT(EV_ABS, OFRoundUpToPowerOf2(
			    OF_ULONG_BIT, ABS_MAX) / OF_ULONG_BIT *
			    sizeof(unsigned long)), _absBits) == -1)
				@throw [OFInitializationFailedException
				    exception];

			if (_vendorID == vendorIDNintendo &&
			    _productID == productIDRightJoyCon) {
				/*
			for (size_t i = 0;
			    i < sizeof(axisIDs) / sizeof(*axisIDs); i++) {
				if (OFBitSetIsSet(_absBits, axisIDs[i])) {
					OFString *axisName;
				 * Make the right analog stick on the right
				 * Joy-Con the left analog stick so that it can
				 * be used as a single controller.
					HIDEvdevGameControllerAxis *axis;
				 */
				_leftAnalogStickXBit = ABS_RX;
				_leftAnalogStickYBit = ABS_RY;
				_rightAnalogStickXBit = ABS_X;
				_rightAnalogStickYBit = ABS_Y;
				_leftTriggerPressureBit = ABS_Z;
				_rightTriggerPressureBit = ABS_RZ;
			} else if (_vendorID == vendorIDGoogle &&
			    _productID == productIDStadia) {
				/*
				 * It's unclear how this can be screwed up
				 * *this* bad.
				 */
				_leftAnalogStickXBit = ABS_X;
				_leftAnalogStickYBit = ABS_Y;
				_rightAnalogStickXBit = ABS_Z;
				_rightAnalogStickYBit = ABS_RZ;
				_leftTriggerPressureBit = ABS_BRAKE;
				_rightTriggerPressureBit = ABS_GAS;
			} else {
				_leftAnalogStickXBit = ABS_X;
				_leftAnalogStickYBit = ABS_Y;
				_rightAnalogStickXBit = ABS_RX;
				_rightAnalogStickYBit = ABS_RY;
				_leftTriggerPressureBit = ABS_Z;
				_rightTriggerPressureBit = ABS_RZ;
			}


					axisName = axisToName(axisIDs[i]);
			if (OFBitSetIsSet(absBits, _leftAnalogStickXBit) &&
			    OFBitSetIsSet(absBits, _leftAnalogStickYBit))
				_hasLeftAnalogStick = true;

					if (axisName == nil)
			if (OFBitSetIsSet(absBits, _rightAnalogStickXBit) &&
			    OFBitSetIsSet(absBits, _rightAnalogStickYBit))
				_hasRightAnalogStick = true;

						continue;
			if (_vendorID == vendorIDNintendo &&
			    _productID == productIDN64Controller &&
			    OFBitSetIsSet(_keyBits, BTN_Y) &&
			    OFBitSetIsSet(_keyBits, BTN_C) &&
			    OFBitSetIsSet(_keyBits, BTN_SELECT) &&
			    OFBitSetIsSet(_keyBits, BTN_X))
				_hasRightAnalogStick = true;

			if (OFBitSetIsSet(absBits, ABS_HAT0X) &&
			    OFBitSetIsSet(absBits, ABS_HAT0Y)) {
				_DPadIsHAT0 = true;

				[_buttons addObject:
				    OFGameControllerDPadLeftButton];
				[_buttons addObject:
				    OFGameControllerDPadRightButton];
				[_buttons addObject:
				    OFGameControllerDPadUpButton];
					axis = [[[HIDEvdevGameControllerAxis
				[_buttons addObject:
				    OFGameControllerDPadDownButton];
			}

					    alloc] initWithName: axisName]
			if (OFBitSetIsSet(absBits, _leftTriggerPressureBit)) {
				_hasLeftTriggerPressure = true;
				[_buttons addObject:
					    autorelease];
				    OFGameControllerLeftTriggerButton];
			}


					[axes setObject: axis forKey: axisName];
			if (OFBitSetIsSet(absBits, _rightTriggerPressureBit)) {
				_hasRightTriggerPressure = true;
				[_buttons addObject:
				    OFGameControllerRightTriggerButton];
			}
		}

		[_buttons makeImmutable];
				}
			}
		}
		[axes makeImmutable];

		@try {
			[self of_pollState];
		[self of_pollState];
		} @catch (OFReadFailedException *e) {
			@throw [OFInitializationFailedException exception];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_path release];

	if (_fd != -1)
		close(_fd);

	OFFreeMemory(_evBits);
	OFFreeMemory(_keyBits);
	OFFreeMemory(_absBits);

	[_name release];
	[_buttons release];
	[_pressedButtons release];
	[_axes release];

	[super dealloc];
}

- (OFNumber *)vendorID
{
	return [OFNumber numberWithUnsignedShort: _vendorID];
475
476
477
478
479
480
481
482
483
484

485
486
487
488
489
490
491


492
493

494
495

496
497
498

499
500
501
502
503
504
505
506
507
508
509
510
511


512
513

514
515
516
517
518
519

520
521
522
523
524
525




526
527
528

529
530
531
532
533
534
535
536
537
538
539
540

541
542
543
544
545
546
547
548
549
550

551
552
553




554
555
556
557


558
559
560

561

562
563
564
565
566

567
568
569
570
571
572
573
574
575
576
577
578





579
580

581
582
583
584
585




586
587
588
589



590
591
592

593
594
595

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614

615


616
617
618
619
620
621
622
454
455
456
457
458
459
460



461
462






463
464


465


466



467





468







469
470


471



472


473






474
475
476
477



478





479






480










481



482
483
484
485
486



487
488


489
490

491





492












493
494
495
496
497
498

499





500
501
502
503
504



505
506
507



508



509











510
511
512
513
514
515
516
517
518

519
520
521
522
523
524
525
526
527







-
-
-
+

-
-
-
-
-
-
+
+
-
-
+
-
-
+
-
-
-
+
-
-
-
-
-

-
-
-
-
-
-
-
+
+
-
-
+
-
-
-

-
-
+
-
-
-
-
-
-
+
+
+
+
-
-
-
+
-
-
-
-
-

-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
+
+
+

-
-
-
+
+
-
-

+
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+

-
+
-
-
-
-
-
+
+
+
+

-
-
-
+
+
+
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-








+
-
+
+








	if (ioctl(_fd, EVIOCGKEY(sizeof(keyState)), &keyState) == -1)
		@throw [OFReadFailedException
		    exceptionWithObject: self
			requestedLength: sizeof(keyState)
				  errNo: errno];

	[_pressedButtons removeAllObjects];

	for (size_t i = 0; i < sizeof(buttons) / sizeof(*buttons);
	for (size_t i = 0; i < sizeof(buttonIDs) / sizeof(*buttonIDs);
	    i++) {
		if (OFBitSetIsSet(_keyBits, buttons[i]) &&
		    OFBitSetIsSet(keyState, buttons[i])) {
			OFGameControllerButton button = buttonToName(
			    buttons[i], _vendorID, _productID);

			if (button != nil)
		OFString *name;
		HIDGameControllerButton *button;
				[_pressedButtons addObject: button];
		}

	}

		if (!OFBitSetIsSet(_keyBits, buttonIDs[i]))
	if (_DPadIsHAT0) {
		struct input_absinfo infoX, infoY;

			continue;
		if (ioctl(_fd, EVIOCGABS(ABS_HAT0X), &infoX) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoX)
					  errNo: errno];

		if (ioctl(_fd, EVIOCGABS(ABS_HAT0Y), &infoY) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoY)
					  errNo: errno];

		if (infoX.value < 0)
		name = buttonToName(buttonIDs[i]);
		if (name == nil)
			[_pressedButtons addObject:
			    OFGameControllerDPadLeftButton];
			continue;
		else if (infoX.value > 0)
			[_pressedButtons addObject:
			    OFGameControllerDPadRightButton];

		if (infoY.value < 0)
			[_pressedButtons addObject:
		button = [_buttons objectForKey: name];
			    OFGameControllerDPadUpButton];
		else if (infoY.value > 0)
			[_pressedButtons addObject:
			    OFGameControllerDPadDownButton];
	}

		if (button == nil)
			continue;

		button.value = (OFBitSetIsSet(keyState, buttonIDs[i]) ? 1 : 0);
	if (_hasLeftAnalogStick) {
		struct input_absinfo infoX, infoY;

	}
		if (ioctl(_fd, EVIOCGABS(_leftAnalogStickXBit), &infoX) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoX)
					  errNo: errno];

		if (ioctl(_fd, EVIOCGABS(_leftAnalogStickYBit), &infoY) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoY)
					  errNo: errno];

	if (OFBitSetIsSet(_evBits, EV_ABS)) {
		_leftAnalogStickMinX = infoX.minimum;
		_leftAnalogStickMaxX = infoX.maximum;
		_leftAnalogStickMinY = infoY.minimum;
		_leftAnalogStickMaxY = infoY.maximum;
		_leftAnalogStickPosition.x = scale(infoX.value,
		    _leftAnalogStickMinX, _leftAnalogStickMaxX);
		_leftAnalogStickPosition.y = scale(infoY.value,
		    _leftAnalogStickMinY, _leftAnalogStickMaxY);
	}

		for (size_t i = 0; i < sizeof(axisIDs) / sizeof(*axisIDs);
	if (!emulateRightAnalogStick(_vendorID, _productID, _pressedButtons,
	    &_rightAnalogStickPosition) && _hasRightAnalogStick) {
		struct input_absinfo infoX, infoY;
		    i++) {
			struct input_absinfo info;
			OFString *name;
			HIDEvdevGameControllerAxis *axis;

		if (ioctl(_fd, EVIOCGABS(_rightAnalogStickXBit), &infoX) == -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
			if (!OFBitSetIsSet(_absBits, axisIDs[i]))
				continue;
				requestedLength: sizeof(infoX)
					  errNo: errno];

			name = axisToName(axisIDs[i]);
		if (ioctl(_fd, EVIOCGABS(_rightAnalogStickYBit), &infoY) == -1)
			if (name == nil)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(infoY)
					  errNo: errno];

				continue;
		_rightAnalogStickMinX = infoX.minimum;
		_rightAnalogStickMaxX = infoX.maximum;
		_rightAnalogStickMinY = infoY.minimum;
		_rightAnalogStickMaxY = infoY.maximum;
		_rightAnalogStickPosition.x = scale(infoX.value,
		    _rightAnalogStickMinX, _rightAnalogStickMaxX);
		_rightAnalogStickPosition.y = scale(infoY.value,
		    _rightAnalogStickMinY, _rightAnalogStickMaxY);
	}

	if (_hasLeftTriggerPressure) {
		struct input_absinfo info;

			axis = (HIDEvdevGameControllerAxis *)
			    [_axes objectForKey: name];
			if (axis == nil)
				continue;

		if (ioctl(_fd, EVIOCGABS( _leftTriggerPressureBit), &info) ==
			if (ioctl(_fd, EVIOCGABS(axisIDs[i]), &info) == -1)
		    -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(info)
					  errNo: errno];
				@throw [OFReadFailedException
				    exceptionWithObject: self
					requestedLength: sizeof(info)
						  errNo: errno];

		_leftTriggerMinPressure = info.minimum;
		_leftTriggerMaxPressure = info.maximum;
		_leftTriggerPressure = scale(info.value,
			axis->_minValue = info.minimum;
			axis->_maxValue = info.maximum;
			axis.value = scale(info.value,
		    _leftTriggerMinPressure, _leftTriggerMaxPressure);
	}

			    info.minimum, info.maximum);
	if (_hasRightTriggerPressure) {
		struct input_absinfo info;

		}
		if (ioctl(_fd, EVIOCGABS(_rightTriggerPressureBit), &info) ==
		    -1)
			@throw [OFReadFailedException
			    exceptionWithObject: self
				requestedLength: sizeof(info)
					  errNo: errno];

		_rightTriggerMinPressure = info.minimum;
		_rightTriggerMaxPressure = info.maximum;
		_rightTriggerPressure = scale(info.value,
		    _rightTriggerMinPressure, _rightTriggerMaxPressure);
	}
}

- (void)retrieveState
{
	struct input_event event;

	for (;;) {
		OFString *name;
		OFGameControllerButton button;
		HIDGameControllerButton *button;
		HIDEvdevGameControllerAxis *axis;

		errno = 0;

		if (read(_fd, &event, sizeof(event)) < (int)sizeof(event)) {
			if (errno == EWOULDBLOCK)
				return;

639
640
641
642
643
644
645
646
647
648
649
650
651







652
653
654

655
656

657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674


675
676
677


678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694


695
696

697
698

699
700
701
702
703
704
705
706

707
708

709
710
711

712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735

736
737
738
739

740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
544
545
546
547
548
549
550






551
552
553
554
555
556
557
558


559


560
561
562
















563
564



565
566

















567
568


569


570








571


572



573










574







575
576
577
578
579

580
581
582
583

584
585
586
587
588
589
590
591
592
593
594
595
596
597






































598







-
-
-
-
-
-
+
+
+
+
+
+
+

-
-
+
-
-
+


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-
-
+
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-

-
-
-
-
-
-
-





-
+



-
+













-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

		case EV_SYN:
			if (event.value == SYN_DROPPED) {
				_discardUntilReport = true;
				continue;
			}
			break;
		case EV_KEY:
			if ((button = buttonToName(event.code, _vendorID,
			    _productID)) != nil) {
				if (event.value)
					[_pressedButtons addObject: button];
				else
					[_pressedButtons removeObject: button];
			name = buttonToName(event.code);
			if (name == nil)
				continue;

			button = [_buttons objectForKey: name];
			if (button == nil)
				continue;

				emulateRightAnalogStick(_vendorID, _productID,
				    _pressedButtons,
			button.value = (event.value ? 1 : 0);
				    &_rightAnalogStickPosition);
			}

			break;
		case EV_ABS:
			if (event.code == _leftAnalogStickXBit)
				_leftAnalogStickPosition.x = scale(event.value,
				    _leftAnalogStickMinX, _leftAnalogStickMaxX);
			else if (event.code == _leftAnalogStickYBit)
				_leftAnalogStickPosition.y = scale(event.value,
				    _leftAnalogStickMinY, _leftAnalogStickMaxY);
			else if (event.code == _rightAnalogStickXBit)
				_rightAnalogStickPosition.x = scale(event.value,
				    _rightAnalogStickMinX,
				    _rightAnalogStickMaxX);
			else if (event.code == _rightAnalogStickYBit)
				_rightAnalogStickPosition.y = scale(event.value,
				    _rightAnalogStickMinY,
				    _rightAnalogStickMaxY);
			else if (event.code == ABS_HAT0X) {
				if (event.value < 0) {
			name = axisToName(event.code);
			if (name == nil)
					[_pressedButtons addObject:
					    OFGameControllerDPadLeftButton];
					[_pressedButtons removeObject:
				continue;

					    OFGameControllerDPadRightButton];
				} else if (event.value > 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadRightButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadLeftButton];
				} else {
					[_pressedButtons removeObject:
					    OFGameControllerDPadLeftButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadRightButton];
				}
			} else if (event.code == ABS_HAT0Y) {
				if (event.value < 0) {
					[_pressedButtons addObject:
					    OFGameControllerDPadUpButton];
					[_pressedButtons removeObject:
			axis = (HIDEvdevGameControllerAxis *)
			    [_axes objectForKey: name];
					    OFGameControllerDPadDownButton];
				} else if (event.value > 0) {
			if (axis == nil)
					[_pressedButtons addObject:
					    OFGameControllerDPadDownButton];
				continue;
					[_pressedButtons removeObject:
					    OFGameControllerDPadUpButton];
				} else {
					[_pressedButtons removeObject:
					    OFGameControllerDPadUpButton];
					[_pressedButtons removeObject:
					    OFGameControllerDPadDownButton];
				}

			} else if (event.code == _leftTriggerPressureBit) {
				_leftTriggerPressure = scale(event.value,
			axis.value = scale(event.value,
				    _leftTriggerMinPressure,
				    _leftTriggerMaxPressure);

			   axis->_minValue, axis->_maxValue);
				if (_leftTriggerPressure > 0)
					[_pressedButtons addObject:
					    OFGameControllerLeftTriggerButton];
				else
					[_pressedButtons removeObject:
					    OFGameControllerLeftTriggerButton];
			} else if (event.code == _rightTriggerPressureBit) {
				_rightTriggerPressure = scale(event.value,
				    _rightTriggerMinPressure,
				    _rightTriggerMaxPressure);

				if (_rightTriggerPressure > 0)
					[_pressedButtons addObject:
					    OFGameControllerRightTriggerButton];
				else
					[_pressedButtons removeObject:
					    OFGameControllerRightTriggerButton];
			}
			break;
		}
	}
}

- (OFComparisonResult)compare: (OFEvdevGameController *)otherController
- (OFComparisonResult)compare: (HIDEvdevGameController *)otherController
{
	unsigned long long selfIndex, otherIndex;

	if (![otherController isKindOfClass: [OFEvdevGameController class]])
	if (![otherController isKindOfClass: [HIDEvdevGameController class]])
		@throw [OFInvalidArgumentException exception];

	selfIndex = [_path substringFromIndex: 16].unsignedLongLongValue;
	otherIndex = [otherController->_path substringFromIndex: 16]
	    .unsignedLongLongValue;

	if (selfIndex > otherIndex)
		return OFOrderedDescending;
	if (selfIndex < otherIndex)
		return OFOrderedAscending;

	return OFOrderedSame;
}

- (OFSet *)pressedButtons
{
	OFMutableSet *pressedButtons =
	    [OFMutableSet setWithCapacity: _pressedButtons.count];

	for (OFGameControllerButton button in _pressedButtons)
		if (![button hasPrefix: @"_"])
			[pressedButtons addObject: button];

	[pressedButtons makeImmutable];

	return pressedButtons;
}

- (OFPoint)leftAnalogStickPosition
{
	if (_vendorID == vendorIDNintendo && _productID == productIDLeftJoyCon)
		return OFMakePoint(
		    _leftAnalogStickPosition.y, -_leftAnalogStickPosition.x);
	if (_vendorID == vendorIDNintendo && _productID == productIDRightJoyCon)
		return OFMakePoint(
		    -_leftAnalogStickPosition.y, _leftAnalogStickPosition.x);

	return _leftAnalogStickPosition;
}

- (float)pressureForButton: (OFGameControllerButton)button
{
	if (button == OFGameControllerLeftTriggerButton &&
	    _hasLeftTriggerPressure)
		return _leftTriggerPressure;
	if (button == OFGameControllerRightTriggerButton &&
	    _hasRightTriggerPressure)
		return _rightTriggerPressure;

	return [super pressureForButton: button];
}
@end