Author: mesyeti <mesyeti@mesyeti.uk>
working-ish paste
source/app.c | 11 +++++++++++ source/app.h | 1 + source/buffer.c | 21 +++++++++++++++------ source/event.c | 8 ++++++++ source/event.h | 22 +++++++++++----------- source/interpreter.c | 4 ++++ source/interpreter.h | 2 ++ source/interpreter/vt100.c | 23 ++++++++++++++++++++++- source/pane.c | 9 +++++++++ source/pane.h | 1 + source/window.h | 1 + source/window/sdl2.c | 4 ++++
diff --git a/source/app.c b/source/app.c index 5f7351c8dd8b0452e2628757718ecdc82b741657..3b200b3f4f68c40397cef1bc49ed04e8fee70dd9 100644 --- a/source/app.c +++ b/source/app.c @@ -33,6 +33,7 @@ app.bindPrevTab = Input_AddKeyBind(KEY_LCTRL, 0, 0, KEY_PAGE_UP); app.bindNextTab = Input_AddKeyBind(KEY_LCTRL, 0, 0, KEY_PAGE_DOWN); app.bindReport = Input_AddKeyBind(KEY_LCTRL, KEY_LSHIFT, 0, KEY_R); app.bindRedraw = Input_AddKeyBind(KEY_LCTRL, KEY_LSHIFT, 0, KEY_D); + app.bindPaste = Input_AddKeyBind(KEY_LCTRL, KEY_LSHIFT, 0, KEY_V); } static Pane* GetSelectedPane(Pane* pane) { @@ -62,16 +63,19 @@ app.tabSel = app.tabNum - 1; app.paneSel = GetSelectedPane(&app.tabs[app.tabSel]); return true; } + if (Input_MatchBind(app.bindPrevTab, e)) { app.tabSel = app.tabSel == 0? app.tabNum - 1 : app.tabSel - 1; app.paneSel = GetSelectedPane(&app.tabs[app.tabSel]); return true; } + if (Input_MatchBind(app.bindNextTab, e)) { app.tabSel = (app.tabSel + 1) % app.tabNum; app.paneSel = GetSelectedPane(&app.tabs[app.tabSel]); return true; } + if (Input_MatchBind(app.bindReport, e)) { Terminal* term = &app.paneSel->v.terminal; @@ -80,10 +84,17 @@ Log("======"); Log("Cursor: %d, %d\n", term->cursor.x, term->cursor.y); return true; } + if (Input_MatchBind(app.bindRedraw, e)) { Backend_SetRedraw(); Log("Redrawing..."); return true; + } + + if (Input_MatchBind(app.bindPaste, e)) { + char* text = Window_GetClipboardText(); + Pane_Paste(app.paneSel, text); + free(text); } return false; diff --git a/source/app.h b/source/app.h index 097ab29a262c993935df22a9965e0b5bd324aacf..817b1fc73c570f4c336fab280a7d4362ae6f620b 100644 --- a/source/app.h +++ b/source/app.h @@ -22,6 +22,7 @@ Input_BindID bindPrevTab; Input_BindID bindNextTab; Input_BindID bindReport; Input_BindID bindRedraw; + Input_BindID bindPaste; } App; extern App app; diff --git a/source/buffer.c b/source/buffer.c index a7818ecf39b4027697768961bdefd39635666c29..402e9584e8ea282e86f7fb8fb7530628231fdc0a 100644 --- a/source/buffer.c +++ b/source/buffer.c @@ -162,14 +162,23 @@ void Buffer_Scroll(Buffer* buf, int by) { size_t rowSize = sizeof(BufCell) * buf->width; - for (int i = by; i < buf->height; ++ i) { - BufCell* src = Buffer_Get(buf, 0, i); - BufCell* dest = Buffer_Get(buf, 0, i - by); + if (by > 0) { + for (int i = by; i < buf->height; ++ i) { + BufCell* src = Buffer_Get(buf, 0, i); + BufCell* dest = Buffer_Get(buf, 0, i - by); + + memcpy(dest, src, rowSize); + } - memcpy(dest, src, rowSize); + for (int i = 0; i < by; ++ i) { + Buffer_ClearLine(buf, buf->height - 1 - i, BufAttr_Default()); + } } + else if (by < 0) { + by = -by; - for (int i = 0; i < by; ++ i) { - Buffer_ClearLine(buf, buf->height - 1 - i, BufAttr_Default()); + for (int i = buf->height - by; i --> 0;) { + + } } } diff --git a/source/event.c b/source/event.c index 0d46fddfb6b51b07497c99f4b1cbc3476e90d02f..38cf4065c13461af13ab7ad36e7f94fee5f92251 100644 --- a/source/event.c +++ b/source/event.c @@ -53,6 +53,14 @@ return old; } void Event_Add(Event e) { + if (e.type == EVENT_MOUSE_MOVE) { + for (size_t i = 0; i < eventsSize; ++ i) { + if (events[i].type == EVENT_MOUSE_MOVE) { + events[i].type = EVENT_NONE; + } + } + } + events[FindFree()] = e; } diff --git a/source/event.h b/source/event.h index e48735a52a1dc69fc52a9f5df8fd223f4c2347a3..8f77e9b4500087fe326c83918b048534f739f528 100644 --- a/source/event.h +++ b/source/event.h @@ -4,17 +4,17 @@ #include "window.h" #include "keyboard.h" -// i don't use an enum because of compiler warnings about missing cases in switch -// statements for event types -#define EVENT_NONE 0 -#define EVENT_MOUSE_MOVE 1 -#define EVENT_MOUSE_BUTTON_DOWN 2 -#define EVENT_MOUSE_BUTTON_UP 3 -#define EVENT_KEY_DOWN 4 -#define EVENT_KEY_UP 5 -#define EVENT_QUIT 6 -#define EVENT_WINDOW_RESIZE 7 -#define EVENT_TEXT_INPUT 8 +enum { + EVENT_NONE = 0, + EVENT_MOUSE_MOVE = 1, + EVENT_MOUSE_BUTTON_DOWN = 2, + EVENT_MOUSE_BUTTON_UP = 3, + EVENT_KEY_DOWN = 4, + EVENT_KEY_UP = 5, + EVENT_QUIT = 6, + EVENT_WINDOW_RESIZE = 7, + EVENT_TEXT_INPUT = 8 +}; // NOTE: when my gpu crashes it spams event 1 diff --git a/source/interpreter/vt100.c b/source/interpreter/vt100.c index 1addcd6d1118a165e2a1d67782141903b5d24e22..14462bc90de85b5c29786a68386a68b2f1c55e76 100644 --- a/source/interpreter/vt100.c +++ b/source/interpreter/vt100.c @@ -743,6 +743,25 @@ } } } +static void OnResize(Interpreter* this, Terminal* terminal) { + (void) this; + (void) terminal; +} + +static void OnPaste(Interpreter* p_this, Terminal* terminal, const char* data) { + Vt100* this = (Vt100*) p_this->data; + + if (this->bracketedPaste) { + SysTerm_WriteString(&terminal->term, "\x1b[200~"); + } + + SysTerm_WriteString(&terminal->term, data); + + if (this->bracketedPaste) { + SysTerm_WriteString(&terminal->term, "\x1b[201~"); + } +} + Interpreter Interpreter_VT100(void) { Vt100* data = SafeMalloc(sizeof(Vt100)); @@ -756,6 +775,8 @@ return (Interpreter) { .data = (void*) data, .sendInput = &SendInput, - .onRead = &OnRead + .onRead = &OnRead, + .onResize = &OnResize, + .onPaste = &OnPaste }; } diff --git a/source/interpreter.c b/source/interpreter.c index dd72ed1f676e2022355d0ca435f3ba8d70e29ae8..3f685d26a9b4453000e66d6e09720fe1d5667e3a 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -12,3 +12,7 @@ void Interpreter_OnResize(Interpreter* this, Terminal* terminal) { this->onResize(this, terminal); } + +void Interpreter_OnPaste(Interpreter* this, Terminal* terminal, const char* data) { + this->onPaste(this, terminal, data); +} diff --git a/source/interpreter.h b/source/interpreter.h index ecf16bd8d5cd9517ea06d5c0fbab155b16e147c3..3c616f7e2a5eb6a8597a2612a29a5b469df992de 100644 --- a/source/interpreter.h +++ b/source/interpreter.h @@ -11,10 +11,12 @@ void (*sendInput)(Interpreter* this, Terminal* terminal, Event* e); void (*onRead)(Interpreter* this, Terminal* terminal, UChar ch); void (*onResize)(Interpreter* this, Terminal* terminal); + void (*onPaste)(Interpreter* this, Terminal* terminal, const char* data); }; void Interpreter_SendInput(Interpreter* this, Terminal* terminal, Event* e); void Interpreter_OnRead(Interpreter* this, Terminal* terminal, UChar ch); void Interpreter_OnResize(Interpreter* this, Terminal* terminal); +void Interpreter_OnPaste(Interpreter* this, Terminal* terminal, const char* data); #endif diff --git a/source/pane.c b/source/pane.c index f7034801b4e77daf5400ceb96cc6561224cbdd1c..5d2e766ff5959140bca2211d1488197346c16e10 100644 --- a/source/pane.c +++ b/source/pane.c @@ -1,6 +1,7 @@ #include "app.h" #include "pane.h" #include "screen.h" +#include "interpreter.h" Pane Pane_NewRootPane(bool* success) { Pane ret; @@ -63,6 +64,14 @@ break; } default: assert(0); } +} + +void Pane_Paste(Pane* pane, const char* text) { + assert(pane->type == PANE_TERMINAL); + + Interpreter* interp = (Interpreter*) pane->v.terminal.interpreter; + + Interpreter_OnPaste(interp, &pane->v.terminal, text); } void Pane_Render(Pane* pane, Rect within) { diff --git a/source/pane.h b/source/pane.h index 6424033d1fd30f8b656d69f340cfbb8c65d821e4..06f4317f1ac53fac83f30fc30a942523f48757ac 100644 --- a/source/pane.h +++ b/source/pane.h @@ -29,6 +29,7 @@ Pane Pane_NewRootPane(bool* success); void Pane_Free(Pane* pane); void Pane_Update(Pane* pane); void Pane_HandleEvent(Pane* pane, Event* e); +void Pane_Paste(Pane* pane, const char* text); void Pane_Render(Pane* pane, Rect within); #endif diff --git a/source/window/sdl2.c b/source/window/sdl2.c index 9915b692813c27577b32273d9aec1d558fed8433..4f7cdd92673373cc1ea2f55817c4fdc4b1b9e7bd 100644 --- a/source/window/sdl2.c +++ b/source/window/sdl2.c @@ -69,4 +69,8 @@ SDL_ShowSimpleMessageBox(flags, title, contents, NULL); } +char* Window_GetClipboardText(void) { + return SDL_GetClipboardText(); +} + #endif diff --git a/source/window.h b/source/window.h index 61094ff8edcd0d1d6e14df8e1233ab4fc626fe11..5977e0909c2e35dd25b8a55e5a61a111183bd4a1 100644 --- a/source/window.h +++ b/source/window.h @@ -25,5 +25,6 @@ void Window_Free(Window* window); void Window_ShowCursor(bool show); void Window_SetRelativeMouseMode(bool enable); void Window_MessageBox(int type, const char* title, const char* contents); +char* Window_GetClipboardText(void); #endif