| 1 | package main |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | const ozonePlatformEnv = "REASONIX_OZONE_PLATFORM" |
| 6 | |
| 7 | // shellOzoneArgs prefixes --ozone-platform=x11 under a Wayland session that |
| 8 | // also offers XWayland: Electron picks Wayland by itself there, and on drivers |
| 9 | // whose EGL fails on that backend the shell dies before its first window |
| 10 | // (#10369). The flag must be in the shell's argv, because Chromium selects the |
| 11 | // Ozone backend before the main script runs. |
| 12 | func shellOzoneArgs(goos string, args []string, getenv func(string) string) []string { |
| 13 | if goos != "linux" || hasOzoneSwitch(args) { |
| 14 | return args |
| 15 | } |
| 16 | switch strings.ToLower(strings.TrimSpace(getenv(ozonePlatformEnv))) { |
| 17 | case "wayland": |
| 18 | return args |
| 19 | case "x11": |
| 20 | return append([]string{"--ozone-platform=x11"}, args...) |
| 21 | } |
| 22 | if !waylandSession(getenv) || getenv("DISPLAY") == "" { |
| 23 | return args |
| 24 | } |
| 25 | return append([]string{"--ozone-platform=x11"}, args...) |
| 26 | } |
| 27 | |
| 28 | func waylandSession(getenv func(string) string) bool { |
| 29 | switch strings.ToLower(strings.TrimSpace(getenv("XDG_SESSION_TYPE"))) { |
| 30 | case "wayland": |
| 31 | return true |
| 32 | case "x11": |
| 33 | return false |
| 34 | } |
| 35 | return getenv("WAYLAND_DISPLAY") != "" |
| 36 | } |
| 37 | |
| 38 | func hasOzoneSwitch(args []string) bool { |
| 39 | for _, arg := range args { |
| 40 | if arg == "--" { |
| 41 | return false |
| 42 | } |
| 43 | name, _, _ := strings.Cut(arg, "=") |
| 44 | if name == "--ozone-platform" || name == "--ozone-platform-hint" { |
| 45 | return true |
| 46 | } |
| 47 | } |
| 48 | return false |
| 49 | } |
| 50 | |
| 51 | func envLookup(env []string) func(string) string { |
| 52 | return func(key string) string { |
| 53 | value := "" |
| 54 | for _, entry := range env { |
| 55 | if k, v, ok := strings.Cut(entry, "="); ok && k == key { |
| 56 | value = v |
| 57 | } |
| 58 | } |
| 59 | return value |
| 60 | } |
| 61 | } |
| 62 |