We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2052

Summary: String conversion of XAudio2 and DirectSound backends are UCS-2BE
Product: SDL Reporter: norfanin
Component: audioAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 2.0   
Hardware: All   
OS: Windows (All)   
Attachments: patch as attachment
patch as attachment again
one last time

Description norfanin 2013-08-21 23:42:10 UTC
SDL_xaudio2.c and SDL_directsound.c didn't get the memo that the iconv UCS-2 defaults to big endian now. This breaks all display-name string conversions for the devices of these backends.

The attached patch removes the duplicated code and uses the WIN_StringToUTF8 macros from the src/core/windows/SDL_windows.h header instead. It's still not UTF-16, but what's the chance device names contain surrogates or other unexpected stuff?

(Mr Bennefall was talking about this issue on the mailing list and I thought I had a look at it.)
Comment 1 norfanin 2013-08-21 23:50:17 UTC
Uhm. Can't attach anything. It errors with "The file you are trying to attach is empty, does not exist, or you don't have permission to read it." Let's hope the comment section survives this.

diff -r adacc93b5213 src/audio/directsound/SDL_directsound.c
--- a/src/audio/directsound/SDL_directsound.c	Wed Aug 21 12:12:04 2013 -0700
+++ b/src/audio/directsound/SDL_directsound.c	Thu Aug 22 01:30:20 2013 +0200
@@ -91,14 +91,6 @@
     return loaded;
 }
 
-static __inline__ char *
-utf16_to_utf8(const WCHAR *S)
-{
-    /* !!! FIXME: this should be UTF-16, not UCS-2! */
-    return SDL_iconv_string("UTF-8", "UCS-2", (char *)(S),
-                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
-}
-
 static int
 SetDSerror(const char *function, int code)
 {
@@ -158,7 +150,7 @@
 {
     SDL_AddAudioDevice addfn = (SDL_AddAudioDevice) data;
     if (guid != NULL) {  /* skip default device */
-        char *str = utf16_to_utf8(desc);
+        char *str = WIN_StringToUTF8(desc);
         if (str != NULL) {
             addfn(str);
             SDL_free(str);  /* addfn() makes a copy of this string. */
@@ -439,7 +431,7 @@
 {
     if (guid != NULL) {  /* skip the default device. */
         FindDevGUIDData *data = (FindDevGUIDData *) _data;
-        char *str = utf16_to_utf8(desc);
+        char *str = WIN_StringToUTF8(desc);
         const int match = (SDL_strcmp(str, data->devname) == 0);
         SDL_free(str);
         if (match) {
diff -r adacc93b5213 src/audio/xaudio2/SDL_xaudio2.c
--- a/src/audio/xaudio2/SDL_xaudio2.c	Wed Aug 21 12:12:04 2013 -0700
+++ b/src/audio/xaudio2/SDL_xaudio2.c	Thu Aug 22 01:30:20 2013 +0200
@@ -60,14 +60,6 @@
 };
 
 
-static __inline__ char *
-utf16_to_utf8(const WCHAR *S)
-{
-    /* !!! FIXME: this should be UTF-16, not UCS-2! */
-    return SDL_iconv_string("UTF-8", "UCS-2", (char *)(S),
-                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
-}
-
 static void
 XAUDIO2_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
 {
@@ -90,7 +82,7 @@
     for (i = 0; i < devcount; i++) {
         XAUDIO2_DEVICE_DETAILS details;
         if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
-            char *str = utf16_to_utf8(details.DisplayName);
+            char *str = WIN_StringToUTF8(details.DisplayName);
             if (str != NULL) {
                 addfn(str);
                 SDL_free(str);  /* addfn() made a copy of the string. */
@@ -265,7 +257,7 @@
         for (i = 0; i < devcount; i++) {
             XAUDIO2_DEVICE_DETAILS details;
             if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
-                char *str = utf16_to_utf8(details.DisplayName);
+                char *str = WIN_StringToUTF8(details.DisplayName);
                 if (str != NULL) {
                     const int match = (SDL_strcmp(str, devname) == 0);
                     SDL_free(str);
Comment 2 Ryan C. Gordon 2013-08-22 17:28:28 UTC
Created attachment 1301 [details]
patch as attachment


Testing attachments...

--ryan.
Comment 3 Ryan C. Gordon 2013-08-22 17:28:59 UTC
Created attachment 1302 [details]
patch as attachment again

Ignore me.
Comment 4 Ryan C. Gordon 2013-08-22 17:29:25 UTC
Created attachment 1303 [details]
one last time

ignore this too
Comment 5 Ryan C. Gordon 2013-08-22 17:30:39 UTC
(In reply to norfanin from comment #1)
> Uhm. Can't attach anything. It errors with "The file you are trying to
> attach is empty, does not exist, or you don't have permission to read it."

Weird, don't know why that happened. If it happens to you again, email me directly and we'll figure it out.

Looking at the actual patch now.

--ryan.
Comment 6 Ryan C. Gordon 2013-08-22 17:32:48 UTC
This is now http://hg.libsdl.org/SDL/rev/c64896ddeeae, thanks!

--ryan.