Guest User

Maik Schulz

a guest
Mar 29th, 2010
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. JoystickHandler.m
  4.  
  5. Oolite
  6. Copyright (C) 2004-2008 Giles C Williams and contributors
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. MA 02110-1301, USA.
  22.  
  23.  
  24. This file may also be distributed under the MIT/X11 license:
  25.  
  26. Copyright (C) 2006 Jens Ayton, 2010 Maik Schulz
  27.  
  28. Permission is hereby granted, free of charge, to any person obtaining a copy
  29. of this software and associated documentation files (the "Software"), to deal
  30. in the Software without restriction, including without limitation the rights
  31. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  32. copies of the Software, and to permit persons to whom the Software is
  33. furnished to do so, subject to the following conditions:
  34.  
  35. The above copyright notice and this permission notice shall be included in all
  36. copies or substantial portions of the Software.
  37.  
  38. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  42. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  43. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  44. SOFTWARE.
  45.  
  46. */
  47.  
  48. #import "JoystickHandler.h"
  49.  
  50. JoystickHandler *sSharedStickHandler = nil;
  51.  
  52. static CFMutableDictionaryRef hu_CreateDeviceMatchingDictionary( UInt32 inUsagePage, UInt32 inUsage )
  53. {
  54.     // create a dictionary to add usage page/usages to
  55.     CFMutableDictionaryRef result = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
  56.     if ( result ) {
  57.         if ( inUsagePage ) {
  58.             // Add key for device type to refine the matching dictionary.
  59.             CFNumberRef pageCFNumberRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &inUsagePage );
  60.             if ( pageCFNumberRef ) {
  61.                 CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsagePageKey ), pageCFNumberRef );
  62.                 CFRelease( pageCFNumberRef );
  63.                
  64.                 // note: the usage is only valid if the usage page is also defined
  65.                 if ( inUsage ) {
  66.                     CFNumberRef usageCFNumberRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &inUsage );
  67.                     if ( usageCFNumberRef ) {
  68.                         CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsageKey ), usageCFNumberRef );
  69.                         CFRelease( usageCFNumberRef );
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     return result;
  76. }
  77.  
  78. @implementation JoystickHandler
  79.  
  80.  
  81. void Handle_IOHIDInputValueCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDValueRef inIOHIDValueRef) {
  82.     IOHIDElementRef elementRef = IOHIDValueGetElement( inIOHIDValueRef ) ;
  83.     IOHIDDeviceRef deviceRef = IOHIDElementGetDevice(elementRef);
  84.     CFArrayRef elements = IOHIDDeviceCopyMatchingElements(deviceRef, NULL, kIOHIDOptionsTypeNone);
  85.     CFIndex i;
  86.     for (i = 0; i < CFArrayGetCount(elements); i++) {
  87.         IOHIDElementRef e = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i);
  88.         int myCookie = (int)IOHIDElementGetCookie(e);
  89.         IOHIDValueRef valueRef;
  90.         IOHIDDeviceGetValue(deviceRef, e, &valueRef);
  91.         if (valueRef != NULL) {
  92.             long v = (long)IOHIDValueGetIntegerValue(valueRef);
  93.             switch (myCookie) {
  94.                 case 10:
  95.                     if (v == 1)
  96.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_VIEWFORWARD];
  97.                     else
  98.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_VIEWFORWARD];
  99.                     break;
  100.                 case 11:
  101.                     if (v == 1)
  102.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_VIEWAFT];
  103.                     else
  104.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_VIEWAFT];
  105.                     break;
  106.                 case 12:
  107.                     if (v == 1)
  108.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_VIEWPORT];
  109.                     else
  110.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_VIEWPORT];
  111.                     break;
  112.                 case 13:
  113.                     if (v == 1)
  114.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_VIEWSTARBOARD];
  115.                     else
  116.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_VIEWSTARBOARD];
  117.                     break;
  118.                 case 14:
  119.                     if (v == 1)
  120.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_CYCLEMISSILE];
  121.                     else
  122.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_CYCLEMISSILE];
  123.                     break;
  124.                 case 15:
  125.                     if (v == 1)
  126.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_ESCAPE];
  127.                     else
  128.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_ESCAPE];
  129.                     break;
  130.                 case 16: //left joystick button
  131.                     break;
  132.                 case 17: //right joystick button
  133.                     break;
  134.                 case 18:
  135.                     if (v == 1)
  136.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_DECTHRUST];
  137.                     else
  138.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_DECTHRUST];
  139.                     break;
  140.                 case 19:
  141.                     if (v == 1)
  142.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_INCTHRUST];
  143.                     else
  144.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_INCTHRUST];
  145.                     break;
  146.                 case 20:
  147.                     if (v == 1)
  148.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_ENERGYBOMB];
  149.                     else
  150.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_ENERGYBOMB];
  151.                     break;
  152.                 case 21:
  153.                     if (v == 1)
  154.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_ARMMISSILE];
  155.                     else
  156.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_ARMMISSILE];
  157.                     break;
  158.                 case 22:
  159.                     if (v == 1)
  160.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_UNARM];
  161.                     else
  162.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_UNARM];
  163.                     break;
  164.                 case 23:
  165.                     if (v == 1)
  166.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_LAUNCHMISSILE];
  167.                     else
  168.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_LAUNCHMISSILE];
  169.                     break;
  170.                 case 24:
  171.                     if (v == 1)
  172.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_ECM];
  173.                     else
  174.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_ECM];
  175.                     break;
  176.                 case 25:
  177.                     if (v > 10)
  178.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_FUELINJECT];
  179.                     else
  180.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_FUELINJECT];
  181.                     break;
  182.                 case 26:
  183.                     if (v > 10)
  184.                         [sSharedStickHandler setButtonState:YES forButton:BUTTON_FIRE];
  185.                     else
  186.                         [sSharedStickHandler setButtonState:NO forButton:BUTTON_FIRE];
  187.                     break;
  188.                 case 27:
  189.                     [sSharedStickHandler setRollAxis: (CGFloat)v / (CGFloat)32768];
  190.                     break;
  191.                 case 28:
  192.                     break;
  193.                 case 29:
  194.                     [sSharedStickHandler setYawAxis: (CGFloat)v / (CGFloat)32768];
  195.                     break;
  196.                 case 30:
  197.                     [sSharedStickHandler setPitchAxis: (CGFloat)v / (CGFloat)32768];
  198.                     break;
  199.                 default:
  200.                     ;
  201.             }
  202.         }
  203.     }
  204. }
  205.  
  206. void Handle_DeviceMatchingCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef) {
  207.     IOHIDManagerRef tIOHIDManagerRef = (IOHIDManagerRef)inSender;
  208.    
  209.     IOReturn tIOReturn = IOHIDManagerOpen( tIOHIDManagerRef, kIOHIDOptionsTypeNone );
  210.     if (tIOReturn != kIOReturnSuccess) {
  211.         return;
  212.     }
  213.     IOHIDManagerRegisterInputValueCallback( tIOHIDManagerRef, Handle_IOHIDInputValueCallback, inContext );
  214.     [sSharedStickHandler setNumSticks:[sSharedStickHandler getNumSticks]+1];
  215.     OOLog(@"all", @"Gamepad attached, numSticks: %d", [sSharedStickHandler getNumSticks])
  216. }
  217.  
  218. void Handle_RemovalCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef) {
  219.     IOHIDManagerClose((IOHIDManagerRef)inSender, kIOHIDOptionsTypeNone);
  220.  
  221.     [sSharedStickHandler setNumSticks:[sSharedStickHandler getNumSticks]-1];
  222.     OOLog(@"all", @"Gamepad detached, numSticks: %d", [sSharedStickHandler getNumSticks] );
  223. }
  224.  
  225. + (id) sharedStickHandler
  226. {
  227.     if (sSharedStickHandler == nil) {
  228.         sSharedStickHandler = [[JoystickHandler alloc] init];
  229.  
  230.         IOHIDManagerRef tIOHIDManagerRef = IOHIDManagerCreate( kCFAllocatorDefault, kIOHIDOptionsTypeNone );
  231.         CFDictionaryRef matchingCFDictRef = hu_CreateDeviceMatchingDictionary( kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad);
  232.         IOHIDManagerSetDeviceMatching( tIOHIDManagerRef, matchingCFDictRef );
  233.         IOHIDManagerRegisterDeviceMatchingCallback( tIOHIDManagerRef, Handle_DeviceMatchingCallback, &sSharedStickHandler );
  234.         IOHIDManagerRegisterDeviceRemovalCallback( tIOHIDManagerRef, Handle_RemovalCallback, &sSharedStickHandler );
  235.         IOHIDManagerScheduleWithRunLoop( tIOHIDManagerRef, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode );    
  236.     }
  237.     return sSharedStickHandler;
  238. }
  239.  
  240. - (id) init
  241. {
  242.     if ( (self = [super init]) )
  243.     {
  244.         [self setNumSticks:0];
  245.         [self setViewAxisX: (CGFloat)STICK_AXISUNASSIGNED];
  246.         [self setViewAxisY: (CGFloat)STICK_AXISUNASSIGNED];
  247.     }
  248.     return self;
  249. }
  250.  
  251. - (int) getNumSticks
  252. {
  253.     return numSticks;
  254. }
  255.  
  256. - (void) setNumSticks:(int) sticks
  257. {
  258.     numSticks = sticks;
  259. }
  260.  
  261.  
  262. - (NSPoint) getRollPitchAxis
  263. {
  264.     return rollPitchAxis;
  265. }
  266.  
  267. - (void) setRollAxis:(CGFloat)roll
  268. {
  269.     rollPitchAxis.x = roll;
  270. }
  271.  
  272. - (void) setPitchAxis:(CGFloat)pitch
  273. {
  274.     rollPitchAxis.y = pitch;
  275. }
  276.  
  277.  
  278. - (NSPoint) getViewAxis
  279. {
  280.     return viewAxis;
  281. }
  282.  
  283. - (void) setViewAxisX: (CGFloat)x
  284. {
  285.     viewAxis.x = x;
  286. }
  287.  
  288. - (void) setViewAxisY: (CGFloat)y
  289. {
  290.     viewAxis.y = y;
  291. }
  292.  
  293. - (double) getYawAxis
  294. {
  295.     return yawAxis;
  296. }
  297.  
  298. - (void) setYawAxis:(double)yaw
  299. {
  300.     yawAxis = yaw;
  301. }
  302.  
  303.  
  304. - (double) getAxisState:(int)function
  305. {
  306.     switch (function) {
  307.         case AXIS_THRUST:
  308.             return STICK_AXISUNASSIGNED;
  309.             break;
  310.         case AXIS_YAW:
  311.             return yawAxis;
  312.             break;
  313.         default:
  314.             break;
  315.     }
  316.     return 0.0;
  317. }
  318.  
  319.  
  320. - (double) getSensitivity
  321. {
  322.     return 1.0;
  323. }
  324.  
  325.  
  326. - (const BOOL *) getAllButtonStates
  327. {
  328.     return butstate;
  329. }
  330.  
  331. - (void) setButtonState:(BOOL)state forButton:(int)button
  332. {
  333.     butstate[button] = state;
  334. }
  335. @end
Advertisement
Add Comment
Please, Sign In to add comment