aboutsummaryrefslogtreecommitdiff
path: root/include/library_loader.h
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-10-16 02:08:40 +0200
committerdec05eba <dec05eba@protonmail.com>2022-10-16 04:15:09 +0200
commita7e0dbd83381377bd05a3fa988511d3713996370 (patch)
tree0e18f4c7b95aa4cf79d6646bca77ed5f03c65fb3 /include/library_loader.h
parent93d46b976730bced7a70be1617a0171600167314 (diff)
Refactor xcomposite into abstract capture api
Refactor c++ files into c files, more usable
Diffstat (limited to 'include/library_loader.h')
-rw-r--r--include/library_loader.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/include/library_loader.h b/include/library_loader.h
new file mode 100644
index 0000000..d359c5b
--- /dev/null
+++ b/include/library_loader.h
@@ -0,0 +1,41 @@
+#ifndef GSR_LIBRARY_LOADER_H
+#define GSR_LIBRARY_LOADER_H
+
+#include <dlfcn.h>
+#include <stdio.h>
+
+typedef struct {
+ void **func;
+ const char *name;
+} dlsym_assign;
+
+static void* dlsym_print_fail(void *handle, const char *name, bool required) {
+ dlerror();
+ void *sym = dlsym(handle, name);
+ char *err_str = dlerror();
+
+ if(!sym)
+ fprintf(stderr, "%s: dlsym(handle, \"%s\") failed, error: %s\n", required ? "error" : "warning", name, err_str ? err_str : "(null)");
+
+ return sym;
+}
+
+/* |dlsyms| should be null terminated */
+static bool dlsym_load_list(void *handle, const dlsym_assign *dlsyms) {
+ bool success = true;
+ for(int i = 0; dlsyms[i].func; ++i) {
+ *dlsyms[i].func = dlsym_print_fail(handle, dlsyms[i].name, true);
+ if(!*dlsyms[i].func)
+ success = false;
+ }
+ return success;
+}
+
+/* |dlsyms| should be null terminated */
+static void dlsym_load_list_optional(void *handle, const dlsym_assign *dlsyms) {
+ for(int i = 0; dlsyms[i].func; ++i) {
+ *dlsyms[i].func = dlsym_print_fail(handle, dlsyms[i].name, false);
+ }
+}
+
+#endif /* GSR_LIBRARY_LOADER_H */ \ No newline at end of file