Implement a simple C module to wait for keypresses
This commit is contained in:
parent
916b3c2836
commit
5c6db0577f
33
evdev/evdev.c
Normal file
33
evdev/evdev.c
Normal 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;
|
||||
}
|
24
evdev/evdev.go
Normal file
24
evdev/evdev.go
Normal file
@ -0,0 +1,24 @@
|
||||
package evdev
|
||||
|
||||
// #cgo CFLAGS: -g -Wall
|
||||
// #include "evdev.h"
|
||||
// #include <stdlib.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
|
||||
func 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
evdev/evdev.h
Normal file
14
evdev/evdev.h
Normal 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);
|
Loading…
Reference in New Issue
Block a user