From 92bc52c25c34d461f64e8274116e358b19f96ff2 Mon Sep 17 00:00:00 2001 From: Chris Waldon Date: Mon, 3 Jul 2023 11:11:38 -0400 Subject: [PATCH] app: [Android] ensure data dirs are set by window init This commit alters the android backend to automatically populate some environment variables as early as possible in application startup. Specifically, this commit sets the XDG_{CONFIG,CACHE}_HOME environment variables which are necessary for the text shaper to infer a valid cache file location. Signed-off-by: Chris Waldon --- app/os_android.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/app/os_android.go b/app/os_android.go index a5db2f0a..23e1f6ba 100644 --- a/app/os_android.go +++ b/app/os_android.go @@ -338,20 +338,6 @@ func (w *window) NewContext() (context, error) { func dataDir() (string, error) { dataDirOnce.Do(func() { dataPath = <-dataDirChan - // Set XDG_CACHE_HOME to make os.UserCacheDir work. - if _, exists := os.LookupEnv("XDG_CACHE_HOME"); !exists { - cachePath := filepath.Join(dataPath, "cache") - os.Setenv("XDG_CACHE_HOME", cachePath) - } - // Set XDG_CONFIG_HOME to make os.UserConfigDir work. - if _, exists := os.LookupEnv("XDG_CONFIG_HOME"); !exists { - cfgPath := filepath.Join(dataPath, "config") - os.Setenv("XDG_CONFIG_HOME", cfgPath) - } - // Set HOME to make os.UserHomeDir work. - if _, exists := os.LookupEnv("HOME"); !exists { - os.Setenv("HOME", dataPath) - } }) return dataPath, nil } @@ -389,6 +375,22 @@ func Java_org_gioui_Gio_runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyt } n := C.jni_GetArrayLength(env, jdataDir) dataDir := C.GoStringN((*C.char)(unsafe.Pointer(dirBytes)), n) + + // Set XDG_CACHE_HOME to make os.UserCacheDir work. + if _, exists := os.LookupEnv("XDG_CACHE_HOME"); !exists { + cachePath := filepath.Join(dataDir, "cache") + os.Setenv("XDG_CACHE_HOME", cachePath) + } + // Set XDG_CONFIG_HOME to make os.UserConfigDir work. + if _, exists := os.LookupEnv("XDG_CONFIG_HOME"); !exists { + cfgPath := filepath.Join(dataDir, "config") + os.Setenv("XDG_CONFIG_HOME", cfgPath) + } + // Set HOME to make os.UserHomeDir work. + if _, exists := os.LookupEnv("HOME"); !exists { + os.Setenv("HOME", dataDir) + } + dataDirChan <- dataDir C.jni_ReleaseByteArrayElements(env, jdataDir, dirBytes)