locale: Implemented SDL_GetPreferredLocales().

This was something I proposed a long time ago, Sylvain Becker did
additional work on it, then back to me.

Fixes Bugzilla #2131.
This commit is contained in:
Ryan C. Gordon
2020-05-04 02:27:29 -04:00
parent 1e5dd06f83
commit fa23e3d00b
42 changed files with 1318 additions and 4 deletions

View File

@@ -44,6 +44,8 @@
#include "../../haptic/android/SDL_syshaptic_c.h"
#include <android/log.h>
#include <android/configuration.h>
#include <android/asset_manager_jni.h>
#include <sys/system_properties.h>
#include <pthread.h>
#include <sys/types.h>
@@ -2796,6 +2798,82 @@ SDL_bool Android_JNI_RequestPermission(const char *permission)
return bPermissionRequestResult;
}
int Android_JNI_GetLocale(char *buf, size_t buflen)
{
struct LocalReferenceHolder refs = LocalReferenceHolder_Setup(__FUNCTION__);
JNIEnv* env = Android_JNI_GetEnv();
int retval = -1;
JNIEnv *mEnv = Android_JNI_GetEnv();
if (!LocalReferenceHolder_Init(&refs, env)) {
LocalReferenceHolder_Cleanup(&refs);
return -1;
}
SDL_assert(buflen > 6);
jmethodID mid;
jobject context;
jobject assetManager;
/* context = SDLActivity.getContext(); */
mid = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"getContext","()Landroid/content/Context;");
context = (*mEnv)->CallStaticObjectMethod(mEnv, mActivityClass, mid);
/* assetManager = context.getAssets(); */
mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
"getAssets", "()Landroid/content/res/AssetManager;");
assetManager = (*mEnv)->CallObjectMethod(mEnv, context, mid);
/* API from NDK: android/configuration.h */
/* API from NDK: android/asset_manager_jni.h */
AAssetManager* asset_mgr = AAssetManager_fromJava(env, assetManager);
AConfiguration *cfg = AConfiguration_new();
if (asset_mgr && cfg)
{
char language[2] = {};
char country[2] = {};
size_t id = 0;
AConfiguration_fromAssetManager(cfg, asset_mgr);
AConfiguration_getLanguage(cfg, language);
AConfiguration_getCountry(cfg, country);
retval = 0;
/* copy language (not null terminated) */
if (language[0]) {
buf[id++] = language[0];
if (language[1]) {
buf[id++] = language[1];
}
}
buf[id++] = '_';
/* copy country (not null terminated) */
if (country[0]) {
buf[id++] = country[0];
if (country[1]) {
buf[id++] = country[1];
}
}
buf[id++] = '\0';
SDL_assert(id <= buflen);
}
if (cfg) {
AConfiguration_delete(cfg);
}
LocalReferenceHolder_Cleanup(&refs);
return retval;
}
#endif /* __ANDROID__ */
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -104,6 +104,9 @@ void Android_JNI_InitTouch(void);
JNIEnv *Android_JNI_GetEnv(void);
int Android_JNI_SetupThread(void);
/* Locale */
int Android_JNI_GetLocale(char *buf, size_t buflen);
/* Generic messages */
int Android_JNI_SendMessage(int command, int param);

View File

@@ -763,3 +763,4 @@
#define SDL_Metal_GetDrawableSize SDL_Metal_GetDrawableSize_REAL
#define SDL_trunc SDL_trunc_REAL
#define SDL_truncf SDL_truncf_REAL
#define SDL_GetPreferredLocales SDL_GetPreferredLocales_REAL

View File

@@ -822,3 +822,4 @@ SDL_DYNAPI_PROC(void*,SDL_Metal_GetLayer,(SDL_MetalView a),(a),return)
SDL_DYNAPI_PROC(void,SDL_Metal_GetDrawableSize,(SDL_Window *a, int *b, int *c),(a,b,c),)
SDL_DYNAPI_PROC(double,SDL_trunc,(double a),(a),return)
SDL_DYNAPI_PROC(float,SDL_truncf,(float a),(a),return)
SDL_DYNAPI_PROC(SDL_Locale *,SDL_GetPreferredLocales,(void),(),return)

View File

@@ -1009,6 +1009,12 @@ SDL_SendKeymapChangedEvent(void)
return SDL_SendAppEvent(SDL_KEYMAPCHANGED);
}
int
SDL_SendLocaleChangedEvent(void)
{
return SDL_SendAppEvent(SDL_LOCALECHANGED);
}
int
SDL_EventsInit(void)
{

View File

@@ -46,6 +46,7 @@ extern void SDL_QuitInterrupt(void);
extern int SDL_SendAppEvent(SDL_EventType eventType);
extern int SDL_SendSysWMEvent(SDL_SysWMmsg * message);
extern int SDL_SendKeymapChangedEvent(void);
extern int SDL_SendLocaleChangedEvent(void);
extern int SDL_SendQuit(void);

103
src/locale/SDL_locale.c Normal file
View File

@@ -0,0 +1,103 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
#include "SDL_syslocale.h"
#include "SDL_hints.h"
static SDL_Locale *
build_locales_from_csv_string(char *csv)
{
size_t num_locales = 1; /* at least one */
size_t slen;
size_t alloclen;
char *ptr;
SDL_Locale *loc;
SDL_Locale *retval;
if (!csv || !csv[0]) {
return NULL; /* nothing to report */
}
for (ptr = csv; *ptr; ptr++) {
if (*ptr == ',') {
num_locales++;
}
}
num_locales++; /* one more for terminator */
slen = ((size_t) (ptr - csv)) + 1; /* strlen(csv) + 1 */
alloclen = slen + (num_locales * sizeof (SDL_Locale));
loc = retval = (SDL_Locale *) SDL_calloc(1, alloclen);
if (!retval) {
SDL_OutOfMemory();
return NULL; /* oh well */
}
ptr = (char *) (retval + num_locales);
SDL_strlcpy(ptr, csv, slen);
while (SDL_TRUE) { /* parse out the string */
while (*ptr == ' ') ptr++; /* skip whitespace. */
if (*ptr == '\0') {
break;
}
loc->language = ptr++;
while (SDL_TRUE) {
const char ch = *ptr;
if (ch == '_') {
*(ptr++) = '\0';
loc->country = ptr;
} else if (ch == ' ') {
*(ptr++) = '\0'; /* trim ending whitespace and keep going. */
} else if (ch == ',') {
*(ptr++) = '\0';
loc++;
break;
} else if (ch == '\0') {
loc++;
break;
} else {
ptr++; /* just keep going, still a valid string */
}
}
}
return retval;
}
SDL_Locale *
SDL_GetPreferredLocales(void)
{
char locbuf[128]; /* enough for 21 "xx_YY," language strings. */
const char *hint = SDL_GetHint(SDL_HINT_PREFERRED_LOCALES);
if (hint) {
SDL_strlcpy(locbuf, hint, sizeof (locbuf));
} else {
SDL_zeroa(locbuf);
SDL_SYS_GetPreferredLocales(locbuf, sizeof (locbuf));
}
return build_locales_from_csv_string(locbuf);
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,29 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
/* This is the system specific header for the SDL locale API */
#include "SDL_locale.h"
extern void SDL_SYS_GetPreferredLocales(char *buf, size_t buflen);
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,32 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
Android_JNI_GetLocale(buf, buflen);
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,33 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
/* dummy implementation. Caller already zero'd out buffer. */
SDL_Unsupported();
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,72 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <emscripten.h>
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
EM_ASM({
var buf = $0;
var buflen = $1;
var list = undefined;
if (navigator.languages && navigator.languages.length) {
list = navigator.languages;
} else {
var oneOfThese = navigator.userLanguage || navigator.language || navigator.browserLanguage || navigator.systemLanguage;
if (oneOfThese !== undefined) {
list = [ oneOfThese ];
}
}
if (list === undefined) {
return; /* we've got nothing. */
}
var str = ""; /* Can't do list.join() because we need to fit in buflen. */
for (var i = 0; i < list.length; i++) {
var item = list[i];
if ((str.length + item.length + 1) > buflen) {
break; /* don't add, we're out of space. */
}
if (str.length > 0) {
str += ",";
}
str += item;
}
str = str.replace(/-/g, "_");
if (buflen > str.length) {
buflen = str.length; /* clamp to size of string. */
}
for (var i = 0; i < buflen; i++) {
setValue(buf + i, str.charCodeAt(i), "i8"); /* fill in C array. */
}
}, buf, buflen);
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,75 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <LocaleRoster.h>
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
BLocaleRoster *roster = BLocaleRoster::Default();
roster->Refresh();
BMessage msg;
if (roster->GetPreferredLanguages(&msg) != B_OK) {
SDL_SetError("BLocaleRoster couldn't get preferred languages");
return;
}
const char *key = "language";
type_code typ = B_ANY_TYPE;
int32 numlangs = 0;
if ((msg.GetInfo(key, &typ, &numlangs) != B_OK) || (typ != B_STRING_TYPE)) {
SDL_SetError("BLocaleRoster message was wrong");
return;
}
for (int32 i = 0; i < numlangs; i++) {
const char *str = NULL;
if (msg.FindString(key, i, &str) != B_OK) {
continue;
}
const size_t len = SDL_strlen(str);
if (buflen <= len) {
break; // can't fit it, we're done.
}
SDL_strlcpy(buf, str, buflen);
buf += len;
buflen -= len;
if (i < (numlangs - 1)) {
if (buflen <= 1) {
break; // out of room, stop looking.
}
buf[0] = ','; // add a comma between entries.
buf[1] = '\0';
buf++;
buflen--;
}
}
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,76 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
#import <Foundation/Foundation.h>
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{ @autoreleasepool {
NSArray *languages = NSLocale.preferredLanguages;
size_t numlangs = 0;
size_t i;
numlangs = (size_t) [languages count];
for (i = 0; i < numlangs; i++) {
NSString *nsstr = [languages objectAtIndex:i];
size_t len;
char *ptr;
if (nsstr == nil) {
break;
}
[nsstr getCString:buf maxLength:buflen encoding:NSASCIIStringEncoding];
len = SDL_strlen(buf);
// convert '-' to '_'...
// These are always full lang-COUNTRY, so we search from the back,
// so things like zh-Hant-CN find the right '-' to convert.
if ((ptr = SDL_strrchr(buf, '-')) != NULL) {
*ptr = '_';
}
if (buflen <= len) {
*buf = '\0'; // drop this one and stop, we can't fit anymore.
break;
}
buf += len;
buflen -= len;
if (i < (numlangs - 1)) {
if (buflen <= 1) {
break; // out of room, stop looking.
}
buf[0] = ','; // add a comma between entries.
buf[1] = '\0';
buf++;
buflen--;
}
}
}}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,110 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
#include "SDL_assert.h"
static void
normalize_locale_str(char *dst, char *str, size_t buflen)
{
char *ptr;
ptr = SDL_strchr(str, '.'); /* chop off encoding if specified. */
if (ptr != NULL) {
*ptr = '\0';
}
ptr = SDL_strchr(str, '@'); /* chop off extra bits if specified. */
if (ptr != NULL) {
*ptr = '\0';
}
/* The "C" locale isn't useful for our needs, ignore it if you see it. */
if ((str[0] == 'C') && (str[1] == '\0')) {
return;
}
if (*str) {
if (*dst) {
SDL_strlcat(dst, ",", buflen); /* SDL has these split by commas */
}
SDL_strlcat(dst, str, buflen);
}
}
static void
normalize_locales(char *dst, char *src, size_t buflen)
{
char *ptr;
/* entries are separated by colons */
while ((ptr = SDL_strchr(src, ':')) != NULL) {
*ptr = '\0';
normalize_locale_str(dst, src, buflen);
src = ptr + 1;
}
normalize_locale_str(dst, src, buflen);
}
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
/* !!! FIXME: should we be using setlocale()? Or some D-Bus thing? */
SDL_bool isstack;
const char *envr;
char *tmp;
SDL_assert(buflen > 0);
tmp = SDL_small_alloc(char, buflen, &isstack);
if (!tmp) {
SDL_OutOfMemory();
return;
}
*tmp = '\0';
/* LANG is the primary locale (maybe) */
envr = SDL_getenv("LANG");
if (envr) {
SDL_strlcpy(tmp, envr, buflen);
}
/* fallback languages */
envr = SDL_getenv("LANGUAGE");
if (envr) {
if (*tmp) {
SDL_strlcat(tmp, ":", buflen);
}
SDL_strlcat(tmp, envr, buflen);
}
if (*tmp == '\0') {
SDL_SetError("LANG environment variable isn't set");
} else {
normalize_locales(buf, tmp, buflen);
}
SDL_small_free(tmp, isstack);
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,119 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../../core/windows/SDL_windows.h"
#include "../SDL_syslocale.h"
#include "SDL_assert.h"
typedef BOOL (WINAPI *pfnGetUserPreferredUILanguages)(DWORD,PULONG,PZZWSTR,PULONG);
#ifndef MUI_LANGUAGE_NAME
#define MUI_LANGUAGE_NAME 0x8
#endif
static pfnGetUserPreferredUILanguages pGetUserPreferredUILanguages = NULL;
static HMODULE kernel32 = 0;
/* this is the fallback for WinXP...one language, not a list. */
static void
SDL_SYS_GetPreferredLocales_winxp(char *buf, size_t buflen)
{
const char **retval = NULL;
char lang[16];
char country[16];
const int langrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO639LANGNAME,
lang, sizeof (lang));
const int ctryrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO3166CTRYNAME,
country, sizeof (country));
/* Win95 systems will fail, because they don't have LOCALE_SISO*NAME ... */
if (langrc == 0) {
SDL_SetError("Couldn't obtain language info");
} else {
SDL_snprintf(buf, buflen, "%s%s%s", lang, ctryrc ? "_" : "", ctryrc ? country : "");
}
}
/* this works on Windows Vista and later. */
static void
SDL_SYS_GetPreferredLocales_vista(char *buf, size_t buflen)
{
ULONG numlangs = 0;
WCHAR *wbuf = NULL;
ULONG wbuflen = 0;
SDL_bool isstack;
SDL_assert(pGetUserPreferredUILanguages != NULL);
pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, NULL, &wbuflen);
wbuf = SDL_small_alloc(WCHAR, wbuflen, &isstack);
if (!wbuf) {
SDL_OutOfMemory();
return;
}
if (!pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, wbuf, &wbuflen)) {
SDL_SYS_GetPreferredLocales_winxp(buf, buflen); /* oh well, try the fallback. */
} else {
const ULONG endidx = SDL_min(buflen, wbuflen - 1);
ULONG str_start = 0;
ULONG i;
for (i = 0; i < endidx; i++) {
const char ch = (char) wbuf[i]; /* these should all be low-ASCII, safe to cast */
if (ch == '\0') {
buf[i] = ','; /* change null separators to commas */
str_start = i;
} else if (ch == '-') {
buf[i] = '_'; /* change '-' to '_' */
} else {
buf[i] = ch; /* copy through as-is. */
}
}
buf[str_start] = '\0'; /* terminate string, chop off final ',' */
}
SDL_small_free(wbuf, isstack);
}
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
if (!kernel32) {
kernel32 = LoadLibraryW(L"kernel32.dll");
if (kernel32) {
pGetUserPreferredUILanguages = (pfnGetUserPreferredUILanguages) GetProcAddress(kernel32, "GetUserPreferredUILanguages");
}
}
if (pGetUserPreferredUILanguages == NULL) {
SDL_SYS_GetPreferredLocales_winxp(buf, buflen); /* this is always available */
} else {
SDL_SYS_GetPreferredLocales_vista(buf, buflen); /* available on Vista and later. */
}
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,58 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <Windows.h>
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
/*using namespace Windows::Graphics::Display;*/
#include <wchar.h>
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
WCHAR wbuffer[128] = L"";
int ret = 0;
/* !!! FIXME: do we not have GetUserPreferredUILanguages on WinPhone or UWP? */
# if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
ret = GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_SNAME, wbuffer, SDL_arraylen(wbuffer));
# else
ret = GetSystemDefaultLocaleName(wbuffer, SDL_arraysize(wbuffer));
# endif
if (ret > 0)
{
/* Need to convert LPWSTR to LPSTR, that is wide char to char. */
int i;
if (ret >= (buflen - 1) ) {
ret = (int) (buflen - 1);
}
for (i = 0; i < ret; i++) {
buf[i] = (char) wbuffer[i]; /* assume this was ASCII anyhow. */
}
}
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -22,6 +22,7 @@
#define SDL_BAPP_H
#include <InterfaceKit.h>
#include <LocaleRoster.h>
#if SDL_VIDEO_OPENGL
#include <OpenGLKit.h>
#endif
@@ -153,6 +154,10 @@ public:
_HandleWindowResized(message);
break;
case B_LOCALE_CHANGED:
SDL_SendLocaleChangedEvent();
break;
case BAPP_SCREEN_CHANGED:
/* TODO: Handle screen resize or workspace change */
break;

View File

@@ -117,6 +117,7 @@ static void Cocoa_DispatchEvent(NSEvent *theEvent)
}
- (id)init;
- (void)localeDidChange:(NSNotification *)notification;
@end
@implementation SDLAppDelegate : NSObject
@@ -137,6 +138,11 @@ static void Cocoa_DispatchEvent(NSEvent *theEvent)
selector:@selector(focusSomeWindow:)
name:NSApplicationDidBecomeActiveNotification
object:nil];
[center addObserver:self
selector:@selector(localeDidChange:)
name:NSCurrentLocaleDidChangeNotification
object:nil];
}
return self;
@@ -148,6 +154,7 @@ static void Cocoa_DispatchEvent(NSEvent *theEvent)
[center removeObserver:self name:NSWindowWillCloseNotification object:nil];
[center removeObserver:self name:NSApplicationDidBecomeActiveNotification object:nil];
[center removeObserver:self name:NSCurrentLocaleDidChangeNotification object:nil];
[super dealloc];
}
@@ -226,6 +233,11 @@ static void Cocoa_DispatchEvent(NSEvent *theEvent)
}
}
- (void)localeDidChange:(NSNotification *)notification;
{
SDL_SendLocaleChangedEvent();
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
return (BOOL)SDL_SendDropFile(NULL, [filename UTF8String]) && SDL_SendDropComplete(NULL);