| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  os_windows.cpp                                                       */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2015-01-10 17:35:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-02 05:32:12 +02:00
										 |  |  | // Must include Winsock before windows.h (included by os_windows.h)
 | 
					
						
							|  |  |  | #include "drivers/unix/net_socket_posix.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "os_windows.h"
 | 
					
						
							| 
									
										
										
										
											2016-12-21 02:29:58 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 03:30:20 +01:00
										 |  |  | #include "core/debugger/engine_debugger.h"
 | 
					
						
							|  |  |  | #include "core/debugger/script_debugger.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/io/marshalls.h"
 | 
					
						
							|  |  |  | #include "core/version_generated.gen.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "drivers/windows/dir_access_windows.h"
 | 
					
						
							|  |  |  | #include "drivers/windows/file_access_windows.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-08 11:13:12 -03:00
										 |  |  | #include "drivers/windows/rw_lock_windows.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "drivers/windows/thread_windows.h"
 | 
					
						
							| 
									
										
										
										
											2019-02-12 15:43:54 +01:00
										 |  |  | #include "joypad_windows.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "lang_table.h"
 | 
					
						
							|  |  |  | #include "main/main.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | #include "platform/windows/display_server_windows.h"
 | 
					
						
							| 
									
										
										
										
											2017-06-10 10:15:33 -03:00
										 |  |  | #include "servers/audio_server.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | #include "servers/rendering/rendering_server_raster.h"
 | 
					
						
							|  |  |  | #include "servers/rendering/rendering_server_wrap_mt.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | #include "windows_terminal_logger.h"
 | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 15:43:54 +01:00
										 |  |  | #include <avrt.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-20 13:00:19 +01:00
										 |  |  | #include <direct.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-22 09:42:11 -07:00
										 |  |  | #include <knownfolders.h>
 | 
					
						
							| 
									
										
										
										
											2016-04-29 13:57:57 -03:00
										 |  |  | #include <process.h>
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include <regstr.h>
 | 
					
						
							|  |  |  | #include <shlobj.h>
 | 
					
						
							| 
									
										
										
										
											2015-02-12 04:17:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | static const WORD MAX_CONSOLE_LINES = 1500; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 02:48:35 +01:00
										 |  |  | extern "C" { | 
					
						
							| 
									
										
										
										
											2018-11-02 23:10:44 +01:00
										 |  |  | __declspec(dllexport) DWORD NvOptimusEnablement = 1; | 
					
						
							|  |  |  | __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; | 
					
						
							| 
									
										
										
										
											2015-01-17 02:48:35 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 20:46:13 +02:00
										 |  |  | // Workaround mingw-w64 < 4.0 bug
 | 
					
						
							|  |  |  | #ifndef WM_TOUCH
 | 
					
						
							|  |  |  | #define WM_TOUCH 576
 | 
					
						
							| 
									
										
										
										
											2016-02-04 17:16:22 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 22:32:11 +02:00
										 |  |  | #ifndef WM_POINTERUPDATE
 | 
					
						
							|  |  |  | #define WM_POINTERUPDATE 0x0245
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 12:07:58 +01:00
										 |  |  | #if defined(__GNUC__)
 | 
					
						
							|  |  |  | // Workaround GCC warning from -Wcast-function-type.
 | 
					
						
							|  |  |  | #define GetProcAddress (void *)GetProcAddress
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 12:11:30 +02:00
										 |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							| 
									
										
										
										
											2017-12-01 08:42:57 -03:00
										 |  |  | static String format_error_message(DWORD id) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	LPWSTR messageBuffer = nullptr; | 
					
						
							| 
									
										
										
										
											2017-12-01 08:42:57 -03:00
										 |  |  | 	size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 			nullptr, id, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, nullptr); | 
					
						
							| 
									
										
										
										
											2017-12-01 08:42:57 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 19:55:38 +01:00
										 |  |  | 	String msg = "Error " + itos(id) + ": " + String(messageBuffer, size); | 
					
						
							| 
									
										
										
										
											2017-12-01 08:42:57 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	LocalFree(messageBuffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return msg; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-15 12:11:30 +02:00
										 |  |  | #endif // DEBUG_ENABLED
 | 
					
						
							| 
									
										
										
										
											2017-12-01 08:42:57 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void RedirectIOToConsole() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int hConHandle; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	intptr_t lStdHandle; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CONSOLE_SCREEN_BUFFER_INFO coninfo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// allocate a console for this app
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AllocConsole(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// set the screen buffer to be big enough to let us scroll text
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			&coninfo); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	coninfo.dwSize.Y = MAX_CONSOLE_LINES; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			coninfo.dwSize); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// redirect unbuffered STDOUT to the console
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lStdHandle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	fp = _fdopen(hConHandle, "w"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	*stdout = *fp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	setvbuf(stdout, nullptr, _IONBF, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// redirect unbuffered STDIN to the console
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lStdHandle = (intptr_t)GetStdHandle(STD_INPUT_HANDLE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	fp = _fdopen(hConHandle, "r"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	*stdin = *fp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	setvbuf(stdin, nullptr, _IONBF, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	// redirect unbuffered STDERR to the console
 | 
					
						
							| 
									
										
										
										
											2015-03-22 19:00:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	lStdHandle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE); | 
					
						
							| 
									
										
										
										
											2015-03-22 19:00:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | 
					
						
							| 
									
										
										
										
											2017-12-27 20:51:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	fp = _fdopen(hConHandle, "w"); | 
					
						
							| 
									
										
										
										
											2017-12-27 20:51:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	*stderr = *fp; | 
					
						
							| 
									
										
										
										
											2017-12-27 20:51:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	setvbuf(stderr, nullptr, _IONBF, 0); | 
					
						
							| 
									
										
										
										
											2017-12-27 20:51:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
 | 
					
						
							| 
									
										
										
										
											2019-11-28 13:41:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	// point to console as well
 | 
					
						
							| 
									
										
										
										
											2019-11-28 13:41:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | BOOL WINAPI HandlerRoutine(_In_ DWORD dwCtrlType) { | 
					
						
							|  |  |  | 	if (!EngineDebugger::is_active()) | 
					
						
							|  |  |  | 		return FALSE; | 
					
						
							| 
									
										
										
										
											2018-12-18 14:17:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	switch (dwCtrlType) { | 
					
						
							|  |  |  | 		case CTRL_C_EVENT: | 
					
						
							|  |  |  | 			EngineDebugger::get_script_debugger()->set_depth(-1); | 
					
						
							|  |  |  | 			EngineDebugger::get_script_debugger()->set_lines_left(1); | 
					
						
							|  |  |  | 			return TRUE; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return FALSE; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-12-18 14:17:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | void OS_Windows::initialize_debugging() { | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	SetConsoleCtrlHandler(HandlerRoutine, TRUE); | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | void OS_Windows::initialize() { | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	crash_handler.initialize(); | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	//RedirectIOToConsole();
 | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	ThreadWindows::make_default(); | 
					
						
							|  |  |  | 	RWLockWindows::make_default(); | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_RESOURCES); | 
					
						
							|  |  |  | 	FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_USERDATA); | 
					
						
							|  |  |  | 	FileAccess::make_default<FileAccessWindows>(FileAccess::ACCESS_FILESYSTEM); | 
					
						
							|  |  |  | 	//FileAccessBufferedFA<FileAccessWindows>::make_default();
 | 
					
						
							|  |  |  | 	DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_RESOURCES); | 
					
						
							|  |  |  | 	DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_USERDATA); | 
					
						
							|  |  |  | 	DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_FILESYSTEM); | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	NetSocketPosix::make_default(); | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	// We need to know how often the clock is updated
 | 
					
						
							|  |  |  | 	if (!QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second)) | 
					
						
							|  |  |  | 		ticks_per_second = 1000; | 
					
						
							|  |  |  | 	// If timeAtGameStart is 0 then we get the time since
 | 
					
						
							|  |  |  | 	// the start of the computer when we call GetGameTime()
 | 
					
						
							|  |  |  | 	ticks_start = 0; | 
					
						
							|  |  |  | 	ticks_start = get_ticks_usec(); | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	// set minimum resolution for periodic timers, otherwise Sleep(n) may wait at least as
 | 
					
						
							|  |  |  | 	//  long as the windows scheduler resolution (~16-30ms) even for calls like Sleep(1)
 | 
					
						
							|  |  |  | 	timeBeginPeriod(1); | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	process_map = memnew((Map<ProcessID, ProcessInfo>)); | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	IP_Unix::make_default(); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	main_loop = nullptr; | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | void OS_Windows::delete_main_loop() { | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	if (main_loop) | 
					
						
							|  |  |  | 		memdelete(main_loop); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	main_loop = nullptr; | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | void OS_Windows::set_main_loop(MainLoop *p_main_loop) { | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	main_loop = p_main_loop; | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | void OS_Windows::finalize() { | 
					
						
							| 
									
										
										
										
											2017-12-10 20:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | #ifdef WINMIDI_ENABLED
 | 
					
						
							|  |  |  | 	driver_midi.close(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-07-09 21:48:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	if (main_loop) | 
					
						
							|  |  |  | 		memdelete(main_loop); | 
					
						
							| 
									
										
										
										
											2016-01-03 04:18:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	main_loop = nullptr; | 
					
						
							| 
									
										
										
										
											2016-01-03 04:18:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | void OS_Windows::finalize_core() { | 
					
						
							| 
									
										
										
										
											2017-07-09 21:48:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	timeEndPeriod(1); | 
					
						
							| 
									
										
										
										
											2017-12-27 20:51:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	memdelete(process_map); | 
					
						
							|  |  |  | 	NetSocketPosix::cleanup(); | 
					
						
							| 
									
										
										
										
											2017-07-09 21:48:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 10:00:10 -03:00
										 |  |  | Error OS_Windows::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 15:42:29 -03:00
										 |  |  | 	String path = p_path; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!FileAccess::exists(path)) { | 
					
						
							|  |  |  | 		//this code exists so gdnative can load .dll files from within the executable path
 | 
					
						
							|  |  |  | 		path = get_executable_path().get_base_dir().plus_file(p_path.get_file()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 14:29:01 +02:00
										 |  |  | 	typedef DLL_DIRECTORY_COOKIE(WINAPI * PAddDllDirectory)(PCWSTR); | 
					
						
							|  |  |  | 	typedef BOOL(WINAPI * PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PAddDllDirectory add_dll_directory = (PAddDllDirectory)GetProcAddress(GetModuleHandle("kernel32.dll"), "AddDllDirectory"); | 
					
						
							|  |  |  | 	PRemoveDllDirectory remove_dll_directory = (PRemoveDllDirectory)GetProcAddress(GetModuleHandle("kernel32.dll"), "RemoveDllDirectory"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	bool has_dll_directory_api = ((add_dll_directory != nullptr) && (remove_dll_directory != nullptr)); | 
					
						
							|  |  |  | 	DLL_DIRECTORY_COOKIE cookie = nullptr; | 
					
						
							| 
									
										
										
										
											2017-11-30 10:00:10 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 14:29:01 +02:00
										 |  |  | 	if (p_also_set_library_path && has_dll_directory_api) { | 
					
						
							| 
									
										
										
										
											2018-01-04 15:42:29 -03:00
										 |  |  | 		cookie = add_dll_directory(path.get_base_dir().c_str()); | 
					
						
							| 
									
										
										
										
											2017-11-30 10:00:10 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	p_library_handle = (void *)LoadLibraryExW(path.c_str(), nullptr, (p_also_set_library_path && has_dll_directory_api) ? LOAD_LIBRARY_SEARCH_DEFAULT_DIRS : 0); | 
					
						
							| 
									
										
										
										
											2019-08-09 06:49:33 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!p_library_handle, ERR_CANT_OPEN, "Can't open dynamic library: " + p_path + ", error: " + format_error_message(GetLastError()) + "."); | 
					
						
							| 
									
										
										
										
											2017-11-30 10:00:10 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 11:48:20 +01:00
										 |  |  | 	if (cookie) { | 
					
						
							| 
									
										
										
										
											2017-12-06 14:29:01 +02:00
										 |  |  | 		remove_dll_directory(cookie); | 
					
						
							| 
									
										
										
										
											2017-11-30 10:00:10 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-08 02:50:13 +01:00
										 |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 16:11:38 +02:00
										 |  |  | Error OS_Windows::close_dynamic_library(void *p_library_handle) { | 
					
						
							|  |  |  | 	if (!FreeLibrary((HMODULE)p_library_handle)) { | 
					
						
							| 
									
										
										
										
											2017-03-08 02:50:13 +01:00
										 |  |  | 		return FAILED; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 09:23:21 +02:00
										 |  |  | Error OS_Windows::get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional) { | 
					
						
							| 
									
										
										
										
											2017-04-03 16:11:38 +02:00
										 |  |  | 	p_symbol_handle = (void *)GetProcAddress((HMODULE)p_library_handle, p_name.utf8().get_data()); | 
					
						
							| 
									
										
										
										
											2017-03-08 02:50:13 +01:00
										 |  |  | 	if (!p_symbol_handle) { | 
					
						
							| 
									
										
										
										
											2017-07-27 09:23:21 +02:00
										 |  |  | 		if (!p_optional) { | 
					
						
							| 
									
										
										
										
											2019-08-09 06:49:33 +02:00
										 |  |  | 			ERR_FAIL_V_MSG(ERR_CANT_RESOLVE, "Can't resolve symbol " + p_name + ", error: " + String::num(GetLastError()) + "."); | 
					
						
							| 
									
										
										
										
											2017-07-27 09:23:21 +02:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			return ERR_CANT_RESOLVE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-08 02:50:13 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 13:36:24 -04:00
										 |  |  | String OS_Windows::get_name() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return "Windows"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-07 15:06:13 +02:00
										 |  |  | OS::Date OS_Windows::get_date(bool utc) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	SYSTEMTIME systemtime; | 
					
						
							| 
									
										
										
										
											2015-06-07 15:06:13 +02:00
										 |  |  | 	if (utc) | 
					
						
							| 
									
										
										
										
											2015-06-06 03:40:56 +02:00
										 |  |  | 		GetSystemTime(&systemtime); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		GetLocalTime(&systemtime); | 
					
						
							| 
									
										
										
										
											2015-06-07 15:06:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Date date; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	date.day = systemtime.wDay; | 
					
						
							|  |  |  | 	date.month = Month(systemtime.wMonth); | 
					
						
							|  |  |  | 	date.weekday = Weekday(systemtime.wDayOfWeek); | 
					
						
							|  |  |  | 	date.year = systemtime.wYear; | 
					
						
							|  |  |  | 	date.dst = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return date; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-06 03:40:56 +02:00
										 |  |  | OS::Time OS_Windows::get_time(bool utc) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	SYSTEMTIME systemtime; | 
					
						
							| 
									
										
										
										
											2015-06-06 03:40:56 +02:00
										 |  |  | 	if (utc) | 
					
						
							|  |  |  | 		GetSystemTime(&systemtime); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		GetLocalTime(&systemtime); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Time time; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	time.hour = systemtime.wHour; | 
					
						
							|  |  |  | 	time.min = systemtime.wMinute; | 
					
						
							|  |  |  | 	time.sec = systemtime.wSecond; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return time; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-06 05:35:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | OS::TimeZoneInfo OS_Windows::get_time_zone_info() const { | 
					
						
							|  |  |  | 	TIME_ZONE_INFORMATION info; | 
					
						
							|  |  |  | 	bool daylight = false; | 
					
						
							| 
									
										
										
										
											2015-06-07 16:10:33 +02:00
										 |  |  | 	if (GetTimeZoneInformation(&info) == TIME_ZONE_ID_DAYLIGHT) | 
					
						
							| 
									
										
										
										
											2015-06-06 05:35:38 +02:00
										 |  |  | 		daylight = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-07 16:10:33 +02:00
										 |  |  | 	TimeZoneInfo ret; | 
					
						
							| 
									
										
										
										
											2015-06-06 05:35:38 +02:00
										 |  |  | 	if (daylight) { | 
					
						
							|  |  |  | 		ret.name = info.DaylightName; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ret.name = info.StandardName; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 21:41:19 -03:00
										 |  |  | 	// Bias value returned by GetTimeZoneInformation is inverted of what we expect
 | 
					
						
							|  |  |  | 	// For example on GMT-3 GetTimeZoneInformation return a Bias of 180, so invert the value to get -180
 | 
					
						
							|  |  |  | 	ret.bias = -info.Bias; | 
					
						
							| 
									
										
										
										
											2015-06-06 05:35:38 +02:00
										 |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-07 15:06:13 +02:00
										 |  |  | uint64_t OS_Windows::get_unix_time() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	FILETIME ft; | 
					
						
							|  |  |  | 	SYSTEMTIME st; | 
					
						
							|  |  |  | 	GetSystemTime(&st); | 
					
						
							|  |  |  | 	SystemTimeToFileTime(&st, &ft); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	SYSTEMTIME ep; | 
					
						
							|  |  |  | 	ep.wYear = 1970; | 
					
						
							|  |  |  | 	ep.wMonth = 1; | 
					
						
							|  |  |  | 	ep.wDayOfWeek = 4; | 
					
						
							|  |  |  | 	ep.wDay = 1; | 
					
						
							|  |  |  | 	ep.wHour = 0; | 
					
						
							|  |  |  | 	ep.wMinute = 0; | 
					
						
							|  |  |  | 	ep.wSecond = 0; | 
					
						
							|  |  |  | 	ep.wMilliseconds = 0; | 
					
						
							|  |  |  | 	FILETIME fep; | 
					
						
							|  |  |  | 	SystemTimeToFileTime(&ep, &fep); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-29 23:52:59 +01:00
										 |  |  | 	// Type punning through unions (rather than pointer cast) as per:
 | 
					
						
							| 
									
										
										
										
											2019-06-15 12:11:30 +02:00
										 |  |  | 	// https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime#remarks
 | 
					
						
							| 
									
										
										
										
											2019-07-29 23:52:59 +01:00
										 |  |  | 	ULARGE_INTEGER ft_punning; | 
					
						
							|  |  |  | 	ft_punning.LowPart = ft.dwLowDateTime; | 
					
						
							|  |  |  | 	ft_punning.HighPart = ft.dwHighDateTime; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ULARGE_INTEGER fep_punning; | 
					
						
							|  |  |  | 	fep_punning.LowPart = fep.dwLowDateTime; | 
					
						
							|  |  |  | 	fep_punning.HighPart = fep.dwHighDateTime; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (ft_punning.QuadPart - fep_punning.QuadPart) / 10000000; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-10 18:24:55 -03:00
										 |  |  | uint64_t OS_Windows::get_system_time_secs() const { | 
					
						
							| 
									
										
										
										
											2016-06-11 14:09:21 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 06:18:52 +09:00
										 |  |  | 	return get_system_time_msecs() / 1000; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint64_t OS_Windows::get_system_time_msecs() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const uint64_t WINDOWS_TICK = 10000; | 
					
						
							|  |  |  | 	const uint64_t MSEC_TO_UNIX_EPOCH = 11644473600000LL; | 
					
						
							| 
									
										
										
										
											2016-06-11 14:09:21 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 10:29:33 -07:00
										 |  |  | 	SYSTEMTIME st; | 
					
						
							|  |  |  | 	GetSystemTime(&st); | 
					
						
							| 
									
										
										
										
											2016-01-10 18:24:55 -03:00
										 |  |  | 	FILETIME ft; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	SystemTimeToFileTime(&st, &ft); | 
					
						
							| 
									
										
										
										
											2016-01-10 18:24:55 -03:00
										 |  |  | 	uint64_t ret; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ret = ft.dwHighDateTime; | 
					
						
							|  |  |  | 	ret <<= 32; | 
					
						
							|  |  |  | 	ret |= ft.dwLowDateTime; | 
					
						
							| 
									
										
										
										
											2016-06-11 14:09:21 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 06:18:52 +09:00
										 |  |  | 	return (uint64_t)(ret / WINDOWS_TICK - MSEC_TO_UNIX_EPOCH); | 
					
						
							| 
									
										
										
										
											2015-08-06 10:29:33 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void OS_Windows::delay_usec(uint32_t p_usec) const { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (p_usec < 1000) | 
					
						
							|  |  |  | 		Sleep(1); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		Sleep(p_usec / 1000); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | uint64_t OS_Windows::get_ticks_usec() const { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	uint64_t ticks; | 
					
						
							|  |  |  | 	uint64_t time; | 
					
						
							|  |  |  | 	// This is the number of clock ticks since start
 | 
					
						
							|  |  |  | 	if (!QueryPerformanceCounter((LARGE_INTEGER *)&ticks)) | 
					
						
							|  |  |  | 		ticks = (UINT64)timeGetTime(); | 
					
						
							|  |  |  | 	// Divide by frequency to get the time in seconds
 | 
					
						
							|  |  |  | 	time = ticks * 1000000L / ticks_per_second; | 
					
						
							|  |  |  | 	// Subtract the time at game start to get
 | 
					
						
							|  |  |  | 	// the time since the game started
 | 
					
						
							|  |  |  | 	time -= ticks_start; | 
					
						
							|  |  |  | 	return time; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-07 15:46:52 -03:00
										 |  |  | Error OS_Windows::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (p_blocking && r_pipe) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		String argss; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		argss = "\"\"" + p_path + "\""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (const List<String>::Element *E = p_arguments.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-01 16:51:53 -06:00
										 |  |  | 			argss += " \"" + E->get() + "\""; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		argss += "\""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (read_stderr) { | 
					
						
							|  |  |  | 			argss += " 2>&1"; // Read stderr too
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		FILE *f = _wpopen(argss.c_str(), L"r"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ERR_FAIL_COND_V(!f, ERR_CANT_OPEN); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		char buf[65535]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		while (fgets(buf, 65535, f)) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-07 15:46:52 -03:00
										 |  |  | 			if (p_pipe_mutex) { | 
					
						
							|  |  |  | 				p_pipe_mutex->lock(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-02-19 15:59:37 -03:00
										 |  |  | 			(*r_pipe) += String::utf8(buf); | 
					
						
							| 
									
										
										
										
											2019-04-07 15:46:52 -03:00
										 |  |  | 			if (p_pipe_mutex) { | 
					
						
							| 
									
										
										
										
											2019-04-24 15:52:15 -03:00
										 |  |  | 				p_pipe_mutex->unlock(); | 
					
						
							| 
									
										
										
										
											2019-04-07 15:46:52 -03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		int rv = _pclose(f); | 
					
						
							|  |  |  | 		if (r_exitcode) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			*r_exitcode = rv; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return OK; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	String cmdline = "\"" + p_path + "\""; | 
					
						
							|  |  |  | 	const List<String>::Element *I = p_arguments.front(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	while (I) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		cmdline += " \"" + I->get() + "\""; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		I = I->next(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ProcessInfo pi; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ZeroMemory(&pi.si, sizeof(pi.si)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	pi.si.cb = sizeof(pi.si); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ZeroMemory(&pi.pi, sizeof(pi.pi)); | 
					
						
							|  |  |  | 	LPSTARTUPINFOW si_w = (LPSTARTUPINFOW)&pi.si; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 02:01:39 -03:00
										 |  |  | 	Vector<CharType> modstr; //windows wants to change this no idea why
 | 
					
						
							|  |  |  | 	modstr.resize(cmdline.size()); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < cmdline.size(); i++) | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		modstr.write[i] = cmdline[i]; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	int ret = CreateProcessW(nullptr, modstr.ptrw(), nullptr, nullptr, 0, NORMAL_PRIORITY_CLASS & CREATE_NO_WINDOW, nullptr, nullptr, si_w, &pi.pi); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	ERR_FAIL_COND_V(ret == 0, ERR_CANT_FORK); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_blocking) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 12:11:30 +02:00
										 |  |  | 		DWORD ret2 = WaitForSingleObject(pi.pi.hProcess, INFINITE); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		if (r_exitcode) | 
					
						
							| 
									
										
										
										
											2019-06-15 12:11:30 +02:00
										 |  |  | 			*r_exitcode = ret2; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 09:55:33 +02:00
										 |  |  | 		CloseHandle(pi.pi.hProcess); | 
					
						
							|  |  |  | 		CloseHandle(pi.pi.hThread); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ProcessID pid = pi.pi.dwProcessId; | 
					
						
							|  |  |  | 		if (r_child_id) { | 
					
						
							|  |  |  | 			*r_child_id = pid; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		process_map->insert(pid, pi); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 17:32:43 +02:00
										 |  |  | Error OS_Windows::kill(const ProcessID &p_pid) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 09:55:33 +02:00
										 |  |  | 	ERR_FAIL_COND_V(!process_map->has(p_pid), FAILED); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 09:55:33 +02:00
										 |  |  | 	const PROCESS_INFORMATION pi = (*process_map)[p_pid].pi; | 
					
						
							|  |  |  | 	process_map->erase(p_pid); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 17:32:43 +02:00
										 |  |  | 	const int ret = TerminateProcess(pi.hProcess, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-30 09:55:33 +02:00
										 |  |  | 	CloseHandle(pi.hProcess); | 
					
						
							|  |  |  | 	CloseHandle(pi.hThread); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 17:32:43 +02:00
										 |  |  | 	return ret != 0 ? OK : FAILED; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | int OS_Windows::get_process_id() const { | 
					
						
							| 
									
										
										
										
											2016-04-29 13:57:57 -03:00
										 |  |  | 	return _getpid(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error OS_Windows::set_cwd(const String &p_cwd) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (_wchdir(p_cwd.c_str()) != 0) | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return ERR_CANT_OPEN; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 02:01:39 -03:00
										 |  |  | String OS_Windows::get_executable_path() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	wchar_t bufname[4096]; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	GetModuleFileNameW(nullptr, bufname, 4096); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	String s = bufname; | 
					
						
							| 
									
										
										
										
											2014-02-15 02:01:39 -03:00
										 |  |  | 	return s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | bool OS_Windows::has_environment(const String &p_var) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:38:52 +01:00
										 |  |  | #ifdef MINGW_ENABLED
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	return _wgetenv(p_var.c_str()) != nullptr; | 
					
						
							| 
									
										
										
										
											2018-10-04 14:38:52 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 	wchar_t *env; | 
					
						
							|  |  |  | 	size_t len; | 
					
						
							|  |  |  | 	_wdupenv_s(&env, &len, p_var.c_str()); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	const bool has_env = env != nullptr; | 
					
						
							| 
									
										
										
										
											2018-10-04 14:38:52 +01:00
										 |  |  | 	free(env); | 
					
						
							|  |  |  | 	return has_env; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | String OS_Windows::get_environment(const String &p_var) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 13:12:05 -03:00
										 |  |  | 	wchar_t wval[0x7Fff]; // MSDN says 32767 char is the maximum
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int wlen = GetEnvironmentVariableW(p_var.c_str(), wval, 0x7Fff); | 
					
						
							|  |  |  | 	if (wlen > 0) { | 
					
						
							| 
									
										
										
										
											2015-05-04 13:12:05 -03:00
										 |  |  | 		return wval; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-13 18:03:28 -03:00
										 |  |  | 	return ""; | 
					
						
							| 
									
										
										
										
											2015-05-04 13:12:05 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 22:59:38 +01:00
										 |  |  | bool OS_Windows::set_environment(const String &p_var, const String &p_value) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (bool)SetEnvironmentVariableW(p_var.c_str(), p_value.c_str()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | String OS_Windows::get_stdin_string(bool p_block) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_block) { | 
					
						
							|  |  |  | 		char buff[1024]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		return fgets(buff, 1024, stdin); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return String(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-18 11:43:54 -03:00
										 |  |  | Error OS_Windows::shell_open(String p_uri) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	ShellExecuteW(nullptr, nullptr, p_uri.c_str(), nullptr, nullptr, SW_SHOWNORMAL); | 
					
						
							| 
									
										
										
										
											2014-04-18 11:43:54 -03:00
										 |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | String OS_Windows::get_locale() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const _WinLocale *wl = &_win_locales[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	LANGID langid = GetUserDefaultUILanguage(); | 
					
						
							|  |  |  | 	String neutral; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int lang = langid & ((1 << 9) - 1); | 
					
						
							|  |  |  | 	int sublang = langid & ~((1 << 9) - 1); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	while (wl->locale) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (wl->main_lang == lang && wl->sublang == SUBLANG_NEUTRAL) | 
					
						
							|  |  |  | 			neutral = wl->locale; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (lang == wl->main_lang && sublang == wl->sublang) | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			return wl->locale; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		wl++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (neutral != "") | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return neutral; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return "en"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 16:46:30 +01:00
										 |  |  | // We need this because GetSystemInfo() is unreliable on WOW64
 | 
					
						
							|  |  |  | // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms724381(v=vs.85).aspx
 | 
					
						
							|  |  |  | // Taken from MSDN
 | 
					
						
							|  |  |  | typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL); | 
					
						
							|  |  |  | LPFN_ISWOW64PROCESS fnIsWow64Process; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | BOOL is_wow64() { | 
					
						
							|  |  |  | 	BOOL wow64 = FALSE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (fnIsWow64Process) { | 
					
						
							|  |  |  | 		if (!fnIsWow64Process(GetCurrentProcess(), &wow64)) { | 
					
						
							|  |  |  | 			wow64 = FALSE; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return wow64; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int OS_Windows::get_processor_count() const { | 
					
						
							|  |  |  | 	SYSTEM_INFO sysinfo; | 
					
						
							|  |  |  | 	if (is_wow64()) | 
					
						
							|  |  |  | 		GetNativeSystemInfo(&sysinfo); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		GetSystemInfo(&sysinfo); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sysinfo.dwNumberOfProcessors; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void OS_Windows::run() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!main_loop) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	main_loop->init(); | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	while (!force_quit) { | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 		DisplayServer::get_singleton()->process_events(); // get rid of pending events
 | 
					
						
							| 
									
										
										
										
											2018-10-03 13:40:37 -04:00
										 |  |  | 		if (Main::iteration()) | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	main_loop->finish(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MainLoop *OS_Windows::get_main_loop() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return main_loop; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Add initial support for the XDG Base Directory spec
Spec version 0.7 from https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
(latest as of this commit).
Three virtual methods are added to OS for the various XDG paths we will use:
- OS::get_data_path gives XDG_DATA_HOME, or if missing:
  ~/.local/share on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_config_path gives XDG_CONFIG_HOME, or if missing:
  ~/.config on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_cache_path gives XDG_CACHE_HOME, or if missing:
  ~/.cache on X11, ~/Library/Caches on macOS and %APPDATA% on Windows
So for Windows there are no changes, for Linux we follow the full split spec
and for macOS stuff will move from ~/.godot to ~/Library/Application Support/Godot.
Support for system-wide installation of templates on Unix was removed for now,
as it's a bit hackish and I don't think anyone uses it.
user:// will still be OS::get_data_path() + "/godot/app_userdata/$name" by
default, but when using the application/config/use_shared_user_dir option
it will now use XDG_DATA_HOME/$name, e.g. ~/.local/share/MyGame.
For now everything still goes in EditorSettings::get_settings_dir(), but
this will be changed in a later commit to make use of the new splitting
where relevant.
Part of #3513.
											
										 
											2017-11-17 17:11:41 +01:00
										 |  |  | String OS_Windows::get_config_path() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (has_environment("XDG_CONFIG_HOME")) { // unlikely, but after all why not?
 | 
					
						
							|  |  |  | 		return get_environment("XDG_CONFIG_HOME"); | 
					
						
							|  |  |  | 	} else if (has_environment("APPDATA")) { | 
					
						
							|  |  |  | 		return get_environment("APPDATA"); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return "."; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String OS_Windows::get_data_path() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (has_environment("XDG_DATA_HOME")) { | 
					
						
							|  |  |  | 		return get_environment("XDG_DATA_HOME"); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return get_config_path(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String OS_Windows::get_cache_path() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (has_environment("XDG_CACHE_HOME")) { | 
					
						
							|  |  |  | 		return get_environment("XDG_CACHE_HOME"); | 
					
						
							|  |  |  | 	} else if (has_environment("TEMP")) { | 
					
						
							|  |  |  | 		return get_environment("TEMP"); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return get_config_path(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get properly capitalized engine name for system paths
 | 
					
						
							|  |  |  | String OS_Windows::get_godot_dir_name() const { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 21:18:01 +01:00
										 |  |  | 	return String(VERSION_SHORT_NAME).capitalize(); | 
					
						
							| 
									
										
											  
											
												Add initial support for the XDG Base Directory spec
Spec version 0.7 from https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
(latest as of this commit).
Three virtual methods are added to OS for the various XDG paths we will use:
- OS::get_data_path gives XDG_DATA_HOME, or if missing:
  ~/.local/share on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_config_path gives XDG_CONFIG_HOME, or if missing:
  ~/.config on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_cache_path gives XDG_CACHE_HOME, or if missing:
  ~/.cache on X11, ~/Library/Caches on macOS and %APPDATA% on Windows
So for Windows there are no changes, for Linux we follow the full split spec
and for macOS stuff will move from ~/.godot to ~/Library/Application Support/Godot.
Support for system-wide installation of templates on Unix was removed for now,
as it's a bit hackish and I don't think anyone uses it.
user:// will still be OS::get_data_path() + "/godot/app_userdata/$name" by
default, but when using the application/config/use_shared_user_dir option
it will now use XDG_DATA_HOME/$name, e.g. ~/.local/share/MyGame.
For now everything still goes in EditorSettings::get_settings_dir(), but
this will be changed in a later commit to make use of the new splitting
where relevant.
Part of #3513.
											
										 
											2017-11-17 17:11:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | String OS_Windows::get_system_dir(SystemDir p_dir) const { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 	KNOWNFOLDERID id; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	switch (p_dir) { | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		case SYSTEM_DIR_DESKTOP: { | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 			id = FOLDERID_Desktop; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SYSTEM_DIR_DCIM: { | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 			id = FOLDERID_Pictures; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SYSTEM_DIR_DOCUMENTS: { | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 			id = FOLDERID_Documents; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SYSTEM_DIR_DOWNLOADS: { | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 			id = FOLDERID_Downloads; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SYSTEM_DIR_MOVIES: { | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 			id = FOLDERID_Videos; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SYSTEM_DIR_MUSIC: { | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 			id = FOLDERID_Music; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SYSTEM_DIR_PICTURES: { | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 			id = FOLDERID_Pictures; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case SYSTEM_DIR_RINGTONES: { | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 			id = FOLDERID_Music; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 	PWSTR szPath; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	HRESULT res = SHGetKnownFolderPath(id, 0, nullptr, &szPath); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(res != S_OK, String()); | 
					
						
							| 
									
										
										
										
											2019-04-18 02:43:33 +00:00
										 |  |  | 	String path = String(szPath); | 
					
						
							|  |  |  | 	CoTaskMemFree(szPath); | 
					
						
							|  |  |  | 	return path; | 
					
						
							| 
									
										
										
										
											2014-12-02 14:02:41 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Add initial support for the XDG Base Directory spec
Spec version 0.7 from https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
(latest as of this commit).
Three virtual methods are added to OS for the various XDG paths we will use:
- OS::get_data_path gives XDG_DATA_HOME, or if missing:
  ~/.local/share on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_config_path gives XDG_CONFIG_HOME, or if missing:
  ~/.config on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_cache_path gives XDG_CACHE_HOME, or if missing:
  ~/.cache on X11, ~/Library/Caches on macOS and %APPDATA% on Windows
So for Windows there are no changes, for Linux we follow the full split spec
and for macOS stuff will move from ~/.godot to ~/Library/Application Support/Godot.
Support for system-wide installation of templates on Unix was removed for now,
as it's a bit hackish and I don't think anyone uses it.
user:// will still be OS::get_data_path() + "/godot/app_userdata/$name" by
default, but when using the application/config/use_shared_user_dir option
it will now use XDG_DATA_HOME/$name, e.g. ~/.local/share/MyGame.
For now everything still goes in EditorSettings::get_settings_dir(), but
this will be changed in a later commit to make use of the new splitting
where relevant.
Part of #3513.
											
										 
											2017-11-17 17:11:41 +01:00
										 |  |  | String OS_Windows::get_user_data_dir() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-26 19:00:53 +01:00
										 |  |  | 	String appname = get_safe_dir_name(ProjectSettings::get_singleton()->get("application/config/name")); | 
					
						
							| 
									
										
											  
											
												Add initial support for the XDG Base Directory spec
Spec version 0.7 from https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
(latest as of this commit).
Three virtual methods are added to OS for the various XDG paths we will use:
- OS::get_data_path gives XDG_DATA_HOME, or if missing:
  ~/.local/share on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_config_path gives XDG_CONFIG_HOME, or if missing:
  ~/.config on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_cache_path gives XDG_CACHE_HOME, or if missing:
  ~/.cache on X11, ~/Library/Caches on macOS and %APPDATA% on Windows
So for Windows there are no changes, for Linux we follow the full split spec
and for macOS stuff will move from ~/.godot to ~/Library/Application Support/Godot.
Support for system-wide installation of templates on Unix was removed for now,
as it's a bit hackish and I don't think anyone uses it.
user:// will still be OS::get_data_path() + "/godot/app_userdata/$name" by
default, but when using the application/config/use_shared_user_dir option
it will now use XDG_DATA_HOME/$name, e.g. ~/.local/share/MyGame.
For now everything still goes in EditorSettings::get_settings_dir(), but
this will be changed in a later commit to make use of the new splitting
where relevant.
Part of #3513.
											
										 
											2017-11-17 17:11:41 +01:00
										 |  |  | 	if (appname != "") { | 
					
						
							| 
									
										
										
										
											2017-11-26 19:00:53 +01:00
										 |  |  | 		bool use_custom_dir = ProjectSettings::get_singleton()->get("application/config/use_custom_user_dir"); | 
					
						
							|  |  |  | 		if (use_custom_dir) { | 
					
						
							|  |  |  | 			String custom_dir = get_safe_dir_name(ProjectSettings::get_singleton()->get("application/config/custom_user_dir_name"), true); | 
					
						
							|  |  |  | 			if (custom_dir == "") { | 
					
						
							|  |  |  | 				custom_dir = appname; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return get_data_path().plus_file(custom_dir).replace("\\", "/"); | 
					
						
							| 
									
										
											  
											
												Add initial support for the XDG Base Directory spec
Spec version 0.7 from https://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
(latest as of this commit).
Three virtual methods are added to OS for the various XDG paths we will use:
- OS::get_data_path gives XDG_DATA_HOME, or if missing:
  ~/.local/share on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_config_path gives XDG_CONFIG_HOME, or if missing:
  ~/.config on X11, ~/Library/Application Support/ on macOS and %APPDATA% on Windows
- OS::get_cache_path gives XDG_CACHE_HOME, or if missing:
  ~/.cache on X11, ~/Library/Caches on macOS and %APPDATA% on Windows
So for Windows there are no changes, for Linux we follow the full split spec
and for macOS stuff will move from ~/.godot to ~/Library/Application Support/Godot.
Support for system-wide installation of templates on Unix was removed for now,
as it's a bit hackish and I don't think anyone uses it.
user:// will still be OS::get_data_path() + "/godot/app_userdata/$name" by
default, but when using the application/config/use_shared_user_dir option
it will now use XDG_DATA_HOME/$name, e.g. ~/.local/share/MyGame.
For now everything still goes in EditorSettings::get_settings_dir(), but
this will be changed in a later commit to make use of the new splitting
where relevant.
Part of #3513.
											
										 
											2017-11-17 17:11:41 +01:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-11-26 19:00:53 +01:00
										 |  |  | 			return get_data_path().plus_file(get_godot_dir_name()).plus_file("app_userdata").plus_file(appname).replace("\\", "/"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-19 17:00:46 -03:00
										 |  |  | 	return ProjectSettings::get_singleton()->get_resource_path(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 21:40:52 +08:00
										 |  |  | String OS_Windows::get_unique_id() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	HW_PROFILE_INFO HwProfInfo; | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!GetCurrentHwProfile(&HwProfInfo), ""); | 
					
						
							|  |  |  | 	return String(HwProfInfo.szHwProfileGuid); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-19 17:00:46 -03:00
										 |  |  | bool OS_Windows::_check_internal_feature_support(const String &p_feature) { | 
					
						
							| 
									
										
										
										
											2017-02-08 18:07:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-26 11:58:47 -03:00
										 |  |  | 	return p_feature == "pc"; | 
					
						
							| 
									
										
										
										
											2017-02-08 18:07:35 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-05 19:14:33 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-07 22:01:49 -03:00
										 |  |  | void OS_Windows::disable_crash_handler() { | 
					
						
							|  |  |  | 	crash_handler.disable(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool OS_Windows::is_disable_crash_handler() const { | 
					
						
							|  |  |  | 	return crash_handler.is_disabled(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-25 10:15:11 -03:00
										 |  |  | Error OS_Windows::move_to_trash(const String &p_path) { | 
					
						
							| 
									
										
										
										
											2018-08-14 11:49:16 +03:00
										 |  |  | 	SHFILEOPSTRUCTW sf; | 
					
						
							| 
									
										
										
										
											2018-08-18 01:30:22 +03:00
										 |  |  | 	WCHAR *from = new WCHAR[p_path.length() + 2]; | 
					
						
							| 
									
										
										
										
											2018-10-04 14:38:52 +01:00
										 |  |  | 	wcscpy_s(from, p_path.length() + 1, p_path.c_str()); | 
					
						
							| 
									
										
										
										
											2018-08-18 01:30:22 +03:00
										 |  |  | 	from[p_path.length() + 1] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	sf.hwnd = main_window; | 
					
						
							| 
									
										
										
										
											2017-09-25 10:15:11 -03:00
										 |  |  | 	sf.wFunc = FO_DELETE; | 
					
						
							| 
									
										
										
										
											2018-08-18 01:30:22 +03:00
										 |  |  | 	sf.pFrom = from; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	sf.pTo = nullptr; | 
					
						
							| 
									
										
										
										
											2017-09-25 10:15:11 -03:00
										 |  |  | 	sf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; | 
					
						
							|  |  |  | 	sf.fAnyOperationsAborted = FALSE; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	sf.hNameMappings = nullptr; | 
					
						
							|  |  |  | 	sf.lpszProgressTitle = nullptr; | 
					
						
							| 
									
										
										
										
											2017-09-25 10:15:11 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 11:49:16 +03:00
										 |  |  | 	int ret = SHFileOperationW(&sf); | 
					
						
							| 
									
										
										
										
											2018-08-18 01:30:22 +03:00
										 |  |  | 	delete[] from; | 
					
						
							| 
									
										
										
										
											2017-09-25 10:15:11 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ret) { | 
					
						
							| 
									
										
										
										
											2019-11-06 17:03:04 +01:00
										 |  |  | 		ERR_PRINT("SHFileOperation error: " + itos(ret)); | 
					
						
							| 
									
										
										
										
											2017-09-25 10:15:11 -03:00
										 |  |  | 		return FAILED; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | OS_Windows::OS_Windows(HINSTANCE _hInstance) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	force_quit = false; | 
					
						
							| 
									
										
										
										
											2018-12-13 22:32:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	hInstance = _hInstance; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #ifdef STDOUT_FILE
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	stdo = fopen("stdout.txt", "wb"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 14:01:34 -03:00
										 |  |  | #ifdef WASAPI_ENABLED
 | 
					
						
							|  |  |  | 	AudioDriverManager::add_driver(&driver_wasapi); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-17 13:40:45 -02:00
										 |  |  | #ifdef XAUDIO2_ENABLED
 | 
					
						
							| 
									
										
										
										
											2017-01-16 19:19:45 +01:00
										 |  |  | 	AudioDriverManager::add_driver(&driver_xaudio2); | 
					
						
							| 
									
										
										
										
											2016-10-17 13:40:45 -02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:56:48 -03:00
										 |  |  | 	DisplayServerWindows::register_windows_driver(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 16:35:01 +07:00
										 |  |  | 	Vector<Logger *> loggers; | 
					
						
							|  |  |  | 	loggers.push_back(memnew(WindowsTerminalLogger)); | 
					
						
							|  |  |  | 	_set_logger(memnew(CompositeLogger(loggers))); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | OS_Windows::~OS_Windows() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #ifdef STDOUT_FILE
 | 
					
						
							|  |  |  | 	fclose(stdo); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } |