KiCad PCB EDA Suite
Loading...
Searching...
No Matches
windows/app.cpp
Go to the documentation of this file.
1/*
2* This program source code file is part of KiCad, a free EDA CAD application.
3*
4* Copyright (C) 2020 Mark Roszko <[email protected]>
5* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6*
7* This program is free software: you can redistribute it and/or modify it
8* under the terms of the GNU General Public License as published by the
9* Free Software Foundation, either version 3 of the License, or (at your
10* option) any later version.
11*
12* This program is distributed in the hope that it will be useful, but
13* WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15* General Public License for more details.
16*
17* You should have received a copy of the GNU General Public License along
18* with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <kiplatform/app.h>
22
23#include <wx/app.h>
24#include <wx/log.h>
25#include <wx/string.h>
26#include <wx/window.h>
27#if wxCHECK_VERSION( 3, 3, 0 )
28#include <wx/msw/darkmode.h>
29#endif
30
31#include <windows.h>
32#include <strsafe.h>
33#include <config.h>
34#include <versionhelpers.h>
35#include <iostream>
36#include <cstdio>
37
38#if defined( _MSC_VER )
39#include <werapi.h> // issues on msys2
40#endif
41
42#ifdef _WIN32
43extern "C"
44{
45 // So there exists this malware called Nahimic by A-Volute, which is marketed as an audio enhancement
46 // software. In reality it's an aggressive form of malware that injects itself wildly into every process
47 // on the system for god knows what reason. It even includes a tracking/analytics package, <insert tinfoil hat>
48 // Our problem is this garbage basically bugs out OpenGL (why an audio driver does that, who knows, its made by morons)
49 // And then we get issues reported both in our issue tracker and sentry reports as a result
50 // At least these malware authors were nice to include a dumb "disable" trick where it checks if the exe is exporting
51 // a symbol called NoHotPatch, so here we are.
52 // Hopefully this works and stops the bug reports. Apparently the worst part is this malware aggressively gets reinstalled
53 // by awful low-tier motherboard vendors like MSI, Alienware and others who bundled it into their driver packages
54 // and distributed it over Windows Update
55 // Did I mention they clearly had issues with other apps so instead of fixing their malware, they blacklisted a hundred common
56 // apps and even some games in their own config? Obviously kicad isn't on that blacklist :(
57 // This malware seems to no longer be distributed as Nahimic and replaced with "Sonar" by SteelSeries.
58 // Time will tell if it's the same garbage, I'm not volunteering to install it.
59 __declspec(dllexport) void NoHotPatch()
60 {
61 // this is a intentionally empty function
62 return;
63 }
64}
65#endif
66
67#if wxCHECK_VERSION( 3, 3, 0 )
68class KICAD_DARK_MODE_SETTINGS : public wxDarkModeSettings
69{
70public:
71 wxColour GetColour( wxSystemColour index ) override
72 {
73 switch( index )
74 {
75 // This fixes "Control Light"
76 case wxSYS_COLOUR_3DLIGHT:
77 return wxColour( 0x2B2B2B );
78
79 default: return wxDarkModeSettings::GetColour( index );
80 }
81 }
82};
83#endif
84
85
87{
88#if defined( _MSC_VER ) && defined( DEBUG )
89 // wxWidgets turns on leak dumping in debug but its "flawed" and will falsely dump
90 // for half a hour _CRTDBG_ALLOC_MEM_DF is the usual default for MSVC.
91 _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF );
92#endif
93
94#if defined( DEBUG )
95 // undo wxwidgets trying to hide errors
96 SetErrorMode( 0 );
97#else
98 SetErrorMode( SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX );
99#endif
100
101 // remove CWD from the dll search paths
102 // just the smallest of security tweaks as we do load DLLs on demand
103 SetDllDirectory( wxT( "" ) );
104
105 // Moves the CWD to the end of the search list for spawning processes
106 SetSearchPathMode( BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE | BASE_SEARCH_PATH_PERMANENT );
107
108 // In order to support GUI and CLI
109 // Let's attach to console when it's possible, or allocate if requested.
110 AttachConsole( wxGetEnv( wxS( "KICAD_ALLOC_CONSOLE" ), nullptr ) );
111
112 // It may be useful to log up to traces in a console, but in Release builds the log level changes to Info
113 // Also we have to force the active target to stderr or else it goes to the void
114 bool forceLog = wxGetEnv( wxS( "KICAD_FORCE_CONSOLE_TRACE" ), nullptr );
115
116 if( forceLog )
117 {
118 wxLog::EnableLogging( true );
119#ifndef DEBUG
120 wxLog::SetLogLevel( wxLOG_Trace );
121#endif
122 wxLog::SetActiveTarget( new wxLogStderr );
123 }
124
125#if wxCHECK_VERSION( 3, 3, 0 )
126 wxTheApp->MSWEnableDarkMode( 0, new KICAD_DARK_MODE_SETTINGS() );
127#endif
128
129 return true;
130}
131
132
133bool KIPLATFORM::APP::AttachConsole( bool aTryAlloc )
134{
135 if( ::AttachConsole( ATTACH_PARENT_PROCESS ) || ( aTryAlloc && ::AllocConsole() ) )
136 {
137 #if !defined( __MINGW32__ ) // These redirections create problems on mingw:
138 // Nothing is printed to the console
139
140 if( ::GetStdHandle( STD_INPUT_HANDLE ) != INVALID_HANDLE_VALUE )
141 {
142 freopen( "CONIN$", "r", stdin );
143 setvbuf( stdin, NULL, _IONBF, 0 );
144 }
145
146 if( ::GetStdHandle( STD_OUTPUT_HANDLE ) != INVALID_HANDLE_VALUE )
147 {
148 freopen( "CONOUT$", "w", stdout );
149 setvbuf( stdout, NULL, _IONBF, 0 );
150 }
151
152 if( ::GetStdHandle( STD_ERROR_HANDLE ) != INVALID_HANDLE_VALUE )
153 {
154 freopen( "CONOUT$", "w", stderr );
155 setvbuf( stderr, NULL, _IONBF, 0 );
156 }
157 #endif
158
159 std::ios::sync_with_stdio( true );
160
161 std::wcout.clear();
162 std::cout.clear();
163 std::wcerr.clear();
164 std::cerr.clear();
165 std::wcin.clear();
166 std::cin.clear();
167
168 return true;
169 }
170
171 return false;
172}
173
174
176{
177#if defined( PYTHON_VERSION_MAJOR ) && ( ( PYTHON_VERSION_MAJOR == 3 && PYTHON_VERSION_MINOR >= 8 ) \
178 || PYTHON_VERSION_MAJOR > 3 )
179 // Python 3.8 switched to Windows 8+ API, we do not support Windows 7 and will not
180 // attempt to hack around it. A normal user will never get here because the Python DLL
181 // is missing dependencies - and because it is not dynamically loaded, KiCad will not even
182 // start without patching Python or its WinAPI dependency. This is just to create a nag dialog
183 // for those who run patched Python and prevent them from submitting bug reports.
184 return !IsWindows8OrGreater();
185#else
186 return false;
187#endif
188}
189
190
191bool KIPLATFORM::APP::RegisterApplicationRestart( const wxString& aCommandLine )
192{
193 // Command line arguments with spaces require quotes.
194 wxString restartCmd = wxS( "\"" ) + aCommandLine + wxS( "\"" );
195
196 // Ensure we don't exceed the maximum allowable size
197 if( restartCmd.length() > RESTART_MAX_CMD_LINE - 1 )
198 {
199 return false;
200 }
201
202 HRESULT hr = S_OK;
203
204 hr = ::RegisterApplicationRestart( restartCmd.wc_str(), RESTART_NO_PATCH );
205
206 return SUCCEEDED( hr );
207}
208
209
211{
212 // Note, this isn't required to be used on Windows if you are just closing the program
213 return SUCCEEDED( ::UnregisterApplicationRestart() );
214}
215
216
218{
219 return true;
220}
221
222
223void KIPLATFORM::APP::RemoveShutdownBlockReason( wxWindow* aWindow )
224{
225 // Destroys any block reason that may have existed
226 ShutdownBlockReasonDestroy( aWindow->GetHandle() );
227}
228
229
230void KIPLATFORM::APP::SetShutdownBlockReason( wxWindow* aWindow, const wxString& aReason )
231{
232 // Sets up the pretty message on the shutdown page on why it's being "blocked"
233 // This is used in conjunction with handling WM_QUERYENDSESSION (wxCloseEvent)
234 // ShutdownBlockReasonCreate does not block by itself
235
236 ShutdownBlockReasonDestroy( aWindow->GetHandle() ); // Destroys any existing or nonexisting reason
237
238 ShutdownBlockReasonCreate( aWindow->GetHandle(), aReason.wc_str() );
239}
240
241
243{
244 // Taken from https://devblogs.microsoft.com/oldnewthing/20191108-00/?p=103080
245 MSG msg;
246 PeekMessage( &msg, nullptr, WM_TIMER, WM_TIMER, PM_NOREMOVE );
247}
248
249
250void KIPLATFORM::APP::AddDynamicLibrarySearchPath( const wxString& aPath )
251{
252 SetDllDirectory( aPath.c_str() );
253}
void SetShutdownBlockReason(wxWindow *aWindow, const wxString &aReason)
Sets the block reason why the window/application is preventing OS shutdown.
Definition: unix/app.cpp:90
bool UnregisterApplicationRestart()
Unregisters the application from automatic restart.
Definition: unix/app.cpp:72
bool AttachConsole(bool aTryAlloc)
Tries to attach a console window with stdout, stderr and stdin.
Definition: unix/app.cpp:51
void RemoveShutdownBlockReason(wxWindow *aWindow)
Removes any shutdown block reason set.
Definition: unix/app.cpp:85
bool RegisterApplicationRestart(const wxString &aCommandLine)
Registers the application for restart with the OS with the given command line string to pass as args.
Definition: unix/app.cpp:65
bool Init()
Perform application-specific initialization tasks.
Definition: unix/app.cpp:40
void ForceTimerMessagesToBeCreatedIfNecessary()
Forces wxTimers to fire more promptly on Win32.
Definition: unix/app.cpp:95
bool IsOperatingSystemUnsupported()
Checks if the Operating System is explicitly unsupported and we want to prevent users from sending bu...
Definition: unix/app.cpp:58
bool SupportsShutdownBlockReason()
Whether or not the window supports setting a shutdown block reason.
Definition: unix/app.cpp:79
void AddDynamicLibrarySearchPath(const wxString &aPath)
Inserts a search path for loading dynamic libraries.
Definition: unix/app.cpp:100