Merge branch 'kitten-desktop-ui-portal-xdg-compat' of https://github.com/azertyfun/kitty

This commit is contained in:
Kovid Goyal 2025-08-04 20:24:46 +05:30
commit b84849a0ab
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C

View file

@ -313,12 +313,24 @@ func show_settings(opts *ShowSettingsOptions) (err error) {
}
var DataDirs = sync.OnceValue(func() (ans []string) {
d := os.Getenv("XDG_DATA_DIRS")
if d == "" {
d = "/usr/local/share/:/usr/share/"
// $XDG_DATA_DIRS defines the preference-ordered set of base directories
// to search for data files **in addition to the $XDG_DATA_HOME** base
// directory. The directories in $XDG_DATA_DIRS should be separated with
// a colon ':'.
// https://specifications.freedesktop.org/basedir-spec/0.8/#variables
data_dirs := os.Getenv("XDG_DATA_DIRS")
if data_dirs == "" {
data_dirs = "/usr/local/share/:/usr/share/"
}
all := []string{os.Getenv("XDG_DATA_HOME")}
all = append(all, strings.Split(d, ":")...)
data_home := os.Getenv("XDG_DATA_HOME")
if data_home == "" {
data_home = os.Getenv("HOME") + "/.local/share"
}
all := []string{data_home}
all = append(all, strings.Split(data_dirs, ":")...)
seen := map[string]bool{}
for _, x := range all {
if !seen[x] {
@ -351,6 +363,7 @@ var AllPortalInterfaces = sync.OnceValue(func() (ans []string) {
func patch_portals_conf(text []byte) []byte {
lines := []string{}
in_preferred := false
patched := false
for _, line := range utils.Splitlines(utils.UnsafeBytesToString(text)) {
sl := strings.TrimSpace(line)
if strings.HasPrefix(sl, "[") {
@ -359,6 +372,7 @@ func patch_portals_conf(text []byte) []byte {
for _, iface := range AllPortalInterfaces() {
lines = append(lines, iface+"=kitty")
}
patched = true
} else if in_preferred {
remove := false
for _, iface := range AllPortalInterfaces() {
@ -372,6 +386,15 @@ func patch_portals_conf(text []byte) []byte {
}
}
}
if !patched {
// the file was empty or did not contain a section
lines = append(lines, "[preferred]")
for _, iface := range AllPortalInterfaces() {
lines = append(lines, iface+"=kitty")
}
}
return utils.UnsafeStringToBytes(strings.Join(lines, "\n"))
}
@ -381,21 +404,13 @@ func enable_portal() (err error) {
}
portals_dir := ""
for _, x := range WritableDataDirs() {
// Find-or-create the first available xdg-desktop-portals/portals directory
q := filepath.Join(x, "xdg-desktop-portal", "portals")
if unix.Access(q, unix.W_OK) == nil && IsDir(q) {
if (unix.Access(q, unix.W_OK) == nil && IsDir(q)) || (os.MkdirAll(q, 0o755) == nil) {
portals_dir = q
break
}
}
if portals_dir == "" {
for _, x := range WritableDataDirs() {
q := filepath.Join(x, "xdg-desktop-portal", "portals")
if err := os.MkdirAll(q, 0o755); err == nil {
portals_dir = q
break
}
}
}
if portals_dir == "" {
return fmt.Errorf("Could not find any writable portals directories. Make sure XDG_DATA_HOME is set and point to a directory for which you have write permission.")
}
@ -454,6 +469,7 @@ Exec=%s desktop-ui run-server
text := patch_portals_conf(text)
if err = os.WriteFile(q, text, 0o644); err == nil {
patched_file = q
fmt.Printf("Patched %s to use the kitty portals\n", patched_file)
break
}
}
@ -466,8 +482,8 @@ Exec=%s desktop-ui run-server
return err
}
patched_file = q
fmt.Printf("Created %s to use the kitty portals\n", patched_file)
}
fmt.Printf("Patched %s to use the kitty portals\n", patched_file)
return
}