kde-lockscreen-suspend-display/xdg/keypressdetector/evdev.c

43 lines
958 B
C
Raw Normal View History

#include "evdev.h"
2021-10-16 20:43:34 +00:00
#include <errno.h>
#include <linux/input.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int block_until_keypress(const char* keyboardPath) {
int keyboardFd = open(keyboardPath, O_RDONLY);
if (keyboardFd < 0) {
return errno;
}
int eventSize = sizeof(struct input_event);
int bytesRead = 0;
struct input_event events[NUM_EVENTS];
int readEvents = 1;
while(readEvents) {
bytesRead = read(keyboardFd, events, eventSize * NUM_EVENTS);
if (bytesRead < 0) {
return errno;
}
for(int i = 0; i < (bytesRead / eventSize); ++i) {
struct input_event event = events[i];
if(event.type == EV_KEY) {
if(event.value == 1) {
readEvents = 0;
}
}
}
}
close(keyboardFd);
return 0;
}