Gamepad (Linux): add support

This commit is contained in:
李通洲 2023-02-01 21:28:42 +08:00
parent 0b8e8ad4f2
commit b49b583e64
2 changed files with 43 additions and 1 deletions

View File

@ -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

View File

@ -0,0 +1,42 @@
#include "gamepad.h"
#include "common/io/io.h"
#include <dirent.h>
#include <ctype.h>
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;
}