b03570e3c286d5c97f617d1d010547a9fb49724f
[blender.git] / source / gameengine / GameLogic / Joystick / SCA_Joystick.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): snailrose.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/GameLogic/Joystick/SCA_Joystick.cpp
29  *  \ingroup gamelogic
30  */
31
32 #ifdef WITH_SDL
33 #  include <SDL.h>
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #include "SCA_Joystick.h"
40 #include "SCA_JoystickPrivate.h"
41
42 #include "BLI_path_util.h"
43
44 #ifdef WITH_SDL
45 #  define SDL_CHECK(x) ((x) != (void *)0)
46 #endif
47
48 SCA_Joystick::SCA_Joystick(short int index)
49         :
50         m_joyindex(index),
51         m_prec(3200),
52         m_axismax(-1),
53         m_buttonmax(-1),
54         m_hatmax(-1),
55         m_isinit(0),
56         m_istrig_axis(0),
57         m_istrig_button(0),
58         m_istrig_hat(0)
59 {
60         for (int i=0; i < JOYAXIS_MAX; i++)
61                 m_axis_array[i] = 0;
62         
63         for (int i=0; i < JOYHAT_MAX; i++)
64                 m_hat_array[i] = 0;
65         
66 #ifdef WITH_SDL
67         m_private = new PrivateData();
68 #endif
69 }
70
71
72 SCA_Joystick::~SCA_Joystick()
73
74 {
75 #ifdef WITH_SDL
76         delete m_private;
77 #endif
78 }
79
80 SCA_Joystick *SCA_Joystick::m_instance[JOYINDEX_MAX];
81 int SCA_Joystick::m_joynum = 0;
82 int SCA_Joystick::m_refCount = 0;
83
84 SCA_Joystick *SCA_Joystick::GetInstance( short int joyindex )
85 {
86 #ifndef WITH_SDL
87         return NULL;
88 #else  /* WITH_SDL */
89         if (!SDL_CHECK(SDL_InitSubSystem)) {
90                 return NULL;
91         }
92         if (joyindex < 0 || joyindex >= JOYINDEX_MAX) {
93                 JOYSTICK_ECHO("Error-invalid joystick index: " << joyindex);
94                 return NULL;
95         }
96
97         if (m_refCount == 0) 
98         {
99                 int i;
100
101                 /* The video subsystem is required for joystick input to work. However,
102                  * when GHOST is running under SDL, video is initialized elsewhere. We
103                  * also need to set the videodriver to dummy, and do it here to avoid
104                  * interfering with addons that may use SDL too.
105                  *
106                  * We also init SDL once only. */
107 #  ifdef WITH_GHOST_SDL
108                 int success = (SDL_InitSubSystem(SDL_INIT_JOYSTICK) != -1 );
109 #  else
110                 /* set and restore environment variable */
111                 char *videodriver = getenv("SDL_VIDEODRIVER");
112                 BLI_setenv("SDL_VIDEODRIVER", "dummy");
113
114 #if SDL_VERSION_ATLEAST(2, 0, 0)
115                 int success = (SDL_InitSubSystem(SDL_INIT_JOYSTICK) != -1 );
116 #else
117                 int success = (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO) != -1 );
118 #endif
119
120                 BLI_setenv("SDL_VIDEODRIVER", videodriver);
121 #  endif
122
123                 if (!success) {
124                         JOYSTICK_ECHO("Error-Initializing-SDL: " << SDL_GetError());
125                         return NULL;
126                 }
127                 
128                 m_joynum = SDL_NumJoysticks();
129                 
130                 for (i = 0; i < m_joynum; i++) {
131                         m_instance[i] = new SCA_Joystick(i);
132                         m_instance[i]->CreateJoystickDevice();
133                 }
134                 m_refCount = 1;
135         }
136         else
137         {
138                 m_refCount++;
139         }
140         return m_instance[joyindex];
141 #endif /* WITH_SDL */
142 }
143
144 void SCA_Joystick::ReleaseInstance()
145 {
146         if (--m_refCount == 0)
147         {
148 #ifdef WITH_SDL
149                 if (!SDL_CHECK(SDL_QuitSubSystem)) {
150                         return;
151                 }
152                 for (int i = 0; i < m_joynum; i++) {
153                         if (m_instance[i]) {
154                                 m_instance[i]->DestroyJoystickDevice();
155                                 delete m_instance[i];
156                         }
157                         m_instance[i] = NULL;
158                 }
159
160                 /* The video subsystem is required for joystick input to work. However,
161                  * when GHOST is running under SDL, video is freed elsewhere.
162                  * Do this once only. */
163 #if SDL_VERSION_ATLEAST(2, 0, 0)
164                 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
165 #else
166                 SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);
167 #endif
168 #endif /* WITH_SDL */
169         }
170 }
171
172 void SCA_Joystick::cSetPrecision(int val)
173 {
174         m_prec = val;
175 }
176
177
178 bool SCA_Joystick::aAxisPairIsPositive(int axis)
179 {
180         return (pAxisTest(axis) > m_prec) ? true:false;
181 }
182
183 bool SCA_Joystick::aAxisPairDirectionIsPositive(int axis, int dir)
184 {
185
186         int res;
187
188         if (dir==JOYAXIS_UP || dir==JOYAXIS_DOWN)
189                 res = pGetAxis(axis, 1);
190         else /* JOYAXIS_LEFT || JOYAXIS_RIGHT */
191                 res = pGetAxis(axis, 0);
192         
193         if (dir==JOYAXIS_DOWN || dir==JOYAXIS_RIGHT)
194                 return (res > m_prec) ? true : false;
195         else /* JOYAXIS_UP || JOYAXIS_LEFT */
196                 return (res < -m_prec) ? true : false;
197 }
198
199 bool SCA_Joystick::aAxisIsPositive(int axis_single)
200 {
201         return abs(m_axis_array[axis_single]) > m_prec ? true:false;
202 }
203
204 bool SCA_Joystick::aAnyButtonPressIsPositive(void)
205 {
206 #ifdef WITH_SDL
207         if (!SDL_CHECK(SDL_JoystickGetButton)) {
208                 return false;
209         }
210         /* this is needed for the "all events" option
211          * so we know if there are no buttons pressed */
212         for (int i=0; i<m_buttonmax; i++)
213                 if (SDL_JoystickGetButton(m_private->m_joystick, i))
214                         return true;
215 #endif
216         return false;
217 }
218
219 bool SCA_Joystick::aButtonPressIsPositive(int button)
220 {
221 #ifndef WITH_SDL
222         return false;
223 #else
224         bool result;
225         result = SDL_CHECK(SDL_JoystickGetButton) && SDL_JoystickGetButton(m_private->m_joystick, button);
226         return result;
227 #endif
228 }
229
230
231 bool SCA_Joystick::aButtonReleaseIsPositive(int button)
232 {
233 #ifndef WITH_SDL
234         return false;
235 #else
236         bool result;
237         result = !(SDL_CHECK(SDL_JoystickGetButton) && SDL_JoystickGetButton(m_private->m_joystick, button));
238         return result;
239 #endif
240 }
241
242
243 bool SCA_Joystick::aHatIsPositive(int hatnum, int dir)
244 {
245         return (GetHat(hatnum)==dir) ? true : false;
246 }
247
248 int SCA_Joystick::GetNumberOfAxes()
249 {
250         return m_axismax;
251 }
252
253
254 int SCA_Joystick::GetNumberOfButtons()
255 {
256         return m_buttonmax;
257 }
258
259
260 int SCA_Joystick::GetNumberOfHats()
261 {
262         return m_hatmax;
263 }
264
265 bool SCA_Joystick::CreateJoystickDevice(void)
266 {
267 #ifndef WITH_SDL
268         m_isinit = true;
269         m_axismax = m_buttonmax = m_hatmax = 0;
270         return false;
271 #else /* WITH_SDL */
272         if (m_isinit == false && SDL_CHECK(SDL_JoystickOpen)) {
273                 if (m_joyindex>=m_joynum) {
274                         /* don't print a message, because this is done anyway */
275                         //JOYSTICK_ECHO("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
276                         
277                         /* Need this so python args can return empty lists */
278                         m_axismax = m_buttonmax = m_hatmax = 0;
279                         return false;
280                 }
281
282                 m_private->m_joystick = SDL_JoystickOpen(m_joyindex);
283                 SDL_JoystickEventState(SDL_ENABLE);
284                 m_isinit = true;
285                 
286                 JOYSTICK_ECHO("Joystick " << m_joyindex << " initialized");
287                 
288                 /* must run after being initialized */
289                 m_axismax     = SDL_JoystickNumAxes(m_private->m_joystick);
290                 m_buttonmax   = SDL_JoystickNumButtons(m_private->m_joystick);
291                 m_hatmax      = SDL_JoystickNumHats(m_private->m_joystick);
292
293                 if      (m_axismax > JOYAXIS_MAX) m_axismax = JOYAXIS_MAX;  /* very unlikely */
294                 else if (m_axismax < 0)           m_axismax = 0;
295                 
296                 if      (m_hatmax > JOYHAT_MAX) m_hatmax = JOYHAT_MAX;  /* very unlikely */
297                 else if (m_hatmax < 0)          m_hatmax = 0;
298                 
299                 if (m_buttonmax < 0) m_buttonmax = 0;
300                 
301         }
302         return true;
303 #endif /* WITH_SDL */
304 }
305
306
307 void SCA_Joystick::DestroyJoystickDevice(void)
308 {
309 #ifdef WITH_SDL
310         if (m_isinit) {
311 #if SDL_VERSION_ATLEAST(2, 0, 0)
312                 if (SDL_CHECK(SDL_JoystickGetAttached) && SDL_JoystickGetAttached(m_private->m_joystick))
313 #else
314                 if (SDL_CHECK(SDL_JoystickOpened) && SDL_JoystickOpened(m_joyindex))
315 #endif
316                 {
317                         JOYSTICK_ECHO("Closing-joystick " << m_joyindex);
318                         SDL_JoystickClose(m_private->m_joystick);
319                 }
320                 m_isinit = false;
321         }
322 #endif /* WITH_SDL */
323 }
324
325 int SCA_Joystick::Connected(void)
326 {
327 #ifdef WITH_SDL
328         if (m_isinit &&
329 #if SDL_VERSION_ATLEAST(2, 0, 0)
330             SDL_CHECK(SDL_JoystickGetAttached) && SDL_JoystickGetAttached(m_private->m_joystick)
331 #else
332             SDL_CHECK(SDL_JoystickOpened) && SDL_JoystickOpened(m_joyindex)
333 #endif
334                 )
335         {
336                 return 1;
337         }
338 #endif
339         return 0;
340 }
341
342 int SCA_Joystick::pGetAxis(int axisnum, int udlr)
343 {
344 #ifdef WITH_SDL
345         return m_axis_array[(axisnum*2)+udlr];
346 #endif
347         return 0;
348 }
349
350 int SCA_Joystick::pAxisTest(int axisnum)
351 {
352 #ifdef WITH_SDL
353         /* Use ints instead of shorts here to avoid problems when we get -32768.
354          * When we take the negative of that later, we should get 32768, which is greater
355          * than what a short can hold. In other words, abs(MIN_SHORT) > MAX_SHRT. */
356         int i1 = m_axis_array[(axisnum * 2)];
357         int i2 = m_axis_array[(axisnum * 2) + 1];
358         
359         /* long winded way to do:
360          * return max_ff(absf(i1), absf(i2))
361          * ...avoid abs from math.h */
362         if (i1 < 0) i1 = -i1;
363         if (i2 < 0) i2 = -i2;
364         if (i1 <i2) return i2;
365         else        return i1;
366 #else /* WITH_SDL */
367         return 0;
368 #endif /* WITH_SDL */
369 }
370
371 const char *SCA_Joystick::GetName()
372 {
373 #ifdef WITH_SDL
374 #if SDL_VERSION_ATLEAST(2, 0, 0)
375         return (SDL_CHECK(SDL_JoystickName)) ? SDL_JoystickName(m_private->m_joystick) : "";
376 #else
377         return (SDL_CHECK(SDL_JoystickName)) ? SDL_JoystickName(m_joyindex) : "";
378 #endif
379 #else /* WITH_SDL */
380         return "";
381 #endif /* WITH_SDL */
382 }