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

73 lines
1.5 KiB
C

#include <sys/types.h>
#include <sys/time.h>
#include <sys/device.h>
#include <sys/hotplug.h>
#include <err.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
#if 0
struct kevent ev;
#endif
struct hotplug_event he;
char *device;
int fd, kq, kev;
if (argc == 2)
device = argv[1];
else
device = "/dev/hotplug";
fd = open(device, O_RDONLY);
if (fd == -1)
err(2, "open %s", device);
#if 0
if ((kq = kqueue()) <= 0)
err(2, "kqueue");
EV_SET(&ev, fd, EVFILT_DEVICE, EV_ADD | EV_ENABLE | EV_CLEAR,
NOTE_CHG_DATA, 0, NULL);
if (kevent(kq, &ev, 1, NULL, 0, NULL) < 0)
err(2, "kevent init");
#endif
while (1) {
#if 0
struct pollfd fds;
int result;
fds.fd = kq;
fds.events = POLLIN;
result = poll(&fds, 1, INFTIM);
printf("poll result %d\n", result);
#endif
int n;
n = read(fd, &he, sizeof(he));
if (n == -1)
err(2, "read");
printf("hotplug event %d %d %s\n",
he.he_devclass, he.he_type, he.he_devname);
if (he.he_devclass != DV_TTY)
continue;
switch (he.he_type) {
case HOTPLUG_DEVAT:
printf("wscons attach %s\n", he.he_devname);
break;
case HOTPLUG_DEVDT:
printf("wscons detach %s\n", he.he_devname);
break;
default:
break;
}
}
return 0;
}