diff options
Diffstat (limited to 'platform/windows')
-rw-r--r-- | platform/windows/SCsub | 3 | ||||
-rw-r--r-- | platform/windows/crash_handler_win.cpp | 211 | ||||
-rw-r--r-- | platform/windows/crash_handler_win.h | 56 | ||||
-rw-r--r-- | platform/windows/detect.py | 11 | ||||
-rw-r--r-- | platform/windows/godot_win.cpp | 20 | ||||
-rw-r--r-- | platform/windows/os_windows.cpp | 39 | ||||
-rw-r--r-- | platform/windows/os_windows.h | 8 | ||||
-rw-r--r-- | platform/windows/power_windows.cpp | 18 | ||||
-rw-r--r-- | platform/windows/power_windows.h | 6 |
9 files changed, 343 insertions, 29 deletions
diff --git a/platform/windows/SCsub b/platform/windows/SCsub index befbe00183..b56a5c6a80 100644 --- a/platform/windows/SCsub +++ b/platform/windows/SCsub @@ -5,6 +5,7 @@ Import('env') common_win = [ "context_gl_win.cpp", + "crash_handler_win.cpp", "os_windows.cpp", "ctxgl_procaddr.cpp", "key_mapping_win.cpp", @@ -12,7 +13,7 @@ common_win = [ "packet_peer_udp_winsock.cpp", "stream_peer_winsock.cpp", "joypad.cpp", - "power_windows.cpp", + "power_windows.cpp", ] restarget = "godot_res" + env["OBJSUFFIX"] diff --git a/platform/windows/crash_handler_win.cpp b/platform/windows/crash_handler_win.cpp new file mode 100644 index 0000000000..2f5ee7956e --- /dev/null +++ b/platform/windows/crash_handler_win.cpp @@ -0,0 +1,211 @@ +/*************************************************************************/ +/* crash_handler_win.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "main/main.h" +#include "os_windows.h" + +#ifdef CRASH_HANDLER_EXCEPTION + +// Backtrace code code based on: https://stackoverflow.com/questions/6205981/windows-c-stack-trace-from-a-running-app + +#include <psapi.h> +#include <algorithm> +#include <iterator> + +#pragma comment(lib, "psapi.lib") +#pragma comment(lib, "dbghelp.lib") + +// Some versions of imagehlp.dll lack the proper packing directives themselves +// so we need to do it. +#pragma pack(push, before_imagehlp, 8) +#include <imagehlp.h> +#pragma pack(pop, before_imagehlp) + +struct module_data { + std::string image_name; + std::string module_name; + void *base_address; + DWORD load_size; +}; + +class symbol { + typedef IMAGEHLP_SYMBOL64 sym_type; + sym_type *sym; + static const int max_name_len = 1024; + +public: + symbol(HANDLE process, DWORD64 address) + : sym((sym_type *)::operator new(sizeof(*sym) + max_name_len)) { + memset(sym, '\0', sizeof(*sym) + max_name_len); + sym->SizeOfStruct = sizeof(*sym); + sym->MaxNameLength = max_name_len; + DWORD64 displacement; + + SymGetSymFromAddr64(process, address, &displacement, sym); + } + + std::string name() { return std::string(sym->Name); } + std::string undecorated_name() { + if (*sym->Name == '\0') + return "<couldn't map PC to fn name>"; + std::vector<char> und_name(max_name_len); + UnDecorateSymbolName(sym->Name, &und_name[0], max_name_len, UNDNAME_COMPLETE); + return std::string(&und_name[0], strlen(&und_name[0])); + } +}; + +class get_mod_info { + HANDLE process; + +public: + get_mod_info(HANDLE h) + : process(h) {} + + module_data operator()(HMODULE module) { + module_data ret; + char temp[4096]; + MODULEINFO mi; + + GetModuleInformation(process, module, &mi, sizeof(mi)); + ret.base_address = mi.lpBaseOfDll; + ret.load_size = mi.SizeOfImage; + + GetModuleFileNameEx(process, module, temp, sizeof(temp)); + ret.image_name = temp; + GetModuleBaseName(process, module, temp, sizeof(temp)); + ret.module_name = temp; + std::vector<char> img(ret.image_name.begin(), ret.image_name.end()); + std::vector<char> mod(ret.module_name.begin(), ret.module_name.end()); + SymLoadModule64(process, 0, &img[0], &mod[0], (DWORD64)ret.base_address, ret.load_size); + return ret; + } +}; + +DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) { + HANDLE process = GetCurrentProcess(); + HANDLE hThread = GetCurrentThread(); + DWORD offset_from_symbol = 0; + IMAGEHLP_LINE64 line = { 0 }; + std::vector<module_data> modules; + DWORD cbNeeded; + std::vector<HMODULE> module_handles(1); + + if (OS::get_singleton() == NULL || OS::get_singleton()->is_disable_crash_handler() || IsDebuggerPresent()) { + return EXCEPTION_CONTINUE_SEARCH; + } + + fprintf(stderr, "%s: Program crashed\n", __FUNCTION__); + + // Load the symbols: + if (!SymInitialize(process, NULL, false)) + return EXCEPTION_CONTINUE_SEARCH; + + SymSetOptions(SymGetOptions() | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); + EnumProcessModules(process, &module_handles[0], module_handles.size() * sizeof(HMODULE), &cbNeeded); + module_handles.resize(cbNeeded / sizeof(HMODULE)); + EnumProcessModules(process, &module_handles[0], module_handles.size() * sizeof(HMODULE), &cbNeeded); + std::transform(module_handles.begin(), module_handles.end(), std::back_inserter(modules), get_mod_info(process)); + void *base = modules[0].base_address; + + // Setup stuff: + CONTEXT *context = ep->ContextRecord; + STACKFRAME64 frame; + bool skip_first = false; + + frame.AddrPC.Mode = AddrModeFlat; + frame.AddrStack.Mode = AddrModeFlat; + frame.AddrFrame.Mode = AddrModeFlat; + +#ifdef _M_X64 + frame.AddrPC.Offset = context->Rip; + frame.AddrStack.Offset = context->Rsp; + frame.AddrFrame.Offset = context->Rbp; +#else + frame.AddrPC.Offset = context->Eip; + frame.AddrStack.Offset = context->Esp; + frame.AddrFrame.Offset = context->Ebp; + + // Skip the first one to avoid a duplicate on 32-bit mode + skip_first = true; +#endif + + line.SizeOfStruct = sizeof(line); + IMAGE_NT_HEADERS *h = ImageNtHeader(base); + DWORD image_type = h->FileHeader.Machine; + int n = 0; + String msg = GLOBAL_GET("debug/settings/crash_handler/message"); + + fprintf(stderr, "Dumping the backtrace. %ls\n", msg.c_str()); + + do { + if (skip_first) { + skip_first = false; + } else { + if (frame.AddrPC.Offset != 0) { + std::string fnName = symbol(process, frame.AddrPC.Offset).undecorated_name(); + + if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &offset_from_symbol, &line)) + fprintf(stderr, "[%d] %s (%s:%d)\n", n, fnName.c_str(), line.FileName, line.LineNumber); + else + fprintf(stderr, "[%d] %s\n", n, fnName.c_str()); + } else + fprintf(stderr, "[%d] ???\n", n); + + n++; + } + + if (!StackWalk64(image_type, process, hThread, &frame, context, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL)) + break; + } while (frame.AddrReturn.Offset != 0 && n < 256); + + fprintf(stderr, "-- END OF BACKTRACE --\n"); + + SymCleanup(process); + + // Pass the exception to the OS + return EXCEPTION_CONTINUE_SEARCH; +} +#endif + +CrashHandler::CrashHandler() { + disabled = false; +} + +CrashHandler::~CrashHandler() { +} + +void CrashHandler::disable() { + if (disabled) + return; + + disabled = true; +} + +void CrashHandler::initialize() { +} diff --git a/platform/windows/crash_handler_win.h b/platform/windows/crash_handler_win.h new file mode 100644 index 0000000000..0b1889e4fe --- /dev/null +++ b/platform/windows/crash_handler_win.h @@ -0,0 +1,56 @@ +/*************************************************************************/ +/* crash_handler_win.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#ifndef CRASH_HANDLER_WIN_H +#define CRASH_HANDLER_WIN_H + +#include <windows.h> + +// Crash handler exception only enabled with MSVC +#if defined(DEBUG_ENABLED) && defined(MSVC) +#define CRASH_HANDLER_EXCEPTION 1 + +extern DWORD CrashHandlerException(EXCEPTION_POINTERS *ep); +#endif + +class CrashHandler { + + bool disabled; + +public: + void initialize(); + + void disable(); + bool is_disabled() const { return disabled; }; + + CrashHandler(); + ~CrashHandler(); +}; + +#endif diff --git a/platform/windows/detect.py b/platform/windows/detect.py index d239ccf7d2..5bd9a78f49 100644 --- a/platform/windows/detect.py +++ b/platform/windows/detect.py @@ -123,7 +123,7 @@ def configure(env): env.Append(LINKFLAGS=['/ENTRY:mainCRTStartup']) elif (env["target"] == "debug"): - env.Append(CCFLAGS=['/Z7', '/DDEBUG_ENABLED', '/DDEBUG_MEMORY_ENABLED', '/DD3D_DEBUG_INFO', '/Od']) + env.Append(CCFLAGS=['/Z7', '/DDEBUG_ENABLED', '/DDEBUG_MEMORY_ENABLED', '/DD3D_DEBUG_INFO', '/Od', '/EHsc']) env.Append(LINKFLAGS=['/SUBSYSTEM:CONSOLE']) env.Append(LINKFLAGS=['/DEBUG']) @@ -226,10 +226,13 @@ def configure(env): else: env["PROGSUFFIX"] = env["PROGSUFFIX"] + ".exe" # for linux cross-compilation - mingw_prefix = "" - if (env["bits"] == "default"): - env["bits"] = "64" if "PROGRAMFILES(X86)" in os.environ else "32" + if (os.name == "nt"): + env["bits"] = "64" if "PROGRAMFILES(X86)" in os.environ else "32" + else: # default to 64-bit on Linux + env["bits"] = "64" + + mingw_prefix = "" if (env["bits"] == "32"): env.Append(LINKFLAGS=['-static']) diff --git a/platform/windows/godot_win.cpp b/platform/windows/godot_win.cpp index d2ac6ecb50..cff2cbad42 100644 --- a/platform/windows/godot_win.cpp +++ b/platform/windows/godot_win.cpp @@ -156,10 +156,7 @@ int widechar_main(int argc, wchar_t **argv) { return os.get_exit_code(); }; -int main(int _argc, char **_argv) { - // _argc and _argv are ignored - // we are going to use the WideChar version of them instead - +int _main() { LPWSTR *wc_argv; int argc; int result; @@ -177,6 +174,21 @@ int main(int _argc, char **_argv) { return result; } +int main(int _argc, char **_argv) { +// _argc and _argv are ignored +// we are going to use the WideChar version of them instead + +#ifdef CRASH_HANDLER_EXCEPTION + __try { + return _main(); + } __except (CrashHandlerException(GetExceptionInformation())) { + return 1; + } +#else + return _main(); +#endif +} + HINSTANCE godot_hinstance = NULL; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 5814d883cd..461caf479c 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -164,6 +164,8 @@ const char *OS_Windows::get_audio_driver_name(int p_driver) const { void OS_Windows::initialize_core() { + crash_handler.initialize(); + last_button_state = 0; //RedirectIOToConsole(); @@ -887,6 +889,12 @@ static int QueryDpiForMonitor(HMONITOR hmon, _MonitorDpiType dpiType = MDT_Defau return (dpiX + dpiY) / 2; } +typedef enum _SHC_PROCESS_DPI_AWARENESS { + SHC_PROCESS_DPI_UNAWARE = 0, + SHC_PROCESS_SYSTEM_DPI_AWARE = 1, + SHC_PROCESS_PER_MONITOR_DPI_AWARE = 2 +} SHC_PROCESS_DPI_AWARENESS; + void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { main_loop = NULL; @@ -894,6 +902,20 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int window_has_focus = true; WNDCLASSEXW wc; + if (is_hidpi_allowed()) { + HMODULE Shcore = LoadLibraryW(L"Shcore.dll"); + + if (Shcore != NULL) { + typedef HRESULT(WINAPI * SetProcessDpiAwareness_t)(SHC_PROCESS_DPI_AWARENESS); + + SetProcessDpiAwareness_t SetProcessDpiAwareness = (SetProcessDpiAwareness_t)GetProcAddress(Shcore, "SetProcessDpiAwareness"); + + if (SetProcessDpiAwareness) { + SetProcessDpiAwareness(SHC_PROCESS_SYSTEM_DPI_AWARE); + } + } + } + video_mode = p_desired; //printf("**************** desired %s, mode %s\n", p_desired.fullscreen?"true":"false", video_mode.fullscreen?"true":"false"); RECT WindowRect; @@ -1059,12 +1081,7 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int power_manager = memnew(PowerWindows); - AudioDriverManager::get_driver(p_audio_driver)->set_singleton(); - - if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) { - - ERR_PRINT("Initializing audio failed."); - } + AudioDriverManager::initialize(p_audio_driver); TRACKMOUSEEVENT tme; tme.cbSize = sizeof(TRACKMOUSEEVENT); @@ -2331,7 +2348,7 @@ bool OS_Windows::is_vsync_enabled() const { return true; } -PowerState OS_Windows::get_power_state() { +OS::PowerState OS_Windows::get_power_state() { return power_manager->get_power_state(); } @@ -2348,6 +2365,14 @@ bool OS_Windows::_check_internal_feature_support(const String &p_feature) { return p_feature == "pc" || p_feature == "s3tc"; } +void OS_Windows::disable_crash_handler() { + crash_handler.disable(); +} + +bool OS_Windows::is_disable_crash_handler() const { + return crash_handler.is_disabled(); +} + OS_Windows::OS_Windows(HINSTANCE _hInstance) { key_event_pos = 0; diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index 0c5965bf51..1a01ac950d 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -31,6 +31,7 @@ #define OS_WINDOWS_H #include "context_gl_win.h" +#include "crash_handler_win.h" #include "drivers/rtaudio/audio_driver_rtaudio.h" #include "drivers/wasapi/audio_driver_wasapi.h" #include "os/input.h" @@ -134,6 +135,8 @@ class OS_Windows : public OS { AudioDriverXAudio2 driver_xaudio2; #endif + CrashHandler crash_handler; + void _drag_event(int p_x, int p_y, int idx); void _touch_event(bool p_pressed, int p_x, int p_y, int idx); @@ -278,12 +281,15 @@ public: virtual void set_use_vsync(bool p_enable); virtual bool is_vsync_enabled() const; - virtual PowerState get_power_state(); + virtual OS::PowerState get_power_state(); virtual int get_power_seconds_left(); virtual int get_power_percent_left(); virtual bool _check_internal_feature_support(const String &p_feature); + void disable_crash_handler(); + bool is_disable_crash_handler() const; + OS_Windows(HINSTANCE _hInstance); ~OS_Windows(); }; diff --git a/platform/windows/power_windows.cpp b/platform/windows/power_windows.cpp index b37e189a3a..8d86f160f1 100644 --- a/platform/windows/power_windows.cpp +++ b/platform/windows/power_windows.cpp @@ -64,19 +64,19 @@ bool PowerWindows::GetPowerInfo_Windows() { /* This API should exist back to Win95. */ if (!GetSystemPowerStatus(&status)) { /* !!! FIXME: push GetLastError() into GetError() */ - power_state = POWERSTATE_UNKNOWN; + power_state = OS::POWERSTATE_UNKNOWN; } else if (status.BatteryFlag == 0xFF) { /* unknown state */ - power_state = POWERSTATE_UNKNOWN; + power_state = OS::POWERSTATE_UNKNOWN; } else if (status.BatteryFlag & (1 << 7)) { /* no battery */ - power_state = POWERSTATE_NO_BATTERY; + power_state = OS::POWERSTATE_NO_BATTERY; } else if (status.BatteryFlag & (1 << 3)) { /* charging */ - power_state = POWERSTATE_CHARGING; + power_state = OS::POWERSTATE_CHARGING; need_details = TRUE; } else if (status.ACLineStatus == 1) { - power_state = POWERSTATE_CHARGED; /* on AC, not charging. */ + power_state = OS::POWERSTATE_CHARGED; /* on AC, not charging. */ need_details = TRUE; } else { - power_state = POWERSTATE_ON_BATTERY; /* not on AC. */ + power_state = OS::POWERSTATE_ON_BATTERY; /* not on AC. */ need_details = TRUE; } @@ -97,11 +97,11 @@ bool PowerWindows::GetPowerInfo_Windows() { return TRUE; /* always the definitive answer on Windows. */ } -PowerState PowerWindows::get_power_state() { +OS::PowerState PowerWindows::get_power_state() { if (GetPowerInfo_Windows()) { return power_state; } else { - return POWERSTATE_UNKNOWN; + return OS::POWERSTATE_UNKNOWN; } } @@ -122,7 +122,7 @@ int PowerWindows::get_power_percent_left() { } PowerWindows::PowerWindows() - : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { + : nsecs_left(-1), percent_left(-1), power_state(OS::POWERSTATE_UNKNOWN) { } PowerWindows::~PowerWindows() { diff --git a/platform/windows/power_windows.h b/platform/windows/power_windows.h index 9da9841f48..0745615195 100644 --- a/platform/windows/power_windows.h +++ b/platform/windows/power_windows.h @@ -33,7 +33,7 @@ #include "os/dir_access.h" #include "os/file_access.h" -#include "os/power.h" +#include "os/os.h" #include <windows.h> @@ -42,7 +42,7 @@ class PowerWindows { private: int nsecs_left; int percent_left; - PowerState power_state; + OS::PowerState power_state; bool GetPowerInfo_Windows(); @@ -50,7 +50,7 @@ public: PowerWindows(); virtual ~PowerWindows(); - PowerState get_power_state(); + OS::PowerState get_power_state(); int get_power_seconds_left(); int get_power_percent_left(); }; |