From 2f60a533552948f7cd5a220c3cb25081baae8e25 Mon Sep 17 00:00:00 2001 From: Ulrik Guenther Date: Tue, 16 Apr 2024 11:29:07 +0200 Subject: [PATCH] Add initThreads() function, being empty on Windows and macOS, and executing XInitThreads() on Linux/X11 --- src/c/common.h | 4 ++++ src/c/linux.h | 19 +++++++++++++++++++ src/c/macos.h | 2 ++ src/c/win32.h | 2 ++ 4 files changed, 27 insertions(+) diff --git a/src/c/common.h b/src/c/common.h index fb96e95..1eda217 100644 --- a/src/c/common.h +++ b/src/c/common.h @@ -29,6 +29,10 @@ // For implementations, see linux.h, macos.h, posix.h, win32.h // =========================================================== +// used for calling XInitThreads on Linux/X11, empty on +// Windows and macOS, defined in linux.h, win32.h, macos.h +void initThreads(); + // run_command implementations in posix.h, win32.h int run_command(const char *command, const char *input[], size_t numInput, diff --git a/src/c/linux.h b/src/c/linux.h index 49f84a2..565077b 100644 --- a/src/c/linux.h +++ b/src/c/linux.h @@ -1,9 +1,28 @@ #include +#include #include "common.h" #define OS_NAME "linux" +void (*xinit_threads_reference)(); + +void initThreads() { + void *libX11Handle = dlopen("libX11.so", RTLD_LAZY); + if(libX11Handle != NULL) { + debug("Running XInitThreads\n"); + xinit_threads_reference = dlsym(libX11Handle, "XInitThreads"); + + if(xinit_threads_reference != NULL) { + xinit_threads_reference(); + } else { + error("Could not find XInitThreads in X11 library: %s\n", dlerror()); + } + } else { + error("Could not find X11 library, not running XInitThreads.\n"); + } +} + int is_command_available(const char *command) { return access(command, X_OK) == 0; } diff --git a/src/c/macos.h b/src/c/macos.h index cb05e24..41caf38 100644 --- a/src/c/macos.h +++ b/src/c/macos.h @@ -7,6 +7,8 @@ #define OS_NAME "macos" +void initThreads() {} + void show_alert(const char *title, const char *message) { /* TODO: Get this objc code working. // Create an NSString from the C string diff --git a/src/c/win32.h b/src/c/win32.h index fb352eb..7ef6182 100644 --- a/src/c/win32.h +++ b/src/c/win32.h @@ -6,6 +6,8 @@ #define SLASH "\\" #define EXE_SUFFIX ".exe" +void initThreads() {} + void *loadlib(const char *path) { return LoadLibrary(path); } void *dlsym(void *library, const char *symbol) { return GetProcAddress(library, symbol); } void dlclose(void *library) { FreeLibrary(library); }