Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOSDLJoystickManager.m
Go to the documentation of this file.
1/*
2
3OOSDLJoystickManager.m
4By Dylan Smith
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
27#import "OOLogging.h"
28
29#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);
42 {
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- (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
#define MAX_STICKS
#define OOLog(class, format,...)
Definition OOLogging.h:88
SDL_Joystick * stick[MAX_STICKS]