SDL_syslocale.c (4077B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20 */ 21 22 #include "../../SDL_internal.h" 23 #include "../../core/windows/SDL_windows.h" 24 #include "../SDL_syslocale.h" 25 26 typedef BOOL (WINAPI *pfnGetUserPreferredUILanguages)(DWORD,PULONG,/*PZZWSTR*/WCHAR*,PULONG); 27 #ifndef MUI_LANGUAGE_NAME 28 #define MUI_LANGUAGE_NAME 0x8 29 #endif 30 31 static pfnGetUserPreferredUILanguages pGetUserPreferredUILanguages = NULL; 32 static HMODULE kernel32 = 0; 33 34 35 /* this is the fallback for WinXP...one language, not a list. */ 36 static void 37 SDL_SYS_GetPreferredLocales_winxp(char *buf, size_t buflen) 38 { 39 char lang[16]; 40 char country[16]; 41 42 const int langrc = GetLocaleInfoA(LOCALE_USER_DEFAULT, 43 LOCALE_SISO639LANGNAME, 44 lang, sizeof (lang)); 45 46 const int ctryrc = GetLocaleInfoA(LOCALE_USER_DEFAULT, 47 LOCALE_SISO3166CTRYNAME, 48 country, sizeof (country)); 49 50 /* Win95 systems will fail, because they don't have LOCALE_SISO*NAME ... */ 51 if (langrc == 0) { 52 SDL_SetError("Couldn't obtain language info"); 53 } else { 54 SDL_snprintf(buf, buflen, "%s%s%s", lang, ctryrc ? "_" : "", ctryrc ? country : ""); 55 } 56 } 57 58 /* this works on Windows Vista and later. */ 59 static void 60 SDL_SYS_GetPreferredLocales_vista(char *buf, size_t buflen) 61 { 62 ULONG numlangs = 0; 63 WCHAR *wbuf = NULL; 64 ULONG wbuflen = 0; 65 SDL_bool isstack; 66 67 SDL_assert(pGetUserPreferredUILanguages != NULL); 68 pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, NULL, &wbuflen); 69 70 wbuf = SDL_small_alloc(WCHAR, wbuflen, &isstack); 71 if (!wbuf) { 72 SDL_OutOfMemory(); 73 return; 74 } 75 76 if (!pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, wbuf, &wbuflen)) { 77 SDL_SYS_GetPreferredLocales_winxp(buf, buflen); /* oh well, try the fallback. */ 78 } else { 79 const ULONG endidx = (ULONG) SDL_min(buflen, wbuflen - 1); 80 ULONG str_start = 0; 81 ULONG i; 82 for (i = 0; i < endidx; i++) { 83 const char ch = (char) wbuf[i]; /* these should all be low-ASCII, safe to cast */ 84 if (ch == '\0') { 85 buf[i] = ','; /* change null separators to commas */ 86 str_start = i; 87 } else if (ch == '-') { 88 buf[i] = '_'; /* change '-' to '_' */ 89 } else { 90 buf[i] = ch; /* copy through as-is. */ 91 } 92 } 93 buf[str_start] = '\0'; /* terminate string, chop off final ',' */ 94 } 95 96 SDL_small_free(wbuf, isstack); 97 } 98 99 void 100 SDL_SYS_GetPreferredLocales(char *buf, size_t buflen) 101 { 102 if (!kernel32) { 103 kernel32 = LoadLibraryW(L"kernel32.dll"); 104 if (kernel32) { 105 pGetUserPreferredUILanguages = (pfnGetUserPreferredUILanguages) GetProcAddress(kernel32, "GetUserPreferredUILanguages"); 106 } 107 } 108 109 if (pGetUserPreferredUILanguages == NULL) { 110 SDL_SYS_GetPreferredLocales_winxp(buf, buflen); /* this is always available */ 111 } else { 112 SDL_SYS_GetPreferredLocales_vista(buf, buflen); /* available on Vista and later. */ 113 } 114 } 115 116 /* vi: set ts=4 sw=4 expandtab: */ 117