forked from 360Controller/360Controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pref360ControlPref.m
1141 lines (1025 loc) · 44.3 KB
/
Pref360ControlPref.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
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
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
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
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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MICE Xbox 360 Controller driver for Mac OS X
Copyright (C) 2006-2013 Colin Munro
Pref360ControlPref.m - main source of the pref pane
This file is part of Xbox360Controller.
Xbox360Controller is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Xbox360Controller 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.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <mach/mach.h>
#include <IOKit/usb/IOUSBLib.h>
#import "Pref360ControlPref.h"
#import "DeviceItem.h"
#import "ControlPrefs.h"
#import "DeviceLister.h"
#import "MyWhole360Controller.h"
#import "MyWhole360ControllerMapper.h"
#import "MyTrigger.h"
#import "MyDeadZoneViewer.h"
#import "MyBatteryMonitor.h"
#import "MyAnalogStick.h"
#define NO_ITEMS @"No devices found"
// Passes a C callback back to the Objective C class
static void CallbackFunction(void *target,IOReturn result,void *refCon,void *sender)
{
if (target) [((__bridge Pref360ControlPref*)target) eventQueueFired:sender withResult:result];
}
// Handle callback for when our device is connected or disconnected. Both events are
// actually handled identically.
static void callbackHandleDevice(void *param,io_iterator_t iterator)
{
io_service_t object = 0;
BOOL update = NO;
while ((object = IOIteratorNext(iterator))) {
IOObjectRelease(object);
update = YES;
}
if (update) [(__bridge Pref360ControlPref*)param handleDeviceChange];
}
@interface Pref360ControlPref ()
@property (strong) NSMutableArray *deviceArray;
@end
@implementation Pref360ControlPref
{
@private
// Internal info
IOHIDElementCookie axis[6],buttons[15];
IOHIDDeviceInterface122 **device;
IOHIDQueueInterface **hidQueue;
FFDeviceObjectReference ffDevice;
io_registry_entry_t registryEntry;
int largeMotor, smallMotor;
IONotificationPortRef notifyPort;
CFRunLoopSourceRef notifySource;
io_iterator_t onIteratorWired, offIteratorWired;
io_iterator_t onIteratorWireless, offIteratorWireless;
FFEFFECT *effect;
FFCUSTOMFORCE *customforce;
FFEffectObjectReference effectRef;
}
-(void)awakeFromNib {
[_aboutPopover setAppearance:NSPopoverAppearanceHUD];
[_rumbleOptions removeAllItems];
[_rumbleOptions addItemsWithTitles:@[@"Default", @"None"]];
if (controllerType == XboxOneController || controllerType == XboxOnePretend360Controller)
[_rumbleOptions addItemsWithTitles:@[@"Triggers Only", @"Both"]];
}
// Set the pattern on the LEDs
- (void)updateLED:(int)ledIndex
{
FFEFFESCAPE escape = {0};
unsigned char c = ledIndex;
if (ffDevice == 0) return;
escape.dwSize = sizeof(escape);
escape.dwCommand = 0x02;
escape.cbInBuffer = sizeof(c);
escape.lpvInBuffer = &c;
FFDeviceEscape(ffDevice, &escape);
}
// This will initialize the ff effect.
- (void)testMotorsInit
{
if (ffDevice == 0) return;
FFCAPABILITIES capabs;
FFDeviceGetForceFeedbackCapabilities(ffDevice, &capabs);
if(capabs.numFfAxes != 2) return;
effect = calloc(1, sizeof(FFEFFECT));
customforce = calloc(1, sizeof(FFCUSTOMFORCE));
LONG *c = calloc(2, sizeof(LONG));
DWORD *a = calloc(2, sizeof(DWORD));
LONG *d = calloc(2, sizeof(LONG));
c[0] = 0;
c[1] = 0;
a[0] = capabs.ffAxes[0];
a[1] = capabs.ffAxes[1];
d[0] = 0;
d[1] = 0;
customforce->cChannels = 2;
customforce->cSamples = 2;
customforce->rglForceData = c;
customforce->dwSamplePeriod = 100*1000;
effect->cAxes = capabs.numFfAxes;
effect->rglDirection = d;
effect->rgdwAxes = a;
effect->dwSamplePeriod = 0;
effect->dwGain = 10000;
effect->dwFlags = FFEFF_OBJECTOFFSETS | FFEFF_SPHERICAL;
effect->dwSize = sizeof(FFEFFECT);
effect->dwDuration = FF_INFINITE;
effect->dwSamplePeriod = 100*1000;
effect->cbTypeSpecificParams = sizeof(FFCUSTOMFORCE);
effect->lpvTypeSpecificParams = customforce;
effect->lpEnvelope = NULL;
FFDeviceCreateEffect(ffDevice, kFFEffectType_CustomForce_ID, effect, &effectRef);
}
- (void)testMotorsCleanUp
{
if (effectRef == NULL) return;
FFDeviceReleaseEffect(ffDevice, effectRef);
free(customforce->rglForceData);
free(effect->rgdwAxes);
free(effect->rglDirection);
free(customforce);
free(effect);
}
- (void)testMotorsLarge:(unsigned char)large small:(unsigned char)small
{
if (effectRef == NULL) return;
customforce->rglForceData[0] = (large * 10000) / 255;
customforce->rglForceData[1] = (small * 10000) / 255;
FFEffectSetParameters(effectRef, effect, FFEP_TYPESPECIFICPARAMS);
FFEffectStart(effectRef, 1, 0);
}
// Update axis GUI component
- (void)axisChanged:(int)index newValue:(int)value
{
NSInteger tabIndex = [_tabView indexOfTabViewItem:[_tabView selectedTabViewItem]];
switch(index) {
case 0:
[_wholeController setLeftStickXPos:value];
[_leftStickAnalog setPositionX:value];
break;
case 1:
[_wholeController setLeftStickYPos:value];
[_leftStickAnalog setPositionY:value];
break;
case 2:
[_wholeController setRightStickXPos:value];
[_rightStickAnalog setPositionX:value];
break;
case 3:
[_wholeController setRightStickYPos:value];
[_rightStickAnalog setPositionY:value];
break;
case 4:
if (tabIndex == 0) { // Controller Test
[_leftTrigger setVal:value];
largeMotor=value;
[self testMotorsLarge:largeMotor small:smallMotor];
}
break;
case 5:
if (tabIndex == 0) {
[_rightTrigger setVal:value];
smallMotor=value;
[self testMotorsLarge:largeMotor small:smallMotor];
}
break;
default:
break;
}
}
// Update button GUI component
- (void)buttonChanged:(int)index newValue:(int)value
{
BOOL b = (value != 0);
NSInteger tabIndex = [_tabView indexOfTabViewItem:[_tabView selectedTabViewItem]];
if (tabIndex == 0) { // Controller Test
switch (index) {
case 0:
[_wholeController setAPressed:b];
break;
case 1:
[_wholeController setBPressed:b];
break;
case 2:
[_wholeController setXPressed:b];
break;
case 3:
[_wholeController setYPressed:b];
break;
case 4:
[_wholeController setLbPressed:b];
break;
case 5:
[_wholeController setRbPressed:b];
break;
case 6:
[_wholeController setLeftStickPressed:b];
break;
case 7:
[_wholeController setRightStickPressed:b];
break;
case 8:
[_wholeController setStartPressed:b];
break;
case 9:
[_wholeController setBackPressed:b];
break;
case 10:
[_wholeController setHomePressed:b];
break;
case 11:
[_wholeController setUpPressed:b];
break;
case 12:
[_wholeController setDownPressed:b];
break;
case 13:
[_wholeController setLeftPressed:b];
break;
case 14:
[_wholeController setRightPressed:b];
break;
default:
break;
}
}
else if (tabIndex == 1) { // Tweaks
if ([_wholeControllerMapper isMapping]) {
if (b == YES) {
[_wholeControllerMapper buttonPressedAtIndex:index];
}
}
}
}
// Handle message from I/O Kit indicating something happened on the device
- (void)eventQueueFired:(void*)sender withResult:(IOReturn)result
{
AbsoluteTime zeroTime = {0,0};
IOHIDEventStruct event;
BOOL found = NO;
int i;
if (sender != hidQueue) return;
while (result == kIOReturnSuccess) {
result = (*hidQueue)->getNextEvent(hidQueue,&event,zeroTime,0);
if (result != kIOReturnSuccess) continue;
// Check axis
for (i = 0, found = NO; (i < 6) && !found; i++) {
if (event.elementCookie == axis[i]) {
[self axisChanged:i newValue:event.value];
found = YES;
}
}
if (found) continue;
// Check buttons
for (i = 0, found = NO; (i < 15) && !found; i++) {
if(event.elementCookie==buttons[i]) {
[self buttonChanged:i newValue:event.value];
found = YES;
}
}
if(found) continue;
// Cookie wasn't for us?
}
}
// Enable input components
- (void)inputEnable:(BOOL)enable
{
[_leftStickDeadzone setEnabled:enable];
[_leftStickDeadzoneAlt setEnabled:enable];
[_leftStickInvertX setEnabled:enable];
[_leftStickInvertXAlt setEnabled:enable];
[_leftStickInvertY setEnabled:enable];
[_leftStickInvertYAlt setEnabled:enable];
[_leftLinked setEnabled:enable];
[_leftLinkedAlt setEnabled:enable];
[_rightStickDeadzone setEnabled:enable];
[_rightStickDeadzoneAlt setEnabled:enable];
[_rightStickInvertX setEnabled:enable];
[_rightStickInvertXAlt setEnabled:enable];
[_rightStickInvertY setEnabled:enable];
[_rightStickInvertYAlt setEnabled:enable];
[_rightLinked setEnabled:enable];
[_rightLinkedAlt setEnabled:enable];
[_normalizeDeadzoneLeft setEnabled:enable];
[_normalizeDeadzoneRight setEnabled:enable];
[_rumbleOptions setEnabled:enable];
[_swapSticks setEnabled:enable];
[_pretend360Button setEnabled:enable];
}
// Reset GUI components
- (void)resetDisplay
{
[_leftTrigger setVal:0];
[_rightTrigger setVal:0];
[_wholeController reset];
[_leftDeadZone setVal:0];
[_leftStickAnalog setDeadzone:0];
[_leftDeadZone setLinked:NO];
[_leftStickAnalog setLinked:NO];
[_rightDeadZone setVal:0];
[_rightStickAnalog setDeadzone:0];
[_rightDeadZone setLinked:NO];
[_rightStickAnalog setLinked:NO];
// Reset inputs
[_leftStickDeadzone setIntValue:0];
[_leftStickDeadzoneAlt setIntValue:0];
[_leftStickInvertX setState:NSOffState];
[_leftStickInvertXAlt setState:NSOffState];
[_leftStickInvertY setState:NSOffState];
[_leftStickInvertYAlt setState:NSOffState];
[_rightStickDeadzone setIntValue:0];
[_rightStickDeadzoneAlt setIntValue:0];
[_rightStickInvertX setState:NSOffState];
[_rightStickInvertXAlt setState:NSOffState];
[_rightStickInvertY setState:NSOffState];
[_rightStickInvertYAlt setState:NSOffState];
[_swapSticks setState:NSOffState];
// Disable inputs
[self inputEnable:NO];
[_powerOff setHidden:YES];
// Hide battery status
[_batteryStatus setHidden:YES];
}
// Stop using the HID device
- (void)stopDevice
{
if(registryEntry==0) return;
[self testMotorsLarge:0 small:0];
[self testMotorsCleanUp];
if (hidQueue) {
CFRunLoopSourceRef eventSource;
(*hidQueue)->stop(hidQueue);
eventSource=(*hidQueue)->getAsyncEventSource(hidQueue);
if((eventSource!=NULL)&&CFRunLoopContainsSource(CFRunLoopGetCurrent(),eventSource,kCFRunLoopCommonModes))
CFRunLoopRemoveSource(CFRunLoopGetCurrent(),eventSource,kCFRunLoopCommonModes);
(*hidQueue)->Release(hidQueue);
hidQueue = NULL;
}
if (device) {
(*device)->close(device);
device = NULL;
}
registryEntry = 0;
}
// Start using a HID device
- (void)startDevice
{
int i = (int)[_deviceList indexOfSelectedItem];
int j;
CFArrayRef CFelements;
IOHIDElementCookie cookie;
long usage,usagePage;
CFRunLoopSourceRef eventSource;
IOReturn ret;
[self resetDisplay];
if(([_deviceArray count]==0)||(i==-1)) {
NSLog(@"No devices found? :( device count==%i, i==%i",(int)[_deviceArray count], i);
return;
}
{
DeviceItem *item = _deviceArray[i];
device = [item hidDevice];
ffDevice = [item ffDevice];
registryEntry = [item rawDevice];
}
if((*device)->copyMatchingElements(device,NULL,&CFelements)!=kIOReturnSuccess) {
NSLog(@"Can't get elements list");
// Make note of failure?
return;
}
NSArray *elements = CFBridgingRelease(CFelements);
for (NSDictionary *element in elements) {
id object;
// Get cookie
object = element[@kIOHIDElementCookieKey];
if (object == nil || ![object isKindOfClass:[NSNumber class]]) {
continue;
}
cookie = (IOHIDElementCookie)[(NSNumber*)object unsignedIntValue];
// Get usage
object = element[@kIOHIDElementUsageKey];
if (object == nil || ![object isKindOfClass:[NSNumber class]]) {
continue;
}
usage = [(NSNumber*)object longValue];
// Get usage page
object = element[@kIOHIDElementUsagePageKey];
if (object == nil || ![object isKindOfClass:[NSNumber class]]) {
continue;
}
usagePage = [(NSNumber*)object longValue];
// Match up items
switch(usagePage) {
case 0x01: // Generic Desktop
j=0;
switch(usage) {
case 0x35: // Right trigger
j++;
case 0x32: // Left trigger
j++;
case 0x34: // Right stick Y
j++;
case 0x33: // Right stick X
j++;
case 0x31: // Left stick Y
j++;
case 0x30: // Left stick X
axis[j]=cookie;
break;
default:
break;
}
break;
case 0x09: // Button
if((usage>=1)&&(usage<=15)) {
// Button 1-11
buttons[usage-1]=cookie;
}
break;
default:
break;
}
}
// Start queue
if((*device)->open(device,0)!=kIOReturnSuccess) {
NSLog(@"Can't open device");
// Make note of failure?
return;
}
hidQueue=(*device)->allocQueue(device);
if(hidQueue==NULL) {
NSLog(@"Unable to allocate queue");
// Error?
return;
}
ret=(*hidQueue)->create(hidQueue,0,32);
if(ret!=kIOReturnSuccess) {
NSLog(@"Unable to create the queue");
// Error?
return;
}
// Create event source
ret=(*hidQueue)->createAsyncEventSource(hidQueue, &eventSource);
if(ret!=kIOReturnSuccess) {
NSLog(@"Unable to create async event source");
// Error?
return;
}
// Set callback
ret=(*hidQueue)->setEventCallout(hidQueue, CallbackFunction, (__bridge void *)(self), NULL);
if(ret!=kIOReturnSuccess) {
NSLog(@"Unable to set event callback");
// Error?
return;
}
// Add to runloop
CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopCommonModes);
// Add some elements
for (i = 0; i < 6; i++)
(*hidQueue)->addElement(hidQueue, axis[i], 0);
for (i = 0; i < 15; i++)
(*hidQueue)->addElement(hidQueue, buttons[i], 0);
// Start
ret = (*hidQueue)->start(hidQueue);
if (ret != kIOReturnSuccess) {
NSLog(@"Unable to start queue - 0x%.8x", ret);
// Error?
return;
}
// Read existing properties
{
// CFDictionaryRef dict=(CFDictionaryRef)IORegistryEntryCreateCFProperty(registryEntry,CFSTR("DeviceData"),NULL,0);
CFDictionaryRef dict = (CFDictionaryRef)CFBridgingRetain(GetController(GetSerialNumber(registryEntry)));
if(dict) {
CFBooleanRef boolValue;
CFNumberRef intValue;
if(CFDictionaryGetValueIfPresent(dict,CFSTR("InvertLeftX"),(void*)&boolValue)) {
[_leftStickInvertX setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
[_leftStickInvertXAlt setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
} else NSLog(@"No value for InvertLeftX\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("InvertLeftY"),(void*)&boolValue)) {
[_leftStickInvertY setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
[_leftStickInvertYAlt setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
} else NSLog(@"No value for InvertLeftY\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("RelativeLeft"),(void*)&boolValue)) {
BOOL enable=CFBooleanGetValue(boolValue);
[_leftLinked setState:enable?NSOnState:NSOffState];
[_leftLinkedAlt setState:enable?NSOnState:NSOffState];
[_leftDeadZone setLinked:enable];
[_leftStickAnalog setLinked:enable];
} else NSLog(@"No value for RelativeLeft\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("DeadzoneLeft"),(void*)&intValue)) {
UInt16 i;
CFNumberGetValue(intValue,kCFNumberShortType,&i);
[_leftStickDeadzone setIntValue:i];
[_leftStickDeadzoneAlt setIntValue:i];
[_leftDeadZone setVal:i];
[_wholeController setLeftStickDeadzone:i];
[_leftStickAnalog setDeadzone:i];
} else NSLog(@"No value for DeadzoneLeft\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("InvertRightX"),(void*)&boolValue)) {
[_rightStickInvertX setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
[_rightStickInvertXAlt setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
} else NSLog(@"No value for InvertRightX\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("InvertRightY"),(void*)&boolValue)) {
[_rightStickInvertY setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
[_rightStickInvertYAlt setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
} else NSLog(@"No value for InvertRightY\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("RelativeRight"),(void*)&boolValue)) {
BOOL enable=CFBooleanGetValue(boolValue);
[_rightLinked setState:enable?NSOnState:NSOffState];
[_rightLinkedAlt setState:enable?NSOnState:NSOffState];
[_rightDeadZone setLinked:enable];
[_wholeController setRightStickDeadzone:i];
[_rightStickAnalog setLinked:enable];
} else NSLog(@"No value for RelativeRight\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("DeadzoneRight"),(void*)&intValue)) {
UInt16 i;
CFNumberGetValue(intValue,kCFNumberShortType,&i);
[_rightStickDeadzone setIntValue:i];
[_rightStickDeadzoneAlt setIntValue:i];
[_rightDeadZone setVal:i];
[_rightStickAnalog setDeadzone:i];
} else NSLog(@"No value for DeadzoneRight\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("ControllerType"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
controllerType = (ControllerType)[num integerValue];
} else NSLog(@"No value for ControllerType\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("RumbleType"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[_rumbleOptions setState:[num integerValue]];
} else NSLog(@"No value for RumbleType\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingUp"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][0] = [num intValue];
} else NSLog(@"No value for BindingUp\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingDown"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][1] = [num intValue];
} else NSLog(@"No value for BindingDown\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingLeft"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][2] = [num intValue];
} else NSLog(@"No value for BindingLeft\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingRight"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][3] = [num intValue];
} else NSLog(@"No value for BindingRight\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingStart"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][4] = [num intValue];
} else NSLog(@"No value for BindingStart\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingBack"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][5] = [num intValue];
} else NSLog(@"No value for BindingBack\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingLSC"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][6] = [num intValue];
} else NSLog(@"No value for BindingLSC\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingRSC"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][7] = [num intValue];
} else NSLog(@"No value for BindingRSC\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingLB"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][8] = [num intValue];
} else NSLog(@"No value for BindingLB\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingRB"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][9] = [num intValue];
} else NSLog(@"No value for BindingRB\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingGuide"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][10] = [num intValue];
} else NSLog(@"No value for BindingGuide\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingA"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][11] = [num intValue];
} else NSLog(@"No value for BindingA\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingB"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][12] = [num intValue];
} else NSLog(@"No value for BindingB\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingX"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][13] = [num intValue];
} else NSLog(@"No value for BindingX\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("BindingY"),(void*)&intValue)) {
NSNumber *num = (__bridge NSNumber *)intValue;
[MyWhole360ControllerMapper mapping][14] = [num intValue];
} else NSLog(@"No value for BindingY\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("SwapSticks"),(void*)&boolValue)) {
[_swapSticks setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
} else NSLog(@"No value for SwapSticks\n");
if(CFDictionaryGetValueIfPresent(dict,CFSTR("Pretend360"),(void*)&boolValue)) {
[_pretend360Button setState:CFBooleanGetValue(boolValue)?NSOnState:NSOffState];
} else NSLog(@"No value for Pretend360");
} else NSLog(@"No settings found\n");
}
// Enable GUI components
[self inputEnable:YES];
// Set device capabilities
// Set FF motor control
[self testMotorsInit];
[self testMotorsLarge:0 small:0];
largeMotor = 0;
smallMotor = 0;
// Battery level?
{
int batteryLevel = -1;
CFTypeRef prop;
if (IOObjectConformsTo(registryEntry, "WirelessHIDDevice")) {
prop = IORegistryEntryCreateCFProperty(registryEntry, CFSTR("BatteryLevel"), NULL, 0);
if (prop != nil) {
unsigned char level;
if (CFNumberGetValue(prop, kCFNumberCharType, &level))
batteryLevel = level / 64;
CFRelease(prop);
}
[_powerOff setHidden:NO];
}
if ( batteryLevel >= 0) {
[_batteryStatus setBars:batteryLevel];
[_batteryStatus setHidden:NO];
} else {
[_batteryStatus setHidden:YES];
}
}
[_mappingTable reloadData];
// Allows the kext to be disabled when you connect a controller once
// FIXME: Allow disabling the driver at any time.
[self.enableDriverCheckBox setEnabled:YES];
}
// Clear out the device lists
- (void)deleteDeviceList
{
[_deviceList removeAllItems];
[_deviceListBinding removeAllItems];
[_deviceListAdvanced removeAllItems];
[_deviceArray removeAllObjects];
}
// Update the device list from the I/O Kit
- (void)updateDeviceList
{
CFMutableDictionaryRef hidDictionary;
IOReturn ioReturn;
io_iterator_t iterator;
io_object_t hidDevice;
int count = 0;
// Scrub old items
[self stopDevice];
[self deleteDeviceList];
// Add new items
hidDictionary=IOServiceMatching(kIOHIDDeviceKey);
ioReturn=IOServiceGetMatchingServices(_masterPort,hidDictionary,&iterator);
if((ioReturn != kIOReturnSuccess) || (iterator == 0)) {
[_deviceList addItemWithTitle:NO_ITEMS];
[_deviceListBinding addItemWithTitle:NO_ITEMS];
[_deviceListAdvanced addItemWithTitle:NO_ITEMS];
return;
}
while ((hidDevice = IOIteratorNext(iterator))) {
io_object_t parent = 0;
IORegistryEntryGetParentEntry(hidDevice, kIOServicePlane, &parent);
BOOL deviceWired = IOObjectConformsTo(parent, "Xbox360Peripheral") && IOObjectConformsTo(hidDevice, "Xbox360ControllerClass");
BOOL deviceWireless = IOObjectConformsTo(hidDevice, "WirelessHIDDevice");
if ((!deviceWired) && (!deviceWireless))
{
IOObjectRelease(hidDevice);
continue;
}
DeviceItem *item = [DeviceItem allocateDeviceItemForDevice:hidDevice];
if (item == nil) continue;
// Add to item
NSString *name = item.name;
if (name == nil)
name = @"Generic Controller";
[_deviceList addItemWithTitle:[NSString stringWithFormat:@"%i: %@ (%@)", ++count, name, deviceWireless ? @"Wireless" : @"Wired"]];
[_deviceListBinding addItemWithTitle:[NSString stringWithFormat:@"%i: %@ (%@)", count, name, deviceWireless ? @"Wireless" : @"Wired"]];
[_deviceListAdvanced addItemWithTitle:[NSString stringWithFormat:@"%i: %@ (%@)", count, name, deviceWireless ? @"Wireless" : @"Wired"]];
[_deviceArray addObject:item];
}
IOObjectRelease(iterator);
if (count==0) {
[_deviceList addItemWithTitle:NO_ITEMS];
[_deviceListBinding addItemWithTitle:NO_ITEMS];
[_deviceListAdvanced addItemWithTitle:NO_ITEMS];
}
[self startDevice];
}
// Start up
- (void)didSelect
{
io_object_t object;
// Get master port, for accessing I/O Kit
IOMasterPort(MACH_PORT_NULL,&_masterPort);
// Set up notification of USB device addition/removal
notifyPort=IONotificationPortCreate(_masterPort);
notifySource=IONotificationPortGetRunLoopSource(notifyPort);
CFRunLoopAddSource(CFRunLoopGetCurrent(), notifySource, kCFRunLoopCommonModes);
// Prepare other fields
_deviceArray = [[NSMutableArray alloc] initWithCapacity:1];
device=NULL;
hidQueue=NULL;
// Activate callbacks
// Wired
IOServiceAddMatchingNotification(notifyPort, kIOFirstMatchNotification, IOServiceMatching(kIOUSBDeviceClassName), callbackHandleDevice, (__bridge void *)(self), &onIteratorWired);
callbackHandleDevice((__bridge void *)(self), onIteratorWired);
IOServiceAddMatchingNotification(notifyPort, kIOTerminatedNotification, IOServiceMatching(kIOUSBDeviceClassName), callbackHandleDevice, (__bridge void *)(self), &offIteratorWired);
while((object = IOIteratorNext(offIteratorWired)) != 0)
IOObjectRelease(object);
// Wireless
IOServiceAddMatchingNotification(notifyPort, kIOFirstMatchNotification, IOServiceMatching("WirelessHIDDevice"), callbackHandleDevice, (__bridge void *)(self), &onIteratorWireless);
callbackHandleDevice((__bridge void *)(self), onIteratorWireless);
IOServiceAddMatchingNotification(notifyPort, kIOTerminatedNotification, IOServiceMatching("WirelessHIDDevice"), callbackHandleDevice, (__bridge void *)(self), &offIteratorWireless);
while((object = IOIteratorNext(offIteratorWireless)) != 0)
IOObjectRelease(object);
// TEMP: Enable the "enable driver" checkbox if the kext is loaded in the memory
int result = system("kextstat | grep com.mice.driver.Xbox360Controller");
NSLog(@"Result of kextstat = %d", result);
if (result == 0) {
[self.enableDriverCheckBox setEnabled:YES];
}
}
// Shut down
- (void)didUnselect
{
unsigned char c;
// Remove notification source
IOObjectRelease(onIteratorWired);
IOObjectRelease(onIteratorWireless);
IOObjectRelease(offIteratorWired);
IOObjectRelease(offIteratorWireless);
CFRunLoopSourceInvalidate(notifySource);
IONotificationPortDestroy(notifyPort);
// Release device and info
[self stopDevice];
for (DeviceItem *item in _deviceArray)
{
FFEFFESCAPE escape = {0};
NSInteger i = [_deviceArray indexOfObject:item];
if ([item ffDevice] == 0)
continue;
c = 0x06 + (i % 0x04);
escape.dwSize = sizeof(escape);
escape.dwCommand = 0x02;
escape.cbInBuffer = sizeof(c);
escape.lpvInBuffer = &c;
escape.cbOutBuffer = 0;
escape.lpvOutBuffer = NULL;
FFDeviceEscape([item ffDevice], &escape);
}
[self deleteDeviceList];
// Close master port
mach_port_deallocate(mach_task_self(), _masterPort);
// Done
}
// Handle selection from drop down menu
- (IBAction)selectDevice:(id)sender
{
NSInteger tabIndex = [_tabView indexOfTabViewItem:[_tabView selectedTabViewItem]];
if (tabIndex == 0) { // Controller Test
[_deviceListBinding selectItemAtIndex:[_deviceList indexOfSelectedItem]];
[_deviceListAdvanced selectItemAtIndex:[_deviceList indexOfSelectedItem]];
}
else if (tabIndex == 1) { // Binding Tab
[_deviceList selectItemAtIndex:[_deviceListBinding indexOfSelectedItem]];
[_deviceListAdvanced selectItemAtIndex:[_deviceListBinding indexOfSelectedItem]];
}
else if (tabIndex == 2) { // Deadzones Tab
[_deviceList selectItemAtIndex:[_deviceListAdvanced indexOfSelectedItem]];
[_deviceListBinding selectItemAtIndex:[_deviceListAdvanced indexOfSelectedItem]];
}
[self startDevice];
}
// Handle changing a setting
- (IBAction)changeSetting:(id)sender
{
// Send normalize to joysticks
[_wholeController setLeftNormalized:(BOOL)[_normalizeDeadzoneLeft state]];
[_leftStickAnalog setNormalized:(BOOL)[_normalizeDeadzoneLeft state]];
[_wholeController setRightNormalized:(BOOL)[_normalizeDeadzoneRight state]];
[_rightStickAnalog setNormalized:(BOOL)[_normalizeDeadzoneRight state]];
// Sync settings between tabs
NSInteger tabIndex = [_tabView indexOfTabViewItem:[_tabView selectedTabViewItem]];
if (tabIndex == 0) { // Controller Test
[_leftLinkedAlt setState:[_leftLinked state]];
[_leftStickDeadzoneAlt setDoubleValue:[_leftStickDeadzone doubleValue]];
[_leftStickInvertXAlt setState:[_leftStickInvertX state]];
[_leftStickInvertYAlt setState:[_leftStickInvertY state]];
[_rightLinkedAlt setState:[_rightLinked state]];
[_rightStickDeadzoneAlt setDoubleValue:[_rightStickDeadzone doubleValue]];
[_rightStickInvertXAlt setState:[_rightStickInvertX state]];
[_rightStickInvertYAlt setState:[_rightStickInvertY state]];
}
else if (tabIndex == 2) { // Advanced Tab
[_leftLinked setState:[_leftLinkedAlt state]];
[_leftStickDeadzone setDoubleValue:[_leftStickDeadzoneAlt doubleValue]];
[_leftStickInvertX setState:[_leftStickInvertXAlt state]];
[_leftStickInvertY setState:[_leftStickInvertYAlt state]];
[_rightLinked setState:[_rightLinkedAlt state]];
[_rightStickDeadzone setDoubleValue:[_rightStickDeadzoneAlt doubleValue]];
[_rightStickInvertX setState:[_rightStickInvertXAlt state]];
[_rightStickInvertY setState:[_rightStickInvertYAlt state]];
}
// Create dictionary
NSDictionary *dict = @{@"InvertLeftX": @((BOOL)([_leftStickInvertX state]==NSOnState)),
@"InvertLeftY": @((BOOL)([_leftStickInvertY state]==NSOnState)),
@"InvertRightX": @((BOOL)([_rightStickInvertX state]==NSOnState)),
@"InvertRightY": @((BOOL)([_rightStickInvertY state]==NSOnState)),
@"DeadzoneLeft": @((UInt16)[_leftStickDeadzone doubleValue]),
@"DeadzoneRight": @((UInt16)[_rightStickDeadzone doubleValue]),
@"RelativeLeft": @((BOOL)([_leftLinked state]==NSOnState)),
@"RelativeRight": @((BOOL)([_rightLinked state]==NSOnState)),
@"DeadOffLeft": @((BOOL)([_normalizeDeadzoneLeft state]==NSOnState)),
@"DeadOffRight": @((BOOL)([_normalizeDeadzoneRight state]==NSOnState)),
@"ControllerType": @((UInt8)(controllerType)),
@"RumbleType": @((UInt8)([_rumbleOptions indexOfSelectedItem])),
@"BindingUp": @((UInt8)([MyWhole360ControllerMapper mapping][0])),
@"BindingDown": @((UInt8)([MyWhole360ControllerMapper mapping][1])),
@"BindingLeft": @((UInt8)([MyWhole360ControllerMapper mapping][2])),
@"BindingRight": @((UInt8)([MyWhole360ControllerMapper mapping][3])),
@"BindingStart": @((UInt8)([MyWhole360ControllerMapper mapping][4])),
@"BindingBack": @((UInt8)([MyWhole360ControllerMapper mapping][5])),
@"BindingLSC": @((UInt8)([MyWhole360ControllerMapper mapping][6])),
@"BindingRSC": @((UInt8)([MyWhole360ControllerMapper mapping][7])),
@"BindingLB": @((UInt8)([MyWhole360ControllerMapper mapping][8])),
@"BindingRB": @((UInt8)([MyWhole360ControllerMapper mapping][9])),
@"BindingGuide": @((UInt8)([MyWhole360ControllerMapper mapping][10])),
@"BindingA": @((UInt8)([MyWhole360ControllerMapper mapping][11])),
@"BindingB": @((UInt8)([MyWhole360ControllerMapper mapping][12])),
@"BindingX": @((UInt8)([MyWhole360ControllerMapper mapping][13])),
@"BindingY": @((UInt8)([MyWhole360ControllerMapper mapping][14])),
@"SwapSticks": @((BOOL)([_swapSticks state]==NSOnState)),
@"Pretend360": @((BOOL)([_pretend360Button state]==NSOnState))};
// Set property
IORegistryEntrySetCFProperties(registryEntry, (__bridge CFTypeRef)(dict));
SetController(GetSerialNumber(registryEntry), dict);
// Update UI
[_leftDeadZone setLinked:[_leftLinked state] == NSOnState];
[_leftStickAnalog setLinked:[_leftLinked state] == NSOnState];
[_leftDeadZone setVal:[_leftStickDeadzone doubleValue]];
[_wholeController setLeftStickDeadzone:[_leftStickDeadzone doubleValue]];
[_leftStickAnalog setDeadzone:[_leftStickDeadzone doubleValue]];
[_rightDeadZone setLinked:[_rightLinked state] == NSOnState];
[_rightStickAnalog setLinked:[_rightLinked state] == NSOnState];
[_rightDeadZone setVal:[_rightStickDeadzone doubleValue]];
[_wholeController setRightStickDeadzone:[_rightStickDeadzone doubleValue]];
[_rightStickAnalog setDeadzone:[_rightStickDeadzone doubleValue]];
}
// Run an AppleScript from String and returns YES on successful execution
- (BOOL)runInlineAppleScript:(NSString *)scriptString
{
NSDictionary* errorDict;
NSAppleEventDescriptor* returnDescriptor = NULL;
NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:scriptString];
returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
scriptObject = nil;
if (returnDescriptor != NULL)
{
// successful execution
if (kAENullEvent != [returnDescriptor descriptorType])
{
return YES;
/* Uncomment this to handle the returned values */
// // script returned an AppleScript result
// if (cAEList == [returnDescriptor descriptorType])
// {
// // result is a list of other descriptors
// }
// else
// {
// // coerce the result to the appropriate ObjC type
// }
}
}
else
{
// no script result, handle error
id val = [errorDict objectForKey:@"NSAppleScriptErrorRange"];
if (!val) {
NSLog(@"APPLESCRIPT ERROR:\n%@", errorDict);
} else {
NSRange r = [val rangeValue];
NSMutableString *errorPoint = [NSMutableString stringWithString:scriptString];
[errorPoint insertString:@"<---***" atIndex:r.location+r.length]; // end
[errorPoint insertString:@"***ERROR_HERE--->" atIndex:r.location]; // start
NSLog(@"APPLESCRIPT ERROR:\n%@"
@"\nERROR LOCATION:\n%@",
errorDict, errorPoint);
}
}
return NO;