Major revision to get the term emulator working with raylib

This commit is contained in:
Joseph Ferano 2022-09-10 20:39:18 +07:00
parent 115e804f32
commit ced79ea7f0
3 changed files with 115 additions and 95 deletions

View File

@ -1,12 +1,10 @@
P=bt
OBJECTS=
CFLAGS=-g -Wall -Wextra -pedantic -O0
LDLIBS=
CC=gcc
RM=rm -vf
bt: clean
$(CC) $(CFLAGS) -lX11 $(P).c -o $(P)
$(CC) $(CFLAGS) -lraylib $(P).c -o $(P)
clean:
$(RM) $(P)

206
bt.c
View File

@ -1,53 +1,22 @@
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <asm-generic/ioctls.h>
#define _GNU_SOURCE
#include <sys/select.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pty.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <raylib.h>
// PTY
int master;
int slave;
// X11
Display *display;
int screen;
Window win;
GC gc;
int x11fd;
// Scrollback
char *scrollback;
void init_x(void) {
unsigned long black,white;
display = XOpenDisplay((char *) 0);
screen = DefaultScreen(display);
black = BlackPixel(display, screen);
white = WhitePixel(display, screen);
win = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 500, 800, 5, white, black);
XSetStandardProperties(display, win, "The window", "Hello", None, NULL, 0, NULL);
XSelectInput(display, win, ExposureMask|KeyPressMask);
gc = XCreateGC(display, win, 0, 0);
XSetBackground(display, gc, white);
XSetForeground(display, gc, black);
x11fd = ConnectionNumber(display);
XClearWindow(display, win);
XMapRaised(display, win);
}
void close_x(void) {
XFreeGC(display, gc);
XDestroyWindow(display, win);
XCloseDisplay(display);
exit(0);
}
void spawn(void) {
openpty(&master, &slave, NULL, NULL, NULL);
pid_t p = fork();
@ -60,74 +29,127 @@ void spawn(void) {
dup2(slave, 1);
dup2(slave, 2);
close(slave);
execle("/bin/dash", "-/bin/dash", (char *)NULL, (char *[]){ "TERM=dumb", NULL });
exit(0);
} else {
close(slave);
}
}
void update(void) {
XEvent event;
KeySym key;
char buf[1];
char x11inputbuf[255];
fd_set readable;
/* char tmp[512]; */
scrollback = malloc(2048);
scrollback[0] = '\0';
int sbcount = 0;
int maxfd = master > x11fd ? master : x11fd;
float fontsize = 12;
for (;;) {
FD_ZERO(&readable);
FD_SET(master, &readable);
FD_SET(x11fd, &readable);
select(maxfd + 1, &readable, NULL, NULL, NULL);
if (FD_ISSET(master, &readable)) {
read(master, buf, 1);
scrollback[sbcount++] = *buf;
scrollback[sbcount] = '\0';
XClearWindow(display, win);
char *curr = scrollback;
while (*curr) {
if (!iscntrl(*curr)) {
XSetForeground(display, gc, 255);
XDrawString(display, win, gc, 10, 10, curr, sbcount);
}
curr++;
}
Font *load_font() {
unsigned int file_size = 0;
char *font = "./fira.ttf";
unsigned char *fontdata = LoadFileData(font, &file_size);
Font *fontDefault = calloc(1, sizeof(Font));
fontDefault->baseSize = fontsize;
fontDefault->glyphCount = 95;
fontDefault->glyphs = LoadFontData(fontdata, file_size, 16, 0, 95, FONT_DEFAULT);
Image atlas = GenImageFontAtlas(fontDefault->glyphs, &fontDefault->recs, 95, 16, 4, 0);
fontDefault->texture = LoadTextureFromImage(atlas);
UnloadImage(atlas);
return fontDefault;
}
int read_pty(char *buf) {
ssize_t nread;
char readbuf[256];
switch (nread = read(master, readbuf, sizeof(readbuf))) {
case -1:
if (errno == EAGAIN) {
return 0;
} else {
return 0;
}
while (XPending(display)) {
XNextEvent(display, &event);
switch (event.type) {
// This gets called when the window is resized
case Expose: // Resized
XClearWindow(display, win);
break;
case KeyPress:
if (XLookupString(&event.xkey,x11inputbuf,255,&key,0) == 1) {
switch (x11inputbuf[0]) {
case 'q':
close_x();
break;
default:
write(master, &x11inputbuf[0], 1);
}
}
break;
}
case 0:
printf("EOF\n");
return 0;
default:
if (readbuf[nread - 1] == '\n') {
printf("this\n");
nread--;
}
readbuf[nread] = '\0';
strcat(buf, readbuf);
printf("Amount of bytes read %lu\n", nread);
printf("FD: %i Buf %s\n", master, buf);
return nread;
}
}
int main(void) {
init_x();
spawn();
update();
const int screenWidth = 800;
const int screenHeight = 550;
InitWindow(screenWidth, screenHeight, "basic term");
SetTargetFPS(120);
Font *fontDefault = load_font();
scrollback = malloc(4098);
scrollback[0] = '\0';
int sb_len = 0;
char buf[128];
int buf_len = 0;
int flags = fcntl(master, F_GETFL);
flags |= O_NONBLOCK;
fcntl(master, F_SETFL, flags);
fd_set rset;
FD_ZERO(&rset);
FD_SET(master, &rset);
while (!WindowShouldClose()) {
int key = GetCharPressed();
fontsize += GetMouseWheelMove();
while (key > 0) {
if ((key >= 32) && (key <= 125)) {
buf[buf_len] = (char)key;
buf[buf_len + 1] = '\0';
buf_len++;
scrollback[sb_len] = (char)key;
scrollback[sb_len + 1] = '\0';
sb_len++;
}
key = GetCharPressed();
}
if (IsKeyPressed(KEY_ENTER)) {
sb_len -= buf_len;
scrollback[sb_len] = '\0';
write(master, buf, buf_len);
write(master, "\r", 1);
buf[0] = '\0';
buf_len = 0;
}
if (IsKeyPressed(KEY_BACKSPACE)) {
if (buf_len > 0) {
buf_len--;
buf[buf_len] = '\0';
sb_len--;
scrollback[sb_len] = '\0';
}
}
// Drawing
BeginDrawing();
/* update(buf, &buf_len, rset); */
{
int nread = read_pty(scrollback);
if (nread > 0) {
/* printf("Amount of bytes read %i\n", nread); */
sb_len += nread;
}
ClearBackground(RAYWHITE);
DrawTextEx(*fontDefault, scrollback, (Vector2){ 10 , 10 }, fontsize, 1, BLACK);
}
EndDrawing();
}
close(master);
CloseWindow();
return 0;
}

BIN
fira.ttf Normal file

Binary file not shown.