diff --git a/Makefile b/Makefile index 05100ed..adf78a9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all clean debug install uninstall +.PHONY: all clean install uninstall DESTDIR= PREFIX=/usr @@ -19,9 +19,6 @@ CFLAGS=-DVERSION=\"$(VERSION)\" \ all: mkdir -p bin $(CC) $(CFLAGS) -O3 src/*.c -o bin/keyd -ludev -debug: - mkdir -p bin - $(CC) $(CFLAGS) -Wall -Wextra -pedantic -DDEBUG -g src/*.c -o bin/keyd -ludev man: pandoc -s -t man man.md | gzip > keyd.1.gz clean: diff --git a/src/main.c b/src/main.c index 7c7c360..deebbea 100644 --- a/src/main.c +++ b/src/main.c @@ -43,12 +43,9 @@ #define UINPUT_DEVICE_NAME "keyd virtual keyboard" #define MAX_KEYBOARDS 256 -#ifdef DEBUG - #define dbg(fmt, ...) warn("%s:%d: "fmt, __FILE__, __LINE__, ## __VA_ARGS__) -#else - #define dbg(...) -#endif +#define dbg(fmt, ...) { if(debug) warn("%s:%d: "fmt, __FILE__, __LINE__, ## __VA_ARGS__); } +static int debug = 0; static int ufd = -1; static struct udev *udev; @@ -128,6 +125,23 @@ static int is_keyboard(struct udev_device *dev) return is_keyboard; } +static const char *evdev_device_name(const char *devnode) +{ + static char name[256]; + + int fd = open(devnode, O_RDONLY); + if(fd < 0) { + perror("open"); + exit(-1); + } + + if(ioctl(fd, EVIOCGNAME(sizeof(name)), &name) == -1) + return NULL; + + close(fd); + return name; +} + static void get_keyboard_nodes(char *nodes[MAX_KEYBOARDS], int *sz) { struct udev *udev; @@ -157,10 +171,13 @@ static void get_keyboard_nodes(char *nodes[MAX_KEYBOARDS], int *sz) const char *path = udev_device_get_devnode(dev); if(is_keyboard(dev)) { + dbg("Detected keyboard node %s (%s)", name, evdev_device_name(path)); nodes[*sz] = malloc(strlen(path)+1); strcpy(nodes[*sz], path); (*sz)++; assert(*sz <= MAX_KEYBOARDS); + } else if(path) { + dbg("Ignoring %s (%s)", evdev_device_name(path), path); } udev_device_unref(dev); @@ -512,25 +529,6 @@ keyseq_cleanup: } } -static const char *evdev_device_name(const char *devnode) -{ - static char name[256]; - - int fd = open(devnode, O_RDONLY); - if(fd < 0) { - perror("open"); - exit(-1); - } - - if(ioctl(fd, EVIOCGNAME(sizeof(name)), &name) == -1) { - perror("ioctl"); - exit(-1); - } - - close(fd); - return name; -} - //Block on the given keyboard nodes until no keys are depressed. static void await_keyboard_neutrality(char **devs, int n) { @@ -886,6 +884,11 @@ static void daemonize() int main(int argc, char *argv[]) { + if(getenv("KEYD_DEBUG")) + debug = 1; + + dbg("Debug mode enabled."); + if(argc > 1) { if(!strcmp(argv[1], "-v")) { fprintf(stderr, "keyd version: %s (%s)\n", VERSION, GIT_COMMIT_HASH);