u/file: Search more paths for hand-tracking models

This commit is contained in:
Jakob Bornecrantz 2023-05-20 20:47:03 +01:00
parent b466d1d338
commit ab2b88d417

View file

@ -55,6 +55,17 @@ mkpath(const char *path)
return 0; return 0;
} }
static bool
is_dir(const char *path)
{
struct stat st = {0};
if (!stat(path, &st)) {
return S_ISDIR(st.st_mode);
} else {
return false;
}
}
ssize_t ssize_t
u_file_get_config_dir(char *out_path, size_t out_path_size) u_file_get_config_dir(char *out_path, size_t out_path_size)
{ {
@ -111,15 +122,40 @@ u_file_open_file_in_config_dir(const char *filename, const char *mode)
ssize_t ssize_t
u_file_get_hand_tracking_models_dir(char *out_path, size_t out_path_size) u_file_get_hand_tracking_models_dir(char *out_path, size_t out_path_size)
{ {
const char *suffix = "/monado/hand-tracking-models";
const char *xdg_data_home = getenv("XDG_DATA_HOME"); const char *xdg_data_home = getenv("XDG_DATA_HOME");
const char *home = getenv("HOME"); const char *home = getenv("HOME");
ssize_t ret = 0;
if (xdg_data_home != NULL) { if (xdg_data_home != NULL) {
return snprintf(out_path, out_path_size, "%s/monado/hand-tracking-models/", xdg_data_home); ret = snprintf(out_path, out_path_size, "%s%s", xdg_data_home, suffix);
} else if (home != NULL) { if (ret > 0 && is_dir(out_path)) {
return snprintf(out_path, out_path_size, "%s/.local/share/monado/hand-tracking-models/", home); return ret;
} else { }
return -1;
} }
if (home != NULL) {
ret = snprintf(out_path, out_path_size, "%s/.local/share%s", home, suffix);
if (ret > 0 && is_dir(out_path)) {
return ret;
}
}
ret = snprintf(out_path, out_path_size, "/usr/local/share%s", suffix);
if (ret > 0 && is_dir(out_path)) {
return ret;
}
ret = snprintf(out_path, out_path_size, "/usr/share%s", suffix);
if (ret > 0 && is_dir(out_path)) {
return ret;
}
if (out_path_size > 0) {
out_path[0] = '\0';
}
return -1;
} }
#endif /* XRT_OS_LINUX */ #endif /* XRT_OS_LINUX */