@@ -120,6 +120,41 @@ static int SDL_GetEmscriptenOSID()
120120 });
121121}
122122
123+ EM_JS_DEPS (sdljoystick , "$stringToUTF8" );
124+
125+ static void SDL_GetEmscriptenNormalizedName (int device_index , char * out , int length )
126+ {
127+ MAIN_THREAD_EM_ASM ({
128+ let gamepad = navigator ['getGamepads' ]()[$0 ];
129+ if (!gamepad ) {
130+ stringToUTF8 ('\0' , $1 , $2 ); // Silence the compiler here, because '' is not valid
131+ return ;
132+ }
133+
134+ let id = gamepad ['id' ];
135+ let output = id ;
136+ // Chrome
137+ if (id ['indexOf' ](' (STANDARD GAMEPAD' ) > 0 ) { // Wireless Controller (STANDARD GAMEPAD Vendor: 054c Product: 09cc)
138+ output = id ['substr' ](0 , id ['indexOf' ](' (STANDARD GAMEPAD' ));
139+ } else if (id ['indexOf' ](' (Vendor:' ) > 0 ) { // usb gamepad (Vendor: 0810 Product: e501)
140+ output = id ['substr' ](0 , id ['indexOf' ](' (Vendor:' ));
141+ } else if (id ['indexOf' ](' (XInput' ) > 0 ) { // Xbox 360 Controller (XInput STANDARD GAMEPAD)
142+ output = id ['substr' ](0 , id ['indexOf' ](' (XInput' ));
143+ }
144+
145+ // Firefox, Safari: "046d-c216-Logitech Dual Action", "46d-c216-Logicool Dual Action", or "xinput"
146+ let id_split = id ['split' ]('-' );
147+ if (id_split ['length' ] > 1 && !isNaN (parseInt (id_split [0 ], 16 ))) {
148+ // Let's not assume the length of the vendor/product IDs in the string
149+ // and just find the second '-' using indexOf
150+ let start = id ['indexOf' ]('-' , id ['indexOf' ]('-' )+ 1 )+ 1 ;
151+ output = id ['substr' ](start );
152+ }
153+
154+ stringToUTF8 (output .trim (), $1 , $2 );
155+ }, device_index , out , length );
156+ }
157+
123158static EM_BOOL Emscripten_JoyStickConnected (int eventType , const EmscriptenGamepadEvent * gamepadEvent , void * userData )
124159{
125160 SDL_joylist_item * item ;
@@ -128,6 +163,7 @@ static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamep
128163 Uint16 vendor , product ;
129164 Uint8 os_id ;
130165 bool is_xinput ;
166+ char name [128 ];
131167
132168 SDL_LockJoysticks ();
133169
@@ -187,14 +223,15 @@ static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamep
187223 os_id += 0x80 ;
188224 }
189225
190- item -> name = SDL_CreateJoystickName (vendor , product , NULL , gamepadEvent -> id );
226+ SDL_GetEmscriptenNormalizedName (gamepadEvent -> index , name , sizeof (name ));
227+ item -> name = SDL_CreateJoystickName (vendor , product , NULL , name );
191228 if (!item -> name ) {
192229 SDL_free (item );
193230 goto done ;
194231 }
195232
196233 if (vendor && product ) {
197- item -> guid = SDL_CreateJoystickGUID (bus , vendor , product , 0 , NULL , gamepadEvent -> id , 0 , os_id );
234+ item -> guid = SDL_CreateJoystickGUID (bus , vendor , product , 0 , NULL , name , 0 , os_id );
198235 } else {
199236 item -> guid = SDL_CreateJoystickGUIDForName (item -> name );
200237 item -> guid .data [15 ] = os_id ;
0 commit comments