From 107ac6b2dbcb1dd33f4813c687a390a320dbd3c1 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 11 Nov 2023 15:04:46 +0700 Subject: [PATCH] Remove infinite loop and clean up after yourself --- Makefile | 2 +- bt.c | 213 +++++++++++++++++++++++++++---------------------------- 2 files changed, 107 insertions(+), 108 deletions(-) diff --git a/Makefile b/Makefile index cf4f26d..9b3c9c2 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS=-g -Wall -Wextra -pedantic -O0 LDLIBS=-lSDL2 -lSDL2_ttf RM=rm -vf -bt: clean +build: clean $(CC) $(CFLAGS) $(LDLIBS) $(P).c -o $(P) clean: diff --git a/bt.c b/bt.c index c22c811..f889ea8 100644 --- a/bt.c +++ b/bt.c @@ -14,6 +14,18 @@ #include #include +const int SCREEN_WIDTH = 900; +const int SCREEN_HEIGHT = 700; +const SDL_Color WHITE = {255, 255, 255, 255}; +const SDL_Color BLACK = {0, 0, 0, 255}; +const SDL_Color RED = {255, 0, 0, 255}; +const SDL_Color GREEN = {0, 255, 0, 255}; +const SDL_Color BLUE = {0, 0, 255, 255}; +const SDL_Color YELLOW = {255, 255, 0, 255}; +const SDL_Color MAGENTA = {255, 0, 255, 255}; +const SDL_Color SKYBLUE = {0, 255, 255, 255}; +const SDL_Color RAYWHITE = {200, 200, 200, 255}; + typedef struct scrollback { float height; float ypos; @@ -45,18 +57,6 @@ void spawn(file_descriptors *fds) { } } -const int SCREEN_WIDTH = 900; -const int SCREEN_HEIGHT = 700; -const SDL_Color WHITE = {255, 255, 255, 255}; -const SDL_Color BLACK = {0, 0, 0, 255}; -const SDL_Color RED = {255, 0, 0, 255}; -const SDL_Color GREEN = {0, 255, 0, 255}; -const SDL_Color BLUE = {0, 0, 255, 255}; -const SDL_Color YELLOW = {255, 255, 0, 255}; -const SDL_Color MAGENTA = {255, 0, 255, 255}; -const SDL_Color SKYBLUE = {0, 255, 255, 255}; -const SDL_Color RAYWHITE = {200, 200, 200, 255}; - int read_pty(file_descriptors *fds, scrollback *sb) { ssize_t nread; char readbuf[256]; @@ -150,18 +150,16 @@ int main(void) { char key = e.key.keysym.sym; // TODO: Translate this all to SDL2 // int key = GetCharPressed(); - while (key > 0) { - if ((key >= 32) && (key <= 125)) { - buf[buf_len] = (char)key; - buf[buf_len + 1] = '\0'; - buf_len++; - sb.buf[sb.length] = (char)key; - sb.buf[sb.length + 1] = '\0'; - sb.length++; - } - // TODO: Don't remember why I was doing this - // key = GetCharPressed(); - // new_char = true; + // TODO: Don't remember why I was doing this + // key = GetCharPressed(); + // new_char = true; + if ((key >= 32) && (key <= 125)) { + buf[buf_len] = (char)key; + buf[buf_len + 1] = '\0'; + buf_len++; + sb.buf[sb.length] = (char)key; + sb.buf[sb.length + 1] = '\0'; + sb.length++; } if (key == SDLK_RETURN) { @@ -213,99 +211,100 @@ int main(void) { // Drawing - { - int nread = read_pty(&fds, &sb); - if (nread > 0) { - sb.length += nread; - new_read = true; - } - SDL_RenderClear(renderer); - SDL_Color current_color = WHITE; - int col_max = 200; - char row_buf[col_max]; - int nrow = 0; - int ncol = 0; - int row_height = 18; - float row_posx = 0; - for (int c = 0; c <= sb.length; c++) { - if (sb.buf[c] == '\r') { - continue; - } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) { - row_buf[ncol] = '\0'; - // TODO: Render new line or something? - // Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; - // DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); - nrow++; - ncol = 0; - row_posx = 0; - } else if (sb.buf[c] == '\x1b') { - int c2 = c + 1; - // Control Sequence Introducer - int csi_args[16]; - char csi_code; - int nargs = 0; - if (sb.buf[c2] == '[') { - char *esc_buf = sb.buf + c2 + 1; - while (true) { - char *substr; - long num = strtol(esc_buf, &substr, 10); - if (esc_buf != substr) { - csi_args[nargs++] = num; - esc_buf = substr; - if (substr[0] == ';') { - esc_buf++; - } - continue; + int nread = read_pty(&fds, &sb); + if (nread > 0) { + sb.length += nread; + new_read = true; + } + SDL_RenderClear(renderer); + SDL_Color current_color = WHITE; + int col_max = 200; + char row_buf[col_max]; + int nrow = 0; + int ncol = 0; + int row_height = 18; + float row_posx = 0; + for (int c = 0; c <= sb.length; c++) { + if (sb.buf[c] == '\r') { + continue; + } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) { + row_buf[ncol] = '\0'; + // TODO: Render new line or something? + // Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; + // DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); + nrow++; + ncol = 0; + row_posx = 0; + } else if (sb.buf[c] == '\x1b') { + int c2 = c + 1; + // Control Sequence Introducer + int csi_args[16]; + char csi_code; + int nargs = 0; + if (sb.buf[c2] == '[') { + char *esc_buf = sb.buf + c2 + 1; + while (true) { + char *substr; + long num = strtol(esc_buf, &substr, 10); + if (esc_buf != substr) { + csi_args[nargs++] = num; + esc_buf = substr; + if (substr[0] == ';') { + esc_buf++; } - csi_code = substr[0]; - SDL_Color new_color = WHITE; - switch (csi_code) { - case 'm': - for (int i = 0; i < nargs; i++) { - if (csi_args[i] == 31) { - new_color = RED; - } else if (csi_args[i] == 32) { - new_color = GREEN; - } else if (csi_args[i] == 33) { - new_color = YELLOW; - } else if (csi_args[i] == 34) { - new_color = BLUE; - } else if (csi_args[i] == 35) { - new_color = MAGENTA; - } else if (csi_args[i] == 36) { - new_color = SKYBLUE; - } else if (csi_args[i] == 0) { - new_color = RAYWHITE; - } - } - row_buf[ncol] = '\0'; - // TODO: This looks like the actual place where we - // draw the text at a line - // Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; - // DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); - current_color = new_color; - ncol = 0; - c += (substr) - (sb.buf + c); - // TODO: Get the text height - // int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x; - // row_posx += width + 0.85; - break; - } - break; + continue; } + csi_code = substr[0]; + SDL_Color new_color = WHITE; + switch (csi_code) { + case 'm': + for (int i = 0; i < nargs; i++) { + if (csi_args[i] == 31) { + new_color = RED; + } else if (csi_args[i] == 32) { + new_color = GREEN; + } else if (csi_args[i] == 33) { + new_color = YELLOW; + } else if (csi_args[i] == 34) { + new_color = BLUE; + } else if (csi_args[i] == 35) { + new_color = MAGENTA; + } else if (csi_args[i] == 36) { + new_color = SKYBLUE; + } else if (csi_args[i] == 0) { + new_color = RAYWHITE; + } + } + row_buf[ncol] = '\0'; + // TODO: This looks like the actual place where we + // draw the text at a line + // Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; + // DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); + current_color = new_color; + ncol = 0; + c += (substr) - (sb.buf + c); + // TODO: Get the text height + // int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x; + // row_posx += width + 0.85; + break; + } + break; } - } else { - row_buf[ncol++] = sb.buf[c]; } + } else { + row_buf[ncol++] = sb.buf[c]; } } + SDL_RenderPresent(renderer); + SDL_DestroyTexture(texture); + SDL_FreeSurface(surfaceMessage); } - // TODO: Figure out when to free this thing - // SDL_FreeSurface(texture); - + free(sb.buf); TTF_CloseFont(font); TTF_Quit(); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); SDL_Quit(); }