diff options
| author | Kirill Petrashin <kirill8201@yandex.ru> | 2026-06-21 13:51:24 +0300 |
|---|---|---|
| committer | Kirill Petrashin <kirill8201@yandex.ru> | 2026-06-21 13:51:24 +0300 |
| commit | 3fed5969034c464e36358fc45a99b57b1f308c11 (patch) | |
| tree | 874dfffb1612d63b03b0198d229f16fe52b96d6f | |
| parent | 8e4a57e034d98777fc58ff90c542c96f18f0e575 (diff) | |
| download | wafflestone-3fed5969034c464e36358fc45a99b57b1f308c11.tar.xz | |
term_init() and term_cleanup(), mostly
| -rw-r--r-- | framebuffer.c | 60 | ||||
| -rw-r--r-- | framebuffer.h | 15 | ||||
| -rw-r--r-- | main.c | 10 |
3 files changed, 78 insertions, 7 deletions
diff --git a/framebuffer.c b/framebuffer.c index b6e922e..66b8c2f 100644 --- a/framebuffer.c +++ b/framebuffer.c @@ -1,7 +1,14 @@ #include <stdlib.h> +#include <termios.h> +#include <unistd.h> +#include <sys/ioctl.h> #include "framebuffer.h" +size_t term_width = 0, + term_height = 0; +struct termios initial_terminal_state = {0}; + Framebuffer fb_new(size_t width, size_t height) { Framebuffer fb; @@ -52,8 +59,55 @@ void inline fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel) { fb.fb[row][col] = pixel; } -void fb_init(void); +void term_init(void) { + tcgetattr(0, &initial_terminal_state); + + struct termios new = initial_terminal_state; + + new.c_lflag &= ~ICANON; /* Turn off canonical mode */ + new.c_lflag &= ~ECHO; /* Turn off echoing */ + new.c_cc[VMIN] = 1; /* One char at a time */ + new.c_cc[VTIME] = 0; /* No input timeout */ + tcsetattr(0, TCSANOW, &new); + + write(STDOUT_FILENO, "\x1b[?1049h", 8); /* Alternative screen buffer */ + write(STDOUT_FILENO, "\x1b[H", 3); /* Move cursor to top-left */ + write(STDOUT_FILENO, "\x1b[?25l", 6); /* Hide the cursor */ + + term_update_size(); +} + +void term_cleanup(void) { + tcsetattr(0, TCSANOW, &initial_terminal_state); + + write(STDOUT_FILENO, "\x1b[?25h", 6); /* Show the cursor */ + write(STDOUT_FILENO, "\x1b[?1049l", 8); /* Alternative screen buffer */ +} + +void term_update_size(void) { +#if defined(TIOCGWINSZ) + struct winsize ws; + + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0) { + term_height = ws.ws_row; + term_width = ws.ws_col; + return; + } +#elif defined(TIOCGSIZE) + struct ttysize ts; + + if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) == 0) { + term_height = ts.ts_row; + term_width = ts.ts_col; + return; + } +#else + #error "Neither TIOCGWINSZ or TIOCGSIZE are defined, can't proceed" +#endif +} -void fb_cleanup(void); +void fb_print(Framebuffer fb) { + write(STDOUT_FILENO, "\x1b[H", 3); /* Move cursor to top-left */ -void fb_print(Framebuffer fb); + /* TODO: finish me */ +} diff --git a/framebuffer.h b/framebuffer.h index 1b0254f..373cfb4 100644 --- a/framebuffer.h +++ b/framebuffer.h @@ -5,8 +5,9 @@ #include <stddef.h> /* wchar_t */ #include <sys/types.h> /* ssize_t */ -/* Globals for the terminal size, set by fb_init() */ -extern size_t TERM_WIDTH, TERM_HEIGHT; +/* Globals for the terminal size, set by term_update_size() */ +extern size_t term_width, term_height; +extern struct termios initial_terminal_state; /* Represents a 24 bit colour, 8 bit per channel */ typedef struct Colour_s { @@ -60,12 +61,18 @@ void fb_fill(Framebuffer fb, Pixel pixel); void inline fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel); /* Initializes the terminal and sets TERM_WIDTH and TERM_HEIGHT */ -void fb_init(void); +void term_init(void); /* Undoes everything fb_init() did */ -void fb_cleanup(void); +void term_cleanup(void); + +/* Updates term_width and term_height */ +void term_update_size(void); /* Prints fb to the screen */ void fb_print(Framebuffer fb); +/* c is a byte */ +#define get_input(c) read(STDIN_FILENO, &c, 1) + #endif /* FRAMEBUFFER_H_ */ @@ -1,5 +1,15 @@ #include <stdio.h> +#include <unistd.h> + +#include "framebuffer.h" int main(void) { + term_init(); + fprintf(stderr, "Not yet implemented\n"); + + char c; + get_input(c); + + term_cleanup(); } |
