119 lines
3.4 KiB
C
Raw Normal View History

2024-12-08 18:33:45 +00:00
#include "sormc.h"
#include <fcntl.h>
#include <linux/input.h>
2024-12-08 15:19:17 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
2024-12-08 18:33:45 +00:00
#include <unistd.h>
2024-12-08 15:19:17 +00:00
#define MAX_DEVICES 32
#define DEVICE_PATH "/dev/input/event"
int is_keyboard(int fd) {
char device_name[256] = {0};
2024-12-08 18:33:45 +00:00
2024-12-08 15:19:17 +00:00
if (ioctl(fd, EVIOCGNAME(sizeof(device_name)), device_name) < 0) {
return 0;
}
return strstr(device_name, "keyboard") != NULL;
}
int main() {
db = sormc("tikker.db");
2024-12-08 18:33:01 +00:00
ulonglong times_repeated = 0;
ulonglong times_pressed = 0;
ulonglong times_released = 0;
2024-12-08 18:33:45 +00:00
sormq(db, "CREATE TABLE IF NOT EXISTS kevent (id INTEGER PRIMARY KEY AUTOINCREMENT, code,event,name,timestamp)");
2024-12-08 15:19:17 +00:00
int keyboard_fds[MAX_DEVICES];
int num_keyboards = 0;
2024-12-08 18:33:45 +00:00
2024-12-08 15:19:17 +00:00
for (int i = 0; i < MAX_DEVICES; i++) {
char device_path[32];
snprintf(device_path, sizeof(device_path), "%s%d", DEVICE_PATH, i);
2024-12-08 18:33:45 +00:00
2024-12-08 15:19:17 +00:00
int fd = open(device_path, O_RDONLY);
if (fd < 0) {
continue;
}
2024-12-08 18:33:45 +00:00
2024-12-08 15:19:17 +00:00
if (is_keyboard(fd)) {
keyboard_fds[num_keyboards++] = fd;
printf("Found keyboard: %s\n", device_path);
} else {
close(fd);
}
}
if (num_keyboards == 0) {
fprintf(stderr, "No keyboard found.\n");
return 1;
}
printf("Monitoring %d keyboards.\n", num_keyboards);
struct input_event ev;
fd_set read_fds;
2024-12-08 18:33:45 +00:00
2024-12-08 15:19:17 +00:00
while (1) {
FD_ZERO(&read_fds);
int max_fd = -1;
2024-12-08 18:33:45 +00:00
2024-12-08 15:19:17 +00:00
for (int i = 0; i < num_keyboards; i++) {
FD_SET(keyboard_fds[i], &read_fds);
if (keyboard_fds[i] > max_fd) {
max_fd = keyboard_fds[i];
}
}
if (select(max_fd + 1, &read_fds, NULL, NULL, NULL) < 0) {
perror("select error");
break;
}
for (int i = 0; i < num_keyboards; i++) {
if (FD_ISSET(keyboard_fds[i], &read_fds)) {
ssize_t bytes = read(keyboard_fds[i], &ev, sizeof(struct input_event));
2024-12-08 18:33:45 +00:00
2024-12-08 15:19:17 +00:00
if (bytes == sizeof(struct input_event)) {
if (ev.type == EV_KEY) {
char key_name[256];
ioctl(keyboard_fds[i], EVIOCGNAME(sizeof(key_name)), key_name);
2024-12-08 18:33:45 +00:00
2024-12-08 15:19:17 +00:00
printf("Keyboard: %s, ", key_name);
2024-12-08 18:33:45 +00:00
char *event_name = NULL;
if (ev.value == 1) {
2024-12-08 15:19:17 +00:00
event_name = "PRESSED";
2024-12-08 18:33:01 +00:00
times_pressed++;
2024-12-08 18:33:45 +00:00
} else if (ev.value == 0) {
2024-12-08 15:19:17 +00:00
event_name = "RELEASED";
2024-12-08 18:33:01 +00:00
times_released++;
2024-12-08 18:33:45 +00:00
} else {
2024-12-08 15:19:17 +00:00
event_name = "REPEATED";
2024-12-08 18:33:01 +00:00
times_repeated++;
2024-12-08 15:19:17 +00:00
}
2024-12-08 18:33:45 +00:00
sormq(db, "INSERT INTO kevent (code, event, name,timestamp) VALUES (%d, %s, %s, DATETIME('now'))", ev.code,
event_name, key_name);
printf("Event: %s, ", ev.value == 1 ? "PRESSED" : ev.value == 0 ? "RELEASED" : "REPEATED");
2024-12-08 18:33:01 +00:00
printf("Key Code: %d ", ev.code);
2024-12-08 18:33:45 +00:00
printf("Pr: %lld Rel: %lld Rep: %lld\n", times_pressed, times_released, times_repeated);
2024-12-08 15:19:17 +00:00
}
}
}
}
}
// Cleanup
for (int i = 0; i < num_keyboards; i++) {
close(keyboard_fds[i]);
}
return 0;
}