static uint8_t sdl_pad_get_button(sdl_joypad_t *pad, unsigned button)
{
#ifdef HAVE_SDL2
   /* TODO: see if a LUT like xinput_joypad.c's button_index_to_bitmap_code is needed. */
   if (pad->controller)
      return SDL_GameControllerGetButton(pad->controller, (SDL_GameControllerButton)button);
#endif
   return SDL_JoystickGetButton(pad->joypad, button);
}
Exemple #2
0
void JoystickInfo::SaveState()
{
    for (int i = 0; i < numbuttons; ++i)
        SetButtonState(i, SDL_JoystickGetButton(joy, i));
    for (int i = 0; i < numaxes; ++i)
        SetAxisState(i, SDL_JoystickGetAxis(joy, i));
    for (int i = 0; i < numhats; ++i)
        SetHatState(i, SDL_JoystickGetHat(joy, i));
}
Exemple #3
0
int
GfctrlSDL2JoyGetCurrentStates(tCtrlJoyInfo *joyInfo)
{
   int			ind;
   int			i,j;
   unsigned int	b;
   unsigned int	mask;
   if (gfctrlJoyPresent == GFCTRL_JOY_PRESENT)
   {
      // Update all the joysticks
      SDL_JoystickUpdate();
      for (ind = 0; ind < gfctrlJoyPresent; ind++) 
      {
         if (Joysticks[ind])
         {
            j = SDL_JoystickNumAxes(Joysticks[ind]);
            if (j > GFCTRL_JOY_MAX_AXES) j = GFCTRL_JOY_MAX_AXES;

            for (i=0; i < j;i++)
               joyInfo->ax[GFCTRL_JOY_MAX_AXES * ind + i] = ((float) SDL_JoystickGetAxis(Joysticks[ind],i)) / 32768;

            b = 0;
            for (i=0; i < GFCTRL_JOY_MAX_BUTTONS;i++) 
            {
               mask = (unsigned int)SDL_JoystickGetButton(Joysticks[ind], i);
               b |= (mask << i);
            }

            /* Joystick buttons */
            for (i = 0, mask = 1; i < GFCTRL_JOY_MAX_BUTTONS; i++, mask *= 2) 
            {
               if (((b & mask) != 0) && ((joyInfo->oldb[ind] & mask) == 0)) {
                  joyInfo->edgeup[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 1;
               } else {
                  joyInfo->edgeup[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 0;
               }
               if (((b & mask) == 0) && ((joyInfo->oldb[ind] & mask) != 0)) {
                  joyInfo->edgedn[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 1;
               } else {
                  joyInfo->edgedn[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 0;
               }
               if ((b & mask) != 0) {
                  joyInfo->levelup[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 1;
               } else {
                  joyInfo->levelup[i + GFCTRL_JOY_MAX_BUTTONS * ind] = 0;
               }
            }
            joyInfo->oldb[ind] = b;
         }
      }
   }
   else
   {
      return -1;
   }
   return 0;
}
Exemple #4
0
//getbuttonjoystick(i,a)						: Get the current state of a given button on a given joystick
int getbuttonjoystick(int index,int button)
{
    int ret;
    SDL_JoystickUpdate();
    if (SDLjoy[index])
	ret= SDL_JoystickGetButton(SDLjoy[index],button);
    else
	ret=-1;
    return ret;
}
Exemple #5
0
void cJoystickSDL::Update() {
	if ( NULL != mJoystick ) {
		ClearStates();

		for ( Int32 i = 0; i < mButtons; i++ ) {
			UpdateButton( i, 0 != SDL_JoystickGetButton( mJoystick, i ) );
		}

	}
}
Exemple #6
0
bool SCA_Joystick::aButtonReleaseIsPositive(int button)
{
#ifndef WITH_SDL
	return false;
#else
	bool result;
	SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
	return result;
#endif
}
bool JoystickController::bindJoystickKey(SDL_Joystick *joy, KM_Key &k)
{
    int val=0, dx=0, dy=0;
    //SDL_PumpEvents();
    SDL_JoystickUpdate();

    for(int i=0; i<SDL_JoystickNumBalls(joy);i++)
    {
        dx=0; dy=0;
        SDL_JoystickGetBall(joy, i, &dx, &dy);
        if(dx!=0)
        {
            k.val=dx;
            k.id=i;
            k.type=(int)KeyMapJoyCtrls::JoyBallX;
            return true;
        }else if(dy!=0)
        {
            k.val=dy;
            k.id=i;
            k.type=(int)KeyMapJoyCtrls::JoyBallY;
            return true;
        }
    }

    for(int i=0; i<SDL_JoystickNumHats(joy);i++)
    {
        val=0;
        val=SDL_JoystickGetHat(joy, i);
        if(val!=0)
        {
            k.val=val;
            k.id=i;
            k.type=(int)KeyMapJoyCtrls::JoyHat;
            return true;
        }
    }
    for(int i=0; i<SDL_JoystickNumButtons(joy);i++)
    {
        val=0;
        val=SDL_JoystickGetButton(joy, i);
        if(val==1)
        {
            k.val=val;
            k.id=i;
            k.type=(int)KeyMapJoyCtrls::JoyButton;
            return true;
        }
    }

    k.val=0;
    k.id=0;
    k.type=(int)KeyMapJoyCtrls::NoControl;
    return false;
}
Exemple #8
0
bool SCA_Joystick::aAnyButtonPressIsPositive(void)
{
#ifdef WITH_SDL
	/* this is needed for the "all events" option
	 * so we know if there are no buttons pressed */
	for (int i=0; i<m_buttonmax; i++)
		if (SDL_JoystickGetButton(m_private->m_joystick, i))
			return true;
#endif
	return false;
}
Exemple #9
0
Sint16 get_joystick_state (Sint16 *mx, Sint16 *my, Sint16 *button, Sint16 check_button)
{
	SDL_PumpEvents ();
	
	*mx = SDL_JoystickGetAxis (joystick, 0);
	*my = SDL_JoystickGetAxis (joystick, 1);
	
	*button = SDL_JoystickGetButton (joystick, check_button);

	return (0);
}
Exemple #10
0
static mrb_value
mrb_sdl2_joystick_joystick_get_button(mrb_state *mrb, mrb_value self)
{
  Uint8 result;
  mrb_int button;
  SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self);
  mrb_get_args(mrb, "i", &button);
  result = SDL_JoystickGetButton(joystick_p, button);

  return (0 == result) ? mrb_false_value() : mrb_true_value();
}
Exemple #11
0
int IN_JoyButtons()
{
    if(!Joystick) return 0;

    SDL_JoystickUpdate();

    int res = 0;
    for(int i = 0; i < JoyNumButtons && i < 32; i++)
        res |= SDL_JoystickGetButton(Joystick, i) << i;
    return res;
}
Exemple #12
0
Controller::Button	Controller::Peripheral::retJoyButton()
{
  SDL_Joystick * joy = getJoy();

  for (register int i = static_cast<int>(BUTTON_MAX); i >= 0 ; i--)
    {
      if (SDL_JoystickGetButton(joy, static_cast<SDL_GameControllerButton>(i)))
	return (static_cast<Button>(i));
    }
  return (BUTTON_INVALID);
}
Exemple #13
0
/** Updates the state info in CApplication and on change, creates SDL events and pushes them to SDL event queue.
    This way, the events get handled properly in the main event loop and besides, SDL_PushEvent() ensures thread-safety. */
void CApplication::UpdateJoystick()
{
    if (! m_joystickEnabled)
        return;

    SDL_JoystickUpdate();

    for (int axis = 0; axis < static_cast<int>( m_joyAxeState.size() ); ++axis)
    {
        int newValue = SDL_JoystickGetAxis(m_private->joystick, axis);

        if (m_joyAxeState[axis] != newValue)
        {
            m_joyAxeState[axis] = newValue;

            SDL_Event joyAxisEvent;

            joyAxisEvent.jaxis.type = SDL_JOYAXISMOTION;
            joyAxisEvent.jaxis.which = 0;
            joyAxisEvent.jaxis.axis = axis;
            joyAxisEvent.jaxis.value = newValue;

            SDL_PushEvent(&joyAxisEvent);
        }
    }

    for (int button = 0; button < static_cast<int>( m_joyButtonState.size() ); ++button)
    {
        bool newValue = SDL_JoystickGetButton(m_private->joystick, button) == 1;

        if (m_joyButtonState[button] != newValue)
        {
            m_joyButtonState[button] = newValue;

            SDL_Event joyButtonEvent;

            if (newValue)
            {
                joyButtonEvent.jbutton.type = SDL_JOYBUTTONDOWN;
                joyButtonEvent.jbutton.state = SDL_PRESSED;
            }
            else
            {
                joyButtonEvent.jbutton.type = SDL_JOYBUTTONUP;
                joyButtonEvent.jbutton.state = SDL_RELEASED;
            }
            joyButtonEvent.jbutton.which = 0;
            joyButtonEvent.jbutton.button = button;

            SDL_PushEvent(&joyButtonEvent);
        }
    }
}
Exemple #14
0
void ndof_update(NDOF_Device *in_dev)
{
    int i;

    LinJoystickPrivate *priv = (LinJoystickPrivate *) in_dev->private_data;
    assert(priv != NULL);

    if(priv->USE_SDL)
    {
        SDL_JoystickUpdate();
        SDL_Joystick *j = priv->j;

        for(i = 0; i < in_dev->axes_count; i++)
        {
            in_dev->axes[i] = (int) (SDL_JoystickGetAxis(j, i));
        }

        for(i = 0; i < in_dev->btn_count; i++)
        {
            in_dev->buttons[i] = SDL_JoystickGetButton(j, i);
        }
    } else {
        // update SpaceNavigator

        struct input_event ev;

        while(read(priv->fd, &ev, sizeof(struct input_event)) > 0)
        {
            switch (ev.type)
            {
                case EV_KEY:
                    // printf("Key %d pressed %d.\n", ev.code, ev.value);
                    priv->buttons[ev.code & 0xff] = ev.value;
                    break;

                case EV_REL:
                case EV_ABS: // 2.6.35 and up, maybe earlier kernels too send EV_ABS instead of EV_REL
                    // printf("%d %g\n", ev.code, ev.value);

                    // clean up small values
                    priv->axes[ev.code] = abs(ev.value) > SPACE_NAVIG_THRESHOLD ? ev.value : 0;
                    break;

                default:
                    break;
            }
        }

        memcpy(in_dev->axes, priv->axes, in_dev->axes_count * sizeof(long int));
        memcpy(in_dev->buttons, priv->buttons, in_dev->btn_count * sizeof(long int));
    }
}
Exemple #15
0
static void stroke_timer(int id, float dt)
{
    float g[3] = { 0.f, 0.f, 0.f };

    //senquack - allowed more flexible aim-speed control
#ifdef GCWZERO
    float rot_dir = stroke_rotate;
    float rot_speed = 0.0f;
    if (l_pressed && r_pressed) {
        rot_dir = 0.0f;
//        k = 0.0f;
    } else if (l_pressed) {
        rot_dir = -1.0f;
        rot_speed = 24.0f;
    } else if (r_pressed) {
        rot_dir = 1.0f;
        rot_speed = 24.0f;
    } else {
        if (fast_aim) {
            rot_speed = 24.0f;
        } else {
            rot_speed = 2.0f;
        }
    }
    game_set_rot(rot_dir * rot_speed);

    if (fast_aim) {
        game_set_mag(stroke_mag * 24.0f);
    } else {
        game_set_mag(stroke_mag * 6.0f);
    }

#else
    float k;

    if (SDL_GetModState() & KMOD_SHIFT ||
        (joystick && SDL_JoystickGetButton(joystick,
                                           config_get_d(CONFIG_JOYSTICK_BUTTON_X)))) {
        k = 0.25f;
    }
    else
    {
        k = 1.0f;
    }

    game_set_rot(stroke_rotate * k);
    game_set_mag(stroke_mag * k);
#endif //GCWZERO

    game_update_view(dt);
    game_step(g, dt);
}
Exemple #16
0
void JoystickReader::OnTimer(void* userdata){
	JoystickReader* self = (JoystickReader*)userdata;
	SDL_JoystickUpdate();
	const int numButtons = SDL_JoystickNumButtons(self->js);
	for (int i = 0; i < numButtons; i++) {
		if (SDL_JoystickGetButton(self->js, i)){
			if (self->callbackFunc)
				if (self->callbackFunc(i, self->callbackUserdata))
					return;
		}
	}
	Fl::repeat_timeout(0.1, OnTimer, self);
}
Exemple #17
0
void	joystick_event( void )
{
  int j;
  T_JOY	*joy = &joy_info;

  int	now = 0, chg;
  Sint16 x, y;
  Uint8 button;

  if( enable_joystick ){

    SDL_JoystickUpdate();

    if( joy->dev ){

      /* Uint8 pad = SDL_JoystickGetHat( joy->dev, 
				      SDL_HAT_UP   | SDL_HAT_RIGHT | 
				      SDL_HAT_DOWN | SDL_HAT_LEFT ); */

      x = SDL_JoystickGetAxis( joy->dev, 0 );
      y = SDL_JoystickGetAxis( joy->dev, 1 );

      if     ( x < -0x4000 ) now |= AXIS_L;
      else if( x >  0x4000 ) now |= AXIS_R;

      if     ( y < -0x4000 ) now |= AXIS_U;
      else if( y >  0x4000 ) now |= AXIS_D;

      chg = joy->axis ^ now;

      if( chg & AXIS_L ) pc88_pad( KEY88_PAD_LEFT,  (now & AXIS_L) );
      if( chg & AXIS_R ) pc88_pad( KEY88_PAD_RIGHT, (now & AXIS_R) );
      if( chg & AXIS_U ) pc88_pad( KEY88_PAD_UP,    (now & AXIS_U) );
      if( chg & AXIS_D ) pc88_pad( KEY88_PAD_DOWN,  (now & AXIS_D) );

      joy->axis = now;

      for( j=0; j<joy->nr_button; j++ ){

	button = SDL_JoystickGetButton( joy->dev, j );

	if( joy->button[j] != button ){
	  pc88_pad( KEY88_PAD_A + j, ( button ) );
	  joy->button[j] = button;
	}

      }

    }
  }
}
Exemple #18
0
void PlatformJoyCommands(int *axis_vals, int *axis_map) {
  int i;
  int key_index;
  in_state_t *in_state = getState();
  if (joy) {
    for (i=0 ; i < joy_numbuttons ; i++) {
      if ( SDL_JoystickGetButton(joy, i) && joy_oldbuttonstate!=i ) {
	key_index = (i < 4) ? K_JOY1 : K_AUX1;
	in_state->Key_Event_fp (key_index + i, true);
	joy_oldbuttonstate = i;
      }
      
      if ( !SDL_JoystickGetButton(joy, i) && joy_oldbuttonstate!=i ) {
	key_index = (i < 4) ? K_JOY1 : K_AUX1;
	in_state->Key_Event_fp (key_index + i, false);
	joy_oldbuttonstate = i;
      }
    }
    for (i=0;i<6;i++) {
      axis_vals[axis_map[i]] = (int)SDL_JoystickGetAxis(joy, i);
    }
  }
}
Exemple #19
0
int	Controller::Peripheral::checkJoyButton()
{
  SDL_Joystick * joy = getJoy();

  for (register int i = static_cast<int>(BUTTON_MAX); i >= 0 ; i--)
    {
      if (SDL_JoystickGetButton(joy, static_cast<SDL_GameControllerButton>(i)))
	{
	  _but = static_cast<Button>(i);
	  break ;
	}
    }
  return (-1);
}
Exemple #20
0
//bjoy									: return joystick buttons pressed in boolean expression
int bjoy(int index)
{
    int ret,i,a;
    if (index>=SDL_NumJoysticks())return emulate_bjoy(index);
    ret=0;
    a=1;
    SDL_JoystickUpdate();
    for(i=0;i<SDL_JoystickNumButtons(SDLjoy[index]);i++){
	if(SDL_JoystickGetButton(SDLjoy[index],i))ret+=a;
	a*=2;
    }
    if (autotimer()!=0)return -1;
    return ret;
}
Exemple #21
0
	bool Joystick::isDown(int index, int * buttonlist)
	{
		if (!verifyJoystick(index))
			return false;

		int num = getNumButtons(index);

		for (int button = *buttonlist; button != -1; button = *(++buttonlist))
		{
			if (button >= 0 && button < num && SDL_JoystickGetButton(joysticks[index], button) == 1)
				return true;
		}

		return false;
	}
Exemple #22
0
void Joystick::update() {
    SDL_JoystickUpdate();

    for (int i = 0; i < numButtons; i++) {
        ButtonState state;
        state.lastState = buttons.at(i).currentState;
        state.currentState = SDL_JoystickGetButton(sdlJoystick, i);

        buttons[i] = state;
    }

    for (int i = 0; i < numAxes; i++) {
        qint16 axisValue = SDL_JoystickGetAxis(sdlJoystick, i);
        axes[i] = axisValue;
    }
}
Exemple #23
0
void es_joystick_getButton(sdl_data *sd, int len,char *buff)
{
   int sendlen;
   char *bp, *start;
   SDL_Joystick *joy;
   int state;
   Uint8 button;
   bp = buff;
   POPGLPTR(joy, bp);
   button = get8(bp);
   bp = start = sdl_get_temp_buff(sd, 1);
   state = SDL_JoystickGetButton(joy, button);
   put8(bp,state);
   sendlen = bp - start;
   sdl_send(sd, sendlen);
}
/* gives back value 0..joystick_analog_max indicating that one of the assigned
 * buttons has been pressed or that one of the assigned axes/hats has been moved
 * in the assigned direction
 */
int check_assigned( SDL_Joystick *joystick_handle, const Joystick_assignment assignment[2] )
{
	int result = 0;
	
	for (int i = 0; i < 2; i++)
	{
		int temp = 0;
		
		switch (assignment[i].type)
		{
		case NONE:
			continue;
			
		case AXIS:
			temp = SDL_JoystickGetAxis(joystick_handle, assignment[i].num);
			
			if (assignment[i].negative_axis)
				temp = -temp;
			break;
		
		case BUTTON:
			temp = SDL_JoystickGetButton(joystick_handle, assignment[i].num) == 1 ? joystick_analog_max : 0;
			break;
		
		case HAT:
			temp = SDL_JoystickGetHat(joystick_handle, assignment[i].num);
			
			if (assignment[i].x_axis)
				temp &= SDL_HAT_LEFT | SDL_HAT_RIGHT;
			else
				temp &= SDL_HAT_UP | SDL_HAT_DOWN;
			
			if (assignment[i].negative_axis)
				temp &= SDL_HAT_LEFT | SDL_HAT_UP;
			else
				temp &= SDL_HAT_RIGHT | SDL_HAT_DOWN;
			
			temp = temp ? joystick_analog_max : 0;
			break;
		}
		
		if (temp > result)
			result = temp;
	}
	
	return result;
}
Exemple #25
0
static int GetButtonState(void)
{
    int i;
    int result;

    result = 0;

    for (i=0; i<SDL_JoystickNumButtons(joystick); ++i) 
    {
        if (SDL_JoystickGetButton(joystick, i))
        {
            result |= 1 << i;
        }
    }

    return result;
}
Exemple #26
0
	bool Joystick::isButtonPressed(int8_t button) const {
		if (button < 0 || !isConnected()) {
			return false;
		}
		if (!isController()) {
			if (SDL_JoystickGetButton(m_joystickHandle, button) == 1) {
				return true;
			}
			return false;
		}

		SDL_GameControllerButton sdlButton = static_cast<SDL_GameControllerButton>(button);
		if (SDL_GameControllerGetButton(m_controllerHandle, sdlButton) == 1) {
			return true;
		}
		return false;
	}
void QJoystick::getdata()
{
    axis.clear();
    buttons.clear();

    SDL_Event event;
    SDL_PollEvent(&event);

    for(int i=0;i<SDL_JoystickNumAxes(m_joystick);i++)
    {
        axis.append(SDL_JoystickGetAxis(m_joystick,i));
    }

    for(int i=0;i<SDL_JoystickNumButtons(m_joystick);i++)
    {
        buttons.append(SDL_JoystickGetButton(m_joystick,i));
    }
}
Exemple #28
0
bool Joystick::isDown(const std::vector<int> &buttonlist) const
{
	if (!isConnected())
		return false;

	int numbuttons = getButtonCount();

	for (int button : buttonlist)
	{
		if (button < 0 || button >= numbuttons)
			continue;

		if (SDL_JoystickGetButton(joyhandle, button) == 1)
			return true;
	}

	return false;
}
void JoystickControl::JoyState(){
	Sint16 x_mov = SDL_JoystickGetAxis(joystick,0);
	Sint16 y_mov = SDL_JoystickGetAxis(joystick,1);
	int sentido_personaje = personaje->getSentidoDeMovimiento();

	//frena si no se esta manteniendo para un lado el analogico
	if ( ( sentido_personaje > 0 && x_mov < JOYSTICK_DEAD_ZONE ) || (sentido_personaje < 0 && x_mov > -JOYSTICK_DEAD_ZONE)  )
		personaje->Frenar();

	//se levanta si no esta manteniendo para abajo el analogico
	if (!(y_mov > JOYSTICK_DEAD_ZONE))
		personaje->Levantarse();

	//se deja de cubrir si no esta apretado
	if (SDL_JoystickGetButton(joystick,JOY_R1) == BUTTON_UNPRESSED)
		personaje->dejarDeCubrirse();

}
Exemple #30
0
static void stroke_timer(int id, float dt)
{
    float g[3] = { 0.f, 0.f, 0.f };

    float k;

    if (SDL_GetModState() & KMOD_SHIFT ||
        (joystick && SDL_JoystickGetButton(joystick,
                                           config_get_d(CONFIG_JOYSTICK_BUTTON_B))))
        k = 0.25;
    else
        k = 1.0;

    game_set_rot(stroke_rotate * k);
    game_set_mag(stroke_mag * k);

    game_update_view(dt);
    game_step(g, dt);
}