Blender V2.61 - r43446

SCA_Joystick.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): snailrose.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00032 #ifdef WITH_SDL
00033 #  include <SDL.h>
00034 #endif
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 
00039 #include "SCA_Joystick.h"
00040 #include "SCA_JoystickPrivate.h"
00041 
00042 SCA_Joystick::SCA_Joystick(short int index)
00043     :
00044     m_joyindex(index),
00045     m_prec(3200),
00046     m_axismax(-1),
00047     m_buttonmax(-1),
00048     m_hatmax(-1),
00049     m_isinit(0),
00050     m_istrig_axis(0),
00051     m_istrig_button(0),
00052     m_istrig_hat(0)
00053 {
00054     for(int i=0; i<JOYAXIS_MAX; i++)
00055         m_axis_array[i]= 0;
00056     
00057     for(int i=0; i<JOYHAT_MAX; i++)
00058         m_hat_array[i]= 0;
00059     
00060 #ifdef WITH_SDL
00061     m_private = new PrivateData();
00062 #endif
00063 }
00064 
00065 
00066 SCA_Joystick::~SCA_Joystick()
00067 
00068 {
00069 #ifdef WITH_SDL
00070     delete m_private;
00071 #endif
00072 }
00073 
00074 SCA_Joystick *SCA_Joystick::m_instance[JOYINDEX_MAX];
00075 int SCA_Joystick::m_joynum = 0;
00076 int SCA_Joystick::m_refCount = 0;
00077 
00078 SCA_Joystick *SCA_Joystick::GetInstance( short int joyindex )
00079 {
00080 #ifndef WITH_SDL
00081     return NULL;
00082 #else  /* WITH_SDL */
00083     if (joyindex < 0 || joyindex >= JOYINDEX_MAX) {
00084         echo("Error-invalid joystick index: " << joyindex);
00085         return NULL;
00086     }
00087 
00088     if (m_refCount == 0) 
00089     {
00090         int i;
00091         // The video subsystem is required for joystick input to work. However,
00092         // when GHOST is running under SDL, video is initialised elsewhere.
00093         // Do this once only.
00094 #  ifdef WITH_GHOST_SDL
00095         if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1 ){
00096 #  else
00097         if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO) == -1 ){
00098 #  endif
00099             echo("Error-Initializing-SDL: " << SDL_GetError());
00100             return NULL;
00101         }
00102         
00103         m_joynum = SDL_NumJoysticks();
00104         
00105         for (i=0; i<JOYINDEX_MAX; i++) {
00106             m_instance[i] = new SCA_Joystick(i);
00107             m_instance[i]->CreateJoystickDevice();
00108         }
00109         m_refCount = 1;
00110     }
00111     else
00112     {
00113         m_refCount++;
00114     }
00115     return m_instance[joyindex];
00116 #endif /* WITH_SDL */
00117 }
00118 
00119 void SCA_Joystick::ReleaseInstance()
00120 {
00121     if (--m_refCount == 0)
00122     {
00123 #ifdef WITH_SDL
00124         int i;
00125         for (i=0; i<JOYINDEX_MAX; i++) {
00126             if (m_instance[i]) {
00127                 m_instance[i]->DestroyJoystickDevice();
00128                 delete m_instance[i];
00129             }
00130             m_instance[i]= NULL;
00131         }
00132 
00133         // The video subsystem is required for joystick input to work. However,
00134         // when GHOST is running under SDL, video is freed elsewhere.
00135         // Do this once only.
00136 #  ifdef WITH_GHOST_SDL
00137         SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
00138 #  else
00139         SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);
00140 #  endif
00141 #endif /* WITH_SDL */
00142     }
00143 }
00144 
00145 void SCA_Joystick::cSetPrecision(int val)
00146 {
00147     m_prec = val;
00148 }
00149 
00150 
00151 bool SCA_Joystick::aAxisPairIsPositive(int axis)
00152 {
00153     return (pAxisTest(axis) > m_prec) ? true:false;
00154 }
00155 
00156 bool SCA_Joystick::aAxisPairDirectionIsPositive(int axis, int dir)
00157 {
00158 
00159     int res;
00160 
00161     if (dir==JOYAXIS_UP || dir==JOYAXIS_DOWN)
00162         res = pGetAxis(axis, 1);
00163     else /* JOYAXIS_LEFT || JOYAXIS_RIGHT */
00164         res = pGetAxis(axis, 0);
00165     
00166     if (dir==JOYAXIS_DOWN || dir==JOYAXIS_RIGHT)
00167         return (res > m_prec) ? true : false;
00168     else /* JOYAXIS_UP || JOYAXIS_LEFT */
00169         return (res < -m_prec) ? true : false;
00170 }
00171 
00172 bool SCA_Joystick::aAxisIsPositive(int axis_single)
00173 {
00174     return abs(m_axis_array[axis_single]) > m_prec ? true:false;
00175 }
00176 
00177 bool SCA_Joystick::aAnyButtonPressIsPositive(void)
00178 {
00179 #ifdef WITH_SDL
00180     /* this is needed for the "all events" option
00181      * so we know if there are no buttons pressed */
00182     for (int i=0; i<m_buttonmax; i++)
00183         if (SDL_JoystickGetButton(m_private->m_joystick, i))
00184             return true;
00185 #endif
00186     return false;
00187 }
00188 
00189 bool SCA_Joystick::aButtonPressIsPositive(int button)
00190 {
00191 #ifndef WITH_SDL
00192     return false;
00193 #else
00194     bool result;
00195     SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
00196     return result;
00197 #endif
00198 }
00199 
00200 
00201 bool SCA_Joystick::aButtonReleaseIsPositive(int button)
00202 {
00203 #ifndef WITH_SDL
00204     return false;
00205 #else
00206     bool result;
00207     SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
00208     return result;
00209 #endif
00210 }
00211 
00212 
00213 bool SCA_Joystick::aHatIsPositive(int hatnum, int dir)
00214 {
00215     return (GetHat(hatnum)==dir) ? true : false;
00216 }
00217 
00218 int SCA_Joystick::GetNumberOfAxes()
00219 {
00220     return m_axismax;
00221 }
00222 
00223 
00224 int SCA_Joystick::GetNumberOfButtons()
00225 {
00226     return m_buttonmax;
00227 }
00228 
00229 
00230 int SCA_Joystick::GetNumberOfHats()
00231 {
00232     return m_hatmax;
00233 }
00234 
00235 bool SCA_Joystick::CreateJoystickDevice(void)
00236 {
00237 #ifndef WITH_SDL
00238     m_isinit = true;
00239     m_axismax = m_buttonmax = m_hatmax = 0;
00240     return false;
00241 #else /* WITH_SDL */
00242     if(m_isinit == false){
00243         if (m_joyindex>=m_joynum) {
00244             // don't print a message, because this is done anyway
00245             //echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
00246             
00247             // Need this so python args can return empty lists
00248             m_axismax = m_buttonmax = m_hatmax = 0;
00249             return false;
00250         }
00251 
00252         m_private->m_joystick = SDL_JoystickOpen(m_joyindex);
00253         SDL_JoystickEventState(SDL_ENABLE);
00254         m_isinit = true;
00255         
00256         echo("Joystick " << m_joyindex << " initialized");
00257         
00258         /* must run after being initialized */
00259         m_axismax =     SDL_JoystickNumAxes(m_private->m_joystick);
00260         m_buttonmax =   SDL_JoystickNumButtons(m_private->m_joystick);
00261         m_hatmax =      SDL_JoystickNumHats(m_private->m_joystick);
00262         
00263         if (m_axismax > JOYAXIS_MAX) m_axismax= JOYAXIS_MAX;        /* very unlikely */
00264         else if (m_axismax < 0) m_axismax = 0;
00265         
00266         if (m_hatmax > JOYHAT_MAX) m_hatmax= JOYHAT_MAX;            /* very unlikely */
00267         else if(m_hatmax<0) m_hatmax= 0;
00268         
00269         if(m_buttonmax<0) m_buttonmax= 0;
00270         
00271     }
00272     return true;
00273 #endif /* WITH_SDL */
00274 }
00275 
00276 
00277 void SCA_Joystick::DestroyJoystickDevice(void)
00278 {
00279 #ifdef WITH_SDL
00280     if (m_isinit){
00281         if(SDL_JoystickOpened(m_joyindex)){
00282             echo("Closing-joystick " << m_joyindex);
00283             SDL_JoystickClose(m_private->m_joystick);
00284         }
00285         m_isinit = false;
00286     }
00287 #endif /* WITH_SDL */
00288 }
00289 
00290 int SCA_Joystick::Connected(void)
00291 {
00292 #ifdef WITH_SDL
00293     if (m_isinit && SDL_JoystickOpened(m_joyindex))
00294         return 1;
00295 #endif
00296     return 0;
00297 }
00298 
00299 int SCA_Joystick::pGetAxis(int axisnum, int udlr)
00300 {
00301 #ifdef WITH_SDL
00302     return m_axis_array[(axisnum*2)+udlr];
00303 #endif
00304     return 0;
00305 }
00306 
00307 int SCA_Joystick::pAxisTest(int axisnum)
00308 {
00309 #ifdef WITH_SDL
00310     short i1= m_axis_array[(axisnum*2)];
00311     short i2= m_axis_array[(axisnum*2)+1];
00312     
00313     /* long winded way to do
00314      *   return MAX2(abs(i1), abs(i2))
00315      * avoid abs from math.h */
00316     if (i1 < 0) i1 = -i1;
00317     if (i2 < 0) i2 = -i2;
00318     if (i1 <i2) return i2;
00319     else        return i1;
00320 #else /* WITH_SDL */
00321     return 0;
00322 #endif /* WITH_SDL */
00323 }