Line data Source code
1 0 : /* 2 : 3 : OOSDLJoystickManager.m 4 : By Dylan Smith 5 : 6 : Oolite 7 : Copyright (C) 2004-2013 Giles C Williams and contributors 8 : 9 : This program is free software; you can redistribute it and/or 10 : modify it under the terms of the GNU General Public License 11 : as published by the Free Software Foundation; either version 2 12 : of the License, or (at your option) any later version. 13 : 14 : This program is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU General Public License for more details. 18 : 19 : You should have received a copy of the GNU General Public License 20 : along with this program; if not, write to the Free Software 21 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 22 : MA 02110-1301, USA. 23 : 24 : */ 25 : 26 : #import "OOSDLJoystickManager.h" 27 : #import "OOLogging.h" 28 : 29 0 : #define kOOLogUnconvertedNSLog @"unclassified.OOSDLJoystickManager" 30 : 31 : 32 : @implementation OOSDLJoystickManager 33 : 34 : - (id) init 35 : { 36 : int i; 37 : 38 : // Find and open the sticks. Make sure that we don't fail if more joysticks than MAX_STICKS are detected. 39 : stickCount = SDL_NumJoysticks(); 40 : OOLog(@"joystick.init", @"Number of joysticks detected: %ld", (long)stickCount); 41 : if (stickCount > MAX_STICKS) 42 : { 43 : stickCount = MAX_STICKS; 44 : OOLog(@"joystick.init", @"Number of joysticks detected exceeds maximum number of joysticks allowed. Setting number of active joysticks to %d.", MAX_STICKS); 45 : } 46 : if(stickCount) 47 : { 48 : for(i = 0; i < stickCount; i++) 49 : { 50 : // it's doubtful MAX_STICKS will ever get exceeded, but 51 : // we need to be defensive. 52 : if(i > MAX_STICKS) 53 : break; 54 : 55 : stick[i]=SDL_JoystickOpen(i); 56 : if(!stick[i]) 57 : { 58 : OOLog(@"joystick.init", @"Failed to open joystick #%d", i); 59 : } 60 : } 61 : SDL_JoystickEventState(SDL_ENABLE); 62 : } 63 : return [super init]; 64 : } 65 : 66 : 67 : - (BOOL) handleSDLEvent: (SDL_Event *)evt 68 : { 69 : BOOL rc=NO; 70 : switch(evt->type) 71 : { 72 : case SDL_JOYAXISMOTION: 73 : [self decodeAxisEvent: (JoyAxisEvent *)evt]; 74 : rc=YES; 75 : break; 76 : case SDL_JOYBUTTONDOWN: 77 : case SDL_JOYBUTTONUP: 78 : [self decodeButtonEvent: (JoyButtonEvent *)evt]; 79 : rc=YES; 80 : break; 81 : case SDL_JOYHATMOTION: 82 : [self decodeHatEvent: (JoyHatEvent *)evt]; 83 : rc=YES; 84 : break; 85 : default: 86 : OOLog(@"handleSDLEvent.unknownEvent", @"%@", @"JoystickHandler was sent an event it doesn't know"); 87 : } 88 : return rc; 89 : } 90 : 91 : 92 : // Overrides 93 : 94 0 : - (NSUInteger) joystickCount 95 : { 96 : return stickCount; 97 : } 98 : 99 : 100 : - (NSString *) nameOfJoystick:(NSUInteger)stickNumber 101 : { 102 : return [NSString stringWithUTF8String:SDL_JoystickName((int)stickNumber)]; 103 : } 104 : 105 : 106 : - (int16_t) getAxisWithStick:(NSUInteger) stickNum axis:(NSUInteger) axisNum 107 : { 108 : return SDL_JoystickGetAxis(stick[stickNum], axisNum); 109 : } 110 : 111 : 112 : 113 : @end