22 #include "libsigrokdecode-internal.h" 98 static int searchpath_add_xdg_dir(
const char *datadir)
103 decdir = g_build_filename(datadir, PACKAGE_TARNAME,
"decoders", NULL);
105 if (g_file_test(decdir, G_FILE_TEST_IS_DIR))
106 ret = srd_decoder_searchpath_add(decdir);
115 static void print_versions(
void)
118 GSList *l, *l_orig, *m;
120 const char *lib, *version;
122 srd_dbg(
"libsigrokdecode %s/%s (rt: %s/%s).",
123 SRD_PACKAGE_VERSION_STRING, SRD_LIB_VERSION_STRING,
126 s = g_string_sized_new(200);
127 g_string_append(s,
"Libs: ");
129 for (l = l_orig; l; l = l->next) {
132 version = m->next->data;
133 g_string_append_printf(s,
"%s %s, ", lib, version);
134 g_slist_free_full(m, g_free);
136 g_slist_free(l_orig);
137 s->str[s->len - 2] =
'.';
138 s->str[s->len - 1] =
'\0';
139 srd_dbg(
"%s", s->str);
140 g_string_free(s,
TRUE);
143 srd_dbg(
"Host: %s.", str);
147 static int print_searchpaths(
void)
149 PyObject *py_paths, *py_path, *py_bytes;
150 PyGILState_STATE gstate;
155 s = g_string_sized_new(500);
156 g_string_append(s,
"Protocol decoder search paths:\n");
157 for (l = searchpaths; l; l = l->next)
158 g_string_append_printf(s,
" - %s\n", (
const char *)l->data);
159 s->str[s->len - 1] =
'\0';
160 srd_dbg(
"%s", s->str);
161 g_string_free(s,
TRUE);
163 gstate = PyGILState_Ensure();
165 py_paths = PySys_GetObject(
"path");
169 s = g_string_sized_new(500);
170 g_string_append(s,
"Python system search paths:\n");
171 for (i = 0; i < PyList_Size(py_paths); i++) {
172 py_path = PyList_GetItem(py_paths, i);
173 py_bytes = PyUnicode_AsUTF8String(py_path);
174 g_string_append_printf(s,
" - %s\n", PyBytes_AsString(py_bytes));
177 s->str[s->len - 1] =
'\0';
178 srd_dbg(
"%s", s->str);
179 g_string_free(s,
TRUE);
181 PyGILState_Release(gstate);
186 srd_err(
"Unable to query Python system search paths.");
187 PyGILState_Release(gstate);
221 const char *
const *sys_datadirs;
222 const char *env_path;
226 if (max_session_id != -1) {
227 srd_err(
"libsigrokdecode is already initialized.");
233 srd_dbg(
"Initializing libsigrokdecode.");
236 PyImport_AppendInittab(
"sigrokdecode", PyInit_sigrokdecode);
242 sys_datadirs = g_get_system_data_dirs();
243 for (i = g_strv_length((
char **)sys_datadirs); i > 0; i--) {
244 ret = searchpath_add_xdg_dir(sys_datadirs[i - 1]);
252 if ((ret = srd_decoder_searchpath_add(DECODERS_DIR)) !=
SRD_OK) {
258 ret = searchpath_add_xdg_dir(g_get_user_data_dir());
266 if ((ret = srd_decoder_searchpath_add(path)) !=
SRD_OK) {
282 if ((env_path = g_getenv(
"SIGROKDECODE_DIR"))) {
283 if ((ret = srd_decoder_searchpath_add(env_path)) !=
SRD_OK) {
288 env_path = g_getenv(
"SIGROKDECODE_PATH");
290 char **dir_list, **dir_iter, *dir_item;
291 dir_list = g_strsplit(env_path, G_SEARCHPATH_SEPARATOR_S, 0);
292 for (dir_iter = dir_list; *dir_iter; dir_iter++) {
293 dir_item = *dir_iter;
294 if (!dir_item || !*dir_item)
296 ret = srd_decoder_searchpath_add(dir_item);
302 g_strfreev(dir_list);
305 #if PY_VERSION_HEX < 0x03090000 311 PyEval_InitThreads();
315 (void)PyEval_SaveThread();
324 static void srd_session_destroy_cb(
void *arg,
void *ignored)
346 srd_dbg(
"Exiting libsigrokdecode.");
348 g_slist_foreach(sessions, srd_session_destroy_cb, NULL);
349 g_slist_free(sessions);
353 g_slist_free_full(searchpaths, g_free);
360 if (Py_IsInitialized())
361 (void)PyGILState_Ensure();
390 SRD_PRIV int srd_decoder_searchpath_add(
const char *path)
392 PyObject *py_cur_path, *py_item;
393 PyGILState_STATE gstate;
395 srd_dbg(
"Adding '%s' to module path.", path);
397 gstate = PyGILState_Ensure();
399 py_cur_path = PySys_GetObject(
"path");
403 py_item = PyUnicode_FromString(path);
405 srd_exception_catch(
"Failed to create Unicode object");
408 if (PyList_Insert(py_cur_path, 0, py_item) < 0) {
409 srd_exception_catch(
"Failed to insert path element");
415 PyGILState_Release(gstate);
417 searchpaths = g_slist_prepend(searchpaths, g_strdup(path));
422 PyGILState_Release(gstate);
436 GSList *paths = NULL;
438 for (GSList *l = searchpaths; l; l = l->next)
439 paths = g_slist_append(paths, g_strdup(l->data));
GSList * srd_searchpaths_get(void)
Return the list of protocol decoder search paths.
char * srd_buildinfo_host_get(void)
int srd_decoder_unload_all(void)
Unload all loaded protocol decoders.
const char * srd_package_version_string_get(void)
Get the libsigrokdecode package version number as a string.
Generic/unspecified error.
int srd_exit(void)
Shutdown libsigrokdecode.
int srd_session_destroy(struct srd_session *sess)
Destroy a decoding session.
int srd_init(const char *path)
Initialize libsigrokdecode.
const char * srd_lib_version_string_get(void)
Get the libsigrokdecode library version number as a string.
GSList * srd_buildinfo_libs_get(void)
The public libsigrokdecode header file to be used by frontends.