fix: now working on glibc

fixed a bug were stdin was set to nonblocking and never reset back to blocking.
This caused STDOUT to become nonblock, and for some reason glibc cannot deal
with it, but every other libc could. uh.
This commit is contained in:
violette 2025-05-13 00:48:04 -04:00
parent be90134ebe
commit 4f8fde9eb5
2 changed files with 58 additions and 51 deletions

View file

@ -1,7 +1,7 @@
CFLAGS = -lm CFLAGS = -lm
build: build:
cc ./main.c $(CFLAGS) -o ./cube cc ./main.c $(CFLAGS) -DVBUF -o ./cube
run: build run: build
./cube ./cube

107
main.c
View file

@ -7,6 +7,7 @@
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
*/ */
#include <sys/ioctl.h>
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
@ -17,7 +18,6 @@
#include <float.h> #include <float.h>
#include "json.h" #include "json.h"
#define VBUF 1
#define SCREEN_WIDTH 80 #define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 32 #define SCREEN_HEIGHT 32
#define CUBE_WIDTH 10 #define CUBE_WIDTH 10
@ -71,7 +71,7 @@ double interpolationStep = 0;
double zBuffer[SCREEN_HEIGHT * SCREEN_WIDTH]; double zBuffer[SCREEN_HEIGHT * SCREEN_WIDTH];
char output[SCREEN_HEIGHT * SCREEN_WIDTH]; char output[SCREEN_HEIGHT * SCREEN_WIDTH];
#ifdef VBUF #ifdef VBUF
char buf[SCREEN_HEIGHT * SCREEN_WIDTH + 30 * 20]; // stdout buffer char buf[SCREEN_HEIGHT * SCREEN_WIDTH * 7 * 2]; // stdout buffer
#endif #endif
static volatile char shouldBreak = 1; static volatile char shouldBreak = 1;
@ -94,7 +94,7 @@ struct Quaternions
mult(struct Quaternions q, double x, double y, double z) mult(struct Quaternions q, double x, double y, double z)
{ {
//p = q * p * qbar //p = q * p * qbar
struct Quaternions res; struct Quaternions res;
res.w = 0; res.w = 0;
res.x = x * (SQ(q.w) + SQ(q.x) - SQ(q.y) - SQ(q.z)) res.x = x * (SQ(q.w) + SQ(q.x) - SQ(q.y) - SQ(q.z))
@ -154,7 +154,7 @@ chooseColor(char c)
return " "; return " ";
default: default:
//unused, but issues no warning //unused, but issues no warning
return ""; return "\033[0m";
} }
} }
@ -181,29 +181,29 @@ chooseMainFace()
} }
void void
printFrontFace(struct json_object_element_s *elt) printFrontFace(struct json_object_element_s *elt, int row)
{ {
printf("Contacts:\r\n"); printf("\033[%dCContacts:\r\n", row);
struct json_object_element_s *obj = json_value_as_object(elt->value)->start; struct json_object_element_s *obj = json_value_as_object(elt->value)->start;
while (obj != NULL) { while (obj != NULL) {
struct json_string_s *str = json_value_as_string(obj->value); struct json_string_s *str = json_value_as_string(obj->value);
printf(" %s: %s\r\n", obj->name->string, str->string); printf("\033[%dC%s: %s\r\n", row + 4, obj->name->string, str->string);
obj = obj->next; obj = obj->next;
} }
} }
void void
printLefttFace(struct json_object_element_s *elt) printLefttFace(struct json_object_element_s *elt, int row)
{ {
printf("Skills:\r\n"); printf("\033[%dCSkills:\r\n", row);
struct json_array_element_s *arr = json_value_as_array(elt->value)->start; struct json_array_element_s *arr = json_value_as_array(elt->value)->start;
while (arr != NULL) { while (arr != NULL) {
struct json_object_element_s *obj = json_value_as_object(arr->value)->start; struct json_object_element_s *obj = json_value_as_object(arr->value)->start;
printf(" %s\r\n", json_value_as_string(obj->value)->string); printf("\033[%dC%s\r\n", row + 4, json_value_as_string(obj->value)->string);
struct json_array_element_s *arr2 = json_value_as_array(obj->next->value)->start; struct json_array_element_s *arr2 = json_value_as_array(obj->next->value)->start;
while (arr2 != NULL) { while (arr2 != NULL) {
struct json_string_s *str = json_value_as_string(arr2->value); struct json_string_s *str = json_value_as_string(arr2->value);
printf(" %s\r\n", str->string); printf("\033[%dC%s\r\n", row + 8, str->string);
arr2 = arr2->next; arr2 = arr2->next;
} }
arr = arr->next; arr = arr->next;
@ -211,21 +211,21 @@ printLefttFace(struct json_object_element_s *elt)
} }
void void
printRightFace(struct json_object_element_s *elt) printRightFace(struct json_object_element_s *elt, int row)
{ {
printf("Languages:\r\n"); printf("\033[%dCLanguages:\r\n", row);
struct json_array_element_s *arr = json_value_as_array(elt->value)->start; struct json_array_element_s *arr = json_value_as_array(elt->value)->start;
while (arr != NULL) { while (arr != NULL) {
struct json_object_element_s *obj = json_value_as_object(arr->value)->start; struct json_object_element_s *obj = json_value_as_object(arr->value)->start;
printf(" %s: %s / 5\r\n", json_value_as_string(obj->value)->string, json_value_as_number(obj->next->value)->number); printf("\033[%dC%s: %s / 5\r\n", row + 4, json_value_as_string(obj->value)->string, json_value_as_number(obj->next->value)->number);
arr = arr->next; arr = arr->next;
} }
} }
void void
printBottomFace(struct json_object_element_s *elt) printBottomFace(struct json_object_element_s *elt, int row)
{ {
printf("Professional experience:\r\n"); printf("\033[%dCProfessional experience:\r\n", row);
struct json_array_element_s *arr = json_value_as_array(elt->value)->start; struct json_array_element_s *arr = json_value_as_array(elt->value)->start;
while (arr != NULL) { while (arr != NULL) {
struct json_object_element_s *obj = json_value_as_object(arr->value)->start; struct json_object_element_s *obj = json_value_as_object(arr->value)->start;
@ -235,9 +235,9 @@ printBottomFace(struct json_object_element_s *elt)
struct json_string_s *to = json_value_as_string(obj->next->next->next->value); struct json_string_s *to = json_value_as_string(obj->next->next->next->value);
struct json_array_element_s *descr = json_value_as_array(obj->next->next->next->next->value)->start; struct json_array_element_s *descr = json_value_as_array(obj->next->next->next->next->value)->start;
printf(" %s: %s ; %s -> %s\r\n", json_value_as_string(company->value)->string, position->string, from->string, to->string); printf("\033[%dC%s: %s ; %s -> %s\r\n", row + 4, json_value_as_string(company->value)->string, position->string, from->string, to->string);
while (descr != NULL) { while (descr != NULL) {
printf(" %s\r\n", json_value_as_string(descr->value)->string); printf("\033[%dC%s\r\n", row + 8, json_value_as_string(descr->value)->string);
descr = descr->next; descr = descr->next;
} }
printf("\r\n"); printf("\r\n");
@ -246,13 +246,13 @@ printBottomFace(struct json_object_element_s *elt)
} }
void void
printBackFace(struct json_object_element_s *elt) printBackFace(struct json_object_element_s *elt, int row)
{ {
printf("Projects:\r\n"); printf("\033[%dCProjects:\r\n", row);
struct json_object_element_s *obj = json_value_as_object(elt->value)->start; struct json_object_element_s *obj = json_value_as_object(elt->value)->start;
//ignore personnal projects //ignore personnal projects
printf(" - This cube! its made using Quaternion for 3d rotation, " printf("\033[%dC- This cube! its made using Quaternion for 3d rotation, "
"from scratch (except the CV text), in C\r\n"); "from scratch (except the CV text), in C\r\n", row + 4);
printf("\r\n"); printf("\r\n");
struct json_array_element_s *arr = json_value_as_array(obj->next->value)->start; struct json_array_element_s *arr = json_value_as_array(obj->next->value)->start;
while (arr != NULL) { while (arr != NULL) {
@ -261,9 +261,9 @@ printBackFace(struct json_object_element_s *elt)
struct json_array_element_s *descr = json_value_as_array(json_value_as_object(arr->value)->start->next->value)->start; struct json_array_element_s *descr = json_value_as_array(json_value_as_object(arr->value)->start->next->value)->start;
struct json_number_s *year = json_value_as_number(json_value_as_object(arr->value)->start->next->next->next->value); struct json_number_s *year = json_value_as_number(json_value_as_object(arr->value)->start->next->next->next->value);
printf(" - %s, %s\r\n", json_value_as_string(descr->value)->string, year->number); printf("\033[%dC- %s, %s\r\n", row + 4, json_value_as_string(descr->value)->string, year->number);
while (descr != NULL) { while (descr != NULL) {
printf(" o %s\r\n", json_value_as_string(descr->value)->string); printf("\033[%dCo %s\r\n", row + 8, json_value_as_string(descr->value)->string);
descr = descr->next; descr = descr->next;
} }
printf("\r\n"); printf("\r\n");
@ -272,9 +272,9 @@ printBackFace(struct json_object_element_s *elt)
} }
void void
printTopFace(struct json_object_element_s *elt) printTopFace(struct json_object_element_s *elt, int row)
{ {
printf("Education:\r\n"); printf("\033[%dCEducation:\r\n", row);
struct json_array_element_s *arr = json_value_as_array(elt->value)->start; struct json_array_element_s *arr = json_value_as_array(elt->value)->start;
while (arr != NULL) { while (arr != NULL) {
struct json_object_element_s *obj = json_value_as_object(arr->value)->start; struct json_object_element_s *obj = json_value_as_object(arr->value)->start;
@ -289,18 +289,18 @@ printTopFace(struct json_object_element_s *elt)
struct json_string_s *to = struct json_string_s *to =
json_value_as_string(obj->next->next->next->next->next->value); json_value_as_string(obj->next->next->next->next->next->value);
printf(" %s: %s -> %s \r\n", univ_name->string, from->string, to->string); printf("\033[%dC%s: %s -> %s \r\n", row + 4, univ_name->string, from->string, to->string);
if (track == NULL || strcmp(track->string, "None") == 0) if (track == NULL || strcmp(track->string, "None") == 0)
printf(" %s, %s\r\n", degree->string, major->string); printf("\033[%dC%s, %s\r\n", row + 8, degree->string, major->string);
else else
printf(" %s %s, %s\r\n", degree->string, major->string, track->string); printf("\033[%dC%s %s, %s\r\n", row + 8, degree->string, major->string, track->string);
printf("\r\n"); printf("\r\n");
arr = arr->next; arr = arr->next;
} }
} }
void void
printCV(char face, struct json_object_element_s *r) printCV(char face, struct json_object_element_s *r, int row)
{ {
struct json_object_element_s start = *r; struct json_object_element_s start = *r;
struct json_object_element_s *next = &start; struct json_object_element_s *next = &start;
@ -310,25 +310,25 @@ printCV(char face, struct json_object_element_s *r)
for (; k < face; ++k) for (; k < face; ++k)
next = next->next; next = next->next;
char *res; char *res;
switch (face) { switch (face) {
case FACE_FRONT: case FACE_FRONT:
printFrontFace(next); printFrontFace(next, row);
break; break;
case FACE_LEFT: case FACE_LEFT:
printLefttFace(next); printLefttFace(next, row);
break; break;
case FACE_RIGHT: case FACE_RIGHT:
printRightFace(next); printRightFace(next, row);
break; break;
case FACE_BOTTOM: case FACE_BOTTOM:
printBottomFace(next); printBottomFace(next, row);
break; break;
case FACE_BACK: case FACE_BACK:
printBackFace(next); printBackFace(next, row);
break; break;
case FACE_TOP: case FACE_TOP:
printTopFace(next); printTopFace(next, row);
break; break;
default: default:
break; break;
@ -340,14 +340,21 @@ printAscii(struct json_value_s *json)
{ {
char face = chooseMainFace(); char face = chooseMainFace();
struct json_object_element_s *root = json_value_as_object(json)->start; struct json_object_element_s *root = json_value_as_object(json)->start;
printCV(face, root);
for (int k = 0; k < SCREEN_WIDTH * SCREEN_HEIGHT; ++k) struct winsize w;
printf("%s", k % SCREEN_WIDTH ? chooseColor(output[k]) : "\r\n"); ioctl(0, TIOCGWINSZ, &w);
printf("\r\n");
#ifdef VBUF printCV(face, root, w.ws_row);
printf("\033[%d;%dH", (int)((w.ws_row - SCREEN_HEIGHT) * 1.5 / 2), (w.ws_col - SCREEN_WIDTH) / 2);
for (int k = 0; k < SCREEN_WIDTH * SCREEN_HEIGHT; ++k) {
if (k % SCREEN_WIDTH)
printf("%s", chooseColor(output[k]));
else
printf("\r\n\033[%dC", w.ws_row);
}
fflush(stdout); fflush(stdout);
#endif
} }
void void
@ -363,10 +370,9 @@ rotateCube(double cubeX, double cubeY, double cubeZ, char ch)
int screenY = (int)(SCREEN_HEIGHT / 2) + floor(((y) * K1 / 2) * invZ); int screenY = (int)(SCREEN_HEIGHT / 2) + floor(((y) * K1 / 2) * invZ);
//TODO luminescence //TODO luminescence
if (screenX > SCREEN_WIDTH || screenX < 0) if (screenX > SCREEN_WIDTH || screenX < 0) return;
return;
int idx = screenY * SCREEN_WIDTH + screenX; int idx = screenY * SCREEN_WIDTH + screenX;
if (idx >= 0 && idx < SCREEN_WIDTH * SCREEN_HEIGHT) if (idx >= 0 && idx < SCREEN_WIDTH * SCREEN_HEIGHT)
if (zBuffer[idx] < invZ) { if (zBuffer[idx] < invZ) {
zBuffer[idx] = invZ; zBuffer[idx] = invZ;
@ -379,6 +385,7 @@ intHandler(int unused)
{ {
shouldBreak = 0; shouldBreak = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &originalTerm); tcsetattr(STDIN_FILENO, TCSANOW, &originalTerm);
printf("\033[2J\033[?1049l"); // escape fullscreen mode
} }
void void
@ -496,12 +503,11 @@ handleAngle(char *input)
char char
getInput(char *keyboardInput) getInput(char *keyboardInput)
{ {
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
char c = getchar(); char c = getchar();
if (c == '\033') { if (c == '\033') {
if ((c = getchar()) == '[') if ((c = getchar()) == '[')
switch (c = getchar()) { switch (c = getchar()) {
//the real value
case 'A': case 'A':
c = 'W'; c = 'W';
break; break;
@ -521,7 +527,7 @@ getInput(char *keyboardInput)
*keyboardInput = c; *keyboardInput = c;
while ((c = getchar()) != '\n' && c != EOF) {} //clean stdin while ((c = getchar()) != '\n' && c != EOF) {} //clean stdin
// //
fcntl(STDIN_FILENO, F_SETFL, ~O_NONBLOCK); fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) & ~O_NONBLOCK);
handleAngle(keyboardInput); handleAngle(keyboardInput);
return c; return c;
} }
@ -547,6 +553,7 @@ readJson()
int int
main() main()
{ {
printf("\033[?1049h\033[H"); // enter fullscreen mode
char keyboardInput = 0; char keyboardInput = 0;
signal(SIGINT, intHandler); signal(SIGINT, intHandler);
tcgetattr(STDIN_FILENO, &originalTerm); tcgetattr(STDIN_FILENO, &originalTerm);
@ -583,7 +590,7 @@ main()
} }
} }
printf("\x1b[2J"); printf("\033[1;1H\033[2J");
printAscii(json); printAscii(json);
getInput(&keyboardInput); getInput(&keyboardInput);
usleep(1000000 / 60); // 60fps max usleep(1000000 / 60); // 60fps max