Add a timed getche function

This commit is contained in:
Andrew Pamment 2022-06-02 12:00:01 +10:00
parent ddeede0ebe
commit 286b508f10
2 changed files with 13 additions and 3 deletions

View File

@ -17,6 +17,15 @@ extern time_t mdtimeout;
extern time_t mdtimeremaining; extern time_t mdtimeremaining;
char md_getc() { char md_getc() {
char c;
do {
c = md_getche(60, 0);
} while(c == -1);
return c;
}
char md_getche(uint32_t sec, uint32_t usec) {
char c = 'x'; char c = 'x';
int ret; int ret;
int rs; int rs;
@ -34,8 +43,8 @@ char md_getc() {
FD_SET(mdcontrol.socket, &rfd); FD_SET(mdcontrol.socket, &rfd);
} }
tv.tv_sec = 60; tv.tv_sec = sec;
tv.tv_usec = 0; tv.tv_usec = usec;
if (mdcontrol.socket == -1) { if (mdcontrol.socket == -1) {
rs = select(STDIN_FILENO + 1, &rfd, NULL, NULL, &tv); rs = select(STDIN_FILENO + 1, &rfd, NULL, NULL, &tv);
@ -52,6 +61,7 @@ char md_getc() {
md_printf("\r\nOut of time!\r\n"); md_printf("\r\nOut of time!\r\n");
md_exit(0); md_exit(0);
} }
return -1;
} else if (rs == -1 && errno != EINTR) { } else if (rs == -1 && errno != EINTR) {
md_exit(0); md_exit(0);
} else if (mdcontrol.socket != -1 && FD_ISSET(mdcontrol.socket, &rfd)) { } else if (mdcontrol.socket != -1 && FD_ISSET(mdcontrol.socket, &rfd)) {

View File

@ -24,7 +24,7 @@ typedef struct MDDoorControl {
extern MDDoorControl_t mdcontrol; extern MDDoorControl_t mdcontrol;
extern char md_getche(uint32_t sec, uint32_t usec);
extern void md_init(const char* dropfile, int socket); extern void md_init(const char* dropfile, int socket);
extern void md_exit(int exitcode); extern void md_exit(int exitcode);
extern void md_putchar(char c); extern void md_putchar(char c);