From: Daniel Elstner Date: Sat, 3 Oct 2015 11:18:09 +0000 (+0200) Subject: init: Also look for decoders in XDG data directories X-Git-Tag: libsigrokdecode-0.4.0~57 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=40c6ac1d3fbded276dcbff23e8bc099896ab2fb5;p=libsigrokdecode.git init: Also look for decoders in XDG data directories --- diff --git a/srd.c b/srd.c index b152e0c..7b18c9a 100644 --- a/srd.c +++ b/srd.c @@ -95,6 +95,23 @@ extern SRD_PRIV int max_session_id; * @{ */ +static int searchpath_add_xdg_dir(const char *datadir) +{ + char *decdir; + int ret; + + decdir = g_build_filename(datadir, PACKAGE_TARNAME, "decoders", NULL); + + if (g_file_test(decdir, G_FILE_TEST_IS_DIR)) + ret = srd_decoder_searchpath_add(decdir); + else + ret = SRD_OK; /* just ignore non-existing directory */ + + g_free(decdir); + + return ret; +} + /** * Initialize libsigrokdecode. * @@ -124,8 +141,10 @@ extern SRD_PRIV int max_session_id; */ SRD_API int srd_init(const char *path) { + const char *const *sys_datadirs; + const char *env_path; + size_t i; int ret; - char *env_path; if (max_session_id != -1) { srd_err("libsigrokdecode is already initialized."); @@ -140,11 +159,28 @@ SRD_API int srd_init(const char *path) /* Initialize the Python interpreter. */ Py_Initialize(); - /* Installed decoders. */ + /* Locations relative to the XDG system data directories. */ + sys_datadirs = g_get_system_data_dirs(); + for (i = g_strv_length((char **)sys_datadirs); i > 0; i--) { + ret = searchpath_add_xdg_dir(sys_datadirs[i-1]); + if (ret != SRD_OK) { + Py_Finalize(); + return ret; + } + } +#ifdef DECODERS_DIR + /* Hardcoded decoders install location, if defined. */ if ((ret = srd_decoder_searchpath_add(DECODERS_DIR)) != SRD_OK) { Py_Finalize(); return ret; } +#endif + /* Location relative to the XDG user data directory. */ + ret = searchpath_add_xdg_dir(g_get_user_data_dir()); + if (ret != SRD_OK) { + Py_Finalize(); + return ret; + } /* Path specified by the user. */ if (path) { @@ -155,7 +191,7 @@ SRD_API int srd_init(const char *path) } /* Environment variable overrides everything, for debugging. */ - if ((env_path = getenv("SIGROKDECODE_DIR"))) { + if ((env_path = g_getenv("SIGROKDECODE_DIR"))) { if ((ret = srd_decoder_searchpath_add(env_path)) != SRD_OK) { Py_Finalize(); return ret;