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));
176 s->str[s->len - 1] =
'\0';
177 srd_dbg(
"%s", s->str);
178 g_string_free(s, TRUE);
180 PyGILState_Release(gstate);
185 srd_err(
"Unable to query Python system search paths.");
186 PyGILState_Release(gstate);
220 const char *
const *sys_datadirs;
221 const char *env_path;
225 if (max_session_id != -1) {
226 srd_err(
"libsigrokdecode is already initialized.");
232 srd_dbg(
"Initializing libsigrokdecode.");
235 PyImport_AppendInittab(
"sigrokdecode", PyInit_sigrokdecode);
241 sys_datadirs = g_get_system_data_dirs();
242 for (i = g_strv_length((
char **)sys_datadirs); i > 0; i--) {
243 ret = searchpath_add_xdg_dir(sys_datadirs[i - 1]);
251 if ((ret = srd_decoder_searchpath_add(DECODERS_DIR)) !=
SRD_OK) {
257 ret = searchpath_add_xdg_dir(g_get_user_data_dir());
265 if ((ret = srd_decoder_searchpath_add(path)) !=
SRD_OK) {
272 if ((env_path = g_getenv(
"SIGROKDECODE_DIR"))) {
273 if ((ret = srd_decoder_searchpath_add(env_path)) !=
SRD_OK) {
280 PyEval_InitThreads();
283 (void)PyEval_SaveThread();
308 srd_dbg(
"Exiting libsigrokdecode.");
310 for (GSList *l = sessions; l; l = l->next)
314 g_slist_free_full(searchpaths, g_free);
321 if (Py_IsInitialized())
322 (void)PyGILState_Ensure();
353 SRD_PRIV int srd_decoder_searchpath_add(
const char *path)
355 PyObject *py_cur_path, *py_item;
356 PyGILState_STATE gstate;
358 srd_dbg(
"Adding '%s' to module path.", path);
360 gstate = PyGILState_Ensure();
362 py_cur_path = PySys_GetObject(
"path");
366 py_item = PyUnicode_FromString(path);
368 srd_exception_catch(
"Failed to create Unicode object");
371 if (PyList_Insert(py_cur_path, 0, py_item) < 0) {
372 srd_exception_catch(
"Failed to insert path element");
378 PyGILState_Release(gstate);
380 searchpaths = g_slist_prepend(searchpaths, g_strdup(path));
385 PyGILState_Release(gstate);
399 return g_slist_copy_deep(searchpaths, (GCopyFunc)g_strdup, NULL);
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.