/** * 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; }
/** * 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; }
/** * input_dinput_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_dinput_check_key_pressed(uint16_t key) { // If the key value is <256, it's a keyboard key. if (key < 0x100) return ((input_dinput_keys[key] & 0x80) ? TRUE : FALSE); // Joystick "key" check. // Determine which joystick we're looking for. int joyNum = ((key >> 8) & 0xF); // Check that this joystick exists. if (!input_dinput_joy_exists(joyNum)) return FALSE; // Joystick exists. Check the state. switch (INPUT_JOYSTICK_GET_TYPE(key)) { case INPUT_JOYSTICK_TYPE_AXIS: // Joystick axis. // TODO: Determine the correct axis order and the correct axis values. switch (key & 0xFF) { case 0: if (input_dinput_joy_state[joyNum].lX <= 0x3FFF) return TRUE; break; case 1: if (input_dinput_joy_state[joyNum].lX >= 0xC000) return TRUE; break; case 2: if (input_dinput_joy_state[joyNum].lY <= 0x3FFF) return TRUE; break; case 3: if (input_dinput_joy_state[joyNum].lY >= 0xC000) return TRUE; break; case 4: if (input_dinput_joy_state[joyNum].lZ <= 0x3FFF) return TRUE; break; case 5: if (input_dinput_joy_state[joyNum].lZ >= 0xC000) return TRUE; break; case 6: if (input_dinput_joy_state[joyNum].lRx <= 0x3FFF) return TRUE; break; case 7: if (input_dinput_joy_state[joyNum].lRx >= 0xC000) return TRUE; break; case 8: if (input_dinput_joy_state[joyNum].lRy <= 0x3FFF) return TRUE; break; case 9: if (input_dinput_joy_state[joyNum].lRy >= 0xC000) return TRUE; break; case 10: if (input_dinput_joy_state[joyNum].lRz <= 0x3FFF) return TRUE; break; case 11: if (input_dinput_joy_state[joyNum].lRz >= 0xC000) return TRUE; break; default: // Unknown axis. // TODO: Add support for rglSlider (formerly u-axis and v-axis). break; } break; case INPUT_JOYSTICK_TYPE_BUTTON: // Joystick button. if (input_dinput_joy_state[joyNum].rgbButtons[key & 0xFF]) return TRUE; break; case INPUT_JOYSTICK_TYPE_POVHAT: { // Joystick POV hat. unsigned int povAngle = input_dinput_joy_state[joyNum].rgdwPOV[(key >> 2) & 0x3F]; // A value of -1 or 65,535 indicates the POV hat switch is centered. if (LOWORD(povAngle) == 0xFFFF) return FALSE; // Check the angles based on the key value. switch (key & 0x03) { case INPUT_JOYSTICK_POVHAT_UP: if (povAngle >= 29250 || povAngle <= 6750) return TRUE; break; case INPUT_JOYSTICK_POVHAT_RIGHT: if (povAngle >= 2250 && povAngle <= 15750) return TRUE; break; case INPUT_JOYSTICK_POVHAT_DOWN: if (povAngle >= 11250 && povAngle <= 24750) return TRUE; break; case INPUT_JOYSTICK_POVHAT_LEFT: if (povAngle >= 20250 && povAngle <= 33750) return TRUE; break; } break; } } // Key is not pressed. return FALSE; }