twscons/twskbd.c
2024-11-07 15:21:46 +01:00

115 lines
2.3 KiB
C

#include <sys/ioctl.h>
#include <sys/time.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplay_usl_io.h>
#include <err.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
volatile int done = 0;
void
usage(void)
{
errx(2, "usage: twskbd [-rR] [-v version]");
}
void
sighandler(int sig)
{
done = 1;
}
int
main(int argc, char **argv)
{
struct wscons_event ev;
struct wscons_event_ex evx;
struct termios kbdtty, okbdtty;
int ch, fd, n, option = 0;
char *dev;
unsigned int type;
unsigned char buf[1];
bool raw = false;
bool reset = false;
int version = 0;
while ((ch = getopt(argc, argv, "rRv:")) != -1) {
switch (ch) {
case 'r':
raw = true;
break;
case 'R':
reset = true;
break;
case 'v':
version = atoi(optarg);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
printf("Signals installed\n");
dev = argv[0] != NULL ? argv[0]:"/dev/wskbd";
if ((fd = open(dev, O_RDWR)) == -1)
err(1, "open '%s'", dev);
tcgetattr(fd, &kbdtty);
okbdtty = kbdtty;
cfmakeraw(&kbdtty);
/* kbdtty.c_cc[VTIME] = 0;
kbdtty.c_cc[VMIN] = 1; */
tcsetattr(fd, TCSANOW, &kbdtty);
if (version > 0)
if (ioctl(fd, WSKBDIO_SETVERSION, &version) == -1)
err(1, "SETVERSION %d", version);
if (raw)
option = WSKBD_RAW;
if (ioctl(fd, WSKBDIO_SETMODE, &option) == -1)
err(1, "SETMODE RAW");
if (ioctl(fd, WSKBDIO_GTYPE, &type) == -1)
err(2, "ioctl WSKBDIO_GTYPE");
printf("keyboard type: %ud\n", type);
if (reset)
exit(0);
printf("entering loop\n");
done = 0;
while (!done) {
if (!raw) {
if (version == 0) {
n = read(fd, &ev, sizeof(ev));
printf("%d dev: %#x, type: %#x, value %#x\n", n,
/* ev.device */ 0, ev.type, ev.value);
} else {
n = read(fd, &evx, sizeof(evx));
printf("%d dev: %#x, type: %#x, value %#x\n", n,
evx.device, evx.type, evx.value);
}
} else {
n = read(fd, buf, sizeof buf);
printf("-> %d\n", n);
for (int i = 0; i < n; i++) {
printf("0x%02x\n", buf[i]);
}
}
}
tcsetattr(fd, TCSANOW, &okbdtty);
option = WSKBD_TRANSLATED;
ioctl(fd, WSKBDIO_SETMODE, &option);
exit(0);
}