libudev - как получить уникальные устройства и пути разработчиков?
Я пишу и использую приложение в Debian 9 (без X или Wayland и т. Д.), И мне потребовалась базовая клавиатура, мышь, IO джойстика. Я получил код, работающий для этого, но я хочу контролировать все уникальные клавиатуры, мыши и джойстики. Для этого мне нужно автоматически определить путь разработки для открытия. Т.е.
/dev/input/event0
/dev/input/event1
... etc
Я думал, что мог бы использовать libudev для этого, но это дает мне больше результатов, чем я ожидаю увидеть. Моя реализация для сканирования устройств показана ниже.
void IOManager::scanMice()
{
// Create a list of input devices.
struct udev_enumerate * enumerator = udev_enumerate_new(fUDEV);
// Check if the enumerator object is created.
if(!enumerator)
{
// TODO - Throw something better here.
throw "Failed to create enumerator object.";
}
// Only scan for Mice.
if(udev_enumerate_add_match_property(enumerator, "ID_INPUT_MOUSE", "1") < 0)
{
// TODO - Throw something better here.
throw "Failed to set enumerate property.";
}
// Scan the input sub system.
if(udev_enumerate_add_match_subsystem(enumerator, "input") < 0)
{
// TODO - Throw something better here.
throw "Failed to set enumerate sub system.";
}
// Scan for the devices of interest.
if(udev_enumerate_scan_devices(enumerator) < 0)
{
// TODO - Throw something better here.
throw "Failed to scan for devices.";
}
// The first entry in the list of devices.
struct udev_list_entry * firstDevListEntry =
udev_enumerate_get_list_entry(enumerator);
// Check there is alteast a first entry.
if(firstDevListEntry)
{
// The current device.
struct udev_list_entry * curDevListEntry = nullptr;
// Iterate through all the devices.
udev_list_entry_foreach(curDevListEntry, firstDevListEntry)
{
// Get the filename of the /sys entry for the device and create a
// udev_device object (dev) representing it.
const char * path = udev_list_entry_get_name(curDevListEntry);
// Check if the path is valid.
if(path)
{
// Get a handle to the device.
struct udev_device * device = udev_device_new_from_syspath(fUDEV, path);
// The dev path for the mouse.
const char * devNode = udev_device_get_devnode(device);
// Check the device handle is good.
if(devNode)
{
// Print the device path to open.
std::cout << "Dev Path: " << devNode << std::endl;
}
}
}
}
// Free the enumerator object.
udev_enumerate_unref(enumerator);
}
Сгенерированный вывод показан ниже. (Debian 9 в виртуальной коробке.)
Dev Path: /dev/input/event7
Dev Path: /dev/input/event2
Dev Path: /dev/input/js0
Dev Path: /dev/input/mouse1
Dev Path: /dev/input/event1
Dev Path: /dev/input/mouse0
Мне трудно поверить, что все это уникальные мыши, и если это не так, я обязательно получу много повторных входных событий.
Нет точных сведений об установке Debian или количестве подключенных устройств ввода. Как я могу уточнить результаты только для уникальных записей?
Является /dev/input/js0
не джойстик?