* options:
* --dump default
* --[no-]grab --nograb is default
+ * --expect-sysfs /sys/class/input/inputX/eventY/dev
*/
#include "common.h"
static Mode mode;
static int grab;
+static const char *expect_sysfs;
static void pr_hex(unsigned long value) { printf("%#lx",value); }
static const ModeInfo mode_dump= { dump_event, dump_died, mainloop };
+static void check_expect_sysfs(int fd, const char *path, const char *efn) {
+ char buf[50], *ep;
+ unsigned long maj, min;
+ struct stat stab;
+ FILE *sysfs;
+ int r;
+
+ r= fstat(fd, &stab); if (r) diee("%s: fstat failed", path);
+ if (!S_ISCHR(stab.st_mode)) die("%s: not a character device", path);
+
+ sysfs= fopen(efn,"r");
+ if (!sysfs) diee("%s: failed to open sysfs %s", path, efn);
+ if (!fgets(buf,sizeof(buf)-1,sysfs)) {
+ if (ferror(sysfs)) diee("%s: failed to read sysfs %s", path, efn);
+ assert(feof(sysfs)); die("%s: eof on sysfs %s", path, efn);
+ }
+ buf[sizeof(buf)-1]= 0;
+ errno=0; maj=strtoul(buf,&ep,0);
+ if (errno || *ep!=':') die("%s: bad major number or no colon in sysfs"
+ " dev file %s", path, efn);
+ errno=0; min=strtoul(ep+1,&ep,0);
+ if (errno || *ep!='\n') die("%s: bad minor number or no colon in sysfs"
+ " dev file %s", path, efn);
+
+ if (maj != major(stab.st_rdev) || min != minor(stab.st_rdev))
+ die("%s: is %lu:%lu, expected %lu:%lu", path,
+ (unsigned long)major(stab.st_rdev),
+ (unsigned long)minor(stab.st_rdev),
+ maj, min);
+
+ if (fclose(sysfs)) die("%s: failed to close sysfs %s", path, efn);
+}
+
static void getdevice(const char *path) {
int r;
struct input_id iid;
d->path= mstrdup(path);
d->fd= open(path, O_RDONLY); if (d->fd<0) diee("%s: failed to open",path);
+ if (expect_sysfs) {
+ check_expect_sysfs(d->fd, path, expect_sysfs);
+ expect_sysfs= 0;
+ }
+
r= ioctl(d->fd, EVIOCGID, &iid); if (r) diee("%s: failed to get id",path);
printf("device %s bustype ", path);
PR_TABLE_STR(bus, iid.bustype);
if (arg[0] != '-') {
getdevice(arg);
}
+ else if (!strcmp(arg,"--expect-sysfs")) {
+ if (!(expect_sysfs= *++argv)) badusage("missing arg for --expect-sysfs");
+ }
else if (!strcmp(arg,"--dump")) { mode= &mode_dump; }
else if (!strcmp(arg,"--grab")) { grab= 1; }
else if (!strcmp(arg,"--no-grab")) { grab= 0; }