summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/locale/windows
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/locale/windows')
-rw-r--r--contrib/SDL-3.2.8/src/locale/windows/SDL_syslocale.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/locale/windows/SDL_syslocale.c b/contrib/SDL-3.2.8/src/locale/windows/SDL_syslocale.c
new file mode 100644
index 0000000..7f69c56
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/locale/windows/SDL_syslocale.c
@@ -0,0 +1,111 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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
26typedef BOOL(WINAPI *pfnGetUserPreferredUILanguages)(DWORD, PULONG, WCHAR *, PULONG);
27#ifndef MUI_LANGUAGE_NAME
28#define MUI_LANGUAGE_NAME 0x8
29#endif
30
31static pfnGetUserPreferredUILanguages pGetUserPreferredUILanguages = NULL;
32static HMODULE kernel32 = 0;
33
34// this is the fallback for WinXP...one language, not a list.
35static bool SDL_SYS_GetPreferredLocales_winxp(char *buf, size_t buflen)
36{
37 char lang[16];
38 char country[16];
39
40 const int langrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
41 LOCALE_SISO639LANGNAME,
42 lang, sizeof(lang));
43
44 const int ctryrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
45 LOCALE_SISO3166CTRYNAME,
46 country, sizeof(country));
47
48 /* Win95 systems will fail, because they don't have LOCALE_SISO*NAME ... */
49 if (langrc == 0) {
50 return SDL_SetError("Couldn't obtain language info");
51 } else {
52 (void)SDL_snprintf(buf, buflen, "%s%s%s", lang, ctryrc ? "_" : "", ctryrc ? country : "");
53 return true;
54 }
55}
56
57// this works on Windows Vista and later.
58static bool SDL_SYS_GetPreferredLocales_vista(char *buf, size_t buflen)
59{
60 ULONG numlangs = 0;
61 WCHAR *wbuf = NULL;
62 ULONG wbuflen = 0;
63 bool isstack;
64
65 SDL_assert(pGetUserPreferredUILanguages != NULL);
66 pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, NULL, &wbuflen);
67
68 wbuf = SDL_small_alloc(WCHAR, wbuflen, &isstack);
69 if (!wbuf) {
70 return false;
71 }
72
73 if (!pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, wbuf, &wbuflen)) {
74 SDL_SYS_GetPreferredLocales_winxp(buf, buflen); // oh well, try the fallback.
75 } else {
76 const ULONG endidx = (ULONG)SDL_min(buflen, wbuflen - 1);
77 ULONG str_start = 0;
78 ULONG i;
79 for (i = 0; i < endidx; i++) {
80 const char ch = (char)wbuf[i]; // these should all be low-ASCII, safe to cast
81 if (ch == '\0') {
82 buf[i] = ','; // change null separators to commas
83 str_start = i;
84 } else if (ch == '-') {
85 buf[i] = '_'; // change '-' to '_'
86 } else {
87 buf[i] = ch; // copy through as-is.
88 }
89 }
90 buf[str_start] = '\0'; // terminate string, chop off final ','
91 }
92
93 SDL_small_free(wbuf, isstack);
94 return true;
95}
96
97bool SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
98{
99 if (!kernel32) {
100 kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
101 if (kernel32) {
102 pGetUserPreferredUILanguages = (pfnGetUserPreferredUILanguages)GetProcAddress(kernel32, "GetUserPreferredUILanguages");
103 }
104 }
105
106 if (!pGetUserPreferredUILanguages) {
107 return SDL_SYS_GetPreferredLocales_winxp(buf, buflen); // this is always available
108 } else {
109 return SDL_SYS_GetPreferredLocales_vista(buf, buflen); // available on Vista and later.
110 }
111}