Example #1
0
/**
 * input_sdl_check_key_pressed(): Checks if the specified key is pressed.
 * @param key Key to check.
 * @return TRUE if the key is pressed; FALSE if the key is not pressed.
 */
BOOL input_sdl_check_key_pressed(uint16_t key)
{
	// If the key value is <INPUT_SDL_MAX_KEYS, it's a keyboard key.
	if (key < INPUT_SDL_MAX_KEYS)
		return input_sdl_keys[key];
	
	// If this isn't a joystick input, don't check anything else.
	if (!INPUT_IS_JOYSTICK(key))
		return FALSE;
	
	// Joystick "key" check.
	
	// Determine which joystick we're looking for.
	int joyNum = INPUT_JOYSTICK_GET_NUMBER(key);
	
	// Check that this joystick exists.
	if (!input_sdl_joy_exists(joyNum))
		return FALSE;
	
	// Joystick exists. Check the state.
	switch (INPUT_JOYSTICK_GET_TYPE(key))
	{
		case INPUT_JOYSTICK_TYPE_AXIS:
			// Joystick axis.
			if (INPUT_SDL_JOYSTICK_CHECK_AXIS(input_sdl_joystate, joyNum, key))
				return TRUE;
			break;
		
		case INPUT_JOYSTICK_TYPE_BUTTON:
			// Joystick button.
			if (INPUT_SDL_JOYSTICK_CHECK_BUTTON(input_sdl_joystate, joyNum, key))
				return TRUE;
			break;
		
		case INPUT_JOYSTICK_TYPE_POVHAT:
		{
			// Joystick POV hat.
			static const uint8_t povKeyToBit[4] = {SDL_HAT_UP, SDL_HAT_RIGHT, SDL_HAT_DOWN, SDL_HAT_LEFT};
			uint8_t povBit = povKeyToBit[INPUT_JOYSTICK_GET_POVHAT_DIRECTION(key)];
			
			if (INPUT_SDL_JOYSTICK_CHECK_POVHAT_DIRECTION(input_sdl_joystate, joyNum, key, povBit))
				return TRUE;
			break;
		}
	}
	
	// Key is not pressed.
	return FALSE;
}
Example #2
0
/**
 * input_get_key_name(): Get a key name.
 * @param key Key.
 * @param buf Buffer to store the key name in. ("Unknown Key" is stored on error.)
 * @param size Size of the buffer.
 * @return 0 on success; non-zero on error.
 */
int input_get_key_name(uint16_t key, char* buf, int size)
{
	if (size == 0)
		return -1;
	
	if (key == 0)
	{
		// 0 == not configured.
		strlcpy(buf, "Not Configured", size);
		return 0;
	}
	
	if (!INPUT_IS_JOYSTICK(key))
	{
		// Not a joystick input.
		// Defer the key name lookup to the current input handler.
		if (input_cur_backend)
		{
			int rval = input_cur_backend->get_key_name(key, buf, size);
			if (!rval)
				return 0;
			else
			{
				// Unknown key.
				strlcpy(buf, "Unknown Key", size);
				return rval;
			}
		}
		
		// No backend available. Return an error.
		strlcpy(buf, "Unknown Key", size);
		return -1;
	}
	
	// Joystick input.
	static const char pov_directions[4][8]	= {"Up", "Right", "Down", "Left"};
	static const char axis_names[6][4]	= {"X", "Y", "Z", "Rx", "Ry", "Rz"};
	
	// Joystick number.
	const int joy_num = INPUT_JOYSTICK_GET_NUMBER(key);
	
	switch (INPUT_JOYSTICK_GET_TYPE(key))
	{
		case INPUT_JOYSTICK_TYPE_AXIS:
		{
			int axis = INPUT_JOYSTICK_GET_AXIS(key);
			char dir = (INPUT_JOYSTICK_GET_AXIS_DIRECTION(key) == INPUT_JOYSTICK_AXIS_NEGATIVE ? '-' : '+');
			
			if (axis < 6)
				szprintf(buf, size, "Joy %d, Axis %s%c", joy_num, axis_names[axis], dir);
			else
				szprintf(buf, size, "Joy %d, Axis %d%c", joy_num, axis, dir);
			break;
		}
		
		case INPUT_JOYSTICK_TYPE_BUTTON:
			szprintf(buf, size, "Joy %d, Button %d", joy_num,
				 INPUT_JOYSTICK_GET_BUTTON(key));
			break;
		
		case INPUT_JOYSTICK_TYPE_POVHAT:
			szprintf(buf, size, "Joy %d, POV %d %s", joy_num,
				 INPUT_JOYSTICK_GET_POVHAT_NUMBER(key),
				 pov_directions[INPUT_JOYSTICK_GET_POVHAT_DIRECTION(key)]);
			break;
		
		default:
			strlcpy(buf, "Unknown Joy Key", size);
			return -1;
	}
	
	return 0;
}