|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * (c) 2009-2024 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org> |
| 4 | + * |
| 5 | + * QGroundControl is licensed according to the terms in the file |
| 6 | + * COPYING.md in the root of the source code directory. |
| 7 | + * |
| 8 | + ****************************************************************************/ |
| 9 | + |
1 | 10 | #include "Platform.h"
|
2 | 11 |
|
3 |
| -#ifdef Q_OS_MAC |
4 |
| -#include <CoreFoundation/CoreFoundation.h> |
| 12 | +#include <QtCore/QCoreApplication> |
| 13 | +#include <QtCore/QProcessEnvironment> |
| 14 | + |
| 15 | +#if !defined(Q_OS_IOS) && !defined(Q_OS_ANDROID) |
| 16 | +#include "SignalHandler.h" |
| 17 | +#endif |
5 | 18 |
|
6 |
| -void Platform::disableAppNapViaInfoDict() |
| 19 | +#if defined(Q_OS_MACOS) |
| 20 | + #include <CoreFoundation/CoreFoundation.h> |
| 21 | +#elif defined(Q_OS_WIN) |
| 22 | + #include <qt_windows.h> |
| 23 | + #include <iostream> |
| 24 | + #include <iterator> // std::size |
| 25 | + #include <cwchar> // swprintf |
| 26 | + #if defined(_MSC_VER) |
| 27 | + #include <crtdbg.h> |
| 28 | + #include <stdlib.h> |
| 29 | + #include <cstdio> // _snwprintf_s |
| 30 | + #endif |
| 31 | +#endif |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +#if defined(Q_OS_MACOS) |
| 36 | +void disableAppNapViaInfoDict() |
7 | 37 | {
|
8 | 38 | CFBundleRef bundle = CFBundleGetMainBundle();
|
9 |
| - if (!bundle) return; |
| 39 | + if (!bundle) { |
| 40 | + return; |
| 41 | + } |
| 42 | + CFMutableDictionaryRef infoDict = const_cast<CFMutableDictionaryRef>(CFBundleGetInfoDictionary(bundle)); |
| 43 | + if (infoDict) { |
| 44 | + CFDictionarySetValue(infoDict, CFSTR("NSAppSleepDisabled"), kCFBooleanTrue); |
| 45 | + } |
| 46 | +} |
| 47 | +#endif // Q_OS_MACOS |
10 | 48 |
|
11 |
| - // CFBundleGetInfoDictionary returns the dictionary the OS already |
12 |
| - // parsed from Info.plist. Cast it to mutable so we can tweak it. |
13 |
| - CFMutableDictionaryRef infoDict = (CFMutableDictionaryRef) CFBundleGetInfoDictionary(bundle); |
| 49 | +#if defined(Q_OS_WIN) |
14 | 50 |
|
15 |
| - // Inject the key → true |
16 |
| - if (infoDict) { |
17 |
| - CFDictionarySetValue(infoDict, |
18 |
| - CFSTR("NSAppSleepDisabled"), |
19 |
| - kCFBooleanTrue); |
| 51 | +#if defined(_MSC_VER) |
| 52 | +int __cdecl WindowsCrtReportHook(int reportType, char* message, int* returnValue) |
| 53 | +{ |
| 54 | + if (message) { |
| 55 | + std::cerr << message << std::endl; |
| 56 | + } |
| 57 | + if (reportType == _CRT_ASSERT) { |
| 58 | + if (returnValue) { |
| 59 | + *returnValue = 0; |
| 60 | + } |
| 61 | + return 1; // handled |
| 62 | + } |
| 63 | + return 0; // let CRT continue |
| 64 | +} |
| 65 | + |
| 66 | +void __cdecl WindowsPurecallHandler() |
| 67 | +{ |
| 68 | + (void) OutputDebugStringW(L"QGC: _purecall\n"); |
| 69 | +} |
| 70 | + |
| 71 | +void WindowsInvalidParameterHandler([[maybe_unused]] const wchar_t* expression, |
| 72 | + [[maybe_unused]] const wchar_t* function, |
| 73 | + [[maybe_unused]] const wchar_t* file, |
| 74 | + [[maybe_unused]] unsigned int line, |
| 75 | + [[maybe_unused]] uintptr_t pReserved) |
| 76 | +{ |
| 77 | + |
| 78 | +} |
| 79 | +#endif // _MSC_VER |
| 80 | + |
| 81 | +LPTOP_LEVEL_EXCEPTION_FILTER g_prevUef = nullptr; |
| 82 | + |
| 83 | +LONG WINAPI WindowsUnhandledExceptionFilter(EXCEPTION_POINTERS* ep) |
| 84 | +{ |
| 85 | + const DWORD code = (ep && ep->ExceptionRecord) ? ep->ExceptionRecord->ExceptionCode : 0; |
| 86 | + wchar_t buf[128] = {}; |
| 87 | +#if defined(_MSC_VER) |
| 88 | + (void) _snwprintf_s(buf, _TRUNCATE, L"QGC: unhandled SEH 0x%08lX\n", static_cast<unsigned long>(code)); |
| 89 | +#else |
| 90 | + (void) swprintf(buf, static_cast<int>(std::size(buf)), L"QGC: unhandled SEH 0x%08lX\n", static_cast<unsigned long>(code)); |
| 91 | +#endif |
| 92 | + (void) OutputDebugStringW(buf); |
| 93 | + |
| 94 | + const HANDLE h = GetStdHandle(STD_ERROR_HANDLE); |
| 95 | + if (h && (h != INVALID_HANDLE_VALUE)) { |
| 96 | + DWORD ignored = 0; |
| 97 | + const char narrow[] = "QGC: unhandled SEH\n"; |
| 98 | + (void) WriteFile(h, narrow, (DWORD)sizeof(narrow) - 1, &ignored, nullptr); |
20 | 99 | }
|
| 100 | + |
| 101 | + return EXCEPTION_EXECUTE_HANDLER; |
21 | 102 | }
|
| 103 | + |
| 104 | +void setWindowsErrorModes(bool quietWindowsAsserts) |
| 105 | +{ |
| 106 | + (void) SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); |
| 107 | + g_prevUef = SetUnhandledExceptionFilter(WindowsUnhandledExceptionFilter); |
| 108 | + |
| 109 | +#if defined(_MSC_VER) |
| 110 | + (void) _set_invalid_parameter_handler(WindowsInvalidParameterHandler); |
| 111 | + (void) _set_purecall_handler(WindowsPurecallHandler); |
| 112 | + |
| 113 | + if (quietWindowsAsserts) { |
| 114 | + (void) _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
| 115 | + (void) _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); |
| 116 | + (void) _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG); |
| 117 | + (void) _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, WindowsCrtReportHook); |
| 118 | + (void) _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); |
| 119 | + (void) _set_error_mode(_OUT_TO_STDERR); |
| 120 | + } |
| 121 | +#else |
| 122 | + Q_UNUSED(quietWindowsAsserts); |
22 | 123 | #endif
|
| 124 | +} |
| 125 | +#endif // Q_OS_WIN |
| 126 | + |
| 127 | +} // namespace |
| 128 | + |
| 129 | +void Platform::setupPreApp(bool quietWindowsAsserts) |
| 130 | +{ |
| 131 | +#ifdef Q_OS_UNIX |
| 132 | + if (!qEnvironmentVariableIsSet("QT_ASSUME_STDERR_HAS_CONSOLE")) { |
| 133 | + (void) qputenv("QT_ASSUME_STDERR_HAS_CONSOLE", "1"); |
| 134 | + } |
| 135 | + if (!qEnvironmentVariableIsSet("QT_FORCE_STDERR_LOGGING")) { |
| 136 | + (void) qputenv("QT_FORCE_STDERR_LOGGING", "1"); |
| 137 | + } |
| 138 | +#endif |
| 139 | + |
| 140 | +#ifdef Q_OS_WIN |
| 141 | + if (!qEnvironmentVariableIsSet("QT_WIN_DEBUG_CONSOLE")) { |
| 142 | + (void) qputenv("QT_WIN_DEBUG_CONSOLE", "attach"); |
| 143 | + } |
| 144 | + setWindowsErrorModes(quietWindowsAsserts); |
| 145 | +#endif |
| 146 | + |
| 147 | +#ifdef Q_OS_MACOS |
| 148 | + disableAppNapViaInfoDict(); |
| 149 | +#endif |
| 150 | +} |
| 151 | + |
| 152 | +void Platform::setupPostApp() |
| 153 | +{ |
| 154 | +#if !defined(Q_OS_IOS) && !defined(Q_OS_ANDROID) |
| 155 | + SignalHandler* signalHandler = new SignalHandler(QCoreApplication::instance()); |
| 156 | + (void) signalHandler->setupSignalHandlers(); |
| 157 | +#endif |
| 158 | +} |
0 commit comments