Sophie

Sophie

distrib > Mandriva > 2009.0 > i586 > by-pkgid > d71f0febfd3d07373636e3a57b4a67eb > files > 1

knob-1.0-0.20070406.3mdv2009.0.src.rpm

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <linux/input.h>

int main(int argc, char **argv)
{
	if (argc < 2) {
		printf("Usage: %s /dev/input/event*\n", argv[0]);
		exit(1);
	}

	// Open all event* for reading
	struct pollfd *fd = calloc(argc - 1, sizeof(struct pollfd));
	for (int i = 0; i < argc - 1; i++) {
		fd[i].fd = open(argv[i+1], O_RDONLY);
		if (fd[i].fd < 0) {
			fprintf(stderr, "Unable to open ");
			perror(argv[i+1]);
			free(fd);
			exit(1);
		}
		fd[i].events = POLLIN;
	}

	int keyboard = -1, multimedia = -1;
	struct input_event ev;

	printf("Press the space bar...    ");
	fflush(stdout);
	while (keyboard == -1) {
		if (poll(fd, argc - 1, -1) < 0) {
			perror(0);
		}
		else {
			for (int i = 0; i < argc - 1; i++) {
				if (fd[i].revents == POLLIN) {
					if (read(fd[i].fd, &ev, sizeof(struct input_event)) < 0)
						perror(0);
					else if (ev.code == KEY_SPACE && ev.type == EV_KEY && ev.value == 1) {
						printf("%s\n", argv[i+1]);
						keyboard = i+1;
						break;
					}
				}
			}
		}
	}

	for (int i = 0; i < argc - 1; i++) {
		fd[i].revents = 0;
		ev.code = 0;
	}

	printf("Press the mute button... ");
	fflush(stdout);
	while (multimedia == -1) {
		if (poll(fd, argc - 1, -1) < 0) {
			perror(0);
		}
		else {
			for (int i = 0; i < argc - 1; i++) {
				if (fd[i].revents == POLLIN) {
					if (read(fd[i].fd, &ev, sizeof(struct input_event)) < 0)
						perror(0);
					else if (ev.code == 113 /* Mute button*/ && ev.type == EV_KEY && ev.value == 1) {
						printf("%s\n", argv[i+1]);
						multimedia = i+1;
						break;
					}
				}
			}
		}
	}

	printf("\nDetection done, run: /path/to/knob %s %s\n", argv[keyboard], argv[multimedia]);

	free(fd);
	return 0;
}