Author: mesyeti <mesyeti@mesyeti.uk>
add alternate buffer
source/app.c | 1 source/interpreter/vt100.c | 70 +++++++++++++++++++++++++++++++++------ source/interpreter/vt100.h | 2 source/terminal.c | 2
diff --git a/source/app.c b/source/app.c
index 3eeab25f095e31772a06caf52de5a1b23cf0f01b..4f0eb5a3e58aa00e57f1c37336dd63f0ca724299 100644
--- a/source/app.c
+++ b/source/app.c
@@ -95,6 +95,7 @@ if (Input_MatchBind(app.bindPaste, e)) {
char* text = Window_GetClipboardText();
Pane_Paste(app.paneSel, text);
free(text);
+ return true;
}
return false;
diff --git a/source/interpreter/vt100.c b/source/interpreter/vt100.c
index 465745b5b7776c9acda15579654f27eb458f3bfa..8b15d18ec5ebefa3f299164b6baf070ce66ea5a9 100644
--- a/source/interpreter/vt100.c
+++ b/source/interpreter/vt100.c
@@ -4,6 +4,7 @@ #include "vt100.h"
#include "../mem.h"
#include "../util.h"
#include "../input.h"
+#include "../buffer.h"
#include "../keyboard.h"
enum {
@@ -23,8 +24,9 @@ MOUSE_BOTH
};
typedef struct {
- int mode;
- char reading[1024];
+ int mode;
+ char reading[1024];
+ Buffer altBuffer;
// terminal settings
bool bracketedPaste;
@@ -32,6 +34,7 @@ int mouseMode;
Vec2 savedCursor;
bool appKeypad;
bool appCursor;
+ bool inAltBuff;
} Vt100;
static char* KeyToSequence(Vt100* this, Key key) {
@@ -527,7 +530,17 @@ case 12: terminal->blinkCursor = true; break;
case 25: terminal->showCursor = true; break;
case 66: this->appKeypad = true; break;
case 1047:
- case 47: break; // TODO: show alternate screen buffer
+ case 47: { // switch to alternate buffer
+ if (!this->inAltBuff) {
+ this->inAltBuff = true;
+
+ Buffer temp = this->altBuffer;
+ this->altBuffer = terminal->buffer;
+ terminal->buffer = temp;
+ }
+ puts("Switched to alternate buffer");
+ break;
+ }
case 1000: this->mouseMode = MOUSE_BOTH; break;
case 1001: break; // TODO: use hilite motion mouse tracking
case 1002: break; // TODO: use cell motion mouse tracking
@@ -537,9 +550,15 @@ this->savedCursor = terminal->cursor;
break;
}
case 1049: {
- this->savedCursor = terminal->cursor;
- break;
- // TODO: switch to alternate screen buffer
+ if (!this->inAltBuff) {
+ this->inAltBuff = true;
+ this->savedCursor = terminal->cursor;
+
+ Buffer temp = this->altBuffer;
+ this->altBuffer = terminal->buffer;
+ terminal->buffer = temp;
+ }
+ puts("Switched to alternate buffer");
}
case 2004: break; // TODO: bracketed paste mode
default: printf("Unsupported: %dh\n", param);
@@ -560,7 +579,17 @@ case 12: terminal->blinkCursor = false; break;
case 25: terminal->showCursor = false; break;
case 66: this->appKeypad = false; break;
case 1047:
- case 47: break; // TODO: use normal screen buffer
+ case 47: { // switch to normal screen buffer
+ if (this->inAltBuff) {
+ this->inAltBuff = false;
+
+ Buffer temp = this->altBuffer;
+ this->altBuffer = terminal->buffer;
+ terminal->buffer = temp;
+ }
+ puts("Switched to normal buffer");
+ break;
+ }
case 1000: this->mouseMode = MOUSE_OFF; break;
case 1001: break; // TODO: do not use hilite motion mouse tracking
case 1002: break; // TODO: do not use cell motion mouse tracking
@@ -570,9 +599,16 @@ terminal->cursor = this->savedCursor;
break;
}
case 1049: {
- terminal->cursor = this->savedCursor;
+ if (this->inAltBuff) {
+ this->inAltBuff = false;
+ terminal->cursor = this->savedCursor;
+
+ Buffer temp = this->altBuffer;
+ this->altBuffer = terminal->buffer;
+ terminal->buffer = temp;
+ }
+ puts("Switched to normal buffer");
break;
- // TODO: switch to normal screen buffer
}
case 2004: break; // TODO: bracketed paste mode
default: printf("Unsupported: %dl\n", param);
@@ -693,6 +729,14 @@ }
break;
}
+ case '7': { // save cursor
+ this->savedCursor = terminal->cursor;
+ break;
+ }
+ case '8': { // restore cursor
+ terminal->cursor = this->savedCursor;
+ break;
+ }
default: {
printf("Unimplemented: ESC (%d) '%c'\n", ch, ch);
this->mode = MODE_READY; // invalid
@@ -763,7 +807,8 @@ }
}
static void OnResize(Interpreter* p_this, Terminal* terminal, int w, int h, int ow, int oh) {
- (void) p_this;
+ Vt100* this = (Vt100*) p_this->data;
+
(void) w;
(void) ow;
@@ -776,6 +821,8 @@ else if (h < oh) {
Buffer_Scroll(&terminal->buffer, -(oh - h));
terminal->cursor.y += oh - h;
}
+
+ Buffer_Resize(&this->altBuffer, w, terminal->buffer.height);
}
static void OnPaste(Interpreter* p_this, Terminal* terminal, const char* data) {
@@ -792,11 +839,12 @@ SysTerm_WriteString(&terminal->term, "\x1b[201~");
}
}
-Interpreter Interpreter_VT100(void) {
+Interpreter Interpreter_VT100(Terminal* terminal) {
Vt100* data = SafeMalloc(sizeof(Vt100));
data->reading[0] = 0;
data->mode = MODE_READY;
+ data->altBuffer = Buffer_New(terminal->buffer.width, terminal->buffer.height);
// terminal settings
data->bracketedPaste = false;
diff --git a/source/interpreter/vt100.h b/source/interpreter/vt100.h
index 75db1b81f7f7e2ac8bd468fb31bd2079d87d89b0..cee65878f918b45128466e2031cf225227c0d3ea 100644
--- a/source/interpreter/vt100.h
+++ b/source/interpreter/vt100.h
@@ -3,6 +3,6 @@ #define YT_INTERPRETER_VT100_H
#include "../interpreter.h"
-Interpreter Interpreter_VT100(void);
+Interpreter Interpreter_VT100(Terminal* terminal);
#endif
diff --git a/source/terminal.c b/source/terminal.c
index 74fbadd50cb897ea0c9a59c557f8614a9016995b..8bd37f20cbfcf3b5418b409e8b569876d7632f44 100644
--- a/source/terminal.c
+++ b/source/terminal.c
@@ -24,7 +24,7 @@ }
ret.interpreter = SafeMalloc(sizeof(Interpreter));
- *((Interpreter*) ret.interpreter) = Interpreter_VT100();
+ *((Interpreter*) ret.interpreter) = Interpreter_VT100(&ret);
*success = true;
return ret;