From 41f88dbabcfaca91c2f05573433f50d221883ed8 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Tue, 23 Mar 2021 19:09:57 +0000 Subject: [PATCH] Refactor platform-specific init into two phases The first phase is for the environment before the OS apps are created, and the second phase is for after the OS app is created but before all of our processing. Fixes https://gitlab.com/kicad/code/kicad/issues/7992 --- common/single_top.cpp | 12 +++++++-- kicad/kicad.cpp | 12 +++++++-- libs/kiplatform/gtk/app.cpp | 23 +--------------- libs/kiplatform/gtk/environment.cpp | 26 +++++++++++++++++++ libs/kiplatform/include/kiplatform/app.h | 5 ++-- .../include/kiplatform/environment.h | 6 +++++ libs/kiplatform/msw/app.cpp | 2 +- libs/kiplatform/msw/environment.cpp | 9 ++++++- libs/kiplatform/osx/app.mm | 2 +- libs/kiplatform/osx/environment.mm | 9 ++++++- 10 files changed, 74 insertions(+), 32 deletions(-) diff --git a/common/single_top.cpp b/common/single_top.cpp index 720dbb390e..54d64e1b82 100644 --- a/common/single_top.cpp +++ b/common/single_top.cpp @@ -49,6 +49,7 @@ #include #include +#include // Only a single KIWAY is supported in this single_top top level component, @@ -130,10 +131,17 @@ wxIMPLEMENT_DYNAMIC_CLASS(HtmlModule, wxModule); */ struct APP_SINGLE_TOP : public wxApp { + APP_SINGLE_TOP() : wxApp() + { + // Init the environment each platform wants + KIPLATFORM::ENV::Init(); + } + + bool OnInit() override { - // Init the platform-specific parts - if( !KIPLATFORM::APP::PlatformInit() ) + // Perform platform-specific init tasks + if( !KIPLATFORM::APP::Init() ) return false; // Force wxHtmlWinParser initialization when a wxHtmlWindow is used only diff --git a/kicad/kicad.cpp b/kicad/kicad.cpp index 597cf2e234..6090e9300d 100644 --- a/kicad/kicad.cpp +++ b/kicad/kicad.cpp @@ -50,6 +50,7 @@ #include "kicad_settings.h" #include +#include // a dummy to quiet linking with EDA_BASE_FRAME::config(); @@ -256,10 +257,17 @@ KIWAY Kiway( &Pgm(), KFCTL_CPP_PROJECT_SUITE ); */ struct APP_KICAD : public wxApp { + APP_KICAD() : wxApp() + { + // Init the environment each platform wants + KIPLATFORM::ENV::Init(); + } + + bool OnInit() override { - // Init the platform-specific parts - if( !KIPLATFORM::APP::PlatformInit() ) + // Perform platform-specific init tasks + if( !KIPLATFORM::APP::Init() ) return false; if( !program.OnPgmInit() ) diff --git a/libs/kiplatform/gtk/app.cpp b/libs/kiplatform/gtk/app.cpp index 870a8c5cf2..da4f6e138b 100644 --- a/libs/kiplatform/gtk/app.cpp +++ b/libs/kiplatform/gtk/app.cpp @@ -37,29 +37,8 @@ static GLogWriterOutput nullLogWriter( GLogLevelFlags log_level, const GLogField } -bool KIPLATFORM::APP::PlatformInit() +bool KIPLATFORM::APP::Init() { - // Disable proxy menu in Unity window manager. Only usual menubar works with - // wxWidgets (at least <= 3.1). When the proxy menu menubar is enable, some - // important things for us do not work: menuitems UI events and shortcuts. - wxString wm; - - if( wxGetEnv( wxT( "XDG_CURRENT_DESKTOP" ), &wm ) && wm.CmpNoCase( wxT( "Unity" ) ) == 0 ) - wxSetEnv ( wxT("UBUNTU_MENUPROXY" ), wxT( "0" ) ); - - // Force the use of X11 backend (or wayland-x11 compatibilty layer). This is - // required until wxWidgets supports the Wayland compositors - wxSetEnv( wxT( "GDK_BACKEND" ), wxT( "x11" ) ); - - // Disable overlay scrollbars as they mess up wxWidgets window sizing and cause - // excessive redraw requests. - wxSetEnv( wxT( "GTK_OVERLAY_SCROLLING" ), wxT( "0" ) ); - - // Set GTK2-style input instead of xinput2. This disables touchscreen and smooth - // scrolling. It's needed to ensure that we are not getting multiple mouse scroll - // events. - wxSetEnv( wxT( "GDK_CORE_DEVICE_EVENTS" ), wxT( "1" ) ); - #if !defined( KICAD_SHOW_GTK_MESSAGES ) // Attach a logger that will consume the annoying GTK error messages g_log_set_writer_func( nullLogWriter, NULL, NULL ); diff --git a/libs/kiplatform/gtk/environment.cpp b/libs/kiplatform/gtk/environment.cpp index fb81c69f91..ab2a99d69b 100644 --- a/libs/kiplatform/gtk/environment.cpp +++ b/libs/kiplatform/gtk/environment.cpp @@ -22,6 +22,32 @@ #include #include #include +#include + + +void KIPLATFORM::ENV::Init() +{ + // Disable proxy menu in Unity window manager. Only usual menubar works with + // wxWidgets (at least <= 3.1). When the proxy menu menubar is enable, some + // important things for us do not work: menuitems UI events and shortcuts. + wxString wm; + + if( wxGetEnv( wxT( "XDG_CURRENT_DESKTOP" ), &wm ) && wm.CmpNoCase( wxT( "Unity" ) ) == 0 ) + wxSetEnv ( wxT("UBUNTU_MENUPROXY" ), wxT( "0" ) ); + + // Force the use of X11 backend (or wayland-x11 compatibilty layer). This is + // required until wxWidgets supports the Wayland compositors + wxSetEnv( wxT( "GDK_BACKEND" ), wxT( "x11" ) ); + + // Disable overlay scrollbars as they mess up wxWidgets window sizing and cause + // excessive redraw requests. + wxSetEnv( wxT( "GTK_OVERLAY_SCROLLING" ), wxT( "0" ) ); + + // Set GTK2-style input instead of xinput2. This disables touchscreen and smooth + // scrolling. It's needed to ensure that we are not getting multiple mouse scroll + // events. + wxSetEnv( wxT( "GDK_CORE_DEVICE_EVENTS" ), wxT( "1" ) ); +} bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError ) diff --git a/libs/kiplatform/include/kiplatform/app.h b/libs/kiplatform/include/kiplatform/app.h index 6898fa69d8..2edff8f69d 100644 --- a/libs/kiplatform/include/kiplatform/app.h +++ b/libs/kiplatform/include/kiplatform/app.h @@ -29,11 +29,12 @@ namespace KIPLATFORM namespace APP { /** - * Perform platform-specific initialization tasks. + * Perform application-specific initialization tasks. These tasks should be called + * after the wxApp is constructed (e.g. inside the OnInit method). * * @return true if init successful, false if unsuccessful */ - bool PlatformInit(); + bool Init(); /** * Registers the application for restart with the OS with the given command line string to pass as args diff --git a/libs/kiplatform/include/kiplatform/environment.h b/libs/kiplatform/include/kiplatform/environment.h index f8e2e8474e..c50a02ebb8 100644 --- a/libs/kiplatform/include/kiplatform/environment.h +++ b/libs/kiplatform/include/kiplatform/environment.h @@ -24,6 +24,12 @@ namespace KIPLATFORM { namespace ENV { + /** + * Perform environment initialization tasks. These tasks are called during the wxApp + * constructor and therefore won't have access to the underlying OS application. + */ + void Init(); + /** * Move the specified file/directory to the trash bin/recycle bin. * diff --git a/libs/kiplatform/msw/app.cpp b/libs/kiplatform/msw/app.cpp index d10b566d22..2623e81907 100644 --- a/libs/kiplatform/msw/app.cpp +++ b/libs/kiplatform/msw/app.cpp @@ -31,7 +31,7 @@ #include -bool KIPLATFORM::APP::PlatformInit() +bool KIPLATFORM::APP::Init() { #if defined( _MSC_VER ) && defined( DEBUG ) // wxWidgets turns on leak dumping in debug but its "flawed" and will falsely dump diff --git a/libs/kiplatform/msw/environment.cpp b/libs/kiplatform/msw/environment.cpp index 4e35edf135..f023c0650b 100644 --- a/libs/kiplatform/msw/environment.cpp +++ b/libs/kiplatform/msw/environment.cpp @@ -28,6 +28,13 @@ #include #include + +void KIPLATFORM::ENV::Init() +{ + // No tasks for this platform +} + + bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError ) { // The filename field must be a double-null terminated string @@ -80,4 +87,4 @@ wxString KIPLATFORM::ENV::GetUserCachePath() wxStandardPaths::Get().UseAppInfo( wxStandardPaths::AppInfo_None ); return wxStandardPaths::Get().GetUserLocalDataDir(); -} \ No newline at end of file +} diff --git a/libs/kiplatform/osx/app.mm b/libs/kiplatform/osx/app.mm index 27b78b9cc6..c8e2ee73f8 100644 --- a/libs/kiplatform/osx/app.mm +++ b/libs/kiplatform/osx/app.mm @@ -23,7 +23,7 @@ #include -bool KIPLATFORM::APP::PlatformInit() +bool KIPLATFORM::APP::Init() { // No special OSX init tasks return true; diff --git a/libs/kiplatform/osx/environment.mm b/libs/kiplatform/osx/environment.mm index 3d3652f60f..167768b7f4 100644 --- a/libs/kiplatform/osx/environment.mm +++ b/libs/kiplatform/osx/environment.mm @@ -25,6 +25,13 @@ #include #include + +void KIPLATFORM::ENV::Init() +{ + // No tasks for this platform +} + + bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError ) { bool isDirectory = wxDirExists( aPath ); @@ -73,4 +80,4 @@ wxString KIPLATFORM::ENV::GetUserCachePath() create:NO error:nil]; return wxCFStringRef::AsString((CFStringRef)url.path); -} \ No newline at end of file +}