ViGEmClient icon indicating copy to clipboard operation
ViGEmClient copied to clipboard

vigem_target_x360_get_user_index returns wrong user index

Open stephen9357 opened this issue 2 years ago • 6 comments

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):

  • OS: Windows 10 pro 19044.3086
  • Feeder software: ViGEmBus, download from https://github.com/ViGEm/ViGEmBus/releases/tag/v1.21.442.0
  • Driver Version: 1.21.442.0

Additional context Add any other context about the problem here.

stephen9357 avatar Jun 27 '23 09:06 stephen9357

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.

nefarius avatar Jun 27 '23 10:06 nefarius

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.

stephen9357 avatar Jun 27 '23 10:06 stephen9357

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

nefarius avatar Jun 27 '23 10:06 nefarius

subscribe to a EVT_VIGEM_X360_NOTIFICATION and print the LedNumber.

Thanks you very much, it works perfectly.

stephen9357 avatar Jun 27 '23 10:06 stephen9357

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 😬

nefarius avatar Jun 27 '23 13:06 nefarius

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:

  • I'm calling vigem_target_get_index() first, before vigem_target_x360_get_user_index() (I also call vigem_target_get_vid() and vigem_target_get_pid() first).
  • I'm not adding multiple devices in a tight loop (multiple, yes, but each is user-initiated).

(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...

  • Windows 10 Pro, 22H2, 19045.3324 (also tested with some other Win10 installs)
  • ViGEm driver 1.21.442.0 from main GH project releases
  • ViGEm Client from latest master (no changes at all), x64 Release_LIB built with VS2022 (but keeping v142/VS2019 toolset), static link into another lib which is also built using v142/VS19 toolset.

LMK if any other details or info would be helpful.

Thanks for ViGEm!

-Max

mpaperno avatar Aug 30 '23 22:08 mpaperno