void Joystick::pollUpdate()
{
	SDL_JoystickUpdate();
	// update the state of every button
	for (int i=0; i<buttonsSupported(); i++){
		bool buttonHeld = SDL_JoystickGetButton(joy_, i);

		// map hats to buttons 12 to 15
		bool hatHeld = false;
		if(i==12 && (SDL_JoystickGetHat(joy_, 0) & SDL_HAT_UP)
		|| i==13 && (SDL_JoystickGetHat(joy_, 0) & SDL_HAT_RIGHT)
		|| i==14 && (SDL_JoystickGetHat(joy_, 0) & SDL_HAT_DOWN)
		|| i==15 && (SDL_JoystickGetHat(joy_, 0) & SDL_HAT_LEFT))
			hatHeld = true;
		
		// buttonHeld will be true if either the button or a hat is held
		buttonHeld = buttonHeld|hatHeld;

		// set button pressed and held states
		if (buttonHeld){
			buttonPressedStates_[i] = !buttonHeldStates_[i];
			buttonHeldStates_[i] = true;
		}
		else
			buttonPressedStates_[i] = buttonHeldStates_[i] = false;
	}

	// axes
	for (int i=0; i<axesSupported(); i++)
		// map axis value to -1 to 1
		axisStates_[i] = float(SDL_JoystickGetAxis(joy_, i)) / 32768.0f; // max size of signed short int
}
Esempio n. 2
0
bool JoyData::getState(int key_enum) const
{
    if(index < 0)
        return false;
    switch(key_type[key_enum])
    {
    case POS_AXIS:
        return SDL_JoystickGetAxis(joysticks[index], key_index[key_enum]) > JOY_DEAD_ZONE;
    case NEG_AXIS:
        return SDL_JoystickGetAxis(joysticks[index], key_index[key_enum]) < -JOY_DEAD_ZONE;
    case BUTTON:
        return SDL_JoystickGetButton(joysticks[index], key_index[key_enum]);
    case HAT_UP:
        return (SDL_JoystickGetHat(joysticks[index], key_index[key_enum]) & SDL_HAT_UP);
    case HAT_RIGHT:
        return (SDL_JoystickGetHat(joysticks[index], key_index[key_enum]) & SDL_HAT_RIGHT);
    case HAT_DOWN:
        return (SDL_JoystickGetHat(joysticks[index], key_index[key_enum]) & SDL_HAT_DOWN);
    case HAT_LEFT:
        return (SDL_JoystickGetHat(joysticks[index], key_index[key_enum]) & SDL_HAT_LEFT);
        // Diagonals are ignored because they are combinations of the cardinals
    case HAT_UP_RIGHT:
    case HAT_DOWN_RIGHT:
    case HAT_DOWN_LEFT:
    case HAT_UP_LEFT:
    default:
        return false;
    }
}
Esempio n. 3
0
bool SdlJoystick::sample()
{
   SDL_JoystickUpdate();
   
   for (unsigned int i = 0 ; i < mButtons.size() ; ++i)
   {
      mButtons[i] = gadget::DigitalState::OFF;
      if (SDL_JoystickGetButton(mJoystick, i))
      {
        mButtons[i] = gadget::DigitalState::ON;
      }
      mButtons[i].setTime();
   }

   for (unsigned int i = 0 ; i < mAxes.size() ; ++i)
   {
      mAxes[i].setValue(SDL_JoystickGetAxis(mJoystick, i));
      mAxes[i].setTime();
   }

   for (unsigned int i = 0 ; i < mHats.size() ; ++i)
   {
      mHats[i].setValue(static_cast<HatState::State>(SDL_JoystickGetHat(mJoystick, i)));
      mHats[i].setTime();
   }

   addDigitalSample(mButtons);
   addAnalogSample(mAxes);
   addHatSample(mHats);

   return true;
}
Esempio n. 4
0
static int FindUncenteredHat(SDL_Joystick *joystick, int *axis_invert)
{
    int i, hatval;

    for (i = 0; i < SDL_JoystickNumHats(joystick); ++i)
    {
        hatval = SDL_JoystickGetHat(joystick, i);

        switch (hatval)
        {
            case SDL_HAT_LEFT:
            case SDL_HAT_RIGHT:
                *axis_invert = hatval != SDL_HAT_LEFT;
                return CREATE_HAT_AXIS(i, HAT_AXIS_HORIZONTAL);

            case SDL_HAT_UP:
            case SDL_HAT_DOWN:
                *axis_invert = hatval != SDL_HAT_UP;
                return CREATE_HAT_AXIS(i, HAT_AXIS_VERTICAL);

            // If the hat is centered, or is not pointing in a
            // definite direction, then ignore it. We don't accept
            // the hat being pointed to the upper-left for example,
            // because it's ambiguous.
            case SDL_HAT_CENTERED:
            default:
                break;
        }
    }

    // None found.
    return -1;
}
/*
 * Get the current state of a button on a controller
 */
Uint8
SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
{
    if ( !gamecontroller )
        return 0;

    if ( gamecontroller->mapping.buttons[button] >= 0 )
    {
        return ( SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttons[button] ) );
    }
    else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
    {
        Sint16 value;
        value = SDL_JoystickGetAxis( gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button] );
        if ( ABS(value) > 32768/2 )
            return 1;
        return 0;
    }
    else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
    {
        Uint8 value;
        value = SDL_JoystickGetHat( gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat );

        if ( value & gamecontroller->mapping.hatasbutton[button].mask )
            return 1;
        return 0;
    }

    return 0;
}
Esempio n. 6
0
	void ProcessInput()
	{
		BYTE buttonstate;

		for (int i = 0; i < NumAxes; ++i)
		{
			buttonstate = 0;

			Axes[i].Value = SDL_JoystickGetAxis(Device, i)/32767.0;
			Axes[i].Value = Joy_RemoveDeadZone(Axes[i].Value, Axes[i].DeadZone, &buttonstate);

			// Map button to axis
			// X and Y are handled differently so if we have 2 or more axes then we'll use that code instead.
			if (NumAxes == 1 || (i >= 2 && i < NUM_JOYAXISBUTTONS))
			{
				Joy_GenerateButtonEvents(Axes[i].ButtonValue, buttonstate, 2, KEY_JOYAXIS1PLUS + i*2);
				Axes[i].ButtonValue = buttonstate;
			}
		}

		if(NumAxes > 1)
		{
			buttonstate = Joy_XYAxesToButtons(Axes[0].Value, Axes[1].Value);
			Joy_GenerateButtonEvents(Axes[0].ButtonValue, buttonstate, 4, KEY_JOYAXIS1PLUS);
			Axes[0].ButtonValue = buttonstate;
		}

		// Map POV hats to buttons and axes.  Why axes?  Well apparently I have
		// a gamepad where the left control stick is a POV hat (instead of the
		// d-pad like you would expect, no that's pressure sensitive).  Also
		// KDE's joystick dialog maps them to axes as well.
		for (int i = 0; i < NumHats; ++i)
		{
			AxisInfo &x = Axes[NumAxes + i*2];
			AxisInfo &y = Axes[NumAxes + i*2 + 1];

			buttonstate = SDL_JoystickGetHat(Device, i);

			// If we're going to assume that we can pass SDL's value into
			// Joy_GenerateButtonEvents then we might as well assume the format here.
			if(buttonstate & 0x1) // Up
				y.Value = -1.0;
			else if(buttonstate & 0x4) // Down
				y.Value = 1.0;
			else
				y.Value = 0.0;
			if(buttonstate & 0x2) // Left
				x.Value = 1.0;
			else if(buttonstate & 0x8) // Right
				x.Value = -1.0;
			else
				x.Value = 0.0;

			if(i < 4)
			{
				Joy_GenerateButtonEvents(x.ButtonValue, buttonstate, 4, KEY_JOYPOV1_UP + i*4);
				x.ButtonValue = buttonstate;
			}
		}
	}
Esempio n. 7
0
static bool sdl_joykey_pressed(sdl_input_t *sdl, int port_num, uint16_t joykey)
{
   if (joykey == NO_BTN)
      return false;

   // Check hat.
   if (GET_HAT_DIR(joykey))
   {
      uint16_t hat = GET_HAT(joykey);
      if (hat >= sdl->num_hats[port_num])
         return false;

      Uint8 dir = SDL_JoystickGetHat(sdl->joysticks[port_num], hat);
      switch (GET_HAT_DIR(joykey))
      {
         case HAT_UP_MASK:
            return dir & SDL_HAT_UP;
         case HAT_DOWN_MASK:
            return dir & SDL_HAT_DOWN;
         case HAT_LEFT_MASK:
            return dir & SDL_HAT_LEFT;
         case HAT_RIGHT_MASK:
            return dir & SDL_HAT_RIGHT;
         default:
            return false;
      }
   }
   else // Check the button
   {
      if (joykey < sdl->num_buttons[port_num] && SDL_JoystickGetButton(sdl->joysticks[port_num], joykey))
         return true;

      return false;
   }
}
Esempio n. 8
0
static uint8_t sdl_pad_get_hat(sdl_joypad_t *pad, unsigned hat)
{
#ifdef HAVE_SDL2
   if (pad->controller)
      return sdl_pad_get_button(pad, hat);
#endif
   return SDL_JoystickGetHat(pad->joypad, hat);
}
Esempio n. 9
0
int setup_sdl_joysticks( void ) {

	int
		i, j, n;

	int
		number_of_joysticks_detected;

	number_of_joysticks_detected = SDL_NumJoysticks();

	if(number_of_joysticks_detected < 1)
		return -1;

	n = 0;

	for(i = 0; i < number_of_joysticks_detected; i++) {

		joystick_devices[ n ].input_device = SDL_JoystickOpen( i );

		if( joystick_devices[ n ].input_device ) {

			joystick_devices[ n ].number_of_buttons = SDL_JoystickNumButtons( joystick_devices[ n ].input_device );
			joystick_devices[ n ].number_of_axes = SDL_JoystickNumAxes( joystick_devices[ n ].input_device );
			joystick_devices[ n ].number_of_hats = SDL_JoystickNumHats( joystick_devices[ n ].input_device );
			
			for(j = 0; j < joystick_devices[ n ].number_of_buttons; j++) {
				joystick_devices[ n ].joystick_last_state.buttons[ j ] = SDL_JoystickGetButton( joystick_devices[ n ].input_device, j );
			}

			for(j = 0; j < joystick_devices[ n ].number_of_axes; j++) {
				joystick_devices[ n ].joystick_last_state.axes[ j ] = SDL_JoystickGetAxis( joystick_devices[ n ].input_device, j );
			}

			for(j = 0; j < joystick_devices[ n ].number_of_hats; j++) {
				joystick_devices[ n ].joystick_last_state.hats[ j ] = SDL_JoystickGetHat( joystick_devices[ n ].input_device, j );
			}
		
			strncpy( joystick_devices[ n ].device_name, SDL_JoystickName( i ), 1023 );
			
			strcpy( joystick_devices[ n ].device_product_name, "Unknown" );

#if DEBUG_MODULE

			debug_log ("Got joystick device %s (SDL device %d, Game device %d)", joystick_devices[ n ].device_name, i, n);

#endif

			n++;
		}
		else {
			debug_log("Unable to open joystick device %d, skipped.", i);
		}
	}

	number_of_joystick_devices = n;

	return 0;
}
Esempio n. 10
0
    float get_key_analog_state(const int key_code) override {
        LP3_ASSERT(key_code >= 0);
        LP3_ASSERT(key_code < get_key_count());
        if (key_code < this->button_count) {
            return 1 == SDL_JoystickGetButton(joystick, key_code) ? 1.0f : 0.0f;
        } else if (key_code < this->button_count + (this->hat_count * hat_sides)) {
            const int subtracted_kc = key_code - this->button_count;
            const int position_check = subtracted_kc % 4;
            const int hat_code = subtracted_kc / 4;
            Uint8 r = SDL_JoystickGetHat(joystick, hat_code);
            switch(position_check) {
                case 0:
                    if (SDL_HAT_UP == r) {
                        return 1.0f;
                    } else if (SDL_HAT_LEFTUP == r || SDL_HAT_RIGHTUP == r) {
                        return diag_slop;
                    }
                    return 0.0f;
                case 1:
                    if (SDL_HAT_DOWN == r) {
                        return 1.0f;
                    } else if (SDL_HAT_LEFTDOWN == r || SDL_HAT_RIGHTDOWN == r) {
                        return diag_slop;
                    }
                    return 0.0f;
                case 2:
                    if (SDL_HAT_LEFT == r) {
                        return 1.0f;
                    } else if (SDL_HAT_LEFTUP == r || SDL_HAT_LEFTDOWN == r) {
                        return diag_slop;
                    }
                    return 0.0f;
                case 3:
                    if (SDL_HAT_RIGHT == r) {
                        return 1.0f;
                    } else if (SDL_HAT_RIGHTUP == r || SDL_HAT_RIGHTDOWN == r) {
                        return diag_slop;
                    }
                    return 0.0f;
				default:
					LP3_THROW2(lp3::core::Exception, "Bad case!");
            }
        } else {
            const int subtracted_kc
                = key_code - (button_count + (hat_sides * hat_count));
            const int axis_code = subtracted_kc / axis_sides;

            const Sint16 r = SDL_JoystickGetAxis(joystick, axis_code);
            //LP3_LOG_DEBUG("MARIO, %i is %i", axis_code, r);
            const int position_check = subtracted_kc % axis_sides;
            if (0 == position_check) {
                return lp3::narrow<float>(n_dz_adjust(r)) / -32768.0f;
            } else {
                return lp3::narrow<float>(dz_adjust(r)) / 32767.0f;
            }
        }

    }
Esempio n. 11
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));
}
Esempio n. 12
0
//gethatjoystick(i,a) 							: Get the current state of a joystick hat
int gethatjoystick(int index,int hat)
{
    int ret;
    SDL_JoystickUpdate();
    if (SDLjoy[index])
	ret= SDL_JoystickGetHat(SDLjoy[index],hat);
    else
	ret=-1;
    return ret;
}
Esempio n. 13
0
JNIEXPORT jint JNICALL Java_at_wisch_joystick_Joystick_getPovValueYNative (JNIEnv *, jobject, jint joystickIndex, jint hatIndex){
	int value = SDL_JoystickGetHat(joysticks[joystickIndex], hatIndex);
	if (value & SDL_HAT_DOWN) {
		return at_wisch_joystick_Joystick_POV_AXIS_POSITIVE;
	} else if (value & SDL_HAT_UP) {
		return at_wisch_joystick_Joystick_POV_AXIS_NEGATIVE;
	} else {
		return at_wisch_joystick_Joystick_POV_AXIS_NEUTRAL;
	}
}
Esempio n. 14
0
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;
}
Esempio n. 15
0
Joystick::Hat Joystick::getHat(int hatindex) const
{
	Hat h = HAT_INVALID;

	if (!isConnected() || hatindex < 0 || hatindex >= getHatCount())
		return h;

	getConstant(SDL_JoystickGetHat(joyhandle, hatindex), h);

	return h;
}
Esempio n. 16
0
int libjoy_get_hat( int hat )
{
    if ( _selected_joystick >= 0 && _selected_joystick < _max_joys )
    {
        if ( hat >= 0 && hat <= SDL_JoystickNumHats( _joysticks[ _selected_joystick ] ) )
        {
            return SDL_JoystickGetHat( _joysticks[ _selected_joystick ], hat ) ;
        }
    }
    return 0 ;
}
Esempio n. 17
0
static mrb_value
mrb_sdl2_joystick_joystick_get_hat(mrb_state *mrb, mrb_value self)
{
  Uint8 result;
  mrb_int hat;
  SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self);
  mrb_get_args(mrb, "i", &hat);
  result = SDL_JoystickGetHat(joystick_p, hat);

  return mrb_fixnum_value(result);
}
Esempio n. 18
0
int libjoy_get_hat_specific( int joy, int hat )
{
    if ( joy >= 0 && joy < _max_joys )
    {
        if ( hat >= 0 && hat <= SDL_JoystickNumHats( _joysticks[ joy ] ) )
        {
            return SDL_JoystickGetHat( _joysticks[ joy ], hat ) ;
        }
    }
    return 0 ;
}
Esempio n. 19
0
void UpdateJoystickEvents()
{
	std::list<JoystickEvent_t*>::iterator i;
	event_t    event;

	if(!JoyEventList.size())
		return;

	i = JoyEventList.begin();
	while(i != JoyEventList.end())
	{
		if((*i)->Event.type == SDL_JOYHATMOTION)
		{
			// Hat position released
			if(!(SDL_JoystickGetHat(openedjoy, (*i)->Event.jhat.hat) & (*i)->Event.jhat.value))
				event.type = ev_keyup;
			// Hat button still held - Repeat at key repeat interval
			else if((SDL_GetTicks() - (*i)->RegTick >= SDL_DEFAULT_REPEAT_DELAY) && 
		            (SDL_GetTicks() - (*i)->LastTick >= SDL_DEFAULT_REPEAT_INTERVAL*2))
			{
				(*i)->LastTick = SDL_GetTicks();
				event.type = ev_keydown;
			}
			else
			{
				i++;
				continue;
			}

			event.data1 = event.data2 = event.data3 = 0;

			if((*i)->Event.jhat.value == SDL_HAT_UP)
				event.data1 = ((*i)->Event.jhat.hat * 4) + KEY_HAT1;
			else if((*i)->Event.jhat.value == SDL_HAT_RIGHT)
				event.data1 = ((*i)->Event.jhat.hat * 4) + KEY_HAT2;
			else if((*i)->Event.jhat.value == SDL_HAT_DOWN)
				event.data1 = ((*i)->Event.jhat.hat * 4) + KEY_HAT3;
			else if((*i)->Event.jhat.value == SDL_HAT_LEFT)
				event.data1 = ((*i)->Event.jhat.hat * 4) + KEY_HAT4;

			D_PostEvent(&event);

			if(event.type == ev_keyup)
			{
				// Delete the released event
				delete *i;
				i = JoyEventList.erase(i);
				continue;
			}
		}
		i++;
	}
}
Esempio n. 20
0
void I_PollDirectInputGamepad(void)
{
    if (gamepad && !noinput)
    {
        int     hat = SDL_JoystickGetHat(gamepad, 0);

        gamepadbuttons = (GAMEPAD_X * SDL_JoystickGetButton(gamepad, 0)
            | GAMEPAD_A * SDL_JoystickGetButton(gamepad, 1)
            | GAMEPAD_B * SDL_JoystickGetButton(gamepad, 2)
            | GAMEPAD_Y * SDL_JoystickGetButton(gamepad, 3)
            | GAMEPAD_LEFT_SHOULDER * SDL_JoystickGetButton(gamepad, 4)
            | GAMEPAD_RIGHT_SHOULDER * SDL_JoystickGetButton(gamepad, 5)
            | GAMEPAD_LEFT_TRIGGER * SDL_JoystickGetButton(gamepad, 6)
            | GAMEPAD_RIGHT_TRIGGER * SDL_JoystickGetButton(gamepad, 7)
            | GAMEPAD_BACK * SDL_JoystickGetButton(gamepad, 8)
            | GAMEPAD_START * SDL_JoystickGetButton(gamepad, 9)
            | GAMEPAD_LEFT_THUMB * SDL_JoystickGetButton(gamepad, 10)
            | GAMEPAD_RIGHT_THUMB * SDL_JoystickGetButton(gamepad, 11)
            | GAMEPAD_DPAD_UP * (hat & SDL_HAT_UP)
            | GAMEPAD_DPAD_RIGHT * (hat & SDL_HAT_RIGHT)
            | GAMEPAD_DPAD_DOWN * (hat & SDL_HAT_DOWN)
            | GAMEPAD_DPAD_LEFT * (hat & SDL_HAT_LEFT));

        if (gamepadbuttons)
        {
            idclev = false;
            idmus = false;
            if (idbehold)
            {
                message_clearable = true;
                HU_ClearMessages();
                idbehold = false;
            }
        }

        if (gp_sensitivity || menuactive || (gamepadbuttons & gamepadmenu))
        {
            event_t     ev;

            ev.type = ev_gamepad;
            D_PostEvent(&ev);

            gamepadthumbsfunc(0, 1, 2, 3);
        }
        else
        {
            gamepadbuttons = 0;
            gamepadthumbLX = 0;
            gamepadthumbLY = 0;
            gamepadthumbRX = 0;
        }
    }
}
Esempio n. 21
0
int PERSDLJoyInit(void) {
	int i, j;

	// does not need init if already done
	if ( SDL_PERCORE_INITIALIZED )
	{
		return 0;
	}

#if defined (_MSC_VER) && SDL_VERSION_ATLEAST(2,0,0)
   SDL_SetMainReady();
#endif

	// init joysticks
	if ( SDL_InitSubSystem( SDL_INIT_JOYSTICK ) == -1 )
	{
		return -1;
	}
	
	// ignore joysticks event in sdl event loop
	SDL_JoystickEventState( SDL_IGNORE );
	
	// open joysticks
	SDL_PERCORE_JOYSTICKS_INITIALIZED = SDL_NumJoysticks();
	SDL_PERCORE_JOYSTICKS = malloc(sizeof(PERSDLJoystick) * SDL_PERCORE_JOYSTICKS_INITIALIZED);
	for ( i = 0; i < SDL_PERCORE_JOYSTICKS_INITIALIZED; i++ )
	{
		SDL_Joystick* joy = SDL_JoystickOpen( i );
		
		SDL_JoystickUpdate();
		
		SDL_PERCORE_JOYSTICKS[ i ].mJoystick = joy;
		SDL_PERCORE_JOYSTICKS[ i ].mScanStatus = joy ? malloc(sizeof(s16) * SDL_JoystickNumAxes( joy )) : 0;
		SDL_PERCORE_JOYSTICKS[ i ].mHatStatus = joy ? malloc(sizeof(Uint8) * SDL_JoystickNumHats( joy )) : 0;
		
		if ( joy )
		{
			for ( j = 0; j < SDL_JoystickNumAxes( joy ); j++ )
			{
				SDL_PERCORE_JOYSTICKS[ i ].mScanStatus[ j ] = SDL_JoystickGetAxis( joy, j );
			}
			for ( j = 0; j < SDL_JoystickNumHats( joy ); j++ )
			{
				SDL_PERCORE_JOYSTICKS[ i ].mHatStatus[ j ] = SDL_JoystickGetHat( joy, j );
			}
		}
	}
	
	// success
	SDL_PERCORE_INITIALIZED = 1;
	return 0;
}
Esempio n. 22
0
static int l_joystick_getHat(lua_State *L) {
  Joystick *self = luaL_checkudata(L, 1, CLASS_NAME);
  int idx = luaL_checknumber(L, 2);
  int value = SDL_JoystickGetHat(self->joystick,idx);

  lua_newtable(L);
  luax_setfield_bool(L, "up", value & SDL_HAT_UP);
  luax_setfield_bool(L, "down", value & SDL_HAT_DOWN);
  luax_setfield_bool(L, "left", value & SDL_HAT_LEFT);
  luax_setfield_bool(L, "right", value & SDL_HAT_RIGHT);

  return 1;
}
Esempio n. 23
0
	Joystick::Hat Joystick::getHat(int index, int hat)
	{
		Hat h = HAT_INVALID;

		if (!verifyJoystick(index))
			return h;

		if (hat >= getNumHats(index))
			return h;

		hats.find(SDL_JoystickGetHat(joysticks[index], hat), h);

		return h;
	}
Esempio n. 24
0
void es_joystick_getHat(sdl_data *sd, int len,char *buff)
{
   int sendlen;
   char *bp, *start;
   SDL_Joystick *joy;
   int state;
   Uint8 hat;
   bp = buff;
   POPGLPTR(joy, bp);
   hat = get8(bp);
   bp = start = sdl_get_temp_buff(sd, 1);
   state = SDL_JoystickGetHat(joy, hat);
   put8(bp,state);
   sendlen = bp - start;
   sdl_send(sd, sendlen);
}
Esempio n. 25
0
/* 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;
}
Esempio n. 26
0
/**
 * \brief Returns the direction of a joypad hat.
 * \param hat Index of a joypad hat.
 * \return The direction of that hat (0 to 7, or -1 if centered).
 */
int InputEvent::get_joypad_hat_direction(int hat) {

  if (joystick == nullptr) {
    return -1;
  }

  int state = SDL_JoystickGetHat(joystick, hat);
  int result = -1;

  switch (state) {

    case SDL_HAT_RIGHT:
      result = 0;
      break;

    case SDL_HAT_RIGHTUP:
      result = 1;
      break;

    case SDL_HAT_UP:
      result = 2;
      break;

    case SDL_HAT_LEFTUP:
      result = 3;
      break;

    case SDL_HAT_LEFT:
      result = 4;
      break;

    case SDL_HAT_LEFTDOWN:
      result = 5;
      break;

    case SDL_HAT_DOWN:
      result = 6;
      break;

    case SDL_HAT_RIGHTDOWN:
      result = 7;
      break;

  }

  return result;
}
Esempio n. 27
0
        bool pollHat(const HAT &h) {
#ifdef CURRENT_PLATFORM
            if(opened == false) return false;
            return SDL_JoystickGetHat(joy, static_cast<int>(h));
#else
            ioPadGetInfo (&padinfo);
            if(padinfo.status[cid]) {
                ioPadGetData (cid, &paddata);
                
                if(paddata.BTN_LEFT && h == HAT_LEFT)  return true;
                if(paddata.BTN_RIGHT && h == HAT_RIGHT)  return true;
                if(paddata.BTN_UP && h == HAT_UP)  return true;
                if(paddata.BTN_DOWN && h == HAT_DOWN)  return true;
            }
#endif
            return false;
        }
Esempio n. 28
0
void JOYSTICKGetHat(int nlhs, mxArray *plhs[], int nrhs, CONSTmxArray *prhs[])
{

	ProjectTable *joystickTable=GetProjectTable();
	int  numSticks, numHats, fetchedHat;
	CONSTmxArray *numArg;
	double stickNum;
	SDL_Joystick *pStick;
	Uint8 hatVal; 


	if(joystickTable->giveHelp){GiveHelp(useGetHat,synopsisGetHat);return;}
	numArg = joystickTable->joystickNumberArgument; 
	if(numArg == NULL || nlhs > 1 || nrhs > 1 || nrhs < 1 || !mxIsDouble(numArg) || (mxGetM(numArg) * mxGetN(numArg) != 1 ||
		!mxIsDouble(prhs[0]) || (mxGetM(prhs[0]) * mxGetN(prhs[0])) != 1)){
		GiveUsageExit(useGetHat);
	}
	numSticks = SDL_NumJoysticks();
	stickNum = mxGetPr(numArg)[0];
	if(stickNum > numSticks)
		PrintfExit("The joystick number %d passed to JOYSTICK 'GetHat' exceeds the number of joysticks, %d",stickNum,numSticks);
	if(stickNum < 1)
		PrintfExit("The joystick number passed to JOYSTICK 'GetHat' must be greater than 0");
	pStick = GetJoystickObjFromNum((int)stickNum-1);
	if(pStick == NULL)
		PrintfExit("JOYSTICK 'GetHat' can not open joystick number %d",stickNum);
	numHats = SDL_JoystickNumHats(pStick);
	fetchedHat = (int)mxGetPr(prhs[0])[0];
	if(fetchedHat > numHats || fetchedHat < 0)
		PrintfExit("The axis number %d passed to JOYSTICK 'GetHat' is oustide the allowable range",fetchedHat);
	SDL_JoystickUpdate();
	hatVal = SDL_JoystickGetHat(pStick, fetchedHat-1);
	plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);	
	switch(hatVal){
		case SDL_HAT_CENTERED:	mxGetPr(plhs[0])[0] = 0; break; 
		case SDL_HAT_UP:		mxGetPr(plhs[0])[0] = 1; break; 
		case SDL_HAT_RIGHT:		mxGetPr(plhs[0])[0] = 2; break; 
		case SDL_HAT_DOWN:		mxGetPr(plhs[0])[0] = 3; break;
		case SDL_HAT_LEFT:		mxGetPr(plhs[0])[0] = 4; break; 
		case SDL_HAT_RIGHTUP:	mxGetPr(plhs[0])[0] = 5; break; 
		case SDL_HAT_RIGHTDOWN: mxGetPr(plhs[0])[0] = 6; break; 
		case SDL_HAT_LEFTUP:	mxGetPr(plhs[0])[0] = 7; break; 
		case SDL_HAT_LEFTDOWN:	mxGetPr(plhs[0])[0] = 8; break; 
	}
	
}
Esempio n. 29
0
void I_PollDirectInputGamepad(void)
{
    if (gamepad)
    {
        event_t         ev;
        int             hat = SDL_JoystickGetHat(gamepad, 0);

        if (gamepadsensitivity || menuactive)
            gamepadthumbsfunc(0, 1, 2, 3);

        gamepadbuttons = (GAMEPAD_X * SDL_JoystickGetButton(gamepad, 0)
            | GAMEPAD_A * SDL_JoystickGetButton(gamepad, 1)
            | GAMEPAD_B * SDL_JoystickGetButton(gamepad, 2)
            | GAMEPAD_Y * SDL_JoystickGetButton(gamepad, 3)
            | GAMEPAD_LEFT_SHOULDER * SDL_JoystickGetButton(gamepad, 4)
            | GAMEPAD_RIGHT_SHOULDER * SDL_JoystickGetButton(gamepad, 5)
            | GAMEPAD_LEFT_TRIGGER * SDL_JoystickGetButton(gamepad, 6)
            | GAMEPAD_RIGHT_TRIGGER * SDL_JoystickGetButton(gamepad, 7)
            | GAMEPAD_BACK * SDL_JoystickGetButton(gamepad, 8)
            | GAMEPAD_START * SDL_JoystickGetButton(gamepad, 9)
            | GAMEPAD_LEFT_THUMB * SDL_JoystickGetButton(gamepad, 10)
            | GAMEPAD_RIGHT_THUMB * SDL_JoystickGetButton(gamepad, 11)
            | GAMEPAD_DPAD_UP * (hat & SDL_HAT_UP)
            | GAMEPAD_DPAD_RIGHT * (hat & SDL_HAT_RIGHT)
            | GAMEPAD_DPAD_DOWN * (hat & SDL_HAT_DOWN)
            | GAMEPAD_DPAD_LEFT * (hat & SDL_HAT_LEFT));

        if (gamepadbuttons)
        {
            idclev = false;
            idmus = false;
            if (idbehold)
            {
                HU_clearMessages();
                idbehold = false;
            }
        }

        ev.type = ev_gamepad;
        ev.data1 = gamepadbuttons;
        D_PostEvent(&ev);
    }
}
Esempio n. 30
0
File: Input.cpp Progetto: 456z/gosu
 void pollJoystick(SDL_Joystick *joystick, GamepadBuffer& gamepad)
 {
     int axes = SDL_JoystickNumAxes(joystick),
         hats = SDL_JoystickNumHats(joystick),
         buttons = std::min<int>(gpNumPerGamepad - 4, SDL_JoystickNumButtons(joystick));
     
     for (int axis = 0; axis < axes; ++axis) {
         Sint16 value = SDL_JoystickGetAxis(joystick, axis);
         
         if (value < -DEAD_ZONE) {
             if (axis % 2 == 0)
                 gamepad[gpLeft - gpRangeBegin] = true;
             else
                 gamepad[gpUp - gpRangeBegin] = true;
         }
         else if (value > +DEAD_ZONE) {
             if (axis % 2 == 0)
                 gamepad[gpRight - gpRangeBegin] = true;
             else
                 gamepad[gpDown - gpRangeBegin] = true;
         }
     }
     
     for (int hat = 0; hat < hats; ++hat) {
         Uint8 value = SDL_JoystickGetHat(joystick, hat);
         
         if (value & SDL_HAT_LEFT)
             gamepad[gpLeft - gpRangeBegin] = true;
         if (value & SDL_HAT_RIGHT)
             gamepad[gpRight - gpRangeBegin] = true;
         if (value & SDL_HAT_UP)
             gamepad[gpUp - gpRangeBegin] = true;
         if (value & SDL_HAT_DOWN)
             gamepad[gpDown - gpRangeBegin] = true;
     }
     
     for (int button = 0; button < buttons; ++button) {
         if (SDL_JoystickGetButton(joystick, button)) {
             gamepad[gpButton0 + button - gpRangeBegin] = true;
         }
     }
 }