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