From 04b4634f4a500252653a127bae0576d6f8bdaa11 Mon Sep 17 00:00:00 2001 From: Phil Richards Date: Wed, 23 Aug 2023 11:41:17 +0100 Subject: [PATCH 1/2] Init GTK in `NewFrontend`, not `init` So apps that have a headless / non-gui mode will be able to run, since they needn't call `NewFrontend` (which is called by `CreateApp`). Previously, `init` would call `C.gtk_init` regardless of whether CreateApp was called. Also change to call `C.gtk_init_check` with a panic, instead of `C.gtk_init`, since `gtk_init` just exits the process if it fails, without a sensible error message. Fixes #2628. --- .../frontend/desktop/linux/frontend.go | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/v2/internal/frontend/desktop/linux/frontend.go b/v2/internal/frontend/desktop/linux/frontend.go index 2f6f286fac7..654edae17c0 100644 --- a/v2/internal/frontend/desktop/linux/frontend.go +++ b/v2/internal/frontend/desktop/linux/frontend.go @@ -76,6 +76,7 @@ import "C" import ( "context" "encoding/json" + "errors" "fmt" "log" "net" @@ -83,6 +84,7 @@ import ( "os" "runtime" "strings" + "sync" "text/template" "unsafe" @@ -96,6 +98,8 @@ import ( "github.com/wailsapp/wails/v2/pkg/options" ) +var initOnce = sync.Once{} + const startURL = "wails://wails/" type Frontend struct { @@ -126,18 +130,19 @@ func (f *Frontend) WindowClose() { f.mainWindow.Destroy() } -func init() { - runtime.LockOSThread() - - // Set GDK_BACKEND=x11 if currently unset and XDG_SESSION_TYPE is unset, unspecified or x11 to prevent warnings - if os.Getenv("GDK_BACKEND") == "" && (os.Getenv("XDG_SESSION_TYPE") == "" || os.Getenv("XDG_SESSION_TYPE") == "unspecified" || os.Getenv("XDG_SESSION_TYPE") == "x11") { - _ = os.Setenv("GDK_BACKEND", "x11") - } +func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings, dispatcher frontend.Dispatcher) *Frontend { + initOnce.Do(func() { + runtime.LockOSThread() - C.gtk_init(nil, nil) -} + // Set GDK_BACKEND=x11 if currently unset and XDG_SESSION_TYPE is unset, unspecified or x11 to prevent warnings + if os.Getenv("GDK_BACKEND") == "" && (os.Getenv("XDG_SESSION_TYPE") == "" || os.Getenv("XDG_SESSION_TYPE") == "unspecified" || os.Getenv("XDG_SESSION_TYPE") == "x11") { + _ = os.Setenv("GDK_BACKEND", "x11") + } -func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings, dispatcher frontend.Dispatcher) *Frontend { + if ok := C.gtk_init_check(nil, nil); ok != 1 { + panic(errors.New("failed to init GTK")) + } + }) result := &Frontend{ frontendOptions: appoptions, From a5ac83efd5a978e6c0b4485dd6c1d52ad5753ef0 Mon Sep 17 00:00:00 2001 From: Phil Richards Date: Wed, 23 Aug 2023 13:07:35 +0100 Subject: [PATCH 2/2] Update changelog --- website/src/pages/changelog.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 8b215c91220..5a607de778e 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Avoid app crashing when the Linux GTK key is empty. Fixed by @aminya in [PR](https://github.com/wailsapp/wails/pull/2672) +- Fix issue where app would exit before main() on linux if $DISPLAY env var was not set. Fixed by @phildrip in [PR](https://github.com/wailsapp/wails/pull/2841) ### Added