Invert display and lockscreen control dependencies

This commit is contained in:
2020-08-20 16:57:57 +02:00
parent e6458aba0b
commit ebbd1974df
7 changed files with 59 additions and 17 deletions

33
keypressdetector/evdev.c Normal file
View File

@ -0,0 +1,33 @@
#include "evdev.h"
int block_until_keypress(const char* keyboardPath) {
int keyboardFd = open(keyboardPath, O_RDONLY);
if (keyboardFd < 0) {
return keyboardFd;
}
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 bytesRead;
}
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;
}

28
keypressdetector/evdev.go Normal file
View File

@ -0,0 +1,28 @@
package keypressdetector
// #cgo CFLAGS: -g -Wall
// #include "evdev.h"
// #include <stdlib.h>
import "C"
import (
"unsafe"
"syscall"
)
// Evdev can detect keypresses on /dev/input devices
type Evdev struct {}
// BlockUntilKeypress waits until a key is pressed on the specified device
func (Evdev) BlockUntilKeypress(devicePath string) error {
path := C.CString(devicePath)
defer C.free(unsafe.Pointer(path))
ret := C.block_until_keypress(path)
if ret < 0 {
return syscall.Errno(ret)
}
return nil
}

14
keypressdetector/evdev.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <linux/input.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define NUM_EVENTS 128
int block_until_keypress(const char* keyboardPath);