Exemplo n.º 1
0
static int
SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
{
    if (joystick->hats[hat] != value)
        return SDL_PrivateJoystickHat(joystick, hat, value);
    return 0;
}
Exemplo n.º 2
0
static SDL_INLINE void
HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
{
    struct hwdata_hat *the_hat;
    const Uint8 position_map[3][3] = {
        {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
        {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
        {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
    };

    the_hat = &stick->hwdata->hats[hat];
    if (value < 0) {
        value = 0;
    } else if (value == 0) {
        value = 1;
    } else if (value > 0) {
        value = 2;
    }
    if (value != the_hat->axis[axis]) {
        the_hat->axis[axis] = value;
        SDL_PrivateJoystickHat(stick, hat,
                               position_map[the_hat->
                                            axis[1]][the_hat->axis[0]]);
    }
}
Exemplo n.º 3
0
static void
UpdateDINPUTJoystickState_Buffered(SDL_Joystick * joystick)
{
    int i;
    HRESULT result;
    DWORD numevents;
    DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];

    numevents = INPUT_QSIZE;
    result =
        IDirectInputDevice8_GetDeviceData(joystick->hwdata->InputDevice,
        sizeof(DIDEVICEOBJECTDATA), evtbuf,
        &numevents, 0);
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
        IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
        result =
            IDirectInputDevice8_GetDeviceData(joystick->hwdata->InputDevice,
            sizeof(DIDEVICEOBJECTDATA),
            evtbuf, &numevents, 0);
    }

    /* Handle the events or punt */
    if (FAILED(result)) {
        joystick->hwdata->send_remove_event = SDL_TRUE;
        joystick->hwdata->removed = SDL_TRUE;
        return;
    }

    for (i = 0; i < (int)numevents; ++i) {
        int j;

        for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
            const input_t *in = &joystick->hwdata->Inputs[j];

            if (evtbuf[i].dwOfs != in->ofs)
                continue;

            switch (in->type) {
            case AXIS:
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)evtbuf[i].dwData);
                break;
            case BUTTON:
                SDL_PrivateJoystickButton(joystick, in->num,
                    (Uint8)(evtbuf[i].dwData ? SDL_PRESSED : SDL_RELEASED));
                break;
            case HAT:
                {
                    Uint8 pos = TranslatePOV(evtbuf[i].dwData);
                    SDL_PrivateJoystickHat(joystick, in->num, pos);
                }
                break;
            }
        }
    }
}
Exemplo n.º 4
0
void
SDL_JoystickUpdate(void)
{
    SDL_Joystick *joystick;

    joystick = SDL_joysticks;
    while (joystick) {
        SDL_Joystick *joysticknext;
        /* save off the next pointer, the Update call may cause a joystick removed event
         * and cause our joystick pointer to be freed
         */
        joysticknext = joystick->next;

        SDL_updating_joystick = joystick;

        SDL_SYS_JoystickUpdate(joystick);

        if (joystick->force_recentering) {
            int i;

            /* Tell the app that everything is centered/unpressed...  */
            for (i = 0; i < joystick->naxes; i++) {
                SDL_PrivateJoystickAxis(joystick, i, joystick->axes_zero[i]);
            }

            for (i = 0; i < joystick->nbuttons; i++) {
                SDL_PrivateJoystickButton(joystick, i, 0);
            }

            for (i = 0; i < joystick->nhats; i++) {
                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
            }

            joystick->force_recentering = SDL_FALSE;
        }

        SDL_updating_joystick = NULL;

        /* If the joystick was closed while updating, free it here */
        if (joystick->ref_count <= 0) {
            SDL_JoystickClose(joystick);
        }

        joystick = joysticknext;
    }

    /* this needs to happen AFTER walking the joystick list above, so that any
       dangling hardware data from removed devices can be free'd
     */
    SDL_SYS_JoystickDetect();
}
Exemplo n.º 5
0
static __inline__
void HandleHat(SDL_Joystick *stick, Uint8 hat, int axis, int value)
{
	struct hwdata_hat *the_hat;
	const Uint8 position_map[3][3] = {
		{ SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP },
		{ SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT },
		{ SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN }
	};
	SDL_logical_joydecl(SDL_Joystick *logicaljoy = NULL);
	SDL_logical_joydecl(struct joystick_logical_mapping* hats = NULL);

	if (stick->nhats <= hat) {
		return;  /* whoops, that shouldn't happen! */
	}

	the_hat = &stick->hwdata->hats[hat];
	if ( value < 0 ) {
		value = 0;
	} else
	if ( value == 0 ) {
		value = 1;
	} else
	if ( value > 0 ) {
		value = 2;
	}
	if ( value != the_hat->axis[axis] ) {
		the_hat->axis[axis] = value;

#ifndef NO_LOGICAL_JOYSTICKS
		/* if there's no map then this is just a regular joystick
		*/
		if (SDL_joylist[stick->index].map != NULL) {

			/* get the fake joystick that will receive the event
			*/
			hats = SDL_joylist[stick->index].map->hatmap+hat;
			logicaljoy = FindLogicalJoystick(stick, hats);
		}

		if (logicaljoy) {
			stick = logicaljoy;
			hat = hats->nthing;
		}
#endif /* USE_LOGICAL_JOYSTICKS */

		SDL_PrivateJoystickHat(stick, hat,
			position_map[the_hat->axis[1]][the_hat->axis[0]]);
	}
}
Exemplo n.º 6
0
int
Android_OnHat(int device_id, int hat_id, int x, int y)
{
    const Uint8 position_map[3][3] = {
        {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
        {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
        {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
    };

    if (x >= -1 && x <=1 && y >= -1 && y <= 1) {
        SDL_joylist_item *item = JoystickByDeviceId(device_id);
        if (item && item->joystick) {
            SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1] );
        }
        return 0;
    }

    return -1;
}
Exemplo n.º 7
0
static void
UpdateXInputJoystickState(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState, XINPUT_BATTERY_INFORMATION_EX *pBatteryInformation)
{
    static WORD s_XInputButtons[] = {
        XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y,
        XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_START,
        XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB,
        XINPUT_GAMEPAD_GUIDE
    };
    WORD wButtons = pXInputState->Gamepad.wButtons;
    Uint8 button;
    Uint8 hat = SDL_HAT_CENTERED;

    SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX);
    SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbLY)));
    SDL_PrivateJoystickAxis(joystick, 2, (Sint16)(((int)pXInputState->Gamepad.bLeftTrigger * 65535 / 255) - 32768));
    SDL_PrivateJoystickAxis(joystick, 3, (Sint16)pXInputState->Gamepad.sThumbRX);
    SDL_PrivateJoystickAxis(joystick, 4, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbRY)));
    SDL_PrivateJoystickAxis(joystick, 5, (Sint16)(((int)pXInputState->Gamepad.bRightTrigger * 65535 / 255) - 32768));

    for (button = 0; button < SDL_arraysize(s_XInputButtons); ++button) {
        SDL_PrivateJoystickButton(joystick, button, (wButtons & s_XInputButtons[button]) ? SDL_PRESSED : SDL_RELEASED);
    }

    if (wButtons & XINPUT_GAMEPAD_DPAD_UP) {
        hat |= SDL_HAT_UP;
    }
    if (wButtons & XINPUT_GAMEPAD_DPAD_DOWN) {
        hat |= SDL_HAT_DOWN;
    }
    if (wButtons & XINPUT_GAMEPAD_DPAD_LEFT) {
        hat |= SDL_HAT_LEFT;
    }
    if (wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) {
        hat |= SDL_HAT_RIGHT;
    }
    SDL_PrivateJoystickHat(joystick, 0, hat);

    UpdateXInputJoystickBatteryInformation( joystick, pBatteryInformation );
}
Exemplo n.º 8
0
/* Function to update the state of a joystick - called as a device poll.
 * This function shouldn't update the joystick structure directly,
 * but instead should call SDL_PrivateJoystick*() to deliver events
 * and update joystick device state.
 */
static void
UpdateDINPUTJoystickState_Polled(SDL_Joystick * joystick)
{
    DIJOYSTATE2 state;
    HRESULT result;
    int i;

    result =
        IDirectInputDevice8_GetDeviceState(joystick->hwdata->InputDevice,
        sizeof(DIJOYSTATE2), &state);
    if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
        IDirectInputDevice8_Acquire(joystick->hwdata->InputDevice);
        result =
            IDirectInputDevice8_GetDeviceState(joystick->hwdata->InputDevice,
            sizeof(DIJOYSTATE2), &state);
    }

    if (result != DI_OK) {
        joystick->hwdata->send_remove_event = SDL_TRUE;
        joystick->hwdata->removed = SDL_TRUE;
        return;
    }

    /* Set each known axis, button and POV. */
    for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
        const input_t *in = &joystick->hwdata->Inputs[i];

        switch (in->type) {
        case AXIS:
            switch (in->ofs) {
            case DIJOFS_X:
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)state.lX);
                break;
            case DIJOFS_Y:
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)state.lY);
                break;
            case DIJOFS_Z:
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)state.lZ);
                break;
            case DIJOFS_RX:
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)state.lRx);
                break;
            case DIJOFS_RY:
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)state.lRy);
                break;
            case DIJOFS_RZ:
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)state.lRz);
                break;
            case DIJOFS_SLIDER(0):
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)state.rglSlider[0]);
                break;
            case DIJOFS_SLIDER(1):
                SDL_PrivateJoystickAxis(joystick, in->num, (Sint16)state.rglSlider[1]);
                break;
            }
            break;

        case BUTTON:
            SDL_PrivateJoystickButton(joystick, in->num,
                (Uint8)(state.rgbButtons[in->ofs - DIJOFS_BUTTON0] ? SDL_PRESSED : SDL_RELEASED));
            break;
        case HAT:
        {
            Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs - DIJOFS_POV(0)]);
            SDL_PrivateJoystickHat(joystick, in->num, pos);
            break;
        }
        }
    }
}
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{        
        static int prev_buttons[4] = {0};
        static Sint16 nX = 0, nY = 0;
        static Sint16 nXR = 0, nYR = 0;
        unsigned long b=0; 
        int hat=0, changed=0;

        /* Theres a bug with the current libXenon controller implementation
           that sometimes causes analog values to 'stick' and retain the same x/y values
           after release back to origin.
           */
        
        usb_do_poll();
        
        get_controller_data(&joystick->hwdata->curpad, joystick->index);
    
        if (joystick->hwdata->curpad.a)
        {
                if (!joystick->buttons[0])
                        SDL_PrivateJoystickButton(joystick, (Uint8)0, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[0])
                        SDL_PrivateJoystickButton(joystick, (Uint8)0, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.b)
        {
                if (!joystick->buttons[1])
                        SDL_PrivateJoystickButton(joystick, (Uint8)1, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[1])
                        SDL_PrivateJoystickButton(joystick, (Uint8)1, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.x)
        {
                if (!joystick->buttons[2])
                        SDL_PrivateJoystickButton(joystick, (Uint8)2, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[2])
                        SDL_PrivateJoystickButton(joystick, (Uint8)2, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.y)
        {
                if (!joystick->buttons[3])
                        SDL_PrivateJoystickButton(joystick, (Uint8)3, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[3])
                        SDL_PrivateJoystickButton(joystick, (Uint8)3, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.lb)
        {
                if (!joystick->buttons[4])
                        SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[4])
                        SDL_PrivateJoystickButton(joystick, (Uint8)4, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.rb)
        {
                if (!joystick->buttons[5])
                        SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[5])
                        SDL_PrivateJoystickButton(joystick, (Uint8)5, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.s1_z)
        {
                if (!joystick->buttons[6])
                        SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[6])
                        SDL_PrivateJoystickButton(joystick, (Uint8)6, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.s2_z)
        {
                if (!joystick->buttons[7])
                        SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[7])
                        SDL_PrivateJoystickButton(joystick, (Uint8)7, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.start)
        {
                if (!joystick->buttons[8])
                        SDL_PrivateJoystickButton(joystick, (Uint8)8, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[8])
                        SDL_PrivateJoystickButton(joystick, (Uint8)8, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.back)
        {
                if (!joystick->buttons[9])
                        SDL_PrivateJoystickButton(joystick, (Uint8)9, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[9])
                        SDL_PrivateJoystickButton(joystick, (Uint8)9, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.lt > 200)
        {
                if (!joystick->buttons[10])
                        SDL_PrivateJoystickButton(joystick, (Uint8)10, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[10])
                        SDL_PrivateJoystickButton(joystick, (Uint8)10, SDL_RELEASED);
        }

        if (joystick->hwdata->curpad.rt > 200)
        {
                if (!joystick->buttons[11])
                        SDL_PrivateJoystickButton(joystick, (Uint8)11, SDL_PRESSED);
        }
        else
        {
                if (joystick->buttons[11])
                        SDL_PrivateJoystickButton(joystick, (Uint8)11, SDL_RELEASED);
        }


        // do the HATS baby

        hat = SDL_HAT_CENTERED;
        
        if (joystick->hwdata->curpad.down)
                hat|=SDL_HAT_DOWN;
        if (joystick->hwdata->curpad.up)
                hat|=SDL_HAT_UP;
        if (joystick->hwdata->curpad.left)
                hat|=SDL_HAT_LEFT;
        if (joystick->hwdata->curpad.right)
                hat|=SDL_HAT_RIGHT;


        changed = hat^prev_buttons[joystick->index];

        if ( changed ) {
                SDL_PrivateJoystickHat(joystick, 0, hat);
        }

        prev_buttons[joystick->index] = hat;

        // Axis - LStick

        if ((joystick->hwdata->curpad.s1_x <= -14000) ||
                (joystick->hwdata->curpad.s1_x >= 14000))
        {
                if (joystick->hwdata->curpad.s1_x < 0)
                        joystick->hwdata->curpad.s1_x++;
                nX = ((Sint16)joystick->hwdata->curpad.s1_x);
        }
        else
                nX = 0;

        if ( nX != joystick->axes[0] )
                SDL_PrivateJoystickAxis(joystick, (Uint8)0, (Sint16)nX);


        if ((joystick->hwdata->curpad.s1_y <= -14000) ||
                (joystick->hwdata->curpad.s1_y >= 14000))
        {
                if (joystick->hwdata->curpad.s1_y < 0)
                        joystick->hwdata->curpad.s1_y++;
                nY = -((Sint16)(joystick->hwdata->curpad.s1_y));
        }
        else
                nY = 0;

        if ( nY != joystick->axes[1] )
                SDL_PrivateJoystickAxis(joystick, (Uint8)1, (Sint16)nY);


        // Axis - RStick

        if ((joystick->hwdata->curpad.s2_x <= -14000) ||
                (joystick->hwdata->curpad.s2_x >= 14000))
        {
                if (joystick->hwdata->curpad.s2_x < 0)
                        joystick->hwdata->curpad.s2_x++;
                nXR = ((Sint16)joystick->hwdata->curpad.s2_x);
        }
        else
                nXR = 0;

        if ( nXR != joystick->axes[2] )
                SDL_PrivateJoystickAxis(joystick, (Uint8)2, (Sint16)nXR);


        if ((joystick->hwdata->curpad.s2_y <= -14000) ||
                (joystick->hwdata->curpad.s2_y >= 14000))
        {
                if (joystick->hwdata->curpad.s2_y < 0)
                        joystick->hwdata->curpad.s2_y++;
                nYR = -((Sint16)joystick->hwdata->curpad.s2_y);
        }
        else
                nYR = 0;

        if ( nYR != joystick->axes[3] )
                SDL_PrivateJoystickAxis(joystick, (Uint8)3, (Sint16)nYR);    
        
}
Exemplo n.º 10
0
/* Function to update the state of a joystick - called as a device poll.
 * This function shouldn't update the joystick structure directly,
 * but instead should call SDL_PrivateJoystick*() to deliver events
 * and update joystick device state.
 */
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
	recDevice *device = joystick->hwdata;
	recElement *element;
	SInt32 value;
	int i;

	if (device->removed)  /* device was unplugged; ignore it. */
	{
		if (device->uncentered)
		{
			device->uncentered = 0;

			/* Tell the app that everything is centered/unpressed... */
			for (i = 0; i < device->axes; i++)
				SDL_PrivateJoystickAxis(joystick, i, 0);

			for (i = 0; i < device->buttons; i++)
				SDL_PrivateJoystickButton(joystick, i, 0);

			for (i = 0; i < device->hats; i++)
				SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
		}

		return;
	}

	element = device->firstAxis;
	i = 0;
	while (element)
	{
		value = HIDScaledCalibratedValue(device, element, -32768, 32767);
		if ( value != joystick->axes[i] )
			SDL_PrivateJoystickAxis(joystick, i, value);
		element = element->pNext;
		++i;
	}
	
	element = device->firstButton;
	i = 0;
	while (element)
	{
		value = HIDGetElementValue(device, element);
        if (value > 1)  /* handle pressure-sensitive buttons */
            value = 1;
		if ( value != joystick->buttons[i] )
			SDL_PrivateJoystickButton(joystick, i, value);
		element = element->pNext;
		++i;
	}
	    
	element = device->firstHat;
	i = 0;
	while (element)
	{
		Uint8 pos = 0;

		value = HIDGetElementValue(device, element);
		if (element->max == 3) /* 4 position hatswitch - scale up value */
			value *= 2;
		else if (element->max != 7) /* Neither a 4 nor 8 positions - fall back to default position (centered) */
			value = -1;
		switch(value)
		{
			case 0:
				pos = SDL_HAT_UP;
				break;
			case 1:
				pos = SDL_HAT_RIGHTUP;
				break;
			case 2:
				pos = SDL_HAT_RIGHT;
				break;
			case 3:
				pos = SDL_HAT_RIGHTDOWN;
				break;
			case 4:
				pos = SDL_HAT_DOWN;
				break;
			case 5:
				pos = SDL_HAT_LEFTDOWN;
				break;
			case 6:
				pos = SDL_HAT_LEFT;
				break;
			case 7:
				pos = SDL_HAT_LEFTUP;
				break;
			default:
				/* Every other value is mapped to center. We do that because some
				 * joysticks use 8 and some 15 for this value, and apparently
				 * there are even more variants out there - so we try to be generous.
				 */
				pos = SDL_HAT_CENTERED;
				break;
		}
		if ( pos != joystick->hats[i] )
			SDL_PrivateJoystickHat(joystick, i, pos);
		element = element->pNext;
		++i;
	}
	
	return;
}
Exemplo n.º 11
0
void
SDL_JoystickUpdate(void)
{
    SDL_Joystick *joystick;

    SDL_LockJoystickList();

    if (SDL_updating_joystick) {
        /* The joysticks are already being updated */
        SDL_UnlockJoystickList();
        return;
    }

    SDL_updating_joystick = SDL_TRUE;

    /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
    SDL_UnlockJoystickList();

    for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
        SDL_SYS_JoystickUpdate(joystick);

        if (joystick->force_recentering) {
            int i;

            /* Tell the app that everything is centered/unpressed... */
            for (i = 0; i < joystick->naxes; i++) {
                if (joystick->axes[i].has_initial_value) {
                    SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
                }
            }

            for (i = 0; i < joystick->nbuttons; i++) {
                SDL_PrivateJoystickButton(joystick, i, 0);
            }

            for (i = 0; i < joystick->nhats; i++) {
                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
            }

            joystick->force_recentering = SDL_FALSE;
        }
    }

    SDL_LockJoystickList();

    SDL_updating_joystick = SDL_FALSE;

    /* If any joysticks were closed while updating, free them here */
    for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
        if (joystick->ref_count <= 0) {
            SDL_JoystickClose(joystick);
        }
    }

    /* this needs to happen AFTER walking the joystick list above, so that any
       dangling hardware data from removed devices can be free'd
     */
    SDL_SYS_JoystickDetect();

    SDL_UnlockJoystickList();
}
Exemplo n.º 12
0
/* Function to update the state of a joystick - called as a device poll.
 * This function shouldn't update the joystick structure directly,
 * but instead should call SDL_PrivateJoystick*() to deliver events
 * and update joystick device state.
 */
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
    int     i, j;
    ISpAxisData     a;
    ISpDPadData     b;
    //ISpDeltaData    c;
    ISpButtonData   d;

    for(i = 0, j = 0; i < joystick->naxes; i++, j++)
    {
        Sint16 value;

        ISpElement_GetSimpleState(
            joystick->hwdata->refs[j],
            &a);
        value = (ISpSymmetricAxisToFloat(a)* 32767.0);
        if ( value != joystick->axes[i] ) {
            SDL_PrivateJoystickAxis(joystick, i, value);
        }
    }

    for(i = 0; i < joystick->nhats; i++, j++)
    {
        Uint8 pos;

        ISpElement_GetSimpleState(
            joystick->hwdata->refs[j],
            &b);
        switch(b) {
            case kISpPadIdle:
                pos = SDL_HAT_CENTERED;
                break;
            case kISpPadLeft:
                pos = SDL_HAT_LEFT;
                break;
            case kISpPadUpLeft:
                pos = SDL_HAT_LEFTUP;
                break;
            case kISpPadUp:
                pos = SDL_HAT_UP;
                break;
            case kISpPadUpRight:
                pos = SDL_HAT_RIGHTUP;
                break;
            case kISpPadRight:
                pos = SDL_HAT_RIGHT;
                break;
            case kISpPadDownRight:
                pos = SDL_HAT_RIGHTDOWN;
                break;
            case kISpPadDown:
                pos = SDL_HAT_DOWN;
                break;
            case kISpPadDownLeft:
                pos = SDL_HAT_LEFTDOWN;
                break;
        }
        if ( pos != joystick->hats[i] ) {
            SDL_PrivateJoystickHat(joystick, i, pos);
        }
    }

    for(i = 0; i < joystick->nballs; i++, j++)
    {
        /*  ignore balls right now  */
    }

    for(i = 0; i < joystick->nbuttons; i++, j++)
    {
        ISpElement_GetSimpleState(
            joystick->hwdata->refs[j],
            &d);
        if ( d != joystick->buttons[i] ) {
            SDL_PrivateJoystickButton(joystick, i, d);
        }
    }
}
Exemplo n.º 13
0
/* Function to update the state of a joystick - called as a device poll.
 * This function shouldn't update the joystick structure directly,
 * but instead should call SDL_PrivateJoystick*() to deliver events
 * and update joystick device state.
 */
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
	if(!joystick || !joystick->hwdata)
		return;

	u16 buttons, prev_buttons, changed;
	int i, axis;
	joystick_hwdata *prev_state;

	PAD_ScanPads();

	buttons = PAD_ButtonsHeld(joystick->index);
	prev_state = (joystick_hwdata *)joystick->hwdata;
	prev_buttons = prev_state->gamecube.prev_buttons;
	changed = buttons ^ prev_buttons;

	/* D-Pad */
	if(changed & (PAD_BUTTON_LEFT | PAD_BUTTON_RIGHT |
					PAD_BUTTON_DOWN | PAD_BUTTON_UP))
	{
		int hat = SDL_HAT_CENTERED;
		if(buttons & PAD_BUTTON_UP) hat |= SDL_HAT_UP;
		if(buttons & PAD_BUTTON_DOWN) hat |= SDL_HAT_DOWN;
		if(buttons & PAD_BUTTON_LEFT) hat |= SDL_HAT_LEFT;
		if(buttons & PAD_BUTTON_RIGHT) hat |= SDL_HAT_RIGHT;
		SDL_PrivateJoystickHat(joystick, 0, hat);
	}

	/* All Buttons and trigger presses */
	for(i = 0; i < MAX_GC_BUTTONS; i++)
	{
		if (changed & sdl_buttons_gc[i])
			SDL_PrivateJoystickButton(joystick, i,
				(buttons & sdl_buttons_gc[i]) ? SDL_PRESSED : SDL_RELEASED);
	}

	/* Analog stick */
	prev_state->gamecube.prev_buttons = buttons;
	axis = PAD_StickX(joystick->index);
	if(prev_state->gamecube.stickX != axis)
	{
		SDL_PrivateJoystickAxis(joystick, 0, axis << 8);
		prev_state->gamecube.stickX = axis;
	}

	axis = PAD_StickY(joystick->index);
	if(prev_state->gamecube.stickY != axis)
	{
		SDL_PrivateJoystickAxis(joystick, 1, (-axis) << 8);
		prev_state->gamecube.stickY = axis;
	}

	/* C-Stick */
	axis = PAD_SubStickX(joystick->index);
	if(prev_state->gamecube.substickX != axis)
	{
		SDL_PrivateJoystickAxis(joystick, 2, axis << 8);
		prev_state->gamecube.substickX = axis;
	}

	axis = PAD_SubStickY(joystick->index);
	if(prev_state->gamecube.substickY != axis)
	{
		SDL_PrivateJoystickAxis(joystick, 3, (-axis) << 8); //FABIO
		prev_state->gamecube.substickY = axis;
	}

	/* L-Trigger */
	axis = PAD_TriggerL(joystick->index);
	if(prev_state->gamecube.triggerL != axis)
	{
		SDL_PrivateJoystickAxis(joystick, 4, axis << 7); //FABIO
		prev_state->gamecube.triggerL = axis;
	}

	/* R-Trigger */
	axis = PAD_TriggerR(joystick->index);
	if(prev_state->gamecube.triggerR != axis)
	{
		SDL_PrivateJoystickAxis(joystick, 5, axis << 7); //FABIO
		prev_state->gamecube.triggerR = axis;
	}

	/* done */
	return;
}
/* Function to update the state of a joystick - called as a device poll.
 * This function shouldn't update the joystick structure directly,
 * but instead should call SDL_PrivateJoystick*() to deliver events
 * and update joystick device state.
 */
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
    MMRESULT result;
    int i;
    DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ,
        JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
    };
    DWORD pos[MAX_AXES];
    struct _transaxis *transaxis;
    int value, change;
    JOYINFOEX joyinfo;

    joyinfo.dwSize = sizeof(joyinfo);
    joyinfo.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS;
    if (!joystick->hats) {
        joyinfo.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS);
    }
    result = joyGetPosEx(joystick->hwdata->id, &joyinfo);
    if (result != JOYERR_NOERROR) {
        SetMMerror("joyGetPosEx", result);
        return;
    }

    /* joystick motion events */
    pos[0] = joyinfo.dwXpos;
    pos[1] = joyinfo.dwYpos;
    pos[2] = joyinfo.dwZpos;
    pos[3] = joyinfo.dwRpos;
    pos[4] = joyinfo.dwUpos;
    pos[5] = joyinfo.dwVpos;

    transaxis = joystick->hwdata->transaxis;
    for (i = 0; i < joystick->naxes; i++) {
        if (joyinfo.dwFlags & flags[i]) {
            value =
                (int) (((float) pos[i] +
                        transaxis[i].offset) * transaxis[i].scale);
            change = (value - joystick->axes[i]);
            if ((change < -JOY_AXIS_THRESHOLD)
                || (change > JOY_AXIS_THRESHOLD)) {
                SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value);
            }
        }
    }

    /* joystick button events */
    if (joyinfo.dwFlags & JOY_RETURNBUTTONS) {
        for (i = 0; i < joystick->nbuttons; ++i) {
            if (joyinfo.dwButtons & JOY_BUTTON_FLAG(i)) {
                if (!joystick->buttons[i]) {
                    SDL_PrivateJoystickButton(joystick, (Uint8) i,
                                              SDL_PRESSED);
                }
            } else {
                if (joystick->buttons[i]) {
                    SDL_PrivateJoystickButton(joystick, (Uint8) i,
                                              SDL_RELEASED);
                }
            }
        }
    }

    /* joystick hat events */
    if (joyinfo.dwFlags & JOY_RETURNPOV) {
        Uint8 pos;

        pos = TranslatePOV(joyinfo.dwPOV);
        if (pos != joystick->hats[0]) {
            SDL_PrivateJoystickHat(joystick, 0, pos);
        }
    }
}
Exemplo n.º 15
0
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
	int i, ret;
	int port, slot;
	int rjoy_h, rjoy_v;
	int ljoy_h, ljoy_v;
	struct padButtonStatus buttons;
	u32 paddata;
	u32 old_pad;
	u32 new_pad;

	port = joystick->hwdata->port;
	slot = joystick->hwdata->slot;

	if (wait_pad(port, slot, 10) < 0)
	{
		/* no pad information available */
		return;
	}

	if (joystick->hwdata->deviceid < 0)
	{
		int id;

		/* see if we can probe the MODECURID now */
		id = padInfoMode(port, slot, PAD_MODECURID, 0);
		if (id != 0)
		{
			int ext;

        		ext = padInfoMode(port, slot, PAD_MODECUREXID, 0);
			if (ext != 0)
			{
				id = ext;
			}

			if (id == PAD_TYPE_DIGITAL)
			{
				printf("SDL_Joystick: digital pad detected\n");
			}
			else if (id == PAD_TYPE_DUALSHOCK)
			{
				printf("SDL_Joystick: dualshock detected\n");
			}
			else 
			{
				printf("SDL_Joystick: unknown identifier %d detected\n", id);
			}

			if (id == PAD_TYPE_DIGITAL || id == PAD_TYPE_DUALSHOCK)
			{
				ret = padSetMainMode(port, slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK);
				if (ret == 1) 
				{ 
					printf("JoystickInit: Request received\n"); 
				} 
				else
				{ 
					printf("JoystickInit: padSetMainMode failed\n"); 
				}
			}

			joystick->hwdata->deviceid = id;
			if (wait_pad(port, slot, 10) < 0)
			{
				/* no pad information available */
				return;
			}
		}
	}

        ret = padRead(port, slot, &buttons); 
	if (ret != 0)
	{
		int changed, hat;

		old_pad = joystick->hwdata->prev_buttons;
		paddata = 0xffff ^ buttons.btns;
		new_pad = paddata; 
		changed = paddata ^ old_pad;
		joystick->hwdata->prev_buttons = paddata;

		hat = SDL_HAT_CENTERED;
		if (new_pad & PAD_LEFT)
		{
			hat = hat | SDL_HAT_LEFT;
		}

		if (new_pad & PAD_RIGHT)
		{
			hat = hat | SDL_HAT_RIGHT;
		}

		if (new_pad & PAD_DOWN)
		{
			hat = hat | SDL_HAT_DOWN;
		}

		if (new_pad & PAD_UP)
		{
			hat = hat | SDL_HAT_UP;
		}

		for (i=0; i<MAX_BUTTONS; i++)
		{
			if (changed & sdl_buttons[i])
			{
				int status = (new_pad & sdl_buttons[i]) ? SDL_PRESSED : SDL_RELEASED;

				SDL_PrivateJoystickButton(joystick, i, status);
			}
		}

		if (changed & (PAD_LEFT|PAD_RIGHT|PAD_UP|PAD_DOWN))
		{
			SDL_PrivateJoystickHat(joystick, 0, hat);
		}

		/* now do axis */
		ljoy_h = buttons.ljoy_h - 128;
		ljoy_v = buttons.ljoy_v - 128;
		rjoy_h = buttons.rjoy_h - 128;
		rjoy_v = buttons.rjoy_v - 128;

		/*
		printf("rjoy_h %d rjoy_v %d ljoy_h %d ljoy_v %d (%d, %d, %d, %d)\n",
		rjoy_h, rjoy_v,
		ljoy_h, ljoy_v,
		joystick->hwdata->prev_rjoy_h, joystick->hwdata->prev_rjoy_v,
		joystick->hwdata->prev_ljoy_h, joystick->hwdata->prev_ljoy_v);
		*/

		/* left analog stick */
		if (abs(joystick->hwdata->prev_ljoy_h - ljoy_h) > AXIS_THRESHOLD)
		{
			SDL_PrivateJoystickAxis(joystick, 0, ljoy_h * 127);
			joystick->hwdata->prev_ljoy_h = ljoy_h;
		}

		if (abs(joystick->hwdata->prev_ljoy_v - ljoy_v) > AXIS_THRESHOLD)
		{
			SDL_PrivateJoystickAxis(joystick, 1, ljoy_v * 127);
			joystick->hwdata->prev_ljoy_v = ljoy_v;
		}

		/* right analog stick */
		if (abs(joystick->hwdata->prev_rjoy_h - rjoy_h) > AXIS_THRESHOLD)
		{
			SDL_PrivateJoystickAxis(joystick, 2, rjoy_h  * 127);
			joystick->hwdata->prev_rjoy_h = rjoy_h;
		}

		if (abs(joystick->hwdata->prev_rjoy_v - rjoy_v) > AXIS_THRESHOLD)
		{
			SDL_PrivateJoystickAxis(joystick, 3, rjoy_v * 127);
			joystick->hwdata->prev_rjoy_v = rjoy_v;
		}
	} 
}
Exemplo n.º 16
0
/* Function to update the state of a joystick - called as a device poll.
 * This function shouldn't update the joystick structure directly,
 * but instead should call SDL_PrivateJoystick*() to deliver events
 * and update joystick device state.
 */
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
	recDevice *device = joystick->hwdata;
    recElement *element;
    SInt32 value, range;
    int i;

	if ( !device )
		return;

    if (device->removed) {      /* device was unplugged; ignore it. */
		recDevice *devicelist = gpDeviceList;
		joystick->closed = 1;
		joystick->uncentered = 1;
		
		if ( devicelist == device )
		{
			gpDeviceList = device->pNext;
		}
		else
		{
			while ( devicelist->pNext != device )
			{
				devicelist = devicelist->pNext;
			}
			
			devicelist->pNext = device->pNext;
		}
		
		DisposePtr((Ptr) device);
		joystick->hwdata = NULL;

#if !SDL_EVENTS_DISABLED
		SDL_Event event;
		event.type = SDL_JOYDEVICEREMOVED;
		
		if (SDL_GetEventState(event.type) == SDL_ENABLE) {
			event.jdevice.which = joystick->instance_id;
			if ((SDL_EventOK == NULL)
				|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
				SDL_PushEvent(&event);
			}
		}
#endif /* !SDL_EVENTS_DISABLED */
		
        return;
    }

    element = device->firstAxis;
    i = 0;
    while (element) {
        value = HIDScaledCalibratedValue(device, element, -32768, 32767);
        if (value != joystick->axes[i])
            SDL_PrivateJoystickAxis(joystick, i, value);
        element = element->pNext;
        ++i;
    }

    element = device->firstButton;
    i = 0;
    while (element) {
        value = HIDGetElementValue(device, element);
        if (value > 1)          /* handle pressure-sensitive buttons */
            value = 1;
        if (value != joystick->buttons[i])
            SDL_PrivateJoystickButton(joystick, i, value);
        element = element->pNext;
        ++i;
    }

    element = device->firstHat;
    i = 0;
    while (element) {
        Uint8 pos = 0;

        range = (element->max - element->min + 1);
        value = HIDGetElementValue(device, element) - element->min;
        if (range == 4)         /* 4 position hatswitch - scale up value */
            value *= 2;
        else if (range != 8)    /* Neither a 4 nor 8 positions - fall back to default position (centered) */
            value = -1;
        switch (value) {
        case 0:
            pos = SDL_HAT_UP;
            break;
        case 1:
            pos = SDL_HAT_RIGHTUP;
            break;
        case 2:
            pos = SDL_HAT_RIGHT;
            break;
        case 3:
            pos = SDL_HAT_RIGHTDOWN;
            break;
        case 4:
            pos = SDL_HAT_DOWN;
            break;
        case 5:
            pos = SDL_HAT_LEFTDOWN;
            break;
        case 6:
            pos = SDL_HAT_LEFT;
            break;
        case 7:
            pos = SDL_HAT_LEFTUP;
            break;
        default:
            /* Every other value is mapped to center. We do that because some
             * joysticks use 8 and some 15 for this value, and apparently
             * there are even more variants out there - so we try to be generous.
             */
            pos = SDL_HAT_CENTERED;
            break;
        }
        if (pos != joystick->hats[i])
            SDL_PrivateJoystickHat(joystick, i, pos);
        element = element->pNext;
        ++i;
    }

    return;
}
Exemplo n.º 17
0
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
/*
const	int sdl_buttons[] = {
	CONT_C,
	CONT_B,
	CONT_A,
	CONT_START,
	CONT_Z,
	CONT_Y,
	CONT_X,
	CONT_D
};
*/
const	int sdl_buttons[] = {
	CONT_A,
	CONT_B,
	CONT_X,
	CONT_Y,
	CONT_START,
	CONT_C,
	CONT_D,
	CONT_Z,
};

	uint8 addr;
	cont_cond_t cond,*prev_cond;
	int buttons,prev_buttons,i,changed;

	addr = SYS_Joystick_addr[joystick->index];
	if (cont_get_cond(addr,&cond)<0) return;

	buttons = cond.buttons ^0xffff;
	prev_buttons = joystick->hwdata->prev_buttons;
	changed = buttons^prev_buttons;

	if ((changed)&(CONT_DPAD_UP|CONT_DPAD_DOWN|CONT_DPAD_LEFT|CONT_DPAD_RIGHT)) {
		int hat = SDL_HAT_CENTERED;
		if (buttons&CONT_DPAD_UP) hat|=SDL_HAT_UP;
		if (buttons&CONT_DPAD_DOWN) hat|=SDL_HAT_DOWN;
		if (buttons&CONT_DPAD_LEFT) hat|=SDL_HAT_LEFT;
		if (buttons&CONT_DPAD_RIGHT) hat|=SDL_HAT_RIGHT;
		SDL_PrivateJoystickHat(joystick, 0, hat);
	}
	if ((changed)&(CONT_DPAD2_UP|CONT_DPAD2_DOWN|CONT_DPAD2_LEFT|CONT_DPAD2_RIGHT)) {
		int hat = SDL_HAT_CENTERED;
		if (buttons&CONT_DPAD2_UP) hat|=SDL_HAT_UP;
		if (buttons&CONT_DPAD2_DOWN) hat|=SDL_HAT_DOWN;
		if (buttons&CONT_DPAD2_LEFT) hat|=SDL_HAT_LEFT;
		if (buttons&CONT_DPAD2_RIGHT) hat|=SDL_HAT_RIGHT;
		SDL_PrivateJoystickHat(joystick, 1, hat);
	}

	for(i=0;i<sizeof(sdl_buttons)/sizeof(sdl_buttons[0]);i++) {
		if (changed & sdl_buttons[i]) {
			SDL_PrivateJoystickButton(joystick, i, (buttons & sdl_buttons[i])?SDL_PRESSED:SDL_RELEASED);
		}
	}

	prev_cond = &joystick->hwdata->prev_cond;
	if (cond.joyx!=prev_cond->joyx)
		SDL_PrivateJoystickAxis(joystick, 0, cond.joyx-128);
	if (cond.joyy!=prev_cond->joyy)
		SDL_PrivateJoystickAxis(joystick, 1, cond.joyy-128);
	if (cond.rtrig!=prev_cond->rtrig)
		SDL_PrivateJoystickAxis(joystick, 2, cond.rtrig);
	if (cond.ltrig!=prev_cond->ltrig)
		SDL_PrivateJoystickAxis(joystick, 3, cond.ltrig);
	if (cond.joy2x!=prev_cond->joy2x)
		SDL_PrivateJoystickAxis(joystick, 4, cond.joy2x-128);
	if (cond.joy2y!=prev_cond->joy2y)
		SDL_PrivateJoystickAxis(joystick, 5, cond.joy2y-128);

	joystick->hwdata->prev_buttons = buttons;
	joystick->hwdata->prev_cond = cond;
}
Exemplo n.º 18
0
/* Function to update the state of a joystick - called as a device poll.
 * This function shouldn't update the joystick structure directly,
 * but instead should call SDL_PrivateJoystick*() to deliver events
 * and update joystick device state.
 */
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
{
	int b = 0, hat = 0, hatchanged = 0;
	Sint16 nX = 0, nY = 0;
	Sint16 nXR = 0, nYR = 0;

    if( joystick->hwdata->pPad->hPresent )
    {	
		if (joystick->hwdata->pPad->CurrentButtons.usDigitalButtons & XPAD_START)
		{
			if (!joystick->buttons[8])
				SDL_PrivateJoystickButton(joystick, (Uint8)8, SDL_PRESSED);
		}
		else
		{
			if (joystick->buttons[8])
				SDL_PrivateJoystickButton(joystick, (Uint8)8, SDL_RELEASED);
		}

		if (joystick->hwdata->pPad->CurrentButtons.usDigitalButtons & XPAD_BACK)
		{
			if (!joystick->buttons[9])
				SDL_PrivateJoystickButton(joystick, (Uint8)9, SDL_PRESSED);
		}
		else
		{
			if (joystick->buttons[9])
				SDL_PrivateJoystickButton(joystick, (Uint8)9, SDL_RELEASED);
		}

		if (joystick->hwdata->pPad->CurrentButtons.usDigitalButtons & XPAD_LEFT_THUMB)
		{
			if (!joystick->buttons[10])
				SDL_PrivateJoystickButton(joystick, (Uint8)10, SDL_PRESSED);
		}
		else
		{
			if (joystick->buttons[10])
				SDL_PrivateJoystickButton(joystick, (Uint8)10, SDL_RELEASED);
		}

		if (joystick->hwdata->pPad->CurrentButtons.usDigitalButtons & XPAD_RIGHT_THUMB)
		{
			if (!joystick->buttons[11])
				SDL_PrivateJoystickButton(joystick, (Uint8)11, SDL_PRESSED);
		}
		else
		{
			if (joystick->buttons[11])
				SDL_PrivateJoystickButton(joystick, (Uint8)11, SDL_RELEASED);
		}

			

        // Get the analog buttons that have been pressed or released since
        // the last call.
        for( b=0; b<8; b++ )
        {
            // Turn the 8-bit polled value into a boolean value
            BOOL bPressed = ( joystick->hwdata->pPad->CurrentButtons.ucAnalogButtons[b] );

			if ( bPressed  ) {
				if ( !joystick->buttons[b] ) {
					SDL_PrivateJoystickButton(joystick, (Uint8)b, SDL_PRESSED);
				}
			} else {
				if ( joystick->buttons[b] ) {
					SDL_PrivateJoystickButton(joystick, (Uint8)b, SDL_RELEASED);
				}
			}


        }
	}
	// do the HATS baby

	hat = SDL_HAT_CENTERED;
	if (joystick->hwdata->pPad->CurrentButtons.usDigitalButtons & XPAD_DPAD_DOWN)
		hat|=SDL_HAT_DOWN;
	if (joystick->hwdata->pPad->CurrentButtons.usDigitalButtons & XPAD_DPAD_UP)
		hat|=SDL_HAT_UP;
	if (joystick->hwdata->pPad->CurrentButtons.usDigitalButtons & XPAD_DPAD_LEFT)
		hat|=SDL_HAT_LEFT;
	if (joystick->hwdata->pPad->CurrentButtons.usDigitalButtons & XPAD_DPAD_RIGHT)
		hat|=SDL_HAT_RIGHT;

	hatchanged = hat ^ joystick->hwdata->prevHat;

	if(hatchanged)
		SDL_PrivateJoystickHat(joystick, 0, hat);

	joystick->hwdata->prevHat = hat;
	
	// Axis - LStick

	if ((joystick->hwdata->pPad->sLThumbX <= -10000) || 
		(joystick->hwdata->pPad->sLThumbX >= 10000))
	{
		if (joystick->hwdata->pPad->sLThumbX < 0)
			joystick->hwdata->pPad->sLThumbX++;
		nX = ((Sint16)joystick->hwdata->pPad->sLThumbX);
	}
	else
		nX = 0;

	if ( nX != joystick->axes[0] ) 
		SDL_PrivateJoystickAxis(joystick, (Uint8)0, (Sint16)nX);

	
	if ((joystick->hwdata->pPad->sLThumbY <= -10000) || 
		(joystick->hwdata->pPad->sLThumbY >= 10000))
	{
		if (joystick->hwdata->pPad->sLThumbY < 0)
			joystick->hwdata->pPad->sLThumbY++;
		nY = -((Sint16)(joystick->hwdata->pPad->sLThumbY));
	}
	else
		nY = 0;

	if ( nY != joystick->axes[1] )
		SDL_PrivateJoystickAxis(joystick, (Uint8)1, (Sint16)nY); 


	// Axis - RStick

	if ((joystick->hwdata->pPad->sRThumbX <= -10000) || 
		(joystick->hwdata->pPad->sRThumbX >= 10000))
	{
		if (joystick->hwdata->pPad->sRThumbX < 0)
			joystick->hwdata->pPad->sRThumbX++;
		nXR = ((Sint16)joystick->hwdata->pPad->sRThumbX);
	}
	else
		nXR = 0;

	if ( nXR != joystick->axes[2] ) 
		SDL_PrivateJoystickAxis(joystick, (Uint8)2, (Sint16)nXR);

	
	if ((joystick->hwdata->pPad->sRThumbY <= -10000) || 
		(joystick->hwdata->pPad->sRThumbY >= 10000))
	{
		if (joystick->hwdata->pPad->sRThumbY < 0)
			joystick->hwdata->pPad->sRThumbY++;
		nYR = -((Sint16)joystick->hwdata->pPad->sRThumbY);
	}
	else
		nYR = 0;

	if ( nYR != joystick->axes[3] )
		SDL_PrivateJoystickAxis(joystick, (Uint8)3, (Sint16)nYR); 
}
Exemplo n.º 19
0
void
SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
{
	struct hid_item hitem;
	struct hid_data *hdata;
	struct report *rep;
	int nbutton, naxe = -1;
	Sint32 v;

#if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
	struct joystick gameport;
 
	if (joy->hwdata->type == BSDJOY_JOY) {
		if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport)
			return;
		if (abs(joy->hwdata->x - gameport.x) > 8) {
			joy->hwdata->x = gameport.x;
			if (joy->hwdata->x < joy->hwdata->xmin) {
				joy->hwdata->xmin = joy->hwdata->x;
			}
			if (joy->hwdata->x > joy->hwdata->xmax) {
				joy->hwdata->xmax = joy->hwdata->x;
			}
			if (joy->hwdata->xmin == joy->hwdata->xmax) {
				joy->hwdata->xmin--;
				joy->hwdata->xmax++;
			}
			v = (Sint32)joy->hwdata->x;
			v -= (joy->hwdata->xmax + joy->hwdata->xmin + 1)/2;
			v *= 32768/((joy->hwdata->xmax - joy->hwdata->xmin + 1)/2);
			SDL_PrivateJoystickAxis(joy, 0, v);
		}
		if (abs(joy->hwdata->y - gameport.y) > 8) {
			joy->hwdata->y = gameport.y;
			if (joy->hwdata->y < joy->hwdata->ymin) {
				joy->hwdata->ymin = joy->hwdata->y;
			}
			if (joy->hwdata->y > joy->hwdata->ymax) {
				joy->hwdata->ymax = joy->hwdata->y;
			}
			if (joy->hwdata->ymin == joy->hwdata->ymax) {
				joy->hwdata->ymin--;
				joy->hwdata->ymax++;
			}
			v = (Sint32)joy->hwdata->y;
			v -= (joy->hwdata->ymax + joy->hwdata->ymin + 1)/2;
			v *= 32768/((joy->hwdata->ymax - joy->hwdata->ymin + 1)/2);
			SDL_PrivateJoystickAxis(joy, 1, v);
		}
		if (gameport.b1 != joy->buttons[0]) {
			SDL_PrivateJoystickButton(joy, 0, gameport.b1);
		}
		if (gameport.b2 != joy->buttons[1]) {
			SDL_PrivateJoystickButton(joy, 1, gameport.b2);
		}
		return;
	}
#endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
	
	rep = &joy->hwdata->inreport;

	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
		return;
	}
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_version >= 500111)
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
#else
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
#endif
	if (hdata == NULL) {
		fprintf(stderr, "%s: Cannot start HID parser\n",
		    joy->hwdata->path);
		return;
	}

	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
		switch (hitem.kind) {
		case hid_input:
			switch (HID_PAGE(hitem.usage)) {
			case HUP_GENERIC_DESKTOP: {
			    unsigned usage = HID_USAGE(hitem.usage);
			    int joyaxe = usage_to_joyaxe(usage);
			    if (joyaxe >= 0) {
				naxe = joy->hwdata->axis_map[joyaxe];
				/* scaleaxe */
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
							 &hitem);
				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
				if (v != joy->axes[naxe]) {
				    SDL_PrivateJoystickAxis(joy, naxe, v);
				}
			    } else if (usage == HUG_HAT_SWITCH) {
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
							 &hitem);
				SDL_PrivateJoystickHat(joy, 0,
					hatval_to_sdl(v)-hitem.logical_minimum);
			    }
			    break;
			}
			case HUP_BUTTON:
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
				    &hitem);
				if (joy->buttons[nbutton] != v) {
					SDL_PrivateJoystickButton(joy,
					    nbutton, v);
				}
				nbutton++;
				break;
			default:
				continue;
			}
			break;
		default:
			break;
		}
	}
	hid_end_parse(hdata);

	return;
}
Exemplo n.º 20
0
/* Function to update the state of a joystick - called as a device poll.
 * This function shouldn't update the joystick structure directly,
 * but instead should call SDL_PrivateJoystick*() to deliver events
 * and update joystick device state.
 */
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
    recDevice *device = joystick->hwdata;
    recElement *element;
    SInt32 value, range;
    int i;

    if (!device) {
        return;
    }

    if (device->removed) {      /* device was unplugged; ignore it. */
        joystick->closed = 1;
        joystick->uncentered = 1;
        joystick->hwdata = NULL;
        return;
    }

    element = device->firstAxis;
    i = 0;
    while (element) {
        value = GetHIDScaledCalibratedState(device, element, -32768, 32767);
        if (value != joystick->axes[i]) {
            SDL_PrivateJoystickAxis(joystick, i, value);
        }
        element = element->pNext;
        ++i;
    }

    element = device->firstButton;
    i = 0;
    while (element) {
        value = GetHIDElementState(device, element);
        if (value > 1) {          /* handle pressure-sensitive buttons */
            value = 1;
        }
        if (value != joystick->buttons[i]) {
            SDL_PrivateJoystickButton(joystick, i, value);
        }
        element = element->pNext;
        ++i;
    }

    element = device->firstHat;
    i = 0;
    while (element) {
        Uint8 pos = 0;

        range = (element->max - element->min + 1);
        value = GetHIDElementState(device, element) - element->min;
        if (range == 4) {         /* 4 position hatswitch - scale up value */
            value *= 2;
        } else if (range != 8) {    /* Neither a 4 nor 8 positions - fall back to default position (centered) */
            value = -1;
        }
        switch (value) {
        case 0:
            pos = SDL_HAT_UP;
            break;
        case 1:
            pos = SDL_HAT_RIGHTUP;
            break;
        case 2:
            pos = SDL_HAT_RIGHT;
            break;
        case 3:
            pos = SDL_HAT_RIGHTDOWN;
            break;
        case 4:
            pos = SDL_HAT_DOWN;
            break;
        case 5:
            pos = SDL_HAT_LEFTDOWN;
            break;
        case 6:
            pos = SDL_HAT_LEFT;
            break;
        case 7:
            pos = SDL_HAT_LEFTUP;
            break;
        default:
            /* Every other value is mapped to center. We do that because some
             * joysticks use 8 and some 15 for this value, and apparently
             * there are even more variants out there - so we try to be generous.
             */
            pos = SDL_HAT_CENTERED;
            break;
        }

        if (pos != joystick->hats[i]) {
            SDL_PrivateJoystickHat(joystick, i, pos);
        }

        element = element->pNext;
        ++i;
    }
}
Exemplo n.º 21
0
int
SDL_SYS_JoystickOpen(SDL_Joystick *joy)
{
	char *path = joynames[joy->index];
	struct joystick_hwdata *hw;
	struct hid_item hitem;
	struct hid_data *hdata;
	struct report *rep;
	int fd;
	int i;

	fd = open(path, O_RDONLY);
	if (fd == -1) {
		SDL_SetError("%s: %s", path, strerror(errno));
		return (-1);
	}

	hw = (struct joystick_hwdata *)SDL_malloc(sizeof(struct joystick_hwdata));
	if (hw == NULL) {
		SDL_OutOfMemory();
		close(fd);
		return (-1);
	}
	joy->hwdata = hw;
	hw->fd = fd;
	hw->path = strdup(path);
	hw->x = 0;
	hw->y = 0;
	hw->xmin = 0xffff;
	hw->ymin = 0xffff;
	hw->xmax = 0;
	hw->ymax = 0;
	if (! SDL_strncmp(path, "/dev/joy", 8)) {
		hw->type = BSDJOY_JOY;
		joy->naxes = 2;
		joy->nbuttons = 2;
		joy->nhats = 0;
		joy->nballs = 0;
		joydevnames[joy->index] = strdup("Gameport joystick");
		goto usbend;
	} else {
		hw->type = BSDJOY_UHID;
	}

	{
	    int ax;
	    for (ax = 0; ax < JOYAXE_count; ax++)
		hw->axis_map[ax] = -1;
	}
	hw->repdesc = hid_get_report_desc(fd);
	if (hw->repdesc == NULL) {
		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
		    strerror(errno));
		goto usberr;
	}
#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063)
       rep->rid = hid_get_report_id(fd);
       if (rep->rid < 0) {
#else
	rep = &hw->inreport;
	if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
#endif
		rep->rid = -1; /* XXX */
	}
	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
		goto usberr;
	}
	if (rep->size <= 0) {
		SDL_SetError("%s: Input report descriptor has invalid length",
		    hw->path);
		goto usberr;
	}

#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111)
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
#else
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
#endif
	if (hdata == NULL) {
		SDL_SetError("%s: Cannot start HID parser", hw->path);
		goto usberr;
	}
	joy->naxes = 0;
	joy->nbuttons = 0;
	joy->nhats = 0;
	joy->nballs = 0;
	for (i=0; i<JOYAXE_count; i++)
		hw->axis_map[i] = -1;

	while (hid_get_item(hdata, &hitem) > 0) {
		char *sp;
		const char *s;

		switch (hitem.kind) {
		case hid_collection:
			switch (HID_PAGE(hitem.usage)) {
			case HUP_GENERIC_DESKTOP:
				switch (HID_USAGE(hitem.usage)) {
				case HUG_JOYSTICK:
				case HUG_GAME_PAD:
					s = hid_usage_in_page(hitem.usage);
					sp = SDL_malloc(SDL_strlen(s) + 5);
					SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)", s,
					    joy->index);
					joydevnames[joy->index] = sp;
				}
			}
			break;
		case hid_input:
			switch (HID_PAGE(hitem.usage)) {
			case HUP_GENERIC_DESKTOP: {
			    unsigned usage = HID_USAGE(hitem.usage);
			    int joyaxe = usage_to_joyaxe(usage);
			    if (joyaxe >= 0) {
				hw->axis_map[joyaxe] = 1;
			    } else if (usage == HUG_HAT_SWITCH) {
				joy->nhats++;
			    }
			    break;
			}
			case HUP_BUTTON:
				joy->nbuttons++;
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}
	hid_end_parse(hdata);
	for (i=0; i<JOYAXE_count; i++)
		if (hw->axis_map[i] > 0)
			hw->axis_map[i] = joy->naxes++;

usbend:
	/* The poll blocks the event thread. */
	fcntl(fd, F_SETFL, O_NONBLOCK);

	return (0);
usberr:
	close(hw->fd);
	SDL_free(hw->path);
	SDL_free(hw);
	return (-1);
}

void
SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
{
	struct hid_item hitem;
	struct hid_data *hdata;
	struct report *rep;
	int nbutton, naxe = -1;
	Sint32 v;

#if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
	struct joystick gameport;
 
	if (joy->hwdata->type == BSDJOY_JOY) {
		if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport)
			return;
		if (abs(joy->hwdata->x - gameport.x) > 8) {
			joy->hwdata->x = gameport.x;
			if (joy->hwdata->x < joy->hwdata->xmin) {
				joy->hwdata->xmin = joy->hwdata->x;
			}
			if (joy->hwdata->x > joy->hwdata->xmax) {
				joy->hwdata->xmax = joy->hwdata->x;
			}
			if (joy->hwdata->xmin == joy->hwdata->xmax) {
				joy->hwdata->xmin--;
				joy->hwdata->xmax++;
			}
			v = (Sint32)joy->hwdata->x;
			v -= (joy->hwdata->xmax + joy->hwdata->xmin + 1)/2;
			v *= 32768/((joy->hwdata->xmax - joy->hwdata->xmin + 1)/2);
			SDL_PrivateJoystickAxis(joy, 0, v);
		}
		if (abs(joy->hwdata->y - gameport.y) > 8) {
			joy->hwdata->y = gameport.y;
			if (joy->hwdata->y < joy->hwdata->ymin) {
				joy->hwdata->ymin = joy->hwdata->y;
			}
			if (joy->hwdata->y > joy->hwdata->ymax) {
				joy->hwdata->ymax = joy->hwdata->y;
			}
			if (joy->hwdata->ymin == joy->hwdata->ymax) {
				joy->hwdata->ymin--;
				joy->hwdata->ymax++;
			}
			v = (Sint32)joy->hwdata->y;
			v -= (joy->hwdata->ymax + joy->hwdata->ymin + 1)/2;
			v *= 32768/((joy->hwdata->ymax - joy->hwdata->ymin + 1)/2);
			SDL_PrivateJoystickAxis(joy, 1, v);
		}
		if (gameport.b1 != joy->buttons[0]) {
			SDL_PrivateJoystickButton(joy, 0, gameport.b1);
		}
		if (gameport.b2 != joy->buttons[1]) {
			SDL_PrivateJoystickButton(joy, 1, gameport.b2);
		}
		return;
	}
#endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
	
	rep = &joy->hwdata->inreport;

	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
		return;
	}
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111)
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
#else
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
#endif
	if (hdata == NULL) {
		fprintf(stderr, "%s: Cannot start HID parser\n",
		    joy->hwdata->path);
		return;
	}

	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
		switch (hitem.kind) {
		case hid_input:
			switch (HID_PAGE(hitem.usage)) {
			case HUP_GENERIC_DESKTOP: {
			    unsigned usage = HID_USAGE(hitem.usage);
			    int joyaxe = usage_to_joyaxe(usage);
			    if (joyaxe >= 0) {
				naxe = joy->hwdata->axis_map[joyaxe];
				/* scaleaxe */
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
							 &hitem);
				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
				if (v != joy->axes[naxe]) {
				    SDL_PrivateJoystickAxis(joy, naxe, v);
				}
			    } else if (usage == HUG_HAT_SWITCH) {
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
							 &hitem);
				SDL_PrivateJoystickHat(joy, 0,
					hatval_to_sdl(v)-hitem.logical_minimum);
			    }
			    break;
			}
			case HUP_BUTTON:
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
				    &hitem);
				if (joy->buttons[nbutton] != v) {
					SDL_PrivateJoystickButton(joy,
					    nbutton, v);
				}
				nbutton++;
				break;
			default:
				continue;
			}
			break;
		default:
			break;
		}
	}
	hid_end_parse(hdata);

	return;
}

/* Function to close a joystick after use */
void
SDL_SYS_JoystickClose(SDL_Joystick *joy)
{
	if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8))	{
		report_free(&joy->hwdata->inreport);
		hid_dispose_report_desc(joy->hwdata->repdesc);
	}
	close(joy->hwdata->fd);
	SDL_free(joy->hwdata->path);
	SDL_free(joy->hwdata);

	return;
}

void
SDL_SYS_JoystickQuit(void)
{
	int i;

	for (i = 0; i < MAX_JOYS; i++) {
		if (joynames[i] != NULL)
			SDL_free(joynames[i]);
		if (joydevnames[i] != NULL)
			SDL_free(joydevnames[i]);
	}

	return;
}
Exemplo n.º 22
0
void
SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
{
	struct hid_item hitem;
	struct hid_data *hdata;
	struct report *rep;
	int nbutton, naxe = -1;
	Sint32 v;
	
	rep = &joy->hwdata->inreport;

	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
		return;
	}
#if defined(USBHID_NEW) || (defined(__FreeBSD__) && __FreeBSD_version >= 500111)
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
#else
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
#endif
	if (hdata == NULL) {
		fprintf(stderr, "%s: Cannot start HID parser\n",
		    joy->hwdata->path);
		return;
	}

	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
		switch (hitem.kind) {
		case hid_input:
			switch (HID_PAGE(hitem.usage)) {
			case HUP_GENERIC_DESKTOP: {
			    unsigned usage = HID_USAGE(hitem.usage);
			    int joyaxe = usage_to_joyaxe(usage);
			    if (joyaxe >= 0) {
				naxe = joy->hwdata->axis_map[joyaxe];
				/* scaleaxe */
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
							 &hitem);
				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
				if (v != joy->axes[naxe]) {
				    SDL_PrivateJoystickAxis(joy, naxe, v);
				}
			    } else if (usage == HUG_HAT_SWITCH) {
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
							 &hitem);
				SDL_PrivateJoystickHat(joy, 0, hatval_to_sdl(v));
			    }
			    break;
			}
			case HUP_BUTTON:
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
				    &hitem);
				if (joy->buttons[nbutton] != v) {
					SDL_PrivateJoystickButton(joy,
					    nbutton, v);
				}
				nbutton++;
				break;
			default:
				continue;
			}
			break;
		default:
			break;
		}
	}
	hid_end_parse(hdata);

	return;
}