diff --git a/CMakeLists.txt b/CMakeLists.txt index 27853f76..40b013dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -336,7 +336,7 @@ if(LINUX) src/detection/gtk.c src/detection/host/host_linux.c src/detection/localip/localip_linux.c - src/detection/gamepad/gamepad_nosupport.c + src/detection/gamepad/gamepad_linux.c src/detection/media/media_linux.c src/detection/memory/memory_linux.c src/detection/opengl/opengl_linux.c diff --git a/src/detection/gamepad/gamepad_linux.c b/src/detection/gamepad/gamepad_linux.c new file mode 100644 index 00000000..ce61a571 --- /dev/null +++ b/src/detection/gamepad/gamepad_linux.c @@ -0,0 +1,42 @@ +#include "gamepad.h" +#include "common/io/io.h" + +#include +#include + +const char* ffDetectGamepad(FF_MAYBE_UNUSED const FFinstance* instance, FFlist* devices /* List of FFGamepadDevice */) +{ + DIR* dirp = opendir("/sys/class/input/"); + if(dirp == NULL) + return "opendir(\"/sys/class/input/\") == NULL"; + + FF_STRBUF_AUTO_DESTROY path; + ffStrbufInitS(&path, "/sys/class/input/"); + uint32_t baseLen = path.length; + + struct dirent* entry; + while((entry = readdir(dirp)) != NULL) + { + if(strncmp(entry->d_name, "js", 2) != 0) + continue; + if(!isdigit(entry->d_name[2])) + continue; + + ffStrbufAppendS(&path, entry->d_name); + ffStrbufAppendS(&path, "/device/name"); + + if (ffPathExists(path.chars, FF_PATHTYPE_FILE)) + { + FFGamepadDevice* device = (FFGamepadDevice*) ffListAdd(devices); + ffStrbufInitS(&device->identifier, entry->d_name); + ffStrbufInit(&device->name); + ffAppendFileBuffer(path.chars, &device->name); + } + + ffStrbufSubstrBefore(&path, baseLen); + } + + closedir(dirp); + + return NULL; +}