Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- JoystickHandler.m
- Oolite
- Copyright (C) 2004-2008 Giles C Williams and contributors
- This program 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.
- This program 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 this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- MA 02110-1301, USA.
- This file may also be distributed under the MIT/X11 license:
- Copyright (C) 2006 Jens Ayton, 2010 Maik Schulz
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- #import "JoystickHandler.h"
- JoystickHandler *sSharedStickHandler = nil;
- static CFMutableDictionaryRef hu_CreateDeviceMatchingDictionary( UInt32 inUsagePage, UInt32 inUsage )
- {
- // create a dictionary to add usage page/usages to
- CFMutableDictionaryRef result = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
- if ( result ) {
- if ( inUsagePage ) {
- // Add key for device type to refine the matching dictionary.
- CFNumberRef pageCFNumberRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &inUsagePage );
- if ( pageCFNumberRef ) {
- CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsagePageKey ), pageCFNumberRef );
- CFRelease( pageCFNumberRef );
- // note: the usage is only valid if the usage page is also defined
- if ( inUsage ) {
- CFNumberRef usageCFNumberRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &inUsage );
- if ( usageCFNumberRef ) {
- CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsageKey ), usageCFNumberRef );
- CFRelease( usageCFNumberRef );
- }
- }
- }
- }
- }
- return result;
- }
- @implementation JoystickHandler
- void Handle_IOHIDInputValueCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDValueRef inIOHIDValueRef) {
- IOHIDElementRef elementRef = IOHIDValueGetElement( inIOHIDValueRef ) ;
- IOHIDDeviceRef deviceRef = IOHIDElementGetDevice(elementRef);
- CFArrayRef elements = IOHIDDeviceCopyMatchingElements(deviceRef, NULL, kIOHIDOptionsTypeNone);
- CFIndex i;
- for (i = 0; i < CFArrayGetCount(elements); i++) {
- IOHIDElementRef e = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i);
- int myCookie = (int)IOHIDElementGetCookie(e);
- IOHIDValueRef valueRef;
- IOHIDDeviceGetValue(deviceRef, e, &valueRef);
- if (valueRef != NULL) {
- long v = (long)IOHIDValueGetIntegerValue(valueRef);
- switch (myCookie) {
- case 10:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_VIEWFORWARD];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_VIEWFORWARD];
- break;
- case 11:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_VIEWAFT];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_VIEWAFT];
- break;
- case 12:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_VIEWPORT];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_VIEWPORT];
- break;
- case 13:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_VIEWSTARBOARD];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_VIEWSTARBOARD];
- break;
- case 14:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_CYCLEMISSILE];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_CYCLEMISSILE];
- break;
- case 15:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_ESCAPE];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_ESCAPE];
- break;
- case 16: //left joystick button
- break;
- case 17: //right joystick button
- break;
- case 18:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_DECTHRUST];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_DECTHRUST];
- break;
- case 19:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_INCTHRUST];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_INCTHRUST];
- break;
- case 20:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_ENERGYBOMB];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_ENERGYBOMB];
- break;
- case 21:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_ARMMISSILE];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_ARMMISSILE];
- break;
- case 22:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_UNARM];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_UNARM];
- break;
- case 23:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_LAUNCHMISSILE];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_LAUNCHMISSILE];
- break;
- case 24:
- if (v == 1)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_ECM];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_ECM];
- break;
- case 25:
- if (v > 10)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_FUELINJECT];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_FUELINJECT];
- break;
- case 26:
- if (v > 10)
- [sSharedStickHandler setButtonState:YES forButton:BUTTON_FIRE];
- else
- [sSharedStickHandler setButtonState:NO forButton:BUTTON_FIRE];
- break;
- case 27:
- [sSharedStickHandler setRollAxis: (CGFloat)v / (CGFloat)32768];
- break;
- case 28:
- break;
- case 29:
- [sSharedStickHandler setYawAxis: (CGFloat)v / (CGFloat)32768];
- break;
- case 30:
- [sSharedStickHandler setPitchAxis: (CGFloat)v / (CGFloat)32768];
- break;
- default:
- ;
- }
- }
- }
- }
- void Handle_DeviceMatchingCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef) {
- IOHIDManagerRef tIOHIDManagerRef = (IOHIDManagerRef)inSender;
- IOReturn tIOReturn = IOHIDManagerOpen( tIOHIDManagerRef, kIOHIDOptionsTypeNone );
- if (tIOReturn != kIOReturnSuccess) {
- return;
- }
- IOHIDManagerRegisterInputValueCallback( tIOHIDManagerRef, Handle_IOHIDInputValueCallback, inContext );
- [sSharedStickHandler setNumSticks:[sSharedStickHandler getNumSticks]+1];
- OOLog(@"all", @"Gamepad attached, numSticks: %d", [sSharedStickHandler getNumSticks]);
- }
- void Handle_RemovalCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef) {
- IOHIDManagerClose((IOHIDManagerRef)inSender, kIOHIDOptionsTypeNone);
- [sSharedStickHandler setNumSticks:[sSharedStickHandler getNumSticks]-1];
- OOLog(@"all", @"Gamepad detached, numSticks: %d", [sSharedStickHandler getNumSticks] );
- }
- + (id) sharedStickHandler
- {
- if (sSharedStickHandler == nil) {
- sSharedStickHandler = [[JoystickHandler alloc] init];
- IOHIDManagerRef tIOHIDManagerRef = IOHIDManagerCreate( kCFAllocatorDefault, kIOHIDOptionsTypeNone );
- CFDictionaryRef matchingCFDictRef = hu_CreateDeviceMatchingDictionary( kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad);
- IOHIDManagerSetDeviceMatching( tIOHIDManagerRef, matchingCFDictRef );
- IOHIDManagerRegisterDeviceMatchingCallback( tIOHIDManagerRef, Handle_DeviceMatchingCallback, &sSharedStickHandler );
- IOHIDManagerRegisterDeviceRemovalCallback( tIOHIDManagerRef, Handle_RemovalCallback, &sSharedStickHandler );
- IOHIDManagerScheduleWithRunLoop( tIOHIDManagerRef, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode );
- }
- return sSharedStickHandler;
- }
- - (id) init
- {
- if ( (self = [super init]) )
- {
- [self setNumSticks:0];
- [self setViewAxisX: (CGFloat)STICK_AXISUNASSIGNED];
- [self setViewAxisY: (CGFloat)STICK_AXISUNASSIGNED];
- }
- return self;
- }
- - (int) getNumSticks
- {
- return numSticks;
- }
- - (void) setNumSticks:(int) sticks
- {
- numSticks = sticks;
- }
- - (NSPoint) getRollPitchAxis
- {
- return rollPitchAxis;
- }
- - (void) setRollAxis:(CGFloat)roll
- {
- rollPitchAxis.x = roll;
- }
- - (void) setPitchAxis:(CGFloat)pitch
- {
- rollPitchAxis.y = pitch;
- }
- - (NSPoint) getViewAxis
- {
- return viewAxis;
- }
- - (void) setViewAxisX: (CGFloat)x
- {
- viewAxis.x = x;
- }
- - (void) setViewAxisY: (CGFloat)y
- {
- viewAxis.y = y;
- }
- - (double) getYawAxis
- {
- return yawAxis;
- }
- - (void) setYawAxis:(double)yaw
- {
- yawAxis = yaw;
- }
- - (double) getAxisState:(int)function
- {
- switch (function) {
- case AXIS_THRUST:
- return STICK_AXISUNASSIGNED;
- break;
- case AXIS_YAW:
- return yawAxis;
- break;
- default:
- break;
- }
- return 0.0;
- }
- - (double) getSensitivity
- {
- return 1.0;
- }
- - (const BOOL *) getAllButtonStates
- {
- return butstate;
- }
- - (void) setButtonState:(BOOL)state forButton:(int)button
- {
- butstate[button] = state;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment