Created
December 19, 2012 14:41
-
-
Save anonymous/4337116 to your computer and use it in GitHub Desktop.
This function uses libudev to robustly detect all keyboard input event devices (/dev/input/eventX). Based on /usr/lib/udev/findkeyboards script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libudev.h> | |
#define MAX_KBD_DEVICES 10 | |
const char** detect_kbd_devices() | |
{ | |
const char **devnodes = malloc(MAX_KBD_DEVICES); | |
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