Merge branch 'fix-insane-tty-latency' into 'next'

Fix insane TTY input latency

See merge request STJr/SRB2!2360
This commit is contained in:
sphere 2024-02-24 00:27:38 +00:00
commit aa5383e7e0
2 changed files with 90 additions and 68 deletions

View file

@ -96,6 +96,7 @@ typedef LPVOID (WINAPI *p_MapViewOfFile) (HANDLE, DWORD, DWORD, DWORD, SIZE_T);
#endif #endif
#if defined (__unix__) || (defined (UNIXCOMMON) && !defined (__APPLE__)) #if defined (__unix__) || (defined (UNIXCOMMON) && !defined (__APPLE__))
#include <poll.h>
#include <errno.h> #include <errno.h>
#include <sys/wait.h> #include <sys/wait.h>
#define NEWSIGNALHANDLER #define NEWSIGNALHANDLER
@ -855,50 +856,60 @@ static void I_GetConsoleEvents(void)
// we use this when sending back commands // we use this when sending back commands
event_t ev = {0}; event_t ev = {0};
char key = 0; char key = 0;
ssize_t d; struct pollfd pfd =
{
.fd = STDIN_FILENO,
.events = POLLIN,
.revents = 0,
};
if (!consolevent) if (!consolevent)
return; return;
ev.type = ev_console; for (;;)
ev.key = 0; {
if (read(STDIN_FILENO, &key, 1) == -1 || !key) if (poll(&pfd, 1, 0) < 1 || !(pfd.revents & POLLIN))
return; return;
// we have something ev.type = ev_console;
// backspace? ev.key = 0;
// NOTE TTimo testing a lot of values .. seems it's the only way to get it to work everywhere if (read(STDIN_FILENO, &key, 1) == -1 || !key)
if ((key == tty_erase) || (key == 127) || (key == 8)) return;
{
if (tty_con.cursor > 0) // we have something
// backspace?
// NOTE TTimo testing a lot of values .. seems it's the only way to get it to work everywhere
if ((key == tty_erase) || (key == 127) || (key == 8))
{ {
tty_con.cursor--; if (tty_con.cursor > 0)
tty_con.buffer[tty_con.cursor] = '\0'; {
tty_Back(); tty_con.cursor--;
tty_con.buffer[tty_con.cursor] = '\0';
tty_Back();
}
ev.key = KEY_BACKSPACE;
} }
ev.key = KEY_BACKSPACE; else if (key < ' ') // check if this is a control char
}
else if (key < ' ') // check if this is a control char
{
if (key == '\n')
{ {
tty_Clear(); if (key == '\n')
tty_con.cursor = 0; {
ev.key = KEY_ENTER; tty_Clear();
tty_con.cursor = 0;
ev.key = KEY_ENTER;
}
else continue;
} }
else return; else if (tty_con.cursor < sizeof(tty_con.buffer))
{
// push regular character
ev.key = tty_con.buffer[tty_con.cursor] = key;
tty_con.cursor++;
// print the current line (this is differential)
write(STDOUT_FILENO, &key, 1);
}
if (ev.key) D_PostEvent(&ev);
//tty_FlushIn();
} }
else if (tty_con.cursor < sizeof(tty_con.buffer))
{
// push regular character
ev.key = tty_con.buffer[tty_con.cursor] = key;
tty_con.cursor++;
// print the current line (this is differential)
d = write(STDOUT_FILENO, &key, 1);
}
if (ev.key) D_PostEvent(&ev);
//tty_FlushIn();
(void)d;
} }
#elif defined (_WIN32) #elif defined (_WIN32)

View file

@ -109,6 +109,7 @@ typedef LPVOID (WINAPI *p_MapViewOfFile) (HANDLE, DWORD, DWORD, DWORD, SIZE_T);
#endif #endif
#if defined (__unix__) || (defined (UNIXCOMMON) && !defined (__APPLE__)) #if defined (__unix__) || (defined (UNIXCOMMON) && !defined (__APPLE__))
#include <poll.h>
#include <errno.h> #include <errno.h>
#include <sys/wait.h> #include <sys/wait.h>
#define NEWSIGNALHANDLER #define NEWSIGNALHANDLER
@ -616,50 +617,60 @@ void I_GetConsoleEvents(void)
// we use this when sending back commands // we use this when sending back commands
event_t ev = {0}; event_t ev = {0};
char key = 0; char key = 0;
ssize_t d; struct pollfd pfd =
{
.fd = STDIN_FILENO,
.events = POLLIN,
.revents = 0,
};
if (!consolevent) if (!consolevent)
return; return;
ev.type = ev_console; for (;;)
ev.key = 0; {
if (read(STDIN_FILENO, &key, 1) == -1 || !key) if (poll(&pfd, 1, 0) < 1 || !(pfd.revents & POLLIN))
return; return;
// we have something ev.type = ev_console;
// backspace? ev.key = 0;
// NOTE TTimo testing a lot of values .. seems it's the only way to get it to work everywhere if (read(STDIN_FILENO, &key, 1) == -1 || !key)
if ((key == tty_erase) || (key == 127) || (key == 8)) return;
{
if (tty_con.cursor > 0) // we have something
// backspace?
// NOTE TTimo testing a lot of values .. seems it's the only way to get it to work everywhere
if ((key == tty_erase) || (key == 127) || (key == 8))
{ {
tty_con.cursor--; if (tty_con.cursor > 0)
tty_con.buffer[tty_con.cursor] = '\0'; {
tty_Back(); tty_con.cursor--;
tty_con.buffer[tty_con.cursor] = '\0';
tty_Back();
}
ev.key = KEY_BACKSPACE;
} }
ev.key = KEY_BACKSPACE; else if (key < ' ') // check if this is a control char
}
else if (key < ' ') // check if this is a control char
{
if (key == '\n')
{ {
tty_Clear(); if (key == '\n')
tty_con.cursor = 0; {
ev.key = KEY_ENTER; tty_Clear();
tty_con.cursor = 0;
ev.key = KEY_ENTER;
}
else continue;
} }
else return; else if (tty_con.cursor < sizeof (tty_con.buffer))
{
// push regular character
ev.key = tty_con.buffer[tty_con.cursor] = key;
tty_con.cursor++;
// print the current line (this is differential)
write(STDOUT_FILENO, &key, 1);
}
if (ev.key) D_PostEvent(&ev);
//tty_FlushIn();
} }
else if (tty_con.cursor < sizeof (tty_con.buffer))
{
// push regular character
ev.key = tty_con.buffer[tty_con.cursor] = key;
tty_con.cursor++;
// print the current line (this is differential)
d = write(STDOUT_FILENO, &key, 1);
}
if (ev.key) D_PostEvent(&ev);
//tty_FlushIn();
(void)d;
} }
#elif defined (_WIN32) #elif defined (_WIN32)