nefarius / ViGEmClient

ViGEm Client SDK for feeder development.
https://docs.nefarius.at/projects/ViGEm/
MIT License
130 stars 64 forks source link

vigem_target_x360_get_user_index returns wrong user index #31

Open stephen9357 opened 1 year ago

stephen9357 commented 1 year ago

Describe the bug Create and add four x360 devices to the bus, call vigem_target_x360_get_user_index to query the user index, it always returns 0.

To Reproduce Steps to reproduce the behavior (example): Compile and run this code.

int main()
{
    auto* client = vigem_alloc();
    if (!client) {
        cout << "Allocate client failed." << endl;
        return -1;
    }

    auto ret = vigem_connect(client);
    if (!VIGEM_SUCCESS(ret)) {
        cout << "Connect failed" << endl;
        vigem_free(client);
        return -1;
    }

    for (size_t i = 0; i < XUSER_MAX_COUNT; i++) {
        auto* xbox360 = vigem_target_x360_alloc();
        if (!xbox360) {
            cout << "Allocate xbox360 #" << i << " failed." << endl;
            continue;
        }

        ret = vigem_target_add(client, xbox360);
        if (!VIGEM_SUCCESS(ret)) {
            cout << "Add xbox360 #" << i << " failed." << endl;
            vigem_target_free(xbox360);
            continue;
        }

       ULONG index = 0xff;
        ret = vigem_target_x360_get_user_index(client, xbox360, &index);
        if (!VIGEM_SUCCESS(ret)) {
            cout << "Get user index #" << i << " failed." << endl;
            vigem_target_remove(client, xbox360);
            vigem_target_free(xbox360);
            continue;
        }
        cout << "#" << i << ": user index: " << index << endl;

        index = vigem_target_get_index(xbox360);
        cout << "#" << i << ": serial number: " << index << endl; 
    }

    vigem_disconnect(client);

    vigem_free(client);

    return 0;
}

Expected behavior Output as below.

0: user index: 0

0: serial number: 1

1: user index: 1

1: serial number: 2

2: user index: 2

2: serial number: 3

3: user index: 3

3: serial number: 4

Screenshots image

System details (please complete the following information):

Additional context Add any other context about the problem here.

nefarius commented 1 year ago

This function never really was very reliable, it's timing sensitive due to the now aged code base that never really supported capturing the player index.

Here's a primitive test: add like a sleep 500 or greater after the plugin call and see if that changes things.

stephen9357 commented 1 year ago

add like a sleep 500 or greater after the plugin call and see if that changes things.

Thanks for your reply, it does't works. I add std::this_thread::sleep_for(std::chrono::seconds(10)); and XInputGetState before call vigem_target_x360_get_user_index, still returns 0.

nefarius commented 1 year ago

OK, hm, please subscribe to a EVT_VIGEM_X360_NOTIFICATION and print the LedNumber.

stephen9357 commented 1 year ago

subscribe to a EVT_VIGEM_X360_NOTIFICATION and print the LedNumber.

Thanks you very much, it works perfectly.

nefarius commented 1 year ago

subscribe to a EVT_VIGEM_X360_NOTIFICATION and print the LedNumber.

Thanks you very much, it works perfectly.

Great to hear! I guess figuring out what's wrong with the vigem_target_x360_get_user_index is still open, will leave the issue as a reminder for 2027 😬

mpaperno commented 11 months ago

Hi,

Strangely, and FWIW, vigem_target_x360_get_user_index() is working properly for me.

I haven't tried the repro example in the OP here (yet), but essentially I'm using it the same way -- vigem_target_x360_alloc() then vigem_target_add() and then get the LED number. The obvious differences are that:

(The full code is in here.)

I was a bit surprised when the resulting "user index" is zero-based, but it does increment for each new device, so I guess that's right.

Similar specs to the OP...

LMK if any other details or info would be helpful.

Thanks for ViGEm!

-Max