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

35 lines
809 B
C
Raw Normal View History

#include <errno.h>
#include "evdev.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;
}