406 lines
12 KiB
C

#define _GNU_SOURCE
#include <sys/select.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pty.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_keycode.h>
const int SCREEN_WIDTH = 900;
const int SCREEN_HEIGHT = 700;
const int SCROLLBACK_DEFAULT_SIZE = 4096;
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 {
float height;
float ypos;
int capacity;
int length;
int cursor;
char *buf;
} Scrollback;
typedef struct {
int parent;
int child;
} FileDescriptors;
typedef struct {
Scrollback* sb;
char *readline;
int rline_len;
TTF_Font* font;
SDL_Renderer* renderer;
} Context;
void
die(const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(1);
}
void spawn(FileDescriptors *fds) {
if (openpty(&fds->parent, &fds->child, NULL, NULL, NULL)) {
fprintf(stderr, "openpty() failed: %s\n", strerror(errno));
exit(1);
}
// pid_t p = forkpty(&fds->parent, NULL, NULL, NULL);
// int _stdout = dup(STDOUT_FILENO);
// dup2(fds->child, STDOUT_FILENO);
pid_t p = fork();
if (p == 0) {
setsid();
if (ioctl(fds->child, TIOCSCTTY, NULL))
die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
dup2(fds->child, STDIN_FILENO);
dup2(fds->child, STDOUT_FILENO);
dup2(fds->child, STDERR_FILENO);
close(fds->parent);
close(fds->child);
char **args = NULL;
execvp("/usr/bin/bash", args);
// execvp("/usr/bin/dash", args);
} else {
fds->child = p;
close(fds->child);
}
}
int read_pty(FileDescriptors *fds, Scrollback *sb) {
char readbuf[512];
ssize_t nread = read(fds->parent, readbuf, sizeof(readbuf));
switch (nread) {
case -1:
if (errno == EAGAIN) {
return 0;
} else {
return 0;
}
case 0:
printf("EOF\n");
return 0;
default:
if (sb->length + nread > sb->capacity) {
sb->capacity *= 2;
void *buf = realloc(sb->buf, sb->capacity);
if (buf == NULL) {
fprintf(stderr, "We couldn't get anymore memory");
exit(1);
}
sb->buf = buf;
printf("Reallocated scrollback buffer\n");
}
if (readbuf[nread - 1] == '\n') {
// printf("are you firing when you shouldnt?\n");
nread--;
}
// if (nread > 2 && readbuf[0] == '^' && readbuf[1] == '[') {
// memcpy(sb->buf + sb->length, readbuf, nread);
// Don't do anything for not
// return nread;
// }
if (nread == 3 && readbuf[0] == '\b') {
memmove(sb->buf + sb->length - 1, sb->buf + sb->length, sb->length);
sb->length--;
return nread;
}
for (int i = 0; i < nread; i++) {
if (readbuf[i] == '\r') {
readbuf[i] = '\n';
}
// printf("%ld %d\n", nread, readbuf[i]);
}
memcpy(sb->buf + sb->length, readbuf, nread);
sb->length += nread;
sb->buf[sb->length+1] = '\0';
return nread;
}
}
void draw_line(char *buf, int x, int y, SDL_Color color, Context* ctx) {
SDL_Surface* surface =
TTF_RenderText_Blended(ctx->font, buf, color);
if (surface == NULL) {
fprintf(stderr, "DONT PRINT THERES AN ERROR");
return;
}
// now you can convert it into a texture
SDL_Texture* texture = SDL_CreateTextureFromSurface(ctx->renderer, surface);
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = surface->w;
rect.h = surface->h;
// last_xpos = surface->w;
SDL_RenderCopy(ctx->renderer, texture, NULL, &rect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
// Not being used right now
// This function is to handle a prompt in case that's something we have to do at some point
void handle_prompt(Context* ctx, int last_xpos, int nrow) {
if (ctx->rline_len > 0) {
draw_line(ctx->readline, last_xpos, nrow * 20, WHITE, ctx);
}
}
void render_scrollback(Context* ctx) {
SDL_RenderClear(ctx->renderer);
SDL_Color current_color = WHITE;
int col_max = 120;
char row_buf[col_max];
int nrow = 0;
int ncol = 0;
// int last_xpos = 0;
Scrollback sb = *ctx->sb;
for (int c = 0; c < sb.length; c++) {
if ((sb.buf[c] == '\n' && ncol > 0) || c == sb.length - 1) {
if (c == sb.length - 1) {
row_buf[ncol++] = sb.buf[c];
}
row_buf[ncol] = '\0';
draw_line(row_buf, 0, nrow * 20, current_color, ctx);
// last_xpos = surface->w;
if (c < sb.length - 1) {
nrow++;
ncol = 0;
}
// Control sequence
} 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;
}
csi_code = substr[0];
SDL_Color new_color = current_color;
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;
// new_color = WHITE;
}
// printf("%d %d\n", i, csi_args[i]);
}
c += (substr) - (sb.buf + c);
current_color = new_color;
break;
}
break;
}
}
} else if (sb.buf[c] != '\n') {
row_buf[ncol++] = sb.buf[c];
}
}
// We drew the scrollback buffer, now draw the user's prompt
SDL_RenderPresent(ctx->renderer);
}
int main(void) {
Scrollback sb = { 0 };
FileDescriptors fds = { 0 };
spawn(&fds);
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
int start_x = 20;
int start_y = 20;
SDL_Window* window = SDL_CreateWindow("bt",
start_x,
start_y,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (window == NULL) {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// SDL_SetRenderDrawColor(renderer, 130, 163, 255, 1);
TTF_Init();
TTF_Font* font = TTF_OpenFont( "./fira.ttf", 16);
struct winsize sz;
// int result = ioctl(fds.parent, TIOCGWINSZ, &sz);
ioctl(fds.parent, TIOCGWINSZ, &sz);
sz.ws_col = 200;
sz.ws_row = 120;
// result = ioctl(fds.parent, TIOCSWINSZ, &sz);
ioctl(fds.parent, TIOCSWINSZ, &sz);
// TODO: It would be nice to figure out how to do this in SDL
// SetTargetFPS(60);
sb.capacity = SCROLLBACK_DEFAULT_SIZE;
sb.buf = malloc(sb.capacity);
sb.buf[0] = '\0';
sb.length = 0;
int flags = fcntl(fds.parent, F_GETFL);
flags |= O_NONBLOCK;
fcntl(fds.parent, F_SETFL, flags);
fd_set rset;
FD_ZERO(&rset);
FD_SET(fds.parent, &rset);
sb.ypos = 0;
Context ctx = {
.sb = &sb,
.font = font,
.renderer = renderer,
.readline = malloc(256),
.rline_len = 0
};
bool new_read = false;
bool new_char = false;
SDL_Event e;
bool quit = false;
while (quit == false) {
while (SDL_PollEvent( &e)) {
if (e.type == SDL_QUIT) {
quit = true;
} else if (e.type == SDL_KEYDOWN) {
char key = e.key.keysym.sym;
// TODO: Translate this all to SDL2
// int key = GetCharPressed();
// TODO: Don't remember why I was doing this
// key = GetCharPressed();
// new_char = true;
if (key == SDL_SCANCODE_F1) {
printf("Debug Printing Scrollback Length %d:\n", sb.length);
printf("%s\n", sb.buf);
printf("============\n");
} else if ((key >= 32) && (key <= 125)) {
// ctx.readline[ctx.rline_len] = (char)key;
// ctx.rline_len++;
// ctx.readline[ctx.rline_len] = '\0';
// printf("%d\n", key);
write(fds.parent, &key, 1);
}
if (key == SDLK_RETURN) {
char k = '\r';
write(fds.parent, &k, 1);
new_char = true;
}
if (key == SDLK_BACKSPACE) {
// if (ctx.rline_len > 0) {
// ctx.rline_len--;
// ctx.readline[ctx.rline_len] = '\0';
// }
char k = '\177';
write(fds.parent, &k, 1);
new_char = true;
}
}
}
// float scroll_speed = 35.5f;
// TODO: We have to convert both these lines to SDL
// sb.ypos += GetMouseWheelMoveV().y * scroll_speed;
// sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y;
// TODO: This is probably related to the font height and scrolling up, we just need
// the "fontsize" which I think is probably the height of the text
if (new_read || new_char) {
// if (sb.height - (float)abs((int)sb.ypos) + fontsize > (float)SCREEN_HEIGHT) {
// sb.ypos = -(sb.height - SCREEN_HEIGHT) - fontsize;
// }
new_read = false;
new_char = false;
} else {
// if (sb.ypos > 0) {
// sb.ypos = 0;
// } else if (abs((int)sb.ypos) > sb.height - fontsize) {
// sb.ypos = -(sb.height - fontsize);
// }
}
int nread = read_pty(&fds, &sb);
if (nread > 0) {
new_read = true;
}
// if (nread > 0) {
render_scrollback(&ctx);
// }
}
free(sb.buf);
free(ctx.readline);
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}