1 /*
2 * Tux Racer
3 * Copyright (C) 1999-2001 Jasmin F. Patry
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #include "tuxracer.h"
21 #include "joystick.h"
22
23 #if defined( HAVE_SDL ) && defined( HAVE_SDL_JOYSTICKOPEN )
24
25 #include "SDL.h"
26 #include "SDL_joystick.h"
27
28 static SDL_Joystick *joystick = NULL;
29 static int num_buttons = 0;
30 static int num_axes = 0;
31
init_joystick()32 void init_joystick()
33 {
34 int num_joysticks = 0;
35 char *js_name;
36
37 /* Initialize SDL SDL joystick module */
38 if ( SDL_Init( SDL_INIT_JOYSTICK ) < 0 ) {
39 handle_error( 1, "Couldn't initialize SDL: %s", SDL_GetError() );
40 }
41
42 num_joysticks = SDL_NumJoysticks();
43
44 print_debug( DEBUG_JOYSTICK, "Found %d joysticks", num_joysticks );
45
46 if ( num_joysticks == 0 ) {
47 joystick = NULL;
48 return;
49 }
50
51 js_name = (char*) SDL_JoystickName( 0 );
52
53 print_debug( DEBUG_JOYSTICK, "Using joystick `%s'", js_name );
54
55 joystick = SDL_JoystickOpen( 0 );
56
57 if ( joystick == NULL ) {
58 print_debug( DEBUG_JOYSTICK, "Cannot open joystick" );
59 return;
60 }
61
62 /* Get number of buttons */
63 num_buttons = SDL_JoystickNumButtons( joystick );
64 print_debug( DEBUG_JOYSTICK, "Joystick has %d button%s",
65 num_buttons, num_buttons == 1 ? "" : "s" );
66
67 /* Get number of axes */
68 num_axes = SDL_JoystickNumAxes( joystick );
69 print_debug( DEBUG_JOYSTICK, "Joystick has %d ax%ss",
70 num_axes, num_axes == 1 ? "i" : "e" );
71
72 }
73
is_joystick_active()74 bool_t is_joystick_active()
75 {
76 return (bool_t) ( joystick != NULL );
77 }
78
update_joystick()79 void update_joystick()
80 {
81 SDL_JoystickUpdate();
82 }
83
get_joystick_x_axis()84 scalar_t get_joystick_x_axis()
85 {
86 static bool_t warning_given = False;
87 int axis;
88
89 check_assertion( joystick != NULL,
90 "joystick is null" );
91
92 axis = getparam_joystick_x_axis();
93
94 /* Make sure axis is valid */
95 if ( axis >= num_axes || axis < 0 ) {
96
97 if ( !warning_given ) {
98 print_warning( IMPORTANT_WARNING,
99 "joystick x axis mapped to axis %d "
100 "but joystick only has %d axes", axis, num_axes );
101 warning_given = True;
102 }
103
104 return 0.0;
105 }
106
107 return SDL_JoystickGetAxis( joystick, axis )/32768.0;
108 }
109
get_joystick_y_axis()110 scalar_t get_joystick_y_axis()
111 {
112 static bool_t warning_given = False;
113 int axis;
114
115 check_assertion( joystick != NULL,
116 "joystick is null" );
117
118 axis = getparam_joystick_y_axis();
119
120 /* Make sure axis is valid */
121 if ( axis >= num_axes || axis < 0 ) {
122
123 if ( !warning_given ) {
124 print_warning( IMPORTANT_WARNING,
125 "joystick y axis mapped to axis %d "
126 "but joystick only has %d axes", axis, num_axes );
127 warning_given = True;
128 }
129
130 return 0.0;
131 }
132
133 return SDL_JoystickGetAxis( joystick, getparam_joystick_y_axis() )/32768.0;
134 }
135
is_joystick_button_down(int button)136 bool_t is_joystick_button_down( int button )
137 {
138 static bool_t warning_given = False;
139
140 check_assertion( joystick != NULL,
141 "joystick is null" );
142
143 check_assertion( button >= 0, "button is negative" );
144
145 if ( button >= num_buttons ) {
146 if ( !warning_given ) {
147 print_warning( IMPORTANT_WARNING,
148 "state of button %d requested, but "
149 "joystick only has %d buttons. Further warnings "
150 "of this type will be suppressed",
151 button, num_buttons );
152 warning_given = True;
153 }
154 return False;
155 }
156
157 return (bool_t) SDL_JoystickGetButton( joystick, button );
158 }
159
is_joystick_continue_button_down()160 bool_t is_joystick_continue_button_down()
161 {
162 if ( joystick == NULL ) {
163 return False;
164 }
165
166 if ( getparam_joystick_continue_button() < 0 ) {
167 return False;
168 }
169
170 return is_joystick_button_down( getparam_joystick_continue_button() );
171 }
172
173 #else
174
175 /* Stub functions */
176
init_joystick()177 void init_joystick()
178 {
179 }
180
is_joystick_active()181 bool_t is_joystick_active()
182 {
183 return False;
184 }
185
update_joystick()186 void update_joystick()
187 {
188 }
189
get_joystick_x_axis()190 scalar_t get_joystick_x_axis()
191 {
192 return 0.0;
193 }
194
get_joystick_y_axis()195 scalar_t get_joystick_y_axis()
196 {
197 return 0.0;
198 }
199
is_joystick_button_down(int button)200 bool_t is_joystick_button_down( int button )
201 {
202 return False;
203 }
204
is_joystick_continue_button_down()205 bool_t is_joystick_continue_button_down()
206 {
207 return False;
208 }
209
210 #endif
211
212 /* EOF */
213