Ejemplo n.º 1
0
static PyObject*
joy_get_ball (PyObject* self, PyObject* args)
{
    int joy_id = PyJoystick_AsID (self);
    SDL_Joystick* joy = joystick_stickdata[joy_id];
    int _index, dx, dy;
    Uint32 value;

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

    JOYSTICK_INIT_CHECK ();
    if (!joy) {
        return RAISE (PyExc_SDLError, "Joystick not initialized");
    }
    value = SDL_JoystickNumBalls (joy);
#ifdef DEBUG
    /*printf("SDL_JoystickNumBalls value:%d:\n", value);*/
#endif
    if (_index < 0 || _index >= value) {
        return RAISE (PyExc_SDLError, "Invalid joystick trackball");
    }

    SDL_JoystickGetBall (joy, _index, &dx, &dy);
    return Py_BuildValue ("(ii)", dx, dy);
}
Ejemplo n.º 2
0
eeVector2i cJoystickSDL::GetBallMotion( const Int32& ball ) {
	eeVector2i v;

	if ( ball >= 0 && ball < mBalls )
		SDL_JoystickGetBall( mJoystick, ball, &v.x, &v.y );

	return v;
}
Ejemplo n.º 3
0
static int l_joystick_getBall(lua_State *L) {
  int dx,dy;
  Joystick *self = luaL_checkudata(L, 1, CLASS_NAME);
  int idx = luaL_checknumber(L, 2);
  SDL_JoystickGetBall(self->joystick,idx,&dx,&dy);
  lua_pushnumber(L, dx);
  lua_pushnumber(L, dy);
  return 2;
}
Ejemplo n.º 4
0
CAMLprim value ml_SDL_JoystickGetBall(value j, value ball)
{
  int dx, dy;
  value v;
  SDL_JoystickGetBall(SDLJoystick_val(j), Int_val(ball), &dx, &dy);
  v = alloc_small(2, 0);
  Field(v, 0) = Val_int(dx);
  Field(v, 1) = Val_int(dy);
  return v;
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
0
int libjoy_get_ball_specific( int joy, int ball, int * dx, int * dy )
{
    if ( joy >= 0 && joy < _max_joys )
    {
        if ( ball >= 0 && ball <= SDL_JoystickNumBalls( _joysticks[ joy ] ) )
        {
            return SDL_JoystickGetBall( _joysticks[ joy ], ball, dx, dy ) ;
        }
    }
    return -1 ;
}
Ejemplo n.º 7
0
int libjoy_get_ball( int ball, int * dx, int * dy )
{
    if ( _selected_joystick >= 0 && _selected_joystick < _max_joys )
    {
        if ( ball >= 0 && ball <= SDL_JoystickNumBalls( _joysticks[ball] ) )
        {
            return SDL_JoystickGetBall( _joysticks[ _selected_joystick ], ball, dx, dy ) ;
        }
    }
    return -1 ;
}
Ejemplo n.º 8
0
void JoystickController::updateKey(bool &key, KM_Key &mkey)
{
    int val=0, dx=0, dy=0;
    switch(mkey.type)
    {
    case KeyMapJoyCtrls::JoyAxis:
        val=SDL_JoystickGetAxis(m_joystickController, mkey.id);
            if(mkey.val>0)
                key=(val>0);
            else if(mkey.val<0)
                key=(val<0);
            else key=false;
        break;
    case KeyMapJoyCtrls::JoyBallX:
        SDL_JoystickGetBall(m_joystickController, mkey.id, &dx, &dy);
        if(mkey.id>0)
            key=(dx>0);
        else if(mkey.id<0)
            key=(dx<0);
        else key=false;
        break;
    case KeyMapJoyCtrls::JoyBallY:
        SDL_JoystickGetBall(m_joystickController, mkey.id, &dx, &dy);
        if(mkey.id>0)
            key=(dy>0);
        else if(mkey.id<0)
            key=(dy<0);
        else key=false;
        break;
    case KeyMapJoyCtrls::JoyHat:
        val=SDL_JoystickGetHat(m_joystickController, mkey.id);
        key = (val==mkey.val);
        break;
    case KeyMapJoyCtrls::JoyButton:
        key = SDL_JoystickGetButton(m_joystickController, mkey.id);
        break;
    default:
        key=false;
        break;
    }
}
Ejemplo n.º 9
0
JNIEXPORT jintArray JNICALL Java_at_wisch_joystick_Joystick_getBallDeltaNative (JNIEnv *env, jobject, jint joystickIndex, jint ballIndex) {
	// TODO: This is untested, because I don't have a joystick with trackball.
	int deltaArray[2];
	if (!SDL_JoystickGetBall(joysticks[joystickIndex], ballIndex, &deltaArray[0], &deltaArray[1])) {
		//throwException(env, SDL_GetError()); + also add throws at declaration of Java method
		deltaArray[0] = 0;
		deltaArray[1] = 0;
	}
	jintArray jDeltaArray = (jintArray)env->NewIntArray(2);
	env->SetIntArrayRegion(jDeltaArray, 0, 2, (jint *)deltaArray);
	return (jDeltaArray);
}
Ejemplo n.º 10
0
//ygetballjoystick(i,a) 							: Get relative y trackball motion
int ygetballjoystick(int index,int ball)
{
    int x,y;
    int ret;
    SDL_JoystickUpdate();
    if (SDLjoy[index])
	ret= SDL_JoystickGetBall(SDLjoy[index],ball,&x,&y);
    else
	ret=-1;
    if (ret!=-1)
	return y;
    else
	return ret;
}
Ejemplo n.º 11
0
void es_joystick_getBall(sdl_data *sd, int len,char *buff)
{
   int sendlen;
   char *bp, *start;
   SDL_Joystick *joy;
   int dx, dy;
   Uint8 ball;
   
   bp = buff;
   POPGLPTR(joy, bp);
   ball = get8(bp);
   bp = start = sdl_get_temp_buff(sd, 8);
   if(0 == SDL_JoystickGetBall(joy, ball, &dx, &dy)) {
      put32be(bp, dx);
      put32be(bp, dy);
   }
   sendlen = bp - start;
   sdl_send(sd, sendlen);
}
Ejemplo n.º 12
0
	int Joystick::getBall(lua_State * L)
	{
		love::luax_assert_argc(L, 2, 2);
		int index = (int)lua_tointeger(L, 1) - 1;
		int ball = (int)lua_tointeger(L, 2) - 1;

		if (!verifyJoystick(index))
			return 0;

		if (ball >= getNumBalls(index))
			return 0;

		int dx, dy;
		SDL_JoystickGetBall(joysticks[index], ball, &dx, &dy);

		lua_pushnumber(L, dx);
		lua_pushnumber(L, dy);
		return 2;
	}
Ejemplo n.º 13
0
static mrb_value
mrb_sdl2_joystick_joystick_get_ball(mrb_state *mrb, mrb_value self)
{
  mrb_value array;
  int dx;
  int dy;
  int result;
  mrb_int ball;
  SDL_Joystick * joystick_p = mrb_sdl2_joystick_joystick_get_ptr(mrb, self);
  mrb_get_args(mrb, "i", &ball);
  result = SDL_JoystickGetBall(joystick_p, ball, &dx, &dy);
  if (0 > result) {
    mruby_sdl2_raise_error(mrb);
  }
  array = mrb_ary_new_capa(mrb, 2);
  mrb_ary_push(mrb, array, mrb_fixnum_value(dx));
  mrb_ary_push(mrb, array, mrb_fixnum_value(dy));
  return array;
}
Ejemplo n.º 14
0
static void IN_JoyMove(void)
{
	unsigned int axes  = 0;
	unsigned int hats  = 0;
	int          total = 0;
	int          i     = 0;

	if (!stick)
	{
		return;
	}

	SDL_JoystickUpdate();

	// update the ball state.
	total = SDL_JoystickNumBalls(stick);
	if (total > 0)
	{
		int balldx = 0;
		int balldy = 0;
		int dx;
		int dy;

		for (i = 0; i < total; i++)
		{
			dx = 0;
			dy = 0;

			SDL_JoystickGetBall(stick, i, &dx, &dy);
			balldx += dx;
			balldy += dy;
		}
		if (balldx || balldy)
		{
			// !!! FIXME: is this good for stick balls, or just mice?
			// Scale like the mouse input...
			if (abs(balldx) > 1)
			{
				balldx *= 2;
			}
			if (abs(balldy) > 1)
			{
				balldy *= 2;
			}
			Com_QueueEvent(0, SE_MOUSE, balldx, balldy, 0, NULL);
		}
	}

	// now query the stick buttons...
	total = SDL_JoystickNumButtons(stick);
	if (total > 0)
	{
		if (total > ARRAY_LEN(stick_state.buttons))
		{
			total = ARRAY_LEN(stick_state.buttons);
		}
		for (i = 0; i < total; i++)
		{
			qboolean pressed = (SDL_JoystickGetButton(stick, i) != 0);

			if (pressed != stick_state.buttons[i])
			{
				Com_QueueEvent(0, SE_KEY, K_JOY1 + i, pressed, 0, NULL);
				stick_state.buttons[i] = pressed;
			}
		}
	}

	// look at the hats...
	total = SDL_JoystickNumHats(stick);
	if (total > 0)
	{
		if (total > 4)
		{
			total = 4;
		}
		for (i = 0; i < total; i++)
		{
			((Uint8 *)&hats)[i] = SDL_JoystickGetHat(stick, i);
		}
	}

	// update hat state
	if (hats != stick_state.oldhats)
	{
		for (i = 0; i < 4; i++)
		{
			if (((Uint8 *)&hats)[i] != ((Uint8 *)&stick_state.oldhats)[i])
			{
				// release event
				switch (((Uint8 *)&stick_state.oldhats)[i])
				{
				case SDL_HAT_UP:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 0], qfalse, 0, NULL);
					break;
				case SDL_HAT_RIGHT:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 1], qfalse, 0, NULL);
					break;
				case SDL_HAT_DOWN:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 2], qfalse, 0, NULL);
					break;
				case SDL_HAT_LEFT:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 3], qfalse, 0, NULL);
					break;
				case SDL_HAT_RIGHTUP:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 0], qfalse, 0, NULL);
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 1], qfalse, 0, NULL);
					break;
				case SDL_HAT_RIGHTDOWN:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 2], qfalse, 0, NULL);
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 1], qfalse, 0, NULL);
					break;
				case SDL_HAT_LEFTUP:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 0], qfalse, 0, NULL);
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 3], qfalse, 0, NULL);
					break;
				case SDL_HAT_LEFTDOWN:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 2], qfalse, 0, NULL);
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 3], qfalse, 0, NULL);
					break;
				default:
					break;
				}
				// press event
				switch (((Uint8 *)&hats)[i])
				{
				case SDL_HAT_UP:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 0], qtrue, 0, NULL);
					break;
				case SDL_HAT_RIGHT:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 1], qtrue, 0, NULL);
					break;
				case SDL_HAT_DOWN:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 2], qtrue, 0, NULL);
					break;
				case SDL_HAT_LEFT:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 3], qtrue, 0, NULL);
					break;
				case SDL_HAT_RIGHTUP:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 0], qtrue, 0, NULL);
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 1], qtrue, 0, NULL);
					break;
				case SDL_HAT_RIGHTDOWN:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 2], qtrue, 0, NULL);
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 1], qtrue, 0, NULL);
					break;
				case SDL_HAT_LEFTUP:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 0], qtrue, 0, NULL);
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 3], qtrue, 0, NULL);
					break;
				case SDL_HAT_LEFTDOWN:
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 2], qtrue, 0, NULL);
					Com_QueueEvent(0, SE_KEY, hat_keys[4 * i + 3], qtrue, 0, NULL);
					break;
				default:
					break;
				}
			}
		}
	}

	// save hat state
	stick_state.oldhats = hats;

	// finally, look at the axes...
	total = SDL_JoystickNumAxes(stick);
	if (total > 0)
	{
		if (total > 16)
		{
			total = 16;
		}
		for (i = 0; i < total; i++)
		{
			Sint16 axis = SDL_JoystickGetAxis(stick, i);

			if (in_joystickUseAnalog->integer)
			{
				float f = ((float) abs(axis)) / 32767.0f;

				if (f < in_joystickThreshold->value)
				{
					axis = 0;
				}

				if (axis != stick_state.oldaaxes[i])
				{
					Com_QueueEvent(0, SE_JOYSTICK_AXIS, i, axis, 0, NULL);
					stick_state.oldaaxes[i] = axis;
				}
			}
			else
			{
				float f = ((float) axis) / 32767.0f;

				if (f < -in_joystickThreshold->value)
				{
					axes |= (1 << (i * 2));
				}
				else if (f > in_joystickThreshold->value)
				{
					axes |= (1 << ((i * 2) + 1));
				}
			}
		}
	}

	// Time to update axes state based on old vs. new.
	if (axes != stick_state.oldaxes)
	{
		for (i = 0; i < 16; i++)
		{
			if ((axes & (1 << i)) && !(stick_state.oldaxes & (1 << i)))
			{
				Com_QueueEvent(0, SE_KEY, joy_keys[i], qtrue, 0, NULL);
			}

			if (!(axes & (1 << i)) && (stick_state.oldaxes & (1 << i)))
			{
				Com_QueueEvent(0, SE_KEY, joy_keys[i], qfalse, 0, NULL);
			}
		}
	}

	// Save for future generations.
	stick_state.oldaxes = axes;
}
Ejemplo n.º 15
0
void Joystick::processEvents()
{
  if ( !isOpen() )
    return;

  SDL_JoystickUpdate();

  int i;
  for (i = 0; i < numAxes; i++) {
    Sint16 moved = SDL_JoystickGetAxis(joystick, i);
    if ( abs(moved) >= deadzones[i] ) {
      if ( (moved != axes[i]) ) {
        int deltaMoved = abs(axes[i] - moved);
        if ( deltaMoved >= sensitivities[i] )
          emit axisValueChanged(i, moved);
        axes[i] = moved;
        axisRepeatTimers[i].restart();
      } else if (autoRepeat && moved != 0) {
        if ( axisRepeatTimers[i].elapsed() >= autoRepeatDelay ) {
          emit axisValueChanged(i, moved);
          axes[i] = moved;
        }
      } else
        axisRepeatTimers[i].restart();
    } else
      emit axisValueChanged(i, 0);
  }
  for (i = 0; i < numButtons; i++) {
    Uint8 changed = SDL_JoystickGetButton(joystick, i);
    if ( (changed != buttons[i]) ) {
      emit buttonValueChanged(i, (bool) changed);
      buttons[i] = changed;
      buttonRepeatTimers[i].restart();
    } else if (autoRepeat && changed != 0) {
      if ( buttonRepeatTimers[i].elapsed() >= autoRepeatDelay ) {
        emit buttonValueChanged(i, (bool) changed);
        buttons[i] = changed;
      }
    } else
      buttonRepeatTimers[i].restart();
  }
  for (i = 0; i < numHats; i++) {
    Uint8 changed = SDL_JoystickGetHat(joystick, i);
    if ( (changed != hats[i]) ) {
      emit hatValueChanged(i, changed);
      hats[i] = changed;
      hatRepeatTimers[i].restart();
    } else if (autoRepeat && changed != 0) {
      if ( hatRepeatTimers[i].elapsed() >= autoRepeatDelay ) {
        emit hatValueChanged(i, changed);
        hats[i] = changed;
      }
    } else
      hatRepeatTimers[i].restart();
  }

  for (i = 0; i < numTrackballs; i++) {
    int dx, dy;
    SDL_JoystickGetBall(joystick, i, &dx, &dy);
    if ( dx != 0 || dy != 0 )
      emit trackballValueChanged(i, dx, dy);
  }
}
Ejemplo n.º 16
0
void IN_JoystickMove (void)
{
	bool joy_pressed[lengthof(joy_keys)];
	unsigned int axes = 0;
	unsigned int hats = 0;
	int total = 0;
	int i = 0;

	/* check whether a user has changed the joystick number */
	if (in_joystickNo->modified)
		IN_StartupJoystick();
	/* check whether joysticks are disabled */
	if (!in_joystick->integer)
		return;

	if (!stick)
		return;

	SDL_JoystickUpdate();

	OBJZERO(joy_pressed);

	/* update the ball state */
	total = SDL_JoystickNumBalls(stick);
	if (total > 0) {
		int balldx = 0;
		int balldy = 0;
		for (i = 0; i < total; i++) {
			int dx = 0;
			int dy = 0;
			SDL_JoystickGetBall(stick, i, &dx, &dy);
			balldx += dx;
			balldy += dy;
		}
		if (balldx || balldy) {
			mousePosX = balldx / viddef.rx;
			mousePosY = balldy / viddef.ry;
		}
	}

	/* now query the stick buttons... */
	total = SDL_JoystickNumButtons(stick);
	if (total > 0) {
		if (total > lengthof(stick_state.buttons))
			total = lengthof(stick_state.buttons);
		for (i = 0; i < total; i++) {
			const bool pressed = (SDL_JoystickGetButton(stick, i) != 0);
			if (pressed != stick_state.buttons[i]) {
				IN_EventEnqueue(K_JOY1 + i, 0, pressed);
				stick_state.buttons[i] = pressed;
			}
		}
	}

	/* look at the hats... */
	total = SDL_JoystickNumHats(stick);
	if (total > 0) {
		if (total > 4)
			total = 4;
		for (i = 0; i < total; i++)
			((Uint8 *)&hats)[i] = SDL_JoystickGetHat(stick, i);
	}

	/* update hat state */
	if (hats != stick_state.oldhats) {
		for (i = 0; i < 4; i++) {
			if (((Uint8 *)&hats)[i] != ((Uint8 *)&stick_state.oldhats)[i]) {
				/* release event */
				switch (((Uint8 *)&stick_state.oldhats)[i]) {
				case SDL_HAT_UP:
					IN_EventEnqueue(hat_keys[4 * i + 0], 0, false);
					break;
				case SDL_HAT_RIGHT:
					IN_EventEnqueue(hat_keys[4 * i + 1], 0, false);
					break;
				case SDL_HAT_DOWN:
					IN_EventEnqueue(hat_keys[4 * i + 2], 0, false);
					break;
				case SDL_HAT_LEFT:
					IN_EventEnqueue(hat_keys[4 * i + 3], 0, false);
					break;
				case SDL_HAT_RIGHTUP:
					IN_EventEnqueue(hat_keys[4 * i + 0], 0, false);
					IN_EventEnqueue(hat_keys[4 * i + 1], 0, false);
					break;
				case SDL_HAT_RIGHTDOWN:
					IN_EventEnqueue(hat_keys[4 * i + 2], 0, false);
					IN_EventEnqueue(hat_keys[4 * i + 1], 0, false);
					break;
				case SDL_HAT_LEFTUP:
					IN_EventEnqueue(hat_keys[4 * i + 0], 0, false);
					IN_EventEnqueue(hat_keys[4 * i + 3], 0, false);
					break;
				case SDL_HAT_LEFTDOWN:
					IN_EventEnqueue(hat_keys[4 * i + 2], 0, false);
					IN_EventEnqueue(hat_keys[4 * i + 3], 0, false);
					break;
				default:
					break;
				}
				/* press event */
				switch (((Uint8 *)&hats)[i]) {
				case SDL_HAT_UP:
					IN_EventEnqueue(hat_keys[4 * i + 0], 0, true);
					break;
				case SDL_HAT_RIGHT:
					IN_EventEnqueue(hat_keys[4 * i + 1], 0, true);
					break;
				case SDL_HAT_DOWN:
					IN_EventEnqueue(hat_keys[4 * i + 2], 0, true);
					break;
				case SDL_HAT_LEFT:
					IN_EventEnqueue(hat_keys[4 * i + 3], 0, true);
					break;
				case SDL_HAT_RIGHTUP:
					IN_EventEnqueue(hat_keys[4 * i + 0], 0, true);
					IN_EventEnqueue(hat_keys[4 * i + 1], 0, true);
					break;
				case SDL_HAT_RIGHTDOWN:
					IN_EventEnqueue(hat_keys[4 * i + 2], 0, true);
					IN_EventEnqueue(hat_keys[4 * i + 1], 0, true);
					break;
				case SDL_HAT_LEFTUP:
					IN_EventEnqueue(hat_keys[4 * i + 0], 0, true);
					IN_EventEnqueue(hat_keys[4 * i + 3], 0, true);
					break;
				case SDL_HAT_LEFTDOWN:
					IN_EventEnqueue(hat_keys[4 * i + 2], 0, true);
					IN_EventEnqueue(hat_keys[4 * i + 3], 0, true);
					break;
				default:
					break;
				}
			}
		}
	}

	/* save hat state */
	stick_state.oldhats = hats;

	/* finally, look at the axes... */
	total = SDL_JoystickNumAxes(stick);
	if (total >= 2) {
		/* the first two axes are used for the cursor movement */
		for (i = 0; i < 2; i++) {
			const Sint16 axis = SDL_JoystickGetAxis(stick, i);
			const float velocity = ((float) axis) / 32767.0f;
			if (velocity > -in_joystickThreshold->value && velocity < in_joystickThreshold->value)
				continue;

			if (i & 1) {
				mousePosY += in_joystickSpeed->value * velocity;
				if (mousePosY > (int)viddef.context.height)
					mousePosY = (int)viddef.context.height;
				else if (mousePosY < 0)
					mousePosY = 0;
			} else {
				mousePosX += in_joystickSpeed->value * velocity;
				if (mousePosX > (int)viddef.context.width)
					mousePosX = (int)viddef.context.width;
				else if (mousePosX < 0)
					mousePosX = 0;
			}
		}
	}


	if (total > 2) {
		if (total > 16)
			total = 16;
		/* every axis except the first two can be normally bound to an action */
		for (i = 2; i < total; i++) {
			const Sint16 axis = SDL_JoystickGetAxis(stick, i);
			const float f = ((float) axis) / 32767.0f;
			if (f < -in_joystickThreshold->value) {
				axes |= (1 << (i * 2));
			} else if (f > in_joystickThreshold->value) {
				axes |= (1 << ((i * 2) + 1));
			}
		}
	}


	/* Time to update axes state based on old vs. new. */
	if (axes != stick_state.oldaxes) {
		for (i = 2; i < 16; i++) {
			if ((axes & (1 << i)) && !(stick_state.oldaxes & (1 << i)))
				IN_EventEnqueue(joy_keys[i], 0, true);

			if (!(axes & (1 << i)) && (stick_state.oldaxes & (1 << i)))
				IN_EventEnqueue(joy_keys[i], 0, false);
		}
	}

	/* Save for future generations. */
	stick_state.oldaxes = axes;
}
Ejemplo n.º 17
0
void IN_JoyMove( void )
{
    qboolean joy_pressed[ARRAYLEN(joy_keys)];
    unsigned int axes = 0;
    unsigned int hats = 0;
    int total = 0;
    int i = 0;

    if (!stick)
        return;

    SDL_JoystickUpdate();

    memset(joy_pressed, '\0', sizeof (joy_pressed));

    // update the ball state.
    total = SDL_JoystickNumBalls(stick);
    if (total > 0)
    {
        int balldx = 0;
        int balldy = 0;
        for (i = 0; i < total; i++)
        {
            int dx = 0;
            int dy = 0;
            SDL_JoystickGetBall(stick, i, &dx, &dy);
            balldx += dx;
            balldy += dy;
        }
        if (balldx || balldy)
        {
            // !!! FIXME: is this good for stick balls, or just mice?
            // Scale like the mouse input...
            if (abs(balldx) > 1)
                balldx *= 2;
            if (abs(balldy) > 1)
                balldy *= 2;
            Sys_QueEvent( 0, SE_MOUSE, balldx, balldy, 0, NULL );
        }
    }

    // now query the stick buttons...
    total = SDL_JoystickNumButtons(stick);
    if (total > 0)
    {
        if (total > ARRAYLEN(stick_state.buttons))
            total = ARRAYLEN(stick_state.buttons);
        for (i = 0; i < total; i++)
        {
            qboolean pressed = (SDL_JoystickGetButton(stick, i) != 0);
            if (pressed != stick_state.buttons[i])
            {
                Sys_QueEvent( 0, SE_KEY, K_JOY1 + i, pressed, 0, NULL );
                stick_state.buttons[i] = pressed;
            }
        }
    }

    // look at the hats...
    total = SDL_JoystickNumHats(stick);
    if (total > 0)
    {
        if (total > 4) total = 4;
        for (i = 0; i < total; i++)
        {
	    ((Uint8 *)&hats)[i] = SDL_JoystickGetHat(stick, i);
        }
    }

    // update hat state
    if (hats != stick_state.oldhats)
    {
        for( i = 0; i < 4; i++ ) {
            if( ((Uint8 *)&hats)[i] != ((Uint8 *)&stick_state.oldhats)[i] ) {
	        // release event
	        switch( ((Uint8 *)&stick_state.oldhats)[i] ) {
		case SDL_HAT_UP:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qfalse, 0, NULL );
		  break;
		case SDL_HAT_RIGHT:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qfalse, 0, NULL );
		  break;
		case SDL_HAT_DOWN:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qfalse, 0, NULL );
		  break;
		case SDL_HAT_LEFT:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qfalse, 0, NULL );
		  break;
		case SDL_HAT_RIGHTUP:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qfalse, 0, NULL );
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qfalse, 0, NULL );
		  break;
		case SDL_HAT_RIGHTDOWN:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qfalse, 0, NULL );
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qfalse, 0, NULL );
		  break;
		case SDL_HAT_LEFTUP:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qfalse, 0, NULL );
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qfalse, 0, NULL );
		  break;
		case SDL_HAT_LEFTDOWN:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qfalse, 0, NULL );
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qfalse, 0, NULL );
		  break;
		default:
		  break;
		}
		// press event
	        switch( ((Uint8 *)&hats)[i] ) {
		case SDL_HAT_UP:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qtrue, 0, NULL );
		  break;
		case SDL_HAT_RIGHT:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qtrue, 0, NULL );
		  break;
		case SDL_HAT_DOWN:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qtrue, 0, NULL );
		  break;
		case SDL_HAT_LEFT:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qtrue, 0, NULL );
		  break;
		case SDL_HAT_RIGHTUP:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qtrue, 0, NULL );
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qtrue, 0, NULL );
		  break;
		case SDL_HAT_RIGHTDOWN:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qtrue, 0, NULL );
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qtrue, 0, NULL );
		  break;
		case SDL_HAT_LEFTUP:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qtrue, 0, NULL );
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qtrue, 0, NULL );
		  break;
		case SDL_HAT_LEFTDOWN:
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qtrue, 0, NULL );
                  Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qtrue, 0, NULL );
		  break;
		default:
		  break;
		}
            }
        }
    }

    // save hat state
    stick_state.oldhats = hats;

    // finally, look at the axes...
    total = SDL_JoystickNumAxes(stick);
    if (total > 0)
    {
        if (total > 16) total = 16;
        for (i = 0; i < total; i++)
        {
            Sint16 axis = SDL_JoystickGetAxis(stick, i);
            float f = ( (float) axis ) / 32767.0f;
            if( f < -joy_threshold->value ) {
                axes |= ( 1 << ( i * 2 ) );
            } else if( f > joy_threshold->value ) {
                axes |= ( 1 << ( ( i * 2 ) + 1 ) );
            }
        }
    }

    /* Time to update axes state based on old vs. new. */
    if (axes != stick_state.oldaxes)
    {
        for( i = 0; i < 16; i++ ) {
            if( ( axes & ( 1 << i ) ) && !( stick_state.oldaxes & ( 1 << i ) ) ) {
                Sys_QueEvent( 0, SE_KEY, joy_keys[i], qtrue, 0, NULL );
            }

            if( !( axes & ( 1 << i ) ) && ( stick_state.oldaxes & ( 1 << i ) ) ) {
                Sys_QueEvent( 0, SE_KEY, joy_keys[i], qfalse, 0, NULL );
            }
        }
    }

    /* Save for future generations. */
    stick_state.oldaxes = axes;
}