Skip to content

Commit

Permalink
Dynamically load libudev.so.1 on Linux if udev=yes
Browse files Browse the repository at this point in the history
This makes it possibly to run Linux binaries compiled with udev support on
Linux systems which do not provide udev (typically systemd-less distros).

If udev is missing, we fall back to parsing `/dev/input` like when compiled
without udev support (`udev=no`).

Also adding some verbose debug statements to know which method we're using
when debugging Linux joypad issues.

The libudev so wrappers were generated on Mageia 8 with libudev 246.9 using
https://github.com/hpvb/dynload-wrapper:
```
./generate-wrapper.py --include /usr/include/libudev.h --sys-include '<libudev.h>' \
  --soname libudev.so.1 --init-name libudev --omit-prefix gnu_ \
  --output-header libudev-so_wrap.h --output-implementation libudev-so_wrap.c
```
  • Loading branch information
akien-mga committed Feb 17, 2021
1 parent fa8a47e commit a10c259
Show file tree
Hide file tree
Showing 8 changed files with 1,154 additions and 14 deletions.
3 changes: 3 additions & 0 deletions misc/hooks/pre-commit-clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ do
if grep -q "platform/android/java/lib/src/com" <<< $file; then
continue;
fi
if grep -q "\-so_wrap." <<< $file; then
continue;
fi

# ignore file if we do check for file extensions and the file
# does not match any of the extensions specified in $FILE_EXTS
Expand Down
2 changes: 2 additions & 0 deletions misc/scripts/file_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ while IFS= read -rd '' f; do
continue
elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
continue
elif [[ "$f" == *"-so_wrap."* ]]; then
continue
fi
# Ensure that files are UTF-8 formatted.
recode UTF-8 "$f" 2> /dev/null
Expand Down
3 changes: 3 additions & 0 deletions platform/linuxbsd/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ common_x11 = [
"key_mapping_x11.cpp",
]

if "udev" in env and env["udev"]:
common_x11.append("libudev-so_wrap.c")

prog = env.add_program("#bin/godot", ["godot_linuxbsd.cpp"] + common_x11)

if env["debug_symbols"] and env["separate_debug_symbols"]:
Expand Down
4 changes: 2 additions & 2 deletions platform/linuxbsd/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ def configure(env):

if platform.system() == "Linux":
env.Append(CPPDEFINES=["JOYDEV_ENABLED"])

if env["udev"]:
if os.system("pkg-config --exists libudev") == 0: # 0 means found
print("Enabling udev support")
env.Append(CPPDEFINES=["UDEV_ENABLED"])
env.ParseConfig("pkg-config libudev --cflags --libs")
else:
print("libudev development libraries not found, disabling udev support")
else:
env["udev"] = False # Linux specific

# Linkflags below this line should typically stay the last ones
if not env["builtin_zlib"]:
Expand Down
36 changes: 26 additions & 10 deletions platform/linuxbsd/joypad_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <unistd.h>

#ifdef UDEV_ENABLED
#include <libudev.h>
#include "libudev-so_wrap.h"
#endif

#define LONG_BITS (sizeof(long) * 8)
Expand Down Expand Up @@ -72,13 +72,22 @@ void JoypadLinux::Joypad::reset() {
}

JoypadLinux::JoypadLinux(Input *in) {
exit_udev = false;
#ifdef UDEV_ENABLED
use_udev = initialize_libudev() == 0;
if (use_udev) {
print_verbose("JoypadLinux: udev enabled and loaded successfully.");
} else {
print_verbose("JoypadLinux: udev enabled, but couldn't be loaded. Falling back to /dev/input to detect joypads.");
}
#else
print_verbose("JoypadLinux: udev disabled, parsing /dev/input to detect joypads.");
#endif
input = in;
joy_thread.start(joy_thread_func, this);
}

JoypadLinux::~JoypadLinux() {
exit_udev = true;
exit_monitor = true;
joy_thread.wait_to_finish();
close_joypad();
}
Expand All @@ -92,11 +101,18 @@ void JoypadLinux::joy_thread_func(void *p_user) {

void JoypadLinux::run_joypad_thread() {
#ifdef UDEV_ENABLED
udev *_udev = udev_new();
ERR_FAIL_COND(!_udev);
enumerate_joypads(_udev);
monitor_joypads(_udev);
udev_unref(_udev);
if (use_udev) {
udev *_udev = udev_new();
if (!_udev) {
use_udev = false;
ERR_FAIL_MSG("Failed getting an udev context, falling back to parsing /dev/input.");
}
enumerate_joypads(_udev);
monitor_joypads(_udev);
udev_unref(_udev);
} else {
monitor_joypads();
}
#else
monitor_joypads();
#endif
Expand Down Expand Up @@ -137,7 +153,7 @@ void JoypadLinux::monitor_joypads(udev *p_udev) {
udev_monitor_enable_receiving(mon);
int fd = udev_monitor_get_fd(mon);

while (!exit_udev) {
while (!exit_monitor) {
fd_set fds;
struct timeval tv;
int ret;
Expand Down Expand Up @@ -179,7 +195,7 @@ void JoypadLinux::monitor_joypads(udev *p_udev) {
#endif

void JoypadLinux::monitor_joypads() {
while (!exit_udev) {
while (!exit_monitor) {
{
MutexLock lock(joy_mutex);

Expand Down
7 changes: 5 additions & 2 deletions platform/linuxbsd/joypad_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ class JoypadLinux {
void reset();
};

bool exit_udev;
#ifdef UDEV_ENABLED
bool use_udev = false;
#endif
bool exit_monitor = false;
Mutex joy_mutex;
Thread joy_thread;
Input *input;
Input *input = nullptr;
Joypad joypads[JOYPADS_MAX];
Vector<String> attached_devices;

Expand Down
Loading

0 comments on commit a10c259

Please sign in to comment.