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).",
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) {
273 if ((env_path = g_getenv(
"SIGROKDECODE_DIR"))) {
274 if ((ret = srd_decoder_searchpath_add(env_path)) !=
SRD_OK) {
281 PyEval_InitThreads();
284 (void)PyEval_SaveThread();
293 static void srd_session_destroy_cb(
void *arg,
void *ignored)
315 srd_dbg(
"Exiting libsigrokdecode.");
317 g_slist_foreach(sessions, srd_session_destroy_cb, NULL);
318 g_slist_free(sessions);
322 g_slist_free_full(searchpaths, g_free);
329 if (Py_IsInitialized())
330 (void)PyGILState_Ensure();
359 SRD_PRIV int srd_decoder_searchpath_add(
const char *path)
361 PyObject *py_cur_path, *py_item;
362 PyGILState_STATE gstate;
364 srd_dbg(
"Adding '%s' to module path.", path);
366 gstate = PyGILState_Ensure();
368 py_cur_path = PySys_GetObject(
"path");
372 py_item = PyUnicode_FromString(path);
374 srd_exception_catch(
"Failed to create Unicode object");
377 if (PyList_Insert(py_cur_path, 0, py_item) < 0) {
378 srd_exception_catch(
"Failed to insert path element");
384 PyGILState_Release(gstate);
386 searchpaths = g_slist_prepend(searchpaths, g_strdup(path));
391 PyGILState_Release(gstate);
405 GSList *paths = NULL;
407 for (GSList *l = searchpaths; l; l = l->next)
408 paths = g_slist_append(paths, g_strdup(l->data));
int srd_init(const char *path)
Initialize libsigrokdecode.
#define SRD_PACKAGE_VERSION_STRING
The libsigrokdecode package version ("major.minor.micro") as string.
Generic/unspecified error.
int srd_decoder_unload_all(void)
Unload all loaded protocol decoders.
GSList * srd_buildinfo_libs_get(void)
int srd_session_destroy(struct srd_session *sess)
Destroy a decoding session.
The public libsigrokdecode header file to be used by frontends.
GSList * srd_searchpaths_get(void)
Return the list of protocol decoder search paths.
char * srd_buildinfo_host_get(void)
const char * srd_lib_version_string_get(void)
Get the libsigrokdecode library version number as a string.
#define SRD_LIB_VERSION_STRING
The libsigrokdecode libtool version ("current:revision:age") as string.
const char * srd_package_version_string_get(void)
Get the libsigrokdecode package version number as a string.
int srd_exit(void)
Shutdown libsigrokdecode.