Get the joystick's name
| 120 | |
| 121 | // Get the joystick's name |
| 122 | sf::String getDeviceName(unsigned int index, JOYCAPS caps) |
| 123 | { |
| 124 | // Give the joystick a default name |
| 125 | static const sf::String joystickDescription = "Unknown Joystick"; |
| 126 | |
| 127 | LONG result = 0; |
| 128 | HKEY rootKey = nullptr; |
| 129 | HKEY currentKey = nullptr; |
| 130 | std::basic_string<TCHAR> subkey; |
| 131 | |
| 132 | subkey = REGSTR_PATH_JOYCONFIG; |
| 133 | subkey += TEXT('\\'); |
| 134 | subkey += caps.szRegKey; |
| 135 | subkey += TEXT('\\'); |
| 136 | subkey += REGSTR_KEY_JOYCURR; |
| 137 | |
| 138 | rootKey = HKEY_CURRENT_USER; |
| 139 | result = RegOpenKeyEx(rootKey, subkey.c_str(), 0, KEY_READ, ¤tKey); |
| 140 | |
| 141 | if (result != ERROR_SUCCESS) |
| 142 | { |
| 143 | rootKey = HKEY_LOCAL_MACHINE; |
| 144 | result = RegOpenKeyEx(rootKey, subkey.c_str(), 0, KEY_READ, ¤tKey); |
| 145 | |
| 146 | if (result != ERROR_SUCCESS) |
| 147 | { |
| 148 | sf::err() << "Unable to open registry for joystick at index " << index << ": " |
| 149 | << sf::priv::getErrorString(static_cast<DWORD>(result)) << std::endl; |
| 150 | return joystickDescription; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | std::basic_ostringstream<TCHAR> indexString; |
| 155 | indexString << index + 1; |
| 156 | |
| 157 | subkey = TEXT("Joystick"); |
| 158 | subkey += indexString.str(); |
| 159 | subkey += REGSTR_VAL_JOYOEMNAME; |
| 160 | |
| 161 | std::array<TCHAR, 256> keyData{}; |
| 162 | DWORD keyDataSize = sizeof(keyData); |
| 163 | |
| 164 | result = RegQueryValueEx(currentKey, subkey.c_str(), nullptr, nullptr, reinterpret_cast<LPBYTE>(keyData.data()), &keyDataSize); |
| 165 | RegCloseKey(currentKey); |
| 166 | |
| 167 | if (result != ERROR_SUCCESS) |
| 168 | { |
| 169 | sf::err() << "Unable to query registry key for joystick at index " << index << ": " |
| 170 | << sf::priv::getErrorString(static_cast<DWORD>(result)) << std::endl; |
| 171 | return joystickDescription; |
| 172 | } |
| 173 | |
| 174 | subkey = REGSTR_PATH_JOYOEM; |
| 175 | subkey += TEXT('\\'); |
| 176 | subkey.append(keyData.data(), keyDataSize / sizeof(TCHAR)); |
| 177 | |
| 178 | result = RegOpenKeyEx(rootKey, subkey.c_str(), 0, KEY_READ, ¤tKey); |
| 179 |
no test coverage detected