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 #include "SDL_assert.h"
26 
27 typedef BOOL (WINAPI *pfnGetUserPreferredUILanguages)(DWORD,PULONG,PZZWSTR,PULONG);
28 #ifndef MUI_LANGUAGE_NAME
29 #define MUI_LANGUAGE_NAME 0x8
30 #endif
31 
32 static pfnGetUserPreferredUILanguages pGetUserPreferredUILanguages = NULL;
33 static HMODULE kernel32 = 0;
34 
35 
36 /* this is the fallback for WinXP...one language, not a list. */
37 static void
SDL_SYS_GetPreferredLocales_winxp(char * buf,size_t buflen)38 SDL_SYS_GetPreferredLocales_winxp(char *buf, size_t buflen)
39 {
40     char lang[16];
41     char country[16];
42 
43 	const int langrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
44                                       LOCALE_SISO639LANGNAME,
45                                       lang, sizeof (lang));
46 
47 	const int ctryrc =  GetLocaleInfoA(LOCALE_USER_DEFAULT,
48                                        LOCALE_SISO3166CTRYNAME,
49                                        country, sizeof (country));
50 
51     /* Win95 systems will fail, because they don't have LOCALE_SISO*NAME ... */
52     if (langrc == 0) {
53         SDL_SetError("Couldn't obtain language info");
54     } else {
55         SDL_snprintf(buf, buflen, "%s%s%s", lang, ctryrc ? "_" : "", ctryrc ? country : "");
56     }
57 }
58 
59 /* this works on Windows Vista and later. */
60 static void
SDL_SYS_GetPreferredLocales_vista(char * buf,size_t buflen)61 SDL_SYS_GetPreferredLocales_vista(char *buf, size_t buflen)
62 {
63     ULONG numlangs = 0;
64     WCHAR *wbuf = NULL;
65     ULONG wbuflen = 0;
66     SDL_bool isstack;
67 
68     SDL_assert(pGetUserPreferredUILanguages != NULL);
69     pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, NULL, &wbuflen);
70 
71     wbuf = SDL_small_alloc(WCHAR, wbuflen, &isstack);
72     if (!wbuf) {
73         SDL_OutOfMemory();
74         return;
75     }
76 
77     if (!pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, wbuf, &wbuflen)) {
78         SDL_SYS_GetPreferredLocales_winxp(buf, buflen);  /* oh well, try the fallback. */
79     } else {
80         const ULONG endidx = (ULONG) SDL_min(buflen, wbuflen - 1);
81         ULONG str_start = 0;
82         ULONG i;
83         for (i = 0; i < endidx; i++) {
84             const char ch = (char) wbuf[i];  /* these should all be low-ASCII, safe to cast */
85             if (ch == '\0') {
86                 buf[i] = ',';  /* change null separators to commas */
87                 str_start = i;
88             } else if (ch == '-') {
89                 buf[i] = '_';  /* change '-' to '_' */
90             } else {
91                 buf[i] = ch;   /* copy through as-is. */
92             }
93         }
94         buf[str_start] = '\0';  /* terminate string, chop off final ',' */
95     }
96 
97     SDL_small_free(wbuf, isstack);
98 }
99 
100 void
SDL_SYS_GetPreferredLocales(char * buf,size_t buflen)101 SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
102 {
103     if (!kernel32) {
104         kernel32 = LoadLibraryW(L"kernel32.dll");
105         if (kernel32) {
106             pGetUserPreferredUILanguages = (pfnGetUserPreferredUILanguages) GetProcAddress(kernel32, "GetUserPreferredUILanguages");
107         }
108     }
109 
110     if (pGetUserPreferredUILanguages == NULL) {
111         SDL_SYS_GetPreferredLocales_winxp(buf, buflen);  /* this is always available */
112     } else {
113         SDL_SYS_GetPreferredLocales_vista(buf, buflen);  /* available on Vista and later. */
114     }
115 }
116 
117 /* vi: set ts=4 sw=4 expandtab: */
118 
119