changeset 17819:892a8e0b5834

8177951: Charset problem when the name of the sound device contains Chinese character Reviewed-by: amenkov, serb Contributed-by: Charlie Jiang <cqjjjjzr@126.com>
author serb
date Tue, 01 Aug 2017 14:18:14 +0800
parents 9bae8d7e5b8a
children dbb5b171a16b
files make/lib/SoundLibraries.gmk src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_Charset_Util.cpp src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_Charset_Util.h src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_DirectSound.cpp src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_MidiIn.cpp src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_MidiOut.c src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_Ports.c
diffstat 7 files changed, 129 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/make/lib/SoundLibraries.gmk	Wed Aug 30 16:56:59 2017 +0530
+++ b/make/lib/SoundLibraries.gmk	Tue Aug 01 14:18:14 2017 +0800
@@ -61,6 +61,7 @@
       -DUSE_PLATFORM_MIDI_IN=TRUE \
       -DUSE_PORTS=TRUE
   LIBJSOUND_SRC_FILES += \
+      PLATFORM_API_WinOS_Charset_Util.cpp \
       PLATFORM_API_WinOS_MidiIn.cpp \
       PLATFORM_API_WinOS_MidiOut.c \
       PLATFORM_API_WinOS_Util.c \
@@ -190,6 +191,7 @@
       OUTPUT_DIR := $(INSTALL_LIBRARIES_HERE), \
       SRC := $(LIBJSOUND_SRC_DIRS), \
       INCLUDE_FILES := Utilities.c $(LIBJSOUND_DAUDIOFILES) \
+          PLATFORM_API_WinOS_Charset_Util.cpp \
           PLATFORM_API_WinOS_DirectSound.cpp, \
       OPTIMIZATION := LOW, \
       CFLAGS := $(CFLAGS_JDKLIB) \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_Charset_Util.cpp	Tue Aug 01 14:18:14 2017 +0800
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#include "PLATFORM_API_WinOS_Charset_Util.h"
+
+#include <cstring>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+LPSTR UnicodeToUTF8(const LPCWSTR lpUnicodeStr)
+{
+    DWORD dwUTF8Len = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, nullptr, 0, nullptr, nullptr);
+    LPSTR lpUTF8Str = new CHAR[dwUTF8Len];
+    memset(lpUTF8Str, 0, sizeof(CHAR) * (dwUTF8Len));
+    WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, lpUTF8Str, dwUTF8Len, nullptr, nullptr);
+    return lpUTF8Str;
+}
+
+void UnicodeToUTF8AndCopy(LPSTR dest, LPCWSTR src, SIZE_T maxLength) {
+    LPSTR utf8EncodedName = UnicodeToUTF8(src);
+    strncpy(dest, utf8EncodedName, maxLength - 1);
+    delete[] utf8EncodedName;
+    dest[maxLength - 1] = '\0';
+}
+
+#ifdef __cplusplus
+}
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_Charset_Util.h	Tue Aug 01 14:18:14 2017 +0800
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#ifndef PLATFORM_API_WINOS_CHARSET_UTILS_H
+#define PLATFORM_API_WINOS_CHARSET_UTILS_H
+
+#include <windows.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// NOTE: It's a caller responbility to free the allocated memory using delete[], just like in UnicodeToUTF8AndCopy function
+LPSTR _cdecl UnicodeToUTF8(const LPCWSTR lpAnsiStr);
+
+void _cdecl UnicodeToUTF8AndCopy(LPSTR dest, LPCWSTR src, SIZE_T maxLength);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- a/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_DirectSound.cpp	Wed Aug 30 16:56:59 2017 +0530
+++ b/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_DirectSound.cpp	Tue Aug 01 14:18:14 2017 +0800
@@ -52,6 +52,9 @@
 }
 #endif
 
+/* include to prevent charset problem */
+#include "PLATFORM_API_WinOS_Charset_Util.h"
+
 #ifdef USE_DEBUG_SILENCING
 #define DEBUG_SILENCING0(p) TRACE0(p)
 #define DEBUG_SILENCING1(p1,p2) TRACE1(p1,p2)
@@ -227,13 +230,13 @@
 }
 
 BOOL CALLBACK DS_GetDescEnum(LPGUID lpGuid,
-                             LPCSTR lpstrDescription,
-                             LPCSTR lpstrModule,
+                             LPCWSTR lpstrDescription,
+                             LPCWSTR lpstrModule,
                              DirectAudioDeviceDescription* desc) {
 
     INT32 cacheIndex = findCacheItemByGUID(lpGuid, g_audioDeviceCache[desc->deviceID].isSource);
     if (cacheIndex == desc->deviceID) {
-        strncpy(desc->name, lpstrDescription, DAUDIO_STRING_LENGTH);
+        UnicodeToUTF8AndCopy(desc->name, lpstrDescription, DAUDIO_STRING_LENGTH);
         //strncpy(desc->description, lpstrModule, DAUDIO_STRING_LENGTH);
         desc->maxSimulLines = -1;
         /* do not continue enumeration */
@@ -257,10 +260,10 @@
     }
     desc->maxSimulLines = 0;
     if (g_audioDeviceCache[desc->deviceID].isSource) {
-        DirectSoundEnumerate((LPDSENUMCALLBACK) DS_GetDescEnum, desc);
+        DirectSoundEnumerateW((LPDSENUMCALLBACKW) DS_GetDescEnum, desc);
         strncpy(desc->description, "DirectSound Playback", DAUDIO_STRING_LENGTH);
     } else {
-        DirectSoundCaptureEnumerate((LPDSENUMCALLBACK) DS_GetDescEnum, desc);
+        DirectSoundCaptureEnumerateW((LPDSENUMCALLBACKW) DS_GetDescEnum, desc);
         strncpy(desc->description, "DirectSound Capture", DAUDIO_STRING_LENGTH);
     }
 
--- a/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_MidiIn.cpp	Wed Aug 30 16:56:59 2017 +0530
+++ b/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_MidiIn.cpp	Tue Aug 01 14:18:14 2017 +0800
@@ -31,6 +31,9 @@
 #include "PLATFORM_API_WinOS_Util.h"
 }
 
+/* include to prevent charset problem */
+#include "PLATFORM_API_WinOS_Charset_Util.h"
+
 #if USE_PLATFORM_MIDI_IN == TRUE
 
 #ifdef USE_ERROR
@@ -248,18 +251,17 @@
     return (INT32) midiInGetNumDevs();
 }
 
-INT32 getMidiInCaps(INT32 deviceID, MIDIINCAPS* caps, INT32* err) {
-    (*err) = midiInGetDevCaps(deviceID, caps, sizeof(MIDIINCAPS));
+INT32 getMidiInCaps(INT32 deviceID, MIDIINCAPSW* caps, INT32* err) {
+    (*err) = midiInGetDevCapsW(deviceID, caps, sizeof(MIDIINCAPS));
     return ((*err) == MMSYSERR_NOERROR);
 }
 
 INT32 MIDI_IN_GetDeviceName(INT32 deviceID, char *name, UINT32 nameLength) {
-    MIDIINCAPS midiInCaps;
+    MIDIINCAPSW midiInCaps;
     INT32 err;
 
     if (getMidiInCaps(deviceID, &midiInCaps, &err)) {
-        strncpy(name, midiInCaps.szPname, nameLength-1);
-        name[nameLength-1] = 0;
+        UnicodeToUTF8AndCopy(name, midiInCaps.szPname, nameLength);
         return MIDI_SUCCESS;
     }
     MIDIIN_CHECK_ERROR;
@@ -279,7 +281,7 @@
 
 
 INT32 MIDI_IN_GetDeviceVersion(INT32 deviceID, char *name, UINT32 nameLength) {
-    MIDIINCAPS midiInCaps;
+    MIDIINCAPSW midiInCaps;
     INT32 err = MIDI_NOT_SUPPORTED;
 
     if (getMidiInCaps(deviceID, &midiInCaps, &err) && (nameLength>7)) {
--- a/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_MidiOut.c	Wed Aug 30 16:56:59 2017 +0530
+++ b/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_MidiOut.c	Tue Aug 01 14:18:14 2017 +0800
@@ -28,6 +28,9 @@
 
 #include "PLATFORM_API_WinOS_Util.h"
 
+/* include to prevent charset problem */
+#include "PLATFORM_API_WinOS_Charset_Util.h"
+
 #if USE_PLATFORM_MIDI_OUT == TRUE
 
 
@@ -66,24 +69,23 @@
 }
 
 
-INT32 getMidiOutCaps(INT32 deviceID, MIDIOUTCAPS* caps, INT32* err) {
+INT32 getMidiOutCaps(INT32 deviceID, MIDIOUTCAPSW* caps, INT32* err) {
     if (deviceID == 0) {
         deviceID = MIDI_MAPPER;
     } else {
         deviceID--;
     }
-    (*err) = (INT32) midiOutGetDevCaps(deviceID, caps, sizeof(MIDIOUTCAPS));
+    (*err) = (INT32) midiOutGetDevCapsW(deviceID, caps, sizeof(MIDIOUTCAPS));
     return ((*err) == MMSYSERR_NOERROR);
 }
 
 
 INT32 MIDI_OUT_GetDeviceName(INT32 deviceID, char *name, UINT32 nameLength) {
-    MIDIOUTCAPS midiOutCaps;
+    MIDIOUTCAPSW midiOutCaps;
     INT32 err;
 
     if (getMidiOutCaps(deviceID, &midiOutCaps, &err)) {
-        strncpy(name, midiOutCaps.szPname, nameLength-1);
-        name[nameLength-1] = 0;
+        UnicodeToUTF8AndCopy(name, midiOutCaps.szPname, nameLength);
         return MIDI_SUCCESS;
     }
     MIDIOUT_CHECK_ERROR;
@@ -97,7 +99,7 @@
 
 
 INT32 MIDI_OUT_GetDeviceDescription(INT32 deviceID, char *name, UINT32 nameLength) {
-    MIDIOUTCAPS midiOutCaps;
+    MIDIOUTCAPSW midiOutCaps;
     char *desc;
     INT32 err;
 
@@ -134,7 +136,7 @@
 
 
 INT32 MIDI_OUT_GetDeviceVersion(INT32 deviceID, char *name, UINT32 nameLength) {
-    MIDIOUTCAPS midiOutCaps;
+    MIDIOUTCAPSW midiOutCaps;
     INT32 err;
 
     if (getMidiOutCaps(deviceID, &midiOutCaps, &err) && nameLength>7) {
--- a/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_Ports.c	Wed Aug 30 16:56:59 2017 +0530
+++ b/src/java.desktop/windows/native/libjsound/PLATFORM_API_WinOS_Ports.c	Tue Aug 01 14:18:14 2017 +0800
@@ -38,6 +38,9 @@
 #include <mmsystem.h>
 #include "Ports.h"
 
+/* include to prevent charset problem */
+#include "PLATFORM_API_WinOS_Charset_Util.h"
+
 #if USE_PORTS == TRUE
 
 typedef struct tag_PortControlID PortControlID;
@@ -353,10 +356,9 @@
 ///// implemented functions of Ports.h
 
 INT32 PORT_GetPortMixerDescription(INT32 mixerIndex, PortMixerDescription* description) {
-    MIXERCAPS mixerCaps;
-    if (mixerGetDevCaps(mixerIndex, &mixerCaps, sizeof(MIXERCAPS)) == MMSYSERR_NOERROR) {
-        strncpy(description->name, mixerCaps.szPname, PORT_STRING_LENGTH-1);
-        description->name[PORT_STRING_LENGTH-1] = 0;
+    MIXERCAPSW mixerCaps;
+    if (mixerGetDevCapsW(mixerIndex, &mixerCaps, sizeof(MIXERCAPS)) == MMSYSERR_NOERROR) {
+        UnicodeToUTF8AndCopy(description->name, mixerCaps.szPname, PORT_STRING_LENGTH);
         sprintf(description->version, "%d.%d", (mixerCaps.vDriverVersion & 0xFF00) >> 8, mixerCaps.vDriverVersion & 0xFF);
         strncpy(description->description, "Port Mixer", PORT_STRING_LENGTH-1);
         return TRUE;