diff --git a/headers/os/device/Joystick.h b/headers/os/device/Joystick.h index 776124d4ff..82fdd45244 100644 --- a/headers/os/device/Joystick.h +++ b/headers/os/device/Joystick.h @@ -64,6 +64,16 @@ public: int32 CountButtons(); uint32 ButtonValues(int32 forStick = 0); + // Allows access to the first 32 buttons where + // each set bit indicates a pressed button. + status_t GetButtonValues(bool* outButtons, + int32 forStick = 0); + // Haiku extension. Allows to retrieve the state + // of an arbitrary count of buttons. The + // outButtons argument is an array of boolean + // values with at least CountButtons() elements. + // True means the button is pressed and false + // means it is released. status_t GetButtonNameAt(int32 index, BString* outName); diff --git a/src/kits/device/Joystick.cpp b/src/kits/device/Joystick.cpp index 2b52e0ee18..8c5f120e52 100644 --- a/src/kits/device/Joystick.cpp +++ b/src/kits/device/Joystick.cpp @@ -4,6 +4,7 @@ * Distributed under the terms of the MIT License. */ + #include #include @@ -433,6 +434,35 @@ BJoystick::ButtonValues(int32 forStick) } +status_t +BJoystick::GetButtonValues(bool *outButtons, int32 forStick) +{ + CALLED(); + + if (fJoystickInfo == NULL || fExtendedJoystick == NULL) + return B_NO_INIT; + + if (forStick < 0 + || forStick >= (int32)fJoystickInfo->module_info.num_sticks) + return B_BAD_INDEX; + + extended_joystick *extendedJoystick + = (extended_joystick *)fExtendedJoystick->ItemAt(forStick); + if (extendedJoystick == NULL) + return B_NO_INIT; + + int16 buttonCount = fJoystickInfo->module_info.num_buttons; + for (int16 i = 0; i < buttonCount; i++) { + if (buttonCount >= 32) + outButtons[i] = false; + else + outButtons[i] = (extendedJoystick->buttons & (1 << i)) != 0; + } + + return B_OK; +} + + status_t BJoystick::GetButtonNameAt(int32 index, BString *outName) {