-
-
Save raisjn/01b16286dcc4461a6643f40f8f553966 to your computer and use it in GitHub Desktop.
rm device identification
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
using namespace std; | |
#include <fcntl.h> | |
#include <linux/input.h> | |
#include <sys/ioctl.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
// from evtest.c | |
#define BITS_PER_LONG (sizeof(long) * 8) | |
#define NBITS(x) ((((x)-1) / BITS_PER_LONG) + 1) | |
#define OFF(x) ((x) % BITS_PER_LONG) | |
#define BIT(x) (1UL << OFF(x)) | |
#define LONG(x) ((x) / BITS_PER_LONG) | |
#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1) | |
namespace input { | |
enum EV_TYPE { TOUCH, STYLUS, BUTTONS, UNKNOWN, INVALID }; | |
auto check_bit_set(int fd, int type, int i) { | |
unsigned long bit[NBITS(KEY_MAX)]; | |
ioctl(fd, EVIOCGBIT(type, KEY_MAX), bit); | |
return test_bit(i, bit); | |
} | |
EV_TYPE id_by_capabilities(int fd) { | |
int version; | |
if (ioctl(fd, EVIOCGVERSION, &version)) { | |
return INVALID; | |
} | |
unsigned long bit[EV_MAX]; | |
ioctl(fd, EVIOCGBIT(0, EV_MAX), bit); | |
if (test_bit(EV_KEY, bit)) { | |
if (check_bit_set(fd, EV_KEY, BTN_STYLUS) && test_bit(EV_ABS, bit)) { | |
return STYLUS; | |
} | |
if (check_bit_set(fd, EV_KEY, KEY_POWER)) { | |
return BUTTONS; | |
} | |
} | |
if (test_bit(EV_REL, bit)) { | |
if (check_bit_set(fd, EV_ABS, ABS_MT_SLOT)) { | |
return TOUCH; | |
} | |
} | |
return UNKNOWN; | |
} | |
EV_TYPE id_by_name(int fd) { | |
char name[256]; | |
ioctl(fd, EVIOCGNAME(sizeof(name)), name); | |
auto s = std::string(name); | |
if (s.find("I2C Digitizer") != -1) { | |
return STYLUS; | |
} | |
if (s.find("_mt") != -1) { | |
return TOUCH; | |
} | |
if (s.find("keys") != -1) { | |
return BUTTONS; | |
} | |
return UNKNOWN; | |
} | |
}; // namespace input | |
int main(int argc, char *argv[]) { | |
for (auto s : {"/dev/input/event0", "/dev/input/event1", "/dev/input/event2", | |
"/dev/input/mice"}) { | |
auto fd = open(s, O_RDWR); | |
std::cout << s << ' ' << input::id_by_capabilities(fd) << ' ' | |
<< input::id_by_name(fd) << std::endl; | |
close(fd); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment