test/testnativecocoa.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Nov 2014 04:51:33 -0800
changeset 9246 a761913e5e91
parent 3057 089a77aebb7d
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).
slouken@3057
     1
slouken@3057
     2
#include "testnative.h"
slouken@3057
     3
slouken@3057
     4
#ifdef TEST_NATIVE_COCOA
slouken@3057
     5
slouken@3057
     6
#include <Cocoa/Cocoa.h>
slouken@3057
     7
slouken@3057
     8
static void *CreateWindowCocoa(int w, int h);
slouken@3057
     9
static void DestroyWindowCocoa(void *window);
slouken@3057
    10
slouken@3057
    11
NativeWindowFactory CocoaWindowFactory = {
slouken@3057
    12
    "cocoa",
slouken@3057
    13
    CreateWindowCocoa,
slouken@3057
    14
    DestroyWindowCocoa
slouken@3057
    15
};
slouken@3057
    16
slouken@3057
    17
static void *CreateWindowCocoa(int w, int h)
slouken@3057
    18
{
slouken@3057
    19
    NSAutoreleasePool *pool;
slouken@3057
    20
    NSWindow *nswindow;
slouken@3057
    21
    NSRect rect;
slouken@3057
    22
    unsigned int style;
slouken@3057
    23
slouken@3057
    24
    pool = [[NSAutoreleasePool alloc] init];
slouken@3057
    25
slouken@3057
    26
    rect.origin.x = 0;
slouken@3057
    27
    rect.origin.y = 0;
slouken@3057
    28
    rect.size.width = w;
slouken@3057
    29
    rect.size.height = h;
slouken@3057
    30
    rect.origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - rect.origin.y - rect.size.height;
slouken@3057
    31
slouken@3057
    32
    style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
slouken@3057
    33
slouken@3057
    34
    nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
slouken@3057
    35
    [nswindow makeKeyAndOrderFront:nil];
slouken@3057
    36
slouken@3057
    37
    [pool release];
slouken@3057
    38
slouken@3057
    39
    return nswindow;
slouken@3057
    40
}
slouken@3057
    41
slouken@3057
    42
static void DestroyWindowCocoa(void *window)
slouken@3057
    43
{
slouken@3057
    44
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3057
    45
    NSWindow *nswindow = (NSWindow *)window;
slouken@3057
    46
slouken@3057
    47
    [nswindow close];
slouken@3057
    48
    [pool release];
slouken@3057
    49
}
slouken@3057
    50
slouken@3057
    51
#endif