Questions about Joystick input programming on Haiku

Hi devs…

I’m working on library for Joystick input and got stuck at some part when trying to detect buttons pressed and other things…

Reading about button1 and button2 in BJoystick property made me think that Haiku and BeOS Joystick support is like FreeBSD’s one, Which limited to 2 buttons…

And if no, Here are my questions:

Q1: stick->ButtonValues function of BJoystick class returns 32-bit number of buttons pressed but i don’t have idea what code or index or bit number (Whatever you name it) of each button in Joystick?
(Because Haiku works differently than Microsoft Windows…)

Q2: Also, Is it possible to handle input of more than one Joystick?

Q3: Can same Linux Joystick input code that uses on linux/joystick.h works well by just changing device file path?

And if answers can be given with some sort of C++ code to get the idea i would be thankful!

And thanks!

https://www.haiku-os.org/legacy-docs/bebook/BJoystick.html

1 Like

The leftmost bit is button 1 and then each next bit is button 2, 3, 4, …

They are in the order that the joystick reports them (in USB HID reports for example). We have no control over that.

Yes, BJoystick provides a way to enumerate joysticks (CountDevices() + GetDeviceName()) and you can open different ones using the Open() function.

No, the low-level interface is incompatible.

A simple example would look like this:

	int playerIndex = 0; // or 1 for the second gamepad, etc
	BJoystick gamepad;
	if (gamepad.Update() != B_OK) {
		// We could not read from the gamepad, maybe it was unplugged. Try to reopen it
		char deviceName[B_OS_NAME_LENGTH];
		gamepad.RescanDevices();
		gamepad.GetDeviceName(playerIndex, deviceName);
		gamepad.Open(deviceName, false);
	} else {
		// Test some gamepad buttons
		int32 buttons = gamepad.ButtonsValues();
		bool button1 = buttons & (1 << 0);
		bool button2 = buttons & (1 << 1);
		// axis values are available in gamepad.horizontal and gamepad.vertical, or alternatively using GetAxisValues() (which can handle more than 2 axis)
	}

You can ignore the “forStick” parameter mentioned in the Be Book documentation, it is useful only for legacy gameports where two joysticks could be connected to the same gameport. For USB, each gameport has a separate device accessed by calling Open().

3 Likes

Thanks so much! :slight_smile:

@PulkoMandy
Thanks and sorry if annoyed you with some sort of question:

Is there someway table for gamepad buttons values? Be like…

#define BUTTON_A 0
#define BUTTON_B 1
#define BUTTON_X 2
#define BUTTON_Y 3
// And more...

I scratched values although these are wrong, If you know values of each button in modern Joysticks can you point if i’m doing right or wrong?

#define ICE_JOY_BUTTON_NONE                 -1
#define ICE_JOY_BUTTON_A                    0
#define ICE_JOY_BUTTON_B                    1
#define ICE_JOY_BUTTON_X                    2
#define ICE_JOY_BUTTON_Y                    3
#define ICE_JOY_BUTTON_CROSS                ICE_JOY_BUTTON_A
#define ICE_JOY_BUTTON_CIRCLE               ICE_JOY_BUTTON_B
#define ICE_JOY_BUTTON_SQUARE               ICE_JOY_BUTTON_X
#define ICE_JOY_BUTTON_TRIANGLE             ICE_JOY_BUTTON_Y
#define ICE_JOY_BUTTON_LB                   4
#define ICE_JOY_BUTTON_RB                   5
#define ICE_JOY_BUTTON_LT                   6
#define ICE_JOY_BUTTON_RT                   7
#define ICE_JOY_BUTTON_L1                   ICE_JOY_BUTTON_LB
#define ICE_JOY_BUTTON_R1                   ICE_JOY_BUTTON_RB
#define ICE_JOY_BUTTON_L2                   ICE_JOY_BUTTON_LT
#define ICE_JOY_BUTTON_R2                   ICE_JOY_BUTTON_RT
#define ICE_JOY_BUTTON_L3                   10
#define ICE_JOY_BUTTON_R3                   11
#define ICE_JOY_BUTTON_START                9
#define ICE_JOY_BUTTON_MENU                 ICE_JOY_BUTTON_START
#define ICE_JOY_BUTTON_OPTIONS              ICE_JOY_BUTTON_START
#define ICE_JOY_BUTTON_SELECT               8
#define ICE_JOY_BUTTON_BACK                 ICE_JOY_BUTTON_SELECT
#define ICE_JOY_BUTTON_VIEW                 ICE_JOY_BUTTON_SELECT
#define ICE_JOY_BUTTON_DPAD_UP              12
#define ICE_JOY_BUTTON_DPAD_DOWN            13
#define ICE_JOY_BUTTON_DPAD_LEFT            14
#define ICE_JOY_BUTTON_DPAD_RIGHT           15

As I said, it depends on the joystick/gamepad you use. They all have different layouts and ways to name the buttons. We just keep them in the orders the device sends them to us and have no way to know the button labels.

Your numbering is what I would do if I was designing a gamepad but I can’t guarantee that all hardware manufacturers would agree with it.

1 Like

Thanks so much you helped me a loooooot! :heart:

OK, Gonna leave it so if issues open with fix gonna merge it! :wink: