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