Skip to content

Instantly share code, notes, and snippets.

@mmn80
Forked from anonymous/detect_kbd_devices.c
Created December 19, 2012 14:43
Show Gist options
  • Save mmn80/4337126 to your computer and use it in GitHub Desktop.
Save mmn80/4337126 to your computer and use it in GitHub Desktop.
Uses libudev to detect all keyboard input event devices (/dev/input/eventX). Based on the /usr/lib/udev/findkeyboards script.
#include <libudev.h>
#define MAX_KBD_DEVICES 10
const char** detect_kbd_devices()
{
const char **devnodes = calloc(MAX_KBD_DEVICES, sizeof(char*));
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev;
int i = 0;
udev = udev_new();
if (!udev) {
printf("Can't create udev.\n");
exit(1);
}
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_property(enumerate, "ID_INPUT_KEYBOARD", "1");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices) {
const char *path, *devnode;
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
devnode = udev_device_get_devnode(dev);
if (devnode) {
printf("Found keyboard input device: %s\n", devnode);
devnodes[i] = strdup(devnode);
i++;
}
udev_device_unref(dev);
}
udev_enumerate_unref(enumerate);
udev_unref(udev);
return devnodes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment