cursor input support

This commit is contained in:
y4my4my4m 2023-05-11 01:11:42 +09:00
parent b64d0bb563
commit 4e8be4752b

View file

@ -2,8 +2,8 @@
// Public Domain
#define TELNET_PORT 23
#define BUF_SIZE 8192 // way too big?
#define TIMEOUT_DURATION 5000
#define BUF_SIZE 16384 // way too big?
#define TIMEOUT_DURATION 50000
#define NEGOTIATE 0xFF
@ -14,7 +14,6 @@
CTask *input_task = NULL;
Bool force_disconnect = FALSE;
Bool input_request = FALSE;
U8 IsDigit(U8 ch) {
return '0' <= ch <= '9';
@ -30,11 +29,12 @@ U0 HandleControlCodes(U8 ch) {
break;
case 8: // BS (Backspace)
// "%c%c%c", 8, ' ', 8; // Move cursor back, erase character, move cursor back again
SysLog("CC Backspace happened\n");
"$$CM,-8,0$$";
break;
case 9: // HT (Horizontal Tab)
// " "; // 8 spaces
"$$CM,8,0$$";
" "; // 8 spaces
// "$$CM,8,0$$";
break;
case 10: // LF (Line Feed)
"\n";
@ -93,11 +93,11 @@ U0 InputTask(U0 *args) {
U8 input_buffer[BUF_SIZE];
U8 *temp, ch;
I64 sc;
U8 *line = input_buffer;
I64 input_len = 0;
while (!force_disconnect) {
if (!input_request) {
DocBottom(input_task->put_doc);
"\n$$RED$$$BK,1$Input$BK,0$$$BLACK$$:";
@ -105,44 +105,70 @@ U0 InputTask(U0 *args) {
// https://theasciicode.com.ar/ascii-control-characters/escape-ascii-code-27.html
input_len = 0;
while (1) {
ch = CharGet(, FALSE);
if (ch == '\r' || ch == '\n') {
// input_buffer[input_len++] = '\r';
// input_buffer[input_len++] = '\n';
// Bool shift_pressed = FALSE;
U8 key = KeyGet(&sc);
switch (key)
{
case 0:
switch (sc.u8[0])
{
case SC_CURSOR_LEFT:
// SysLog("Pressed Cursor Left");
TCPSocketSendString(sock, "\x1B[D");
break;
}
else if (ch == CH_SHIFT_ESC) {
force_disconnect = TRUE;
case SC_CURSOR_RIGHT:
// SysLog("Pressed Cursor Right");
TCPSocketSendString(sock, "\x1B[C");
break;
}
else if (ch == CH_ESC) {
TCPSocketSendString(sock, 0x27);
case SC_CURSOR_UP:
// SysLog("Pressed Cursor Up");
TCPSocketSendString(sock, "\x1B[A");
break;
}
// else if (ch == CH_BACKSPACE) {
// break;
// }
else if (ch == 0x0F) { // TAB?
case SC_CURSOR_DOWN:
// SysLog("Pressed Cursor Down");
TCPSocketSendString(sock, "\x1B[B");
break;
case SC_TAB:
TCPSocketSendString(sock, 0x09);
break;
// case 0x2A: // Shift
// shift_pressed = TRUE;
// break;
// case 0x01: // Escape
// if (shift_pressed) {
// // Handle Shift + Escape
// force_disconnect = TRUE;
// shift_pressed = FALSE;
// } else {
// // Handle regular Escape
// }
// break;
default:
break;
}
else {
input_buffer[input_len++] = ch;
DocPrint(input_task->put_doc, "%c", ch);
}
}
if (!force_disconnect) {
break;
// send buffer on enter
case '\n':
SysLog(input_buffer);
temp = MStrPrint("%s\r\n", input_buffer);
TCPSocketSendString(sock, temp);
input_len = 0;
Free(temp);
MemSet(input_buffer, 0, BUF_SIZE);
DocClear;
} else {
"Force disconnecting...\n";
break;
case 3: // CTRL+C
break;
default:
if (key >= ' ' && key <= '~') {
// Handle regular keys
input_buffer[input_len++] = key;
DocPrint(input_task->put_doc, "%c", key);
}
break;
}
}
@ -248,17 +274,13 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
ptr++;
break;
case 'm':
// I64 fg_code, bg_code;
// if (x_code >= 30 && x_code <= 37) {
// fg_code = x_code - 30; // Set foreground color
// } else if (y_code >= 40 && y_code <= 47) {
// bg_code = y_code - 40; // Set background color
// }
// colors might be printed in the wrong order?
// like, <Esc>[1;40m and now <Esc>[40m;1m
I64 m;
for (m = 0; m < ansi_param_count; m++) {
if (ansi_code[m] <= 10) {
// switch (ansi_code[m]) {
// case 0: "$$BG$$$$FG$$"; break; // reset
switch (ansi_code[m]) {
case 0: "$$BG$$$$FG$$"; break; // reset
// case 1: ""; break; // TODO: bold
// case 2: ""; break; // TODO: dim
// case 3: ""; break; // TODO: italic
@ -269,7 +291,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
// case 8: ""; break; // TODO: hide (rare)
// case 9: ""; break; // TODO: strikethrough
// case 10: ""; break; // TODO: primary font
// }
}
}
else if (ansi_code[m] >= 30 && ansi_code[m] <= 37) {
// Set foreground color
@ -326,7 +348,11 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
// Cursor Right
// SysLog("Cursor Right %d %d\n", ansi_param_count, ansi_code[0]);
// "$$CM+LX,+%d,0$$", ansi_code[0];
"$$CM,%d,0$$", ansi_code[0];
// "$$CM,%d,0$$", ansi_code[0];
I64 C;
for (C = 0; C < ansi_code[0]; C++) {
" ";
}
ptr++;
break;
case 'D':
@ -420,12 +446,14 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
SysLog("Scroll Down");
ptr++;
break;
case 'M':
// SysLog("Case M\n");
// TODO: is this correct? cursor should go one line up
"$$CM,0,-1$$";
ptr++;
break;
case '?':
ptr++;
if (*ptr == 's') SysLog("SaveCurrentCursorPosition\n");
if (*ptr == 'u') SysLog("RestoreCurrentCursorPosition\n");
I64 code = 0;
while (IsDigit(*ptr)) {
@ -463,16 +491,22 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
}
break;
case 's':
// self.saveCursorPosition();
SysLog("SaveCurrentCursorPosition\n");
ptr++;
break;
case 'u':
SysLog("RestoreCurrentCursorPosition\n");
ptr++;
break;
case 'r':
// self.restoreCursorPosition();
SysLog("r case \n");
ptr++;
break;
case 'h':
case 'l':
// TODO: Handle 'h' (set mode) or 'l' (reset mode) codes
SysLog("h or l case \n");
ptr++; // Skip 'h' or 'l'
break;
case '=':
@ -480,23 +514,27 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
ptr++;
break;
default:
if(!IsDigit(*ptr)) {
SysLog("Unknown code: %c\n", *ptr);
}
ptr++;
break;
}
}
}
// is this handled by the client or the server?
else if (*ptr == '|') {
// U8 code[3];
// code[0] = ptr++;
// code[1] = ptr++;
// code[2] = '\0';
// I64 fg_code = ToI64(code);
// AppendColorString(fg_code);
SysLog("Pipe code: %c%c\n", ptr[1], ptr[2]);
ptr += 3; // Skip the pipe code characters
// ptr++;
}
// regardless, this is not taking <esc> into account, any actual | is captured
// else if (*ptr == '|') {
// // U8 code[3];
// // code[0] = ptr++;
// // code[1] = ptr++;
// // code[2] = '\0';
// // I64 fg_code = ToI64(code);
// // AppendColorString(fg_code);
// SysLog("Pipe code: %c%c\n", ptr[1], ptr[2]);
// ptr += 3; // Skip the pipe code characters
// // ptr++;
// }
else {
// Print the received character
HandleControlCodes(*ptr);
@ -545,4 +583,3 @@ U0 TelnetPrompt() {
// dev server
Telnet("localhost", 8888);
// Telnet("bbs.bottomlessabyss.net", 2023);