OpenTTD Source 20250905-master-g122023be8d
win32.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "../../stdafx.h"
11#include "../../debug.h"
12#include "../../gfx_func.h"
13#include "../../strings_func.h"
14#include "../../textbuf_gui.h"
15#include "../../fileio_func.h"
16#include <windows.h>
17#include <fcntl.h>
18#include <mmsystem.h>
19#include <regstr.h>
20#define NO_SHOBJIDL_SORTDIRECTION // Avoid multiple definition of SORT_ASCENDING
21#include <shlobj.h> /* SHGetFolderPath */
22#include <shellapi.h>
23#include <winnls.h>
24#include <io.h>
25#include "win32.h"
26#include "../../fios.h"
27#include "../../string_func.h"
28#include <sys/stat.h>
29#include "../../language.h"
30#include "../../thread.h"
31#include "../../library_loader.h"
32
33#include "table/strings.h"
34
35#include "../../safeguards.h"
36
37static bool _has_console;
38static bool _cursor_disable = true;
39static bool _cursor_visible = true;
40
41bool MyShowCursor(bool show, bool toggle)
42{
43 if (toggle) _cursor_disable = !_cursor_disable;
44 if (_cursor_disable) return show;
45 if (_cursor_visible == show) return show;
46
47 _cursor_visible = show;
48 ShowCursor(show);
49
50 return !show;
51}
52
53void ShowOSErrorBox(std::string_view buf, bool)
54{
55 MyShowCursor(true);
56 MessageBox(GetActiveWindow(), OTTD2FS(buf).c_str(), L"Error!", MB_ICONSTOP | MB_TASKMODAL);
57}
58
59void OSOpenBrowser(const std::string &url)
60{
61 ShellExecute(GetActiveWindow(), L"open", OTTD2FS(url).c_str(), nullptr, nullptr, SW_SHOWNORMAL);
62}
63
64bool FiosIsRoot(const std::string &file)
65{
66 return file.size() == 3; // C:\...
67}
68
69void FiosGetDrives(FileList &file_list)
70{
71 wchar_t drives[256];
72 const wchar_t *s;
73
74 GetLogicalDriveStrings(static_cast<DWORD>(std::size(drives)), drives);
75 for (s = drives; *s != '\0';) {
76 FiosItem *fios = &file_list.emplace_back();
77 fios->type = FIOS_TYPE_DRIVE;
78 fios->mtime = 0;
79 fios->name += (char)(s[0] & 0xFF);
80 fios->name += ':';
81 fios->title = GetEncodedString(STR_JUST_RAW_STRING, fios->name);
82 while (*s++ != '\0') { /* Nothing */ }
83 }
84}
85
86bool FiosIsHiddenFile(const std::filesystem::path &path)
87{
88 UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // Disable 'no-disk' message box.
89
90 DWORD attributes = GetFileAttributes(path.c_str());
91
92 SetErrorMode(sem); // Restore previous setting.
93
94 return (attributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
95}
96
97std::optional<uint64_t> FiosGetDiskFreeSpace(const std::string &path)
98{
99 UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
100
101 ULARGE_INTEGER bytes_free;
102 bool retval = GetDiskFreeSpaceEx(OTTD2FS(path).c_str(), &bytes_free, nullptr, nullptr);
103
104 SetErrorMode(sem); // reset previous setting
105
106 if (retval) return bytes_free.QuadPart;
107 return std::nullopt;
108}
109
110void CreateConsole()
111{
112 HANDLE hand;
113 CONSOLE_SCREEN_BUFFER_INFO coninfo;
114
115 if (_has_console) return;
116 _has_console = true;
117
118 if (!AllocConsole()) return;
119
120 hand = GetStdHandle(STD_OUTPUT_HANDLE);
121 GetConsoleScreenBufferInfo(hand, &coninfo);
122 coninfo.dwSize.Y = 500;
123 SetConsoleScreenBufferSize(hand, coninfo.dwSize);
124
125 /* redirect unbuffered STDIN, STDOUT, STDERR to the console */
126#if !defined(__CYGWIN__)
127
128 /* Check if we can open a handle to STDOUT. */
129 int fd = _open_osfhandle((intptr_t)hand, _O_TEXT);
130 if (fd == -1) {
131 /* Free everything related to the console. */
132 FreeConsole();
133 _has_console = false;
134 _close(fd);
135 CloseHandle(hand);
136
137 ShowInfo("Unable to open an output handle to the console. Check known-bugs.md for details.");
138 return;
139 }
140
141#if defined(_MSC_VER)
142 freopen("CONOUT$", "a", stdout);
143 freopen("CONIN$", "r", stdin);
144 freopen("CONOUT$", "a", stderr);
145#else
146 *stdout = *_fdopen(fd, "w");
147 *stdin = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r" );
148 *stderr = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w" );
149#endif
150
151#else
152 /* open_osfhandle is not in cygwin */
153 *stdout = *fdopen(1, "w" );
154 *stdin = *fdopen(0, "r" );
155 *stderr = *fdopen(2, "w" );
156#endif
157
158 setvbuf(stdin, nullptr, _IONBF, 0);
159 setvbuf(stdout, nullptr, _IONBF, 0);
160 setvbuf(stderr, nullptr, _IONBF, 0);
161}
162
168static std::string ConvertLfToCrLf(std::string_view msg)
169{
170 std::string output;
171
172 size_t last = 0;
173 size_t next = 0;
174 while ((next = msg.find('\n', last)) != std::string_view::npos) {
175 output += msg.substr(last, next - last);
176 output += "\r\n";
177 last = next + 1;
178 }
179 output += msg.substr(last);
180
181 return output;
182}
183
185static INT_PTR CALLBACK HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
186{
187 switch (msg) {
188 case WM_INITDIALOG: {
189 std::wstring &msg = *reinterpret_cast<std::wstring *>(lParam);
190 SetDlgItemText(wnd, 11, msg.c_str());
191 SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE);
192 } return TRUE;
193
194 case WM_COMMAND:
195 if (wParam == 12) ExitProcess(0);
196 return TRUE;
197 case WM_CLOSE:
198 ExitProcess(0);
199 }
200
201 return FALSE;
202}
203
204void ShowInfoI(std::string_view str)
205{
206 if (_has_console) {
207 fmt::print(stderr, "{}\n", str);
208 } else {
209 bool old;
210 ReleaseCapture();
212
213 old = MyShowCursor(true);
214 std::wstring native_str = OTTD2FS(ConvertLfToCrLf(str));
215 if (native_str.size() > 2048) {
216 /* The minimum length of the help message is 2048. Other messages sent via
217 * ShowInfo are much shorter, or so long they need this way of displaying
218 * them anyway. */
219 DialogBoxParam(GetModuleHandle(nullptr), MAKEINTRESOURCE(101), nullptr, HelpDialogFunc, reinterpret_cast<LPARAM>(&native_str));
220 } else {
221 MessageBox(GetActiveWindow(), native_str.c_str(), L"OpenTTD", MB_ICONINFORMATION | MB_OK);
222 }
223 MyShowCursor(old);
224 }
225}
226
227char *getcwd(char *buf, size_t size)
228{
229 wchar_t path[MAX_PATH];
230 GetCurrentDirectory(MAX_PATH - 1, path);
231 convert_from_fs(path, {buf, size});
232 return buf;
233}
234
235extern std::string _config_file;
236
237void DetermineBasePaths(std::string_view exe)
238{
239 extern std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
240
241 wchar_t path[MAX_PATH];
242#ifdef WITH_PERSONAL_DIR
243 if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path))) {
244 std::string tmp(FS2OTTD(path));
246 tmp += PERSONAL_DIR;
249
250 tmp += "content_download";
253 } else {
255 }
256
257 if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_COMMON_DOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, path))) {
258 std::string tmp(FS2OTTD(path));
260 tmp += PERSONAL_DIR;
263 } else {
265 }
266#else
269#endif
270
271 if (_config_file.empty()) {
272 char cwd[MAX_PATH];
273 getcwd(cwd, lengthof(cwd));
274 std::string cwd_s(cwd);
275 AppendPathSeparator(cwd_s);
277 } else {
278 /* Use the folder of the config file as working directory. */
279 wchar_t config_dir[MAX_PATH];
281 if (!GetFullPathName(path, static_cast<DWORD>(std::size(config_dir)), config_dir, nullptr)) {
282 Debug(misc, 0, "GetFullPathName failed ({})", GetLastError());
284 } else {
285 std::string tmp(FS2OTTD(config_dir));
286 auto pos = tmp.find_last_of(PATHSEPCHAR);
287 if (pos != std::string::npos) tmp.erase(pos + 1);
288
290 }
291 }
292
293 if (!GetModuleFileName(nullptr, path, static_cast<DWORD>(std::size(path)))) {
294 Debug(misc, 0, "GetModuleFileName failed ({})", GetLastError());
296 } else {
297 wchar_t exec_dir[MAX_PATH];
298 convert_to_fs(exe, path);
299 if (!GetFullPathName(path, static_cast<DWORD>(std::size(exec_dir)), exec_dir, nullptr)) {
300 Debug(misc, 0, "GetFullPathName failed ({})", GetLastError());
302 } else {
303 std::string tmp(FS2OTTD(exec_dir));
304 auto pos = tmp.find_last_of(PATHSEPCHAR);
305 if (pos != std::string::npos) tmp.erase(pos + 1);
306
308 }
309 }
310
313}
314
315
316std::optional<std::string> GetClipboardContents()
317{
318 if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return std::nullopt;
319
320 OpenClipboard(nullptr);
321 HGLOBAL cbuf = GetClipboardData(CF_UNICODETEXT);
322
323 std::string result = FS2OTTD(static_cast<LPCWSTR>(GlobalLock(cbuf)));
324 GlobalUnlock(cbuf);
325 CloseClipboard();
326
327 if (result.empty()) return std::nullopt;
328 return result;
329}
330
331
340std::string FS2OTTD(std::wstring_view name)
341{
342 int name_len = (name.length() >= INT_MAX) ? INT_MAX : static_cast<int>(name.length());
343 int len = WideCharToMultiByte(CP_UTF8, 0, name.data(), name_len, nullptr, 0, nullptr, nullptr);
344 if (len <= 0) return std::string();
345 std::string utf8_buf(len, '\0'); // len includes terminating null
346 WideCharToMultiByte(CP_UTF8, 0, name.data(), name_len, utf8_buf.data(), len, nullptr, nullptr);
347 return utf8_buf;
348}
349
357std::wstring OTTD2FS(std::string_view name)
358{
359 int name_len = (name.length() >= INT_MAX) ? INT_MAX : static_cast<int>(name.length());
360 int len = MultiByteToWideChar(CP_UTF8, 0, name.data(), name_len, nullptr, 0);
361 if (len <= 0) return std::wstring();
362 std::wstring system_buf(len, L'\0'); // len includes terminating null
363 MultiByteToWideChar(CP_UTF8, 0, name.data(), name_len, system_buf.data(), len);
364 return system_buf;
365}
366
367
375std::string_view convert_from_fs(const std::wstring_view src, std::span<char> dst_buf)
376{
377 /* Convert UTF-16 string to UTF-8. */
378 int len = WideCharToMultiByte(CP_UTF8, 0, src.data(), static_cast<int>(src.size()), dst_buf.data(), static_cast<int>(dst_buf.size() - 1U), nullptr, nullptr);
379 dst_buf[len] = '\0';
380
381 return std::string_view(dst_buf.data(), len);
382}
383
384
392wchar_t *convert_to_fs(std::string_view src, std::span<wchar_t> dst_buf)
393{
394 int len = MultiByteToWideChar(CP_UTF8, 0, src.data(), static_cast<int>(src.size()), dst_buf.data(), static_cast<int>(dst_buf.size() - 1U));
395 dst_buf[len] = '\0';
396
397 return dst_buf.data();
398}
399
401std::optional<std::string> GetCurrentLocale(const char *)
402{
403 const LANGID userUiLang = GetUserDefaultUILanguage();
404 const LCID userUiLocale = MAKELCID(userUiLang, SORT_DEFAULT);
405
406 char lang[9], country[9];
407 if (GetLocaleInfoA(userUiLocale, LOCALE_SISO639LANGNAME, lang, static_cast<int>(std::size(lang))) == 0 ||
408 GetLocaleInfoA(userUiLocale, LOCALE_SISO3166CTRYNAME, country, static_cast<int>(std::size(country))) == 0) {
409 /* Unable to retrieve the locale. */
410 return nullptr;
411 }
412 /* Format it as 'en_us'. */
413 return fmt::format("{}_{}", std::string_view{lang, 2}, std::string_view{country, 2});
414}
415
416
417static WCHAR _cur_iso_locale[16] = L"";
418
419void Win32SetCurrentLocaleName(std::string iso_code)
420{
421 /* Convert the iso code into the format that windows expects. */
422 if (iso_code == "zh_TW") {
423 iso_code = "zh-Hant";
424 } else if (iso_code == "zh_CN") {
425 iso_code = "zh-Hans";
426 } else {
427 /* Windows expects a '-' between language and country code, but we use a '_'. */
428 for (char &c : iso_code) {
429 if (c == '_') c = '-';
430 }
431 }
432
433 MultiByteToWideChar(CP_UTF8, 0, iso_code.data(), static_cast<int>(iso_code.size()), _cur_iso_locale, static_cast<int>(std::size(_cur_iso_locale)));
434}
435
436static LibraryLoader::Function GetKernel32Function(const std::string &symbol_name)
437{
438 static LibraryLoader _kernel32("Kernel32.dll");
439 return _kernel32.GetFunction(symbol_name);
440}
441
442int OTTDStringCompare(std::string_view s1, std::string_view s2)
443{
444 typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM);
445 static const PFNCOMPARESTRINGEX _CompareStringEx = GetKernel32Function("CompareStringEx");
446
447#ifndef SORT_DIGITSASNUMBERS
448# define SORT_DIGITSASNUMBERS 0x00000008 // use digits as numbers sort method
449#endif
450#ifndef LINGUISTIC_IGNORECASE
451# define LINGUISTIC_IGNORECASE 0x00000010 // linguistically appropriate 'ignore case'
452#endif
453
454 int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1.data(), (int)s1.size(), nullptr, 0);
455 int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2.data(), (int)s2.size(), nullptr, 0);
456
457 std::wstring str_s1(len_s1, L'\0');
458 std::wstring str_s2(len_s2, L'\0');
459
460 if (len_s1 != 0) MultiByteToWideChar(CP_UTF8, 0, s1.data(), (int)s1.size(), str_s1.data(), len_s1);
461 if (len_s2 != 0) MultiByteToWideChar(CP_UTF8, 0, s2.data(), (int)s2.size(), str_s2.data(), len_s2);
462
463 /* CompareStringEx takes UTF-16 strings, even in ANSI-builds. */
464 if (_CompareStringEx != nullptr) {
465 int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1.data(), len_s1, str_s2.data(), len_s2, nullptr, nullptr, 0);
466 if (result != 0) return result;
467 }
468
469 return CompareString(MAKELCID(_current_language->winlangid, SORT_DEFAULT), NORM_IGNORECASE, str_s1.data(), len_s1, str_s2.data(), len_s2);
470}
471
480int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive)
481{
482 typedef int (WINAPI *PFNFINDNLSSTRINGEX)(LPCWSTR, DWORD, LPCWSTR, int, LPCWSTR, int, LPINT, LPNLSVERSIONINFO, LPVOID, LPARAM);
483 static const PFNFINDNLSSTRINGEX _FindNLSStringEx = GetKernel32Function("FindNLSStringEx");
484
485 if (_FindNLSStringEx != nullptr) {
486 int len_str = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0);
487 int len_value = MultiByteToWideChar(CP_UTF8, 0, value.data(), (int)value.size(), nullptr, 0);
488
489 if (len_str != 0 && len_value != 0) {
490 std::wstring str_str(len_str, L'\0');
491 std::wstring str_value(len_value, L'\0');
492
493 MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), str_str.data(), len_str);
494 MultiByteToWideChar(CP_UTF8, 0, value.data(), (int)value.size(), str_value.data(), len_value);
495
496 return _FindNLSStringEx(_cur_iso_locale, FIND_FROMSTART | (case_insensitive ? LINGUISTIC_IGNORECASE : 0), str_str.data(), len_str, str_value.data(), len_value, nullptr, nullptr, nullptr, 0) >= 0 ? 1 : 0;
497 }
498 }
499
500 return -1; // Failure indication.
501}
502
503#ifdef _MSC_VER
504/* Based on code from MSDN: https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx */
505const DWORD MS_VC_EXCEPTION = 0x406D1388;
506
507PACK_N(struct THREADNAME_INFO {
508 DWORD dwType;
509 LPCSTR szName;
510 DWORD dwThreadID;
511 DWORD dwFlags;
512}, 8);
513
517void SetCurrentThreadName(const std::string &thread_name)
518{
519 THREADNAME_INFO info;
520 info.dwType = 0x1000;
521 info.szName = thread_name.c_str();
522 info.dwThreadID = -1;
523 info.dwFlags = 0;
524
525#pragma warning(push)
526#pragma warning(disable: 6320 6322)
527 __try {
528 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
529 } __except (EXCEPTION_EXECUTE_HANDLER) {
530 }
531#pragma warning(pop)
532}
533#else
534void SetCurrentThreadName(const std::string &) {}
535#endif
List of file information.
Definition fios.h:87
A function loaded from a library.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
void DetermineBasePaths(std::string_view exe)
Determine the base (personal dir and game data dir) paths.
Definition fileio.cpp:750
void AppendPathSeparator(std::string &buf)
Appends, if necessary, the path separator character to the end of the string.
Definition fileio.cpp:345
std::array< std::string, NUM_SEARCHPATHS > _searchpaths
The search paths OpenTTD could search through.
Definition fileio.cpp:65
@ SP_SHARED_DIR
Search in the shared directory, like 'Shared Files' under Windows.
@ SP_INSTALLATION_DIR
Search in the installation directory.
@ SP_BINARY_DIR
Search in the directory where the binary resides.
@ SP_AUTODOWNLOAD_PERSONAL_DIR
Search within the autodownload directory located in the personal directory.
@ SP_PERSONAL_DIR
Search in the personal directory.
@ SP_WORKING_DIR
Search in the working directory.
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
bool _left_button_down
Is left mouse button pressed?
Definition gfx.cpp:42
bool _left_button_clicked
Is left mouse button clicked?
Definition gfx.cpp:43
const LanguageMetadata * _current_language
The currently loaded language.
Definition strings.cpp:54
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
Deals with finding savegames.
Definition fios.h:78
uint16_t winlangid
Windows language ID: Windows cannot and will not convert isocodes to something it can use to determin...
Definition language.h:51
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
Definition win32.cpp:316
std::string_view convert_from_fs(const std::wstring_view src, std::span< char > dst_buf)
Convert to OpenTTD's encoding from that of the environment in UNICODE.
Definition win32.cpp:375
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:357
int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive)
Search if a string is contained in another string using the current locale.
Definition win32.cpp:480
static std::string ConvertLfToCrLf(std::string_view msg)
Replace linefeeds with carriage-return and linefeed.
Definition win32.cpp:168
void SetCurrentThreadName(const std::string &)
Name the thread this function is called on for the debugger.
Definition win32.cpp:534
static INT_PTR CALLBACK HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
Callback function to handle the window.
Definition win32.cpp:185
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:340
wchar_t * convert_to_fs(std::string_view src, std::span< wchar_t > dst_buf)
Convert from OpenTTD's encoding to that of the environment in UNICODE.
Definition win32.cpp:392
std::string _config_file
Configuration file of OpenTTD.
Definition settings.cpp:64
std::optional< std::string > GetCurrentLocale(const char *)
Determine the current user's locale.
Definition win32.cpp:401
declarations of functions for MS windows systems