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 P=bt
OBJECTS=
CFLAGS=-g -Wall -Wextra -pedantic -O0 CFLAGS=-g -Wall -Wextra -pedantic -O0
LDLIBS=
CC=gcc CC=gcc
RM=rm -vf RM=rm -vf
bt: clean bt: clean
$(CC) $(CFLAGS) -lX11 $(P).c -o $(P) $(CC) $(CFLAGS) -lraylib $(P).c -o $(P)
clean: clean:
$(RM) $(P) $(RM) $(P)

202
bt.c
View File

@ -1,53 +1,22 @@
#include <X11/X.h> #define _GNU_SOURCE
#include <X11/Xlib.h> #include <sys/select.h>
#include <X11/Xutil.h> #include <unistd.h>
#include <X11/Xos.h>
#include <asm-generic/ioctls.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pty.h> #include <pty.h>
#include <ctype.h> #include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <raylib.h>
// PTY // PTY
int master; int master;
int slave; int slave;
// X11
Display *display;
int screen;
Window win;
GC gc;
int x11fd;
// Scrollback // Scrollback
char *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) { void spawn(void) {
openpty(&master, &slave, NULL, NULL, NULL); openpty(&master, &slave, NULL, NULL, NULL);
pid_t p = fork(); pid_t p = fork();
@ -60,74 +29,127 @@ void spawn(void) {
dup2(slave, 1); dup2(slave, 1);
dup2(slave, 2); dup2(slave, 2);
close(slave);
execle("/bin/dash", "-/bin/dash", (char *)NULL, (char *[]){ "TERM=dumb", NULL }); execle("/bin/dash", "-/bin/dash", (char *)NULL, (char *[]){ "TERM=dumb", NULL });
exit(0);
} else { } else {
close(slave); close(slave);
} }
} }
void update(void) { float fontsize = 12;
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;
for (;;) { Font *load_font() {
FD_ZERO(&readable); unsigned int file_size = 0;
FD_SET(master, &readable); char *font = "./fira.ttf";
FD_SET(x11fd, &readable); unsigned char *fontdata = LoadFileData(font, &file_size);
select(maxfd + 1, &readable, NULL, NULL, NULL); Font *fontDefault = calloc(1, sizeof(Font));
if (FD_ISSET(master, &readable)) { fontDefault->baseSize = fontsize;
read(master, buf, 1); fontDefault->glyphCount = 95;
scrollback[sbcount++] = *buf;
scrollback[sbcount] = '\0'; fontDefault->glyphs = LoadFontData(fontdata, file_size, 16, 0, 95, FONT_DEFAULT);
XClearWindow(display, win); Image atlas = GenImageFontAtlas(fontDefault->glyphs, &fontDefault->recs, 95, 16, 4, 0);
char *curr = scrollback; fontDefault->texture = LoadTextureFromImage(atlas);
while (*curr) { UnloadImage(atlas);
if (!iscntrl(*curr)) { return fontDefault;
XSetForeground(display, gc, 255);
XDrawString(display, win, gc, 10, 10, curr, sbcount);
} }
curr++;
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;
} }
} case 0:
while (XPending(display)) { printf("EOF\n");
XNextEvent(display, &event); return 0;
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: default:
write(master, &x11inputbuf[0], 1); if (readbuf[nread - 1] == '\n') {
} printf("this\n");
} nread--;
break;
}
} }
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) { int main(void) {
init_x();
spawn(); spawn();
update(); const int screenWidth = 800;
return 0; 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.