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 return true;
126}
127
128
129void KIPLATFORM::APP::EnableDarkMode( bool aForce )
130{
131#if wxCHECK_VERSION( 3, 3, 0 )
132 wxTheApp->MSWEnableDarkMode( aForce ? wxApp::DarkMode_Always : wxApp::DarkMode_Auto, new KICAD_DARK_MODE_SETTINGS() );
133#endif
134}
135
136
137bool KIPLATFORM::APP::AttachConsole( bool aTryAlloc )
138{
139 if( ::AttachConsole( ATTACH_PARENT_PROCESS ) || ( aTryAlloc && ::AllocConsole() ) )
140 {
141 #if !defined( __MINGW32__ ) // These redirections create problems on mingw:
142 // Nothing is printed to the console
143
144 if( ::GetStdHandle( STD_INPUT_HANDLE ) != INVALID_HANDLE_VALUE )
145 {
146 freopen( "CONIN$", "r", stdin );
147 setvbuf( stdin, NULL, _IONBF, 0 );
148 }
149
150 if( ::GetStdHandle( STD_OUTPUT_HANDLE ) != INVALID_HANDLE_VALUE )
151 {
152 freopen( "CONOUT$", "w", stdout );
153 setvbuf( stdout, NULL, _IONBF, 0 );
154 }
155
156 if( ::GetStdHandle( STD_ERROR_HANDLE ) != INVALID_HANDLE_VALUE )
157 {
158 freopen( "CONOUT$", "w", stderr );
159 setvbuf( stderr, NULL, _IONBF, 0 );
160 }
161 #endif
162
163 std::ios::sync_with_stdio( true );
164
165 std::wcout.clear();
166 std::cout.clear();
167 std::wcerr.clear();
168 std::cerr.clear();
169 std::wcin.clear();
170 std::cin.clear();
171
172 return true;
173 }
174
175 return false;
176}
177
178
180{
181#if defined( PYTHON_VERSION_MAJOR ) && ( ( PYTHON_VERSION_MAJOR == 3 && PYTHON_VERSION_MINOR >= 8 ) \
182 || PYTHON_VERSION_MAJOR > 3 )
183 // Python 3.8 switched to Windows 8+ API, we do not support Windows 7 and will not
184 // attempt to hack around it. A normal user will never get here because the Python DLL
185 // is missing dependencies - and because it is not dynamically loaded, KiCad will not even
186 // start without patching Python or its WinAPI dependency. This is just to create a nag dialog
187 // for those who run patched Python and prevent them from submitting bug reports.
188 return !IsWindows8OrGreater();
189#else
190 return false;
191#endif
192}
193
194
195bool KIPLATFORM::APP::RegisterApplicationRestart( const wxString& aCommandLine )
196{
197 // Command line arguments with spaces require quotes.
198 wxString restartCmd = wxS( "\"" ) + aCommandLine + wxS( "\"" );
199
200 // Ensure we don't exceed the maximum allowable size
201 if( restartCmd.length() > RESTART_MAX_CMD_LINE - 1 )
202 {
203 return false;
204 }
205
206 HRESULT hr = S_OK;
207
208 hr = ::RegisterApplicationRestart( restartCmd.wc_str(), RESTART_NO_PATCH );
209
210 return SUCCEEDED( hr );
211}
212
213
215{
216 // Note, this isn't required to be used on Windows if you are just closing the program
217 return SUCCEEDED( ::UnregisterApplicationRestart() );
218}
219
220
222{
223 return true;
224}
225
226
227void KIPLATFORM::APP::RemoveShutdownBlockReason( wxWindow* aWindow )
228{
229 // Destroys any block reason that may have existed
230 ShutdownBlockReasonDestroy( aWindow->GetHandle() );
231}
232
233
234void KIPLATFORM::APP::SetShutdownBlockReason( wxWindow* aWindow, const wxString& aReason )
235{
236 // Sets up the pretty message on the shutdown page on why it's being "blocked"
237 // This is used in conjunction with handling WM_QUERYENDSESSION (wxCloseEvent)
238 // ShutdownBlockReasonCreate does not block by itself
239
240 ShutdownBlockReasonDestroy( aWindow->GetHandle() ); // Destroys any existing or nonexisting reason
241
242 ShutdownBlockReasonCreate( aWindow->GetHandle(), aReason.wc_str() );
243}
244
245
247{
248 // Taken from https://devblogs.microsoft.com/oldnewthing/20191108-00/?p=103080
249 MSG msg;
250 PeekMessage( &msg, nullptr, WM_TIMER, WM_TIMER, PM_NOREMOVE );
251}
252
253
254void KIPLATFORM::APP::AddDynamicLibrarySearchPath( const wxString& aPath )
255{
256 SetDllDirectory( aPath.c_str() );
257}
int index
void SetShutdownBlockReason(wxWindow *aWindow, const wxString &aReason)
Sets the block reason why the window/application is preventing OS shutdown.
Definition unix/app.cpp:102
bool UnregisterApplicationRestart()
Unregisters the application from automatic restart.
Definition unix/app.cpp:84
bool AttachConsole(bool aTryAlloc)
Tries to attach a console window with stdout, stderr and stdin.
Definition unix/app.cpp:63
void RemoveShutdownBlockReason(wxWindow *aWindow)
Removes any shutdown block reason set.
Definition unix/app.cpp:97
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:77
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:107
bool IsOperatingSystemUnsupported()
Checks if the Operating System is explicitly unsupported and we want to prevent users from sending bu...
Definition unix/app.cpp:70
bool SupportsShutdownBlockReason()
Whether or not the window supports setting a shutdown block reason.
Definition unix/app.cpp:91
void EnableDarkMode(bool aForce)
Definition unix/app.cpp:58
void AddDynamicLibrarySearchPath(const wxString &aPath)
Inserts a search path for loading dynamic libraries.
Definition unix/app.cpp:112