summaryrefslogtreecommitdiff
path: root/framebuffer.c
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-06-21 13:51:24 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-06-21 13:51:24 +0300
commit3fed5969034c464e36358fc45a99b57b1f308c11 (patch)
tree874dfffb1612d63b03b0198d229f16fe52b96d6f /framebuffer.c
parent8e4a57e034d98777fc58ff90c542c96f18f0e575 (diff)
downloadwafflestone-3fed5969034c464e36358fc45a99b57b1f308c11.tar.xz
term_init() and term_cleanup(), mostly
Diffstat (limited to 'framebuffer.c')
-rw-r--r--framebuffer.c60
1 files changed, 57 insertions, 3 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 */
+}