Esempio n. 1
0
void
JoystickManager::on_joystick_added(int joystick_index)
{
  log_debug << "on_joystick_added(): " << joystick_index << std::endl;
  SDL_Joystick* joystick = SDL_JoystickOpen(joystick_index);
  if (!joystick)
  {
    log_warning << "failed to open joystick: " << joystick_index
                << ": " << SDL_GetError() << std::endl;
  }
  else
  {
    joysticks.push_back(joystick);
  }

  if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons)
    min_joybuttons = SDL_JoystickNumButtons(joystick);

  if(SDL_JoystickNumButtons(joystick) > max_joybuttons)
    max_joybuttons = SDL_JoystickNumButtons(joystick);

  if(SDL_JoystickNumAxes(joystick) > max_joyaxis)
    max_joyaxis = SDL_JoystickNumAxes(joystick);

  if(SDL_JoystickNumHats(joystick) > max_joyhats)
    max_joyhats = SDL_JoystickNumHats(joystick);
}
void
JoystickKeyboardController::updateAvailableJoysticks()
{
  for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
      i != joysticks.end(); ++i) {
    if(*i != 0)
      SDL_JoystickClose(*i);
  }
  joysticks.clear();
  
  SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  SDL_InitSubSystem(SDL_INIT_JOYSTICK);

  int joystick_count = SDL_NumJoysticks();
  min_joybuttons = -1;
  max_joybuttons = -1;
  max_joyaxis    = -1;
  max_joyhats    = -1;

  if( joystick_count > 0 ){
    for(int i = 0; i < joystick_count; ++i) {
      SDL_Joystick* joystick = SDL_JoystickOpen(i);
      bool good = true;
      if(SDL_JoystickNumButtons(joystick) < 2) {
        log_info << "Joystick " << i << ": " << SDL_JoystickName(i) << " has less than 2 buttons" << std::endl;
        good = false;
      }
      if(SDL_JoystickNumAxes(joystick) < 2
         && SDL_JoystickNumHats(joystick) == 0) {
        log_info << "Joystick " << i << ": " << SDL_JoystickName(i) << " has less than 2 axes and no hat" << std::endl;
        good = false;
      }
      if(!good) {
        SDL_JoystickClose(joystick);
        continue;
      }

      if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons)
        min_joybuttons = SDL_JoystickNumButtons(joystick);

      if(SDL_JoystickNumButtons(joystick) > max_joybuttons)
        max_joybuttons = SDL_JoystickNumButtons(joystick);

      if(SDL_JoystickNumAxes(joystick) > max_joyaxis)
        max_joyaxis = SDL_JoystickNumAxes(joystick);

      if(SDL_JoystickNumHats(joystick) > max_joyhats)
        max_joyhats = SDL_JoystickNumHats(joystick);

      joysticks.push_back(joystick);
    }
  }

  // some joysticks or SDL seem to produce some bogus events after being opened
  Uint32 ticks = SDL_GetTicks();
  while(SDL_GetTicks() - ticks < 200) {
    SDL_Event event;
    SDL_PollEvent(&event);
  }
}
void ControladorJoystick::initialiseJoysticks(
		std::map<string, int>* correspondenciaTeclasJ1,
		std::map<string, int>* correspondenciaEjesJ1,
		std::map<string, int>* correspondenciaTeclasJ2,
		std::map<string, int>* correspondenciaEjesJ2) {

	if(SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
		SDL_InitSubSystem(SDL_INIT_JOYSTICK);

	if (correspondenciaTeclasJ1)
		this->correspondenciaTeclas = correspondenciaTeclasJ1;
	else
		this->correspondenciaTeclas = correspondenciaTeclasPorDefecto();

	if (correspondenciaEjesJ1)
		this->correspondenciaEjes = correspondenciaEjesJ1;
	else
		this->correspondenciaEjes = correspondenciaEjesPorDefecto();

	this->estadoJoystick1    = new std::map<string, bool>;
	this->estadoJoystick2    = new std::map<string, bool>;
	this->estadoJoystickNulo = new std::map<string, bool>;

	if(SDL_NumJoysticks() > 0) {
		for(int i = 0; i < SDL_NumJoysticks(); i++)	{
			SDL_Joystick* joy = SDL_JoystickOpen(i);

			if(joy) {

				// INICIA JOYSTICKS
				m_joysticks.push_back(joy);

				// SETEO DIRECCIONES EN CERO
				m_joystickValues.push_back(std::make_pair(new Vector2D(0,0), new Vector2D(0,0)));

				// SETEA ESTADO DE TECLAS ACTUALES EN FALSE
				//cout << "CANTIDAD BOTONES[" << i << "]:" << SDL_JoystickNumButtons(joy) << endl;
				std::vector<bool> tempButtons_actual;
				for(int j = 0; j < SDL_JoystickNumButtons(joy); j++)
					tempButtons_actual.push_back(false);
				m_buttonStates_actual.push_back(tempButtons_actual);

				// SETEA ESTADO DE TECLAS ANTERIORES EN FALSE
				//cout << "CANTIDAD BOTONES[" << i << "]:" << SDL_JoystickNumButtons(joy) << endl;
				std::vector<int> tempButtons_anterior;
				for(int j = 0; j < SDL_JoystickNumButtons(joy); j++)
					tempButtons_anterior.push_back(0);
				m_cantidad_de_pulsaciones.push_back(tempButtons_anterior);
			}
			else
				std::cout << SDL_GetError();
		}

		SDL_JoystickEventState(SDL_ENABLE);
		m_bJoysticksInitialised = true;
		//std::cout << "Initialised "<< m_joysticks.size() << " joystick(s)";
	}
	else
		m_bJoysticksInitialised = false;
}
Esempio n. 4
0
int
ui_joystick_init( void )
{
  int error, retval;

#ifdef UI_SDL
  error = SDL_InitSubSystem( SDL_INIT_JOYSTICK );
#else
  /* Other UIs could handle joysticks by the SDL library */
  error = SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_VIDEO);
#endif

  if ( error ) {
    ui_error( UI_ERROR_ERROR, "failed to initialise joystick subsystem" );
    return 0;
  }

  retval = SDL_NumJoysticks();

  if( retval >= 2 ) {

    retval = 2;

    if( ( joystick2 = SDL_JoystickOpen( 1 ) ) == NULL ) {
      ui_error( UI_ERROR_ERROR, "failed to initialise joystick 2" );
      return 0;
    }

    if( SDL_JoystickNumAxes( joystick2 ) < 2    ||
        SDL_JoystickNumButtons( joystick2 ) < 1    ) {
      ui_error( UI_ERROR_ERROR, "sorry, joystick 2 is inadequate!" );
      return 0;
    }

  }

  if( retval > 0 ) {

    if( ( joystick1 = SDL_JoystickOpen( 0 ) ) == NULL ) {
      ui_error( UI_ERROR_ERROR, "failed to initialise joystick 1" );
      return 0;
    }
 
    if( SDL_JoystickNumAxes( joystick1 ) < 2    ||
        SDL_JoystickNumButtons( joystick1 ) < 1    ) {
      ui_error( UI_ERROR_ERROR, "sorry, joystick 1 is inadequate!" );
      return 0;
    }
  }

  SDL_JoystickEventState( SDL_ENABLE );

  return retval;
}
Esempio n. 5
0
int libjoy_get_button_specific( int joy, int button )
{
    if ( joy >= 0 && joy < _max_joys )
    {
#ifdef TARGET_CAANOO
        if ( button >= 0 && ( ( joy == 0 && button <= 21 ) || ( joy != 0 && SDL_JoystickNumButtons( _joysticks[ joy ] ) ) ) )
#else
        if ( button >= 0 && button <= SDL_JoystickNumButtons( _joysticks[ joy ] ) )
#endif
        {
#ifdef TARGET_CAANOO
            if ( joy == 0 )
            {
                int vax;

                switch ( button )
                {
                    case    1: /* UPLF                  */  return ( SDL_JoystickGetAxis( _joysticks[ 0 ], 1 ) < -16384 && SDL_JoystickGetAxis( _joysticks[ 0 ], 0 ) < -16384 );
                    case    3: /* DWLF                  */  return ( SDL_JoystickGetAxis( _joysticks[ 0 ], 1 ) >  16384 && SDL_JoystickGetAxis( _joysticks[ 0 ], 0 ) < -16384 );
                    case    5: /* DWRT                  */  return ( SDL_JoystickGetAxis( _joysticks[ 0 ], 1 ) >  16384 && SDL_JoystickGetAxis( _joysticks[ 0 ], 0 ) >  16384 );
                    case    7: /* UPRT                  */  return ( SDL_JoystickGetAxis( _joysticks[ 0 ], 1 ) < -16384 && SDL_JoystickGetAxis( _joysticks[ 0 ], 0 ) >  16384 );
                    case    0: /* UP                    */  vax = SDL_JoystickGetAxis( _joysticks[ 0 ], 0 ) ; return ( SDL_JoystickGetAxis( _joysticks[ 0 ], 1 ) < -16384 && ABS( vax ) < 16384 );
                    case    4: /* DW                    */  vax = SDL_JoystickGetAxis( _joysticks[ 0 ], 0 ) ; return ( SDL_JoystickGetAxis( _joysticks[ 0 ], 1 ) >  16384 && ABS( vax ) < 16384 );
                    case    2: /* LF                    */  vax = SDL_JoystickGetAxis( _joysticks[ 0 ], 1 ) ; return ( SDL_JoystickGetAxis( _joysticks[ 0 ], 0 ) < -16384 && ABS( vax ) < 16384 );
                    case    6: /* RT                    */  vax = SDL_JoystickGetAxis( _joysticks[ 0 ], 1 ) ; return ( SDL_JoystickGetAxis( _joysticks[ 0 ], 0 ) >  16384 && ABS( vax ) < 16384 );

                    case    8:  /* MENU->HOME           */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 6 ) );
                    case    9:  /* SELECT->HELP-II      */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 9 ) );
                    case    10: /* L                    */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 4 ) );
                    case    11: /* R                    */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 5 ) );
                    case    12: /* A                    */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 0 ) );
                    case    13: /* B                    */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 2 ) );
                    case    14: /* X                    */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 1 ) );
                    case    15: /* Y                    */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 3 ) );
                    case    16: /* VOLUP                */  return ( 0 );
                    case    17: /* VOLDOWN              */  return ( 0 );
                    case    18: /* CLICK                */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 10 ) );
                    case    19: /* POWER-LOCK  (CAANOO) */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 7 ) ); /* Only Caanoo */
                    case    20: /* HELP-I      (CAANOO) */  return ( SDL_JoystickGetButton( _joysticks[ 0 ], 8 ) ); /* Only Caanoo */
                    default:                                return ( 0 );
                }
            }
#endif
            return SDL_JoystickGetButton( _joysticks[ joy ], button ) ;
        }
    }
    return 0 ;
}
Esempio n. 6
0
int
ui_joystick_init( void )
{
  int error, retval;

  error = SDL_InitSubSystem( SDL_INIT_JOYSTICK );
  if ( error ) {
    ui_error( UI_ERROR_ERROR, "failed to initialise joystick subsystem" );
    return 0;
  }

  retval = SDL_NumJoysticks();

  if( retval >= 2 ) {

    retval = 2;

    if( ( joystick2 = SDL_JoystickOpen( 1 ) ) == NULL ) {
      ui_error( UI_ERROR_ERROR, "failed to initialise joystick 2" );
      return 0;
    }

    if( SDL_JoystickNumAxes( joystick2 ) < 2    ||
        SDL_JoystickNumButtons( joystick2 ) < 1    ) {
      ui_error( UI_ERROR_ERROR, "sorry, joystick 2 is inadequate!" );
      return 0;
    }

  }

  if( retval > 0 ) {

    if( ( joystick1 = SDL_JoystickOpen( 0 ) ) == NULL ) {
      ui_error( UI_ERROR_ERROR, "failed to initialise joystick 1" );
      return 0;
    }
 
    if( SDL_JoystickNumAxes( joystick1 ) < 2    ||
        SDL_JoystickNumButtons( joystick1 ) < 1    ) {
      ui_error( UI_ERROR_ERROR, "sorry, joystick 1 is inadequate!" );
      return 0;
    }
  }

  SDL_JoystickEventState( SDL_ENABLE );

  return retval;
}
Esempio n. 7
0
/**
 * @brief Makes the game use a joystick by index.
 *
 *    @param indjoystick Index of the joystick to use.
 *    @return 0 on success.
 */
int joystick_use( int indjoystick )
{
    /* Check to see if it exists. */
    if ((indjoystick < 0) || (indjoystick >= SDL_NumJoysticks())) {
        WARN("Joystick of index number %d does not existing, switching to default 0",
             indjoystick);
        indjoystick = 0;
    }

    /* Close if already open. */
    if (joystick != NULL) {
        SDL_JoystickClose(joystick);
        joystick = NULL;
    }

    /* Start using joystick. */
    joystick = SDL_JoystickOpen(indjoystick);
    if (joystick == NULL) {
        WARN("Error opening joystick %d [%s]", indjoystick, SDL_JoystickName(indjoystick));
        return -1;
    }
    LOG("Using joystick %d - %s", indjoystick, SDL_JoystickName(indjoystick));
    DEBUG("   with %d axes, %d buttons, %d balls and %d hats",
          SDL_JoystickNumAxes(joystick), SDL_JoystickNumButtons(joystick),
          SDL_JoystickNumBalls(joystick), SDL_JoystickNumHats(joystick));

    /* Initialize the haptic if possible. */
    joystick_initHaptic();

    /* For style purposes. */
    DEBUG();

    return 0;
}
Esempio n. 8
0
void st_joystick_setup(void)
{

  /* Init Joystick: */

  use_joystick = true;

  if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
    {
      fprintf(stderr, "Warning: I could not initialize joystick!\n"
              "The Simple DirectMedia error that occured was:\n"
              "%s\n\n", SDL_GetError());

      use_joystick = false;
    }
  else
    {
      /* Open joystick: */
      if (SDL_NumJoysticks() <= 0)
        {
          fprintf(stderr, "Warning: No joysticks are available.\n");

          use_joystick = false;
        }
      else
        {
          js = SDL_JoystickOpen(joystick_num);

          if (js == NULL)
            {
              fprintf(stderr, "Warning: Could not open joystick %d.\n"
                      "The Simple DirectMedia error that occured was:\n"
                      "%s\n\n", joystick_num, SDL_GetError());

              use_joystick = false;
            }
          else
            {
              if (SDL_JoystickNumAxes(js) < 2)
                {
                  fprintf(stderr,
                          "Warning: Joystick does not have enough axes!\n");

                  use_joystick = false;
                }
              else
                {
                  if (SDL_JoystickNumButtons(js) < 2)
                    {
                      fprintf(stderr,
                              "Warning: "
                              "Joystick does not have enough buttons!\n");

                      use_joystick = false;
                    }
                }
            }
        }
    }
}
Esempio n. 9
0
INLINE BOOL Input::Initialize()
{
	Log(TAG "Initializing...");
	BOOL r = this->Reset();
	#if defined(WIN32) && defined(DEBUG)
	SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
	#endif

	MEMSET(parJoy, '\0', sizeof(parJoy));
	SDL_InitSubSystem(SDL_INIT_JOYSTICK);

	iJoystickCount = SDL_NumJoysticks();
	if (iJoystickCount)
	{
		SDL_JoystickEventState(SDL_ENABLE);

		Log(TAG "Joystick(s): ");
		for (u32 i = 0; i < iJoystickCount; i++)
		{
			parJoy[i] = SDL_JoystickOpen(i);
			if (parJoy[i])
			{
				Log("Opened Joystick %d:", i);
				Log(TAG "\tName: %s", SDL_JoystickName(i));
				Log(TAG "\t\tAxes: %d", SDL_JoystickNumAxes(parJoy[i]));
				Log(TAG "\t\tButtons: %d", SDL_JoystickNumButtons(parJoy[i]));
				Log(TAG "\t\tHats: %d", SDL_JoystickNumHats(parJoy[i]));
				Log(TAG "\t\tBalls: %d", SDL_JoystickNumBalls(parJoy[i]));
			}
		}
	}
	Log(TAG "Initialization completed.");

	return r;
}
Esempio n. 10
0
void JOYSTICKGetNumButtons(int nlhs, mxArray *plhs[], int nrhs, CONSTmxArray *prhs[])
{

	ProjectTable *joystickTable=GetProjectTable();
	int  numSticks;
	CONSTmxArray *numArg;
	double stickNum;
	SDL_Joystick *pStick;

	prhs;
	if(joystickTable->giveHelp){GiveHelp(useGetNumButtons,synopsisGetNumButtons);return;}
	numArg = joystickTable->joystickNumberArgument; 
	if(numArg == NULL || nlhs > 1 || nrhs > 0 || !mxIsDouble(numArg) || (mxGetM(numArg) * mxGetN(numArg) != 1))
		GiveUsageExit(useGetNumButtons);
	numSticks = SDL_NumJoysticks();
	stickNum = mxGetPr(numArg)[0];
	if(stickNum > numSticks)
		PrintfExit("The joystick number %d passed to JOYSTICK 'GetNumButtons' exceeds the number of joysticks, %d",stickNum,numSticks);
	if(stickNum < 1)
		PrintfExit("The joystick number passed to JOYSTICK 'GetNumButtons' must be greater than 0");
	pStick = GetJoystickObjFromNum((int)stickNum-1);
	if(pStick == NULL)
		PrintfExit("JOYSTICK 'GetNumButtons' can not open joystick number %d",stickNum);
	plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);	
	mxGetPr(plhs[0])[0] = SDL_JoystickNumButtons(pStick);
		
}
Esempio n. 11
0
	uint8_t Joystick::getNumberOfButtons() const {
		uint8_t number = 0;
		if (isConnected()) {
			number = SDL_JoystickNumButtons(m_joystickHandle);
		}
		return number;
	}
Esempio n. 12
0
	//-------------------------------------------------------------------------------------------------------------------//
	Application::Application(): mScreen(NULL), mEvent(), mIsItTimeToClose(false), mJoystick(NULL)
	{
		// On initialise la SDL.
		MTest_T_MSG (SDL_Init(SDL_INIT_EVERYTHING) == -1, "SDL Initialization Error : " << SDL_GetError());

		// Log pour les Joysticks.
		MOut(SDL_NumJoysticks() << " joysticks connected");
		for (int i = 0; i < SDL_NumJoysticks() ; ++i)
		{
			MOut("Joystick_" << i << " : " << SDL_JoystickName(i));
		}

		if (SDL_NumJoysticks() > 0)
		{
			// On active la gestion des evenements des Joysticks.
			SDL_JoystickEventState(SDL_ENABLE);
			// On charge le premier Joystick connecté.
			mJoystick = SDL_JoystickOpen(0);

			MOut("----> " << SDL_JoystickNumButtons(mJoystick) << " buttons");
			MOut("----> " << SDL_JoystickNumAxes(mJoystick) << " axes");
			MOut("----> " << SDL_JoystickNumBalls(mJoystick) << " trackballs");
			MOut("----> " << SDL_JoystickNumHats(mJoystick) << " hats");
		}
	}
Esempio n. 13
0
void controller_joystick_attach( Tank *t ) {
	
	JoystickPrivateData *data;
	
	/* Make sure that this is even a joystick to connect to: */
	if(SDL_NumJoysticks() == 0) {
		/* TODO: exiting isn't all that friendly... we need a better controller API... */
		gamelib_debug("No joysticks connected.\n");
		exit(1);
	}
	
	data = get_object(JoystickPrivateData);
	data->joystick = SDL_JoystickOpen(0);
	
	if(data->joystick) {
		gamelib_debug("Using Joystick #0:\n");
		gamelib_debug("  Name:    %s\n", SDL_JoystickName(0));
		gamelib_debug("  Axes:    %d\n", SDL_JoystickNumAxes(data->joystick));
		gamelib_debug("  Buttons: %d\n", SDL_JoystickNumButtons(data->joystick));
		gamelib_debug("  Balls:   %d\n", SDL_JoystickNumBalls(data->joystick));
	
	} else {
		gamelib_debug("Failed to open Joystick #0.\n");
		exit(1);
	}
	
	tank_set_controller(t, joystick_controller, data);
}
Esempio n. 14
0
void Init_Joy (void)
{
  int num_joy;

  if (SDL_InitSubSystem (SDL_INIT_JOYSTICK) == -1)
    {
      fprintf(stderr, "Couldn't initialize SDL-Joystick: %s\n", SDL_GetError());
      Terminate(ERR);
    } else
      DebugPrintf(1, "\nSDL Joystick initialisation successful.\n");


  DebugPrintf (1, " %d Joysticks found!\n", num_joy = SDL_NumJoysticks ());

  if (num_joy > 0)
    joy = SDL_JoystickOpen (0);

  if (joy)
    {
      DebugPrintf (1, "Identifier: %s\n", SDL_JoystickName (0));
      DebugPrintf (1, "Number of Axes: %d\n", joy_num_axes = SDL_JoystickNumAxes(joy));
      DebugPrintf (1, "Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));

      /* aktivate Joystick event handling */
      SDL_JoystickEventState (SDL_ENABLE); 

    }
  else 
    joy = NULL;  /* signals that no yoystick is present */


  return;
}
Esempio n. 15
0
//------------------------------------------------------------
int ofxSDLAppWindow::getControllerNumButtons(int num) {
  if (num >= numJoys || !joys[num]) {
    return 0;
  }
  
  return SDL_JoystickNumButtons(joys[num]);
}
Esempio n. 16
0
static void read_joy (unsigned int nr)
{
    unsigned int num, i, axes, axis;
    SDL_Joystick *joy;

    if (currprefs.input_selected_setting == 0) {
		if (jsem_isjoy (0, &currprefs) != (int)nr && jsem_isjoy (1, &currprefs) != (int)nr)
		    return;
    }
    joy = joys[nr].joy;
    axes = SDL_JoystickNumAxes (joy);
    for (i = 0; i < axes; i++) {
		axis = SDL_JoystickGetAxis (joy, i);
		setjoystickstate (nr, i, axis, 32767);
    }

    num = SDL_JoystickNumButtons (joy);
    for (i = 0; i < num; i++) {
		int bs = SDL_JoystickGetButton (joy, i) ? 1 : 0;
		setjoybuttonstate (nr, i, bs);	

		if (i == 10 && bs == 1)
		{
			inputdevice_add_inputcode (512, 1);
		}
    }
}
Esempio n. 17
0
bool cJoystick :: OpenStick( unsigned int index )
{
	SDL_Joystick *Joystick = SDL_JoystickOpen( index );

	if( !Joystick )
	{
		printf( "Couldn't open joystick %d\n", index );
		Opened = 0;
		return 0;
	}

	cur_stick = index;

	num_buttons = SDL_JoystickNumButtons( Joystick );
	num_axes = SDL_JoystickNumAxes( Joystick );
	num_balls = SDL_JoystickNumBalls( Joystick );

	if( Debug )
	{
		printf( "Opened Joystick %d\n", index );
		printf( "Name: %s\n", SDL_JoystickName( index ) );
		printf( "Number of Axes: %d\n", num_axes );
		printf( "Number of Buttons: %d\n", num_buttons );
		printf( "Number of Balls: %d\n\n", num_balls );
	}

	Opened = 1;

	return 1;
}
Esempio n. 18
0
Input* Input_create() {
	Input* this = malloc(sizeof(Input));
	this->hotkeys = Vector_Create();

	this->joysticks = Vector_Create();

	int joysticksCount = SDL_NumJoysticks();

	if (joysticksCount > 1) {
		printf("Input: found %d Joysticks/Gamepads:\n", joysticksCount);
	} else if (joysticksCount == 1) {
		printf("Input: found 1 Joystick/Gamepad:\n");
	} else {
		printf("Input: found no Joysticks/Gamepads\n");
	}


	for (int i=0; i < joysticksCount; ++i) {
		SDL_Joystick* j = SDL_JoystickOpen(i);
		int axes = SDL_JoystickNumAxes(j);
		int buttons = SDL_JoystickNumButtons(j);
		int hats = SDL_JoystickNumHats(j);
		Vector_AddElement(this->joysticks, j);
		printf("%d: %s | axes:%d buttons:%d hats:%d \n", i, SDL_JoystickName(j), axes, buttons, hats);
	}

//	SDL_JoystickEventState (SDL_IGNORE);


	return this;
}
void JoystickControl::_verificarMapaComandos(){
	bool mapa_correcto = false;
	if (comandos != NULL && joystick != NULL){
		mapa_correcto = true;
		for (std::map<string,int>::const_iterator it=comandos->begin(); it!=comandos->end(); ++it){
			//si supera la cantidad de botones del joystick
			if(it->second >= SDL_JoystickNumButtons(joystick))
				mapa_correcto = false;
			if (it->first != PINA_BAJA || it->first != PATADA_BAJA || it->first != PINA_ALTA || it->first != PATADA_ALTA || it->first != CUBRIRSE ||it->first != LANZAR_ARMA)
				mapa_correcto = false;
		}
	}

	//si no es correcto hago uno propio
	if(!mapa_correcto){
		log("Los botones del joystick especificados en el Json no son correctos, se crean unos por defecto",LOG_WARNING);
		std::map<std::string, int>* mapita = new std::map<std::string,int>;
		mapita->operator[](PINA_BAJA) = JOY_X;
		mapita->operator[](PATADA_BAJA) = JOY_CIRCULO;
		mapita->operator[](PINA_ALTA) = JOY_CUADRADO;
		mapita->operator[](PATADA_ALTA) = JOY_TRIANGULO;
		mapita->operator[](CUBRIRSE)= JOY_R1;
		mapita->operator[](LANZAR_ARMA) = JOY_L1;
		comandos = mapita;
	}
}
int main(int, char**){
	SDL_Init(SDL_INIT_JOYSTICK);
	SDL_JoystickEventState(SDL_ENABLE);
	atexit(SDL_Quit);
	int num_joysticks = SDL_NumJoysticks();
	if(num_joysticks == 0) {
		std::cout << "No joysticks found! please attach a device and re-run this program" << std::endl;
		return 0;
	}
		js = SDL_JoystickOpen(0);
			//ok we've found our joystick!
		if (js) {
			SDL_JoystickGUID guid = SDL_JoystickGetGUID(js);
			char guid_str[1024];
			SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str));
			const char* name = SDL_JoystickName(js);

			int num_axes = SDL_JoystickNumAxes(js);
			int num_buttons = SDL_JoystickNumButtons(js);
			int num_hats = SDL_JoystickNumHats(js);
			int num_balls = SDL_JoystickNumBalls(js);
			std:: cout << guid_str <<  " \\ " << name << " \\ axes: " << num_axes << " buttons: " << num_buttons << " hats: " << num_hats << " balls: " << num_balls << std::endl;
		}

	//start webserver and bind to socket.
	handler handler_;
	http_server::options options(handler_);
	http_server server(
		options.address("0.0.0.0")
		.port("1111"));
	std::cout << "found joystick! starting server!" << std::endl;
	server.run();	//function BLOCKS! see handler struct at top of page!
	return 0;
}
Esempio n. 21
0
static int32_t init_joystick (void)
{
    int32_t success = 0;

    if (!initialized) {
		if (SDL_InitSubSystem (SDL_INIT_JOYSTICK) == 0) {
		    int32_t i = 0;

		    nr_joysticks = SDL_NumJoysticks ();
		    write_log ("Found %d joystick(s)\n", nr_joysticks);

		    if (nr_joysticks > MAX_INPUT_DEVICES)
				nr_joysticks = MAX_INPUT_DEVICES;

		    for ( ; i < get_joystick_num (); i++) {
				joys[i].joy     = SDL_JoystickOpen (i);
				joys[i].axles   = SDL_JoystickNumAxes (joys[i].joy);
				joys[i].buttons = SDL_JoystickNumButtons (joys[i].joy);
		    }
		    success = initialized = 1;
	} else
	    write_log ("Failed to initialize joysticks\n");
    }

	return success;
}
Esempio n. 22
0
_SGJoystick* SG_CALL _sgJoystickCreate(SGuint id)
{
    _SGJoystick* joy = malloc(sizeof(_SGJoystick));
    if(joy == NULL)
        return NULL;

    joy->id = id;
    joy->numbuttons = 0;
    joy->numaxis = 0;

    joy->handle = SDL_JoystickOpen(id);
    joy->taxis = malloc(SDL_JoystickNumAxes(joy->handle) * sizeof(float));

    joy->numbuttons = SDL_JoystickNumButtons(joy->handle);
    joy->bprev = malloc(joy->numbuttons * sizeof(SGbool));
    memset(joy->bprev, 0, joy->numbuttons * sizeof(SGbool));
    joy->bcurr = malloc(joy->numbuttons * sizeof(SGbool));
    memset(joy->bcurr, 0, joy->numbuttons * sizeof(SGbool));

    joy->numaxis = SDL_JoystickNumAxes(joy->handle);
    joy->aprev = malloc(joy->numaxis * sizeof(float));
    joy->acurr = malloc(joy->numaxis * sizeof(float));
    joy->adelt = malloc(joy->numaxis * sizeof(float));
    size_t i;
    for(i = 0; i < joy->numaxis; i++)
        joy->aprev[i] = joy->acurr[i] = joy->adelt[i] = 0.0;

    return joy;
}
Esempio n. 23
0
File: gl_sdl.c Progetto: ZwS/qudos
qboolean
OpenJoystick(cvar_t * joy_dev)
{
	int		num_joysticks, i;

	joy = NULL;

	if (!(SDL_INIT_JOYSTICK & SDL_WasInit(SDL_INIT_JOYSTICK))) {
		ri.Con_Printf(PRINT_ALL, "SDL Joystick not initialized, trying to init...\n");
		SDL_Init(SDL_INIT_JOYSTICK);
	}
	ri.Con_Printf(PRINT_ALL, "Trying to start-up joystick...\n");
	if ((num_joysticks = SDL_NumJoysticks())) {
		for (i = 0; i < num_joysticks; i++) {
			ri.Con_Printf(PRINT_ALL, "Trying joystick [%s]\n",
			    SDL_JoystickName(i));
			if (!SDL_JoystickOpened(i)) {
				joy = SDL_JoystickOpen(i);
				if (joy) {
					ri.Con_Printf(PRINT_ALL, "Joytick activated.\n");
					joy_numbuttons = SDL_JoystickNumButtons(joy);
					break;
				}
			}
		}
		if (!joy) {
			ri.Con_Printf(PRINT_ALL, "Failed to open any joysticks\n");
			return false;
		}
	} else {
		ri.Con_Printf(PRINT_ALL, "No joysticks available\n");
		return false;
	}
	return true;
}
Esempio n. 24
0
static PyObject*
joy_get_button (PyObject* self, PyObject* args)
{
    int joy_id = PyJoystick_AsID (self);
    SDL_Joystick* joy = joystick_stickdata[joy_id];
    int _index, value;

    if (!PyArg_ParseTuple (args, "i", &_index)) {
        return NULL;
    }

    JOYSTICK_INIT_CHECK ();
    if (!joy) {
        return RAISE (PyExc_SDLError, "Joystick not initialized");
    }
    if (_index < 0 || _index >= SDL_JoystickNumButtons (joy)) {
        return RAISE (PyExc_SDLError, "Invalid joystick button");
    }

    value = SDL_JoystickGetButton (joy, _index);
#ifdef DEBUG
    /*printf("SDL_JoystickGetButton value:%d:\n", value);*/
#endif
    return PyInt_FromLong (value);
}
Esempio n. 25
0
int InputManager::getButtonCountByDevice(SDL_JoystickID id)
{
	if(id == DEVICE_KEYBOARD)
		return 120; //it's a lot, okay.
	else
		return SDL_JoystickNumButtons(mJoysticks[id]);
}
Esempio n. 26
0
static bool sdl_joypad_init(void)
{
   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
      return false;

   unsigned num_sticks = SDL_NumJoysticks();
   if (num_sticks > MAX_PLAYERS)
      num_sticks = MAX_PLAYERS;

   for (unsigned i = 0; i < num_sticks; i++)
   {
      struct sdl_joypad *pad = &g_pads[i];
      pad->joypad = SDL_JoystickOpen(i);
      if (!pad->joypad)
      {
         RARCH_ERR("Couldn't open SDL joystick #%u.\n", i);
         goto error;
      }

      RARCH_LOG("Opened Joystick: %s (#%u).\n", 
            SDL_JoystickName(i), i);

      pad->num_axes    = SDL_JoystickNumAxes(pad->joypad);
      pad->num_buttons = SDL_JoystickNumButtons(pad->joypad);
      pad->num_hats    = SDL_JoystickNumHats(pad->joypad);
      RARCH_LOG("Joypad has: %u axes, %u buttons, %u hats.\n",
            pad->num_axes, pad->num_buttons, pad->num_hats);
   }

   return true;

error:
   sdl_joypad_destroy();
   return false;
}
Esempio n. 27
0
 SdlJoystick(const char * _name, gsl::owner<SDL_Joystick *> js)
 :   name(_name),
     key_names(),
     joystick(js),
     axis_count(SDL_JoystickNumAxes(js)),
     button_count(SDL_JoystickNumButtons(js)),
     hat_count(SDL_JoystickNumHats(js))
 {
     std::string prefix = "Button ";
     for (int i = 0; i < this->button_count; ++i) {
         key_names.push_back(str(boost::format("Button %i") % i));
     }
     for (int i = 0; i < this->hat_count; ++i) {
         key_names.push_back(str(boost::format("Hat %i Up") % i));
         key_names.push_back(str(boost::format("Hat %i Down") % i));
         key_names.push_back(str(boost::format("Hat %i Left") % i));
         key_names.push_back(str(boost::format("Hat %i Right") % i));
     }
     for (int i = 0; i < this->axis_count; ++i) {
         key_names.push_back(str(boost::format("Axis %i Negative") % i));
         key_names.push_back(str(boost::format("Axis %i Positive") % i));
     }
     for (const auto & s : key_names) {
         LP3_LOG_VAR(s)
         LP3_LOG_DEBUG("   [%s]", s);
     }
 }
Esempio n. 28
0
 int buttons() const {
     int result = SDL_JoystickNumButtons(ptr.get());
     if(result < 0) {
         GUM_ERROR_HANDLER(result);
     }
     return result;
 }
Esempio n. 29
0
void print_joystick_info(int joy_idx, SDL_Joystick* joy, SDL_GameController* gamepad)
{
  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joy);
  char guid_str[1024];
  SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str));

  printf("Joystick Name:     '%s'\n", SDL_JoystickName(joy));
  printf("Joystick GUID:     %s\n", guid_str);
  printf("Joystick Number:   %2d\n", joy_idx);
  printf("Number of Axes:    %2d\n", SDL_JoystickNumAxes(joy));
  printf("Number of Buttons: %2d\n", SDL_JoystickNumButtons(joy));
  printf("Number of Hats:    %2d\n", SDL_JoystickNumHats(joy));
  printf("Number of Balls:   %2d\n", SDL_JoystickNumBalls(joy));
  printf("GameController:\n");
  if (!gamepad)
  {
    printf("  not a gamepad\n");
  }
  else
  {
    printf("  Name:    '%s'\n", SDL_GameControllerName(gamepad));
    printf("  Mapping: '%s'\n", SDL_GameControllerMappingForGUID(guid));
  }
  printf("\n");
}
Esempio n. 30
0
bool QJoystick::open(int stick)
{
  if ( isOpen() )
    close();

  if(SDL_NumJoysticks() == 0)
      return false;

  joystick = SDL_JoystickOpen(stick);
  if ( joystick ) {
    joystickNameSelected = stick + 1;
    numAxes = SDL_JoystickNumAxes(joystick);
    numButtons = SDL_JoystickNumButtons(joystick);
    numHats = SDL_JoystickNumHats(joystick);
    numTrackballs = SDL_JoystickNumBalls(joystick);
    qWarning("DEBUG: SDL joystick %d opened; axes = %d, buttons = %d, hats = %d, trackballs = %d", stick, numAxes, numButtons, numHats, numTrackballs);

    axis_old_values.clear();
    for(int i=0; i < numAxes; i++)
        axis_old_values.append(0);

    emit initJoystick(numAxes, numButtons, numHats, joystickNames.at(stick));
    joystickTimer.start(eventTimeout);
    return true;
  } else {
    qWarning("ERROR: couldn't open SDL joystick %d", stick);
    return false;
  }
}