DISTRHO / Cardinal

Virtual modular synthesizer plugin
https://cardinal.kx.studio/
GNU General Public License v3.0
2.28k stars 155 forks source link

Wrong way to look up Documents dir on Linux #553

Closed jn64 closed 1 year ago

jn64 commented 1 year ago

It seems like Cardinal is looking for the env var XDG_DOCUMENTS_DIR:

https://github.com/DISTRHO/Cardinal/blob/a51cb460bc958b60c7b40c0d27943a136fe1f49b/src/CardinalCommon.cpp#L497-L500

XDG_DOCUMENTS_DIR is not a known env var in the XDG basedir spec (unlike say XDG_CONFIG_HOME)

I think this was mistakenly taken from user-dirs.dirs(5). XDG_DOCUMENTS_DIR is a variable only used inside the user-dirs.dirs file, not an env var. This file is meant to be sourced by shell scripts. I know of some programs (e.g. JUCE) parse this file to get the various user folders.

The result is that Cardinal is looking in ~/Documents, even though my documents folder is ~/docs:

# ~/.config/user-dirs.dirs
XDG_DESKTOP_DIR="$HOME"
XDG_DOCUMENTS_DIR="$HOME/docs"
XDG_DOWNLOAD_DIR="$HOME/downloads"
XDG_MUSIC_DIR="$HOME/music"
XDG_PICTURES_DIR="$HOME/pics"
XDG_PUBLICSHARE_DIR="$HOME/public"
XDG_TEMPLATES_DIR="$HOME/templates"
XDG_VIDEOS_DIR="$HOME/vids"
falkTX commented 1 year ago

well that makes it kinda useless.

for storing documents the "config" dir is wrong, we dont want hidden configuration files here. the fallback is basically the same as VCV Rack btw

falkTX commented 1 year ago

so if I understood this right we need to basically:

  1. check if XDG_CONFIG_HOME is set, fallback to $HOME/.config if not
  2. read $_CONFIG_DIR/user-dirs.dirs and try to parse it without errors
  3. use a XDG_DOCUMENTS_DIR inside it if available, otherwise fallback to $HOME/Documents (or maybe use localization to find fallback docs path, but that becomes too much)
jn64 commented 1 year ago

That sounds correct. I found more info here: https://freedesktop.org/wiki/Software/xdg-user-dirs/

For application code the hope is that the various desktops will integrate this and have a nice API to find these directories.

I don't know if there are such APIs or if they'll be useful to you. It seems like other projects implement it themselves, although on a closer look, JUCE assumes ~/.config, which I guess is wrong. wxWidgets seems to have a correct implementation.

(I think the whole thing is stupid, but as a user I can either use the stupid thing to get what I want, or not get what I want...)

falkTX commented 1 year ago

both of those implementations don't bother to check for # that would make something a comment, or duplicated env vars where the last one is what should end up being used, or prefixes added to the var like PLEASE_IGNORE_XDG_DOCUMENTS_DIR=/not/this/one.

both seem to use an awful way for parsing, no error checking done at all besides checking if the folder exists...

that at least makes it easier on our side, as the bar is quite low for a quick and dirty implementation

jn64 commented 1 year ago

Quick and dirty will do IMO. If the user has a malformed file, other things will break before they get to Cardinal.

EnigmaCurry commented 1 year ago

I think you can just exec xdg-user-dir DOCUMENTS to get the correct directory path

falkTX commented 1 year ago

yes, but exec within a plugin is frowned upon as it duplicates file descriptors and other things. anyway the parsing of the file is not that complex