test/testaudioinfo.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Nov 2014 04:51:33 -0800
changeset 9246 a761913e5e91
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed bug 2786 - "UCS-2-INTERNAL" iconv encoding is not supported everywhere, use UTF-16LE instead

Jonas Kulla

src/main/windows/SDL_windows_main.c:137:
cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));

I'm trying to compile an SDL2 application for windows using the mingw-w64 32bit toolchain provided by my distro (Fedora 19). However, even the simplest test program that does nothing at all fails to startup with a "Fatal error - out of memory" message because the mingw iconv library provided by my distro does not support the "UCS-2-INTERNAL" encoding and the conversion returns null.

From my little bit of research, it turns out that even though this encoding is supported by the external GNU libiconv library, some glibc versions (?) don't support it with their internal iconv routines, and will instead provide the native endian encoding when "UCS-2" is specified.

Nonetheless, I wonder why the native endianness is considered in the first place when Windows doesn't even run on any big endian archs (to my knowledge). And true enough, 'WIN_StringToUTF8' from core/windows/SDL_windows.h is used everywhere else in the windows backend, which is just a macro to iconv with "UTF-16LE" as source. Therefore it would IMO make sense to use this macro here as well, which would solve my problem (patch attached).
     1 /*
     2   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 #include <stdio.h>
    13 #include "SDL.h"
    14 
    15 static void
    16 print_devices(int iscapture)
    17 {
    18     const char *typestr = ((iscapture) ? "capture" : "output");
    19     int n = SDL_GetNumAudioDevices(iscapture);
    20 
    21     SDL_Log("%s devices:\n", typestr);
    22 
    23     if (n == -1)
    24         SDL_Log("  Driver can't detect specific %s devices.\n\n", typestr);
    25     else if (n == 0)
    26         SDL_Log("  No %s devices found.\n\n", typestr);
    27     else {
    28         int i;
    29         for (i = 0; i < n; i++) {
    30             SDL_Log("  %s\n", SDL_GetAudioDeviceName(i, iscapture));
    31         }
    32         SDL_Log("\n");
    33     }
    34 }
    35 
    36 int
    37 main(int argc, char **argv)
    38 {
    39     int n;
    40 
    41     /* Enable standard application logging */
    42     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    43 
    44     /* Load the SDL library */
    45     if (SDL_Init(SDL_INIT_AUDIO) < 0) {
    46         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    47         return (1);
    48     }
    49 
    50     /* Print available audio drivers */
    51     n = SDL_GetNumAudioDrivers();
    52     if (n == 0) {
    53         SDL_Log("No built-in audio drivers\n\n");
    54     } else {
    55         int i;
    56         SDL_Log("Built-in audio drivers:\n");
    57         for (i = 0; i < n; ++i) {
    58             SDL_Log("  %s\n", SDL_GetAudioDriver(i));
    59         }
    60         SDL_Log("\n");
    61     }
    62 
    63     SDL_Log("Using audio driver: %s\n\n", SDL_GetCurrentAudioDriver());
    64 
    65     print_devices(0);
    66     print_devices(1);
    67 
    68     SDL_Quit();
    69     return 0;
    70 }