···
+
#include "../flanterm.h"
void *memset(void *, int, size_t);
void *memcpy(void *, const void *, size_t);
···
+
static void flanterm_fb_save_state(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->saved_state_text_fg = ctx->text_fg;
ctx->saved_state_text_bg = ctx->text_bg;
ctx->saved_state_cursor_x = ctx->cursor_x;
ctx->saved_state_cursor_y = ctx->cursor_y;
+
static void flanterm_fb_restore_state(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_fg = ctx->saved_state_text_fg;
ctx->text_bg = ctx->saved_state_text_bg;
ctx->cursor_x = ctx->saved_state_cursor_x;
ctx->cursor_y = ctx->saved_state_cursor_y;
+
static void flanterm_fb_swap_palette(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
uint32_t tmp = ctx->text_bg;
ctx->text_bg = ctx->text_fg;
+
static void plot_char(struct flanterm_context *_ctx, struct flanterm_fb_char *c, size_t x, size_t y) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
if (x >= _ctx->cols || y >= _ctx->rows) {
···
+
static void plot_char_fast(struct flanterm_context *_ctx, struct flanterm_fb_char *old, struct flanterm_fb_char *c, size_t x, size_t y) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
if (x >= _ctx->cols || y >= _ctx->rows) {
···
+
static inline bool compare_char(struct flanterm_fb_char *a, struct flanterm_fb_char *b) {
return !(a->c != b->c || a->bg != b->bg || a->fg != b->fg);
+
static void push_to_queue(struct flanterm_context *_ctx, struct flanterm_fb_char *c, size_t x, size_t y) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
if (x >= _ctx->cols || y >= _ctx->rows) {
···
size_t i = y * _ctx->cols + x;
+
struct flanterm_fb_queue_item *q = ctx->map[i];
if (compare_char(&ctx->grid[i], c)) {
···
+
static void flanterm_fb_revscroll(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
for (size_t i = (_ctx->scroll_bottom_margin - 1) * _ctx->cols - 1;
i >= _ctx->scroll_top_margin * _ctx->cols; i--) {
+
struct flanterm_fb_char *c;
+
struct flanterm_fb_queue_item *q = ctx->map[i];
···
// Clear the first line of the screen.
+
struct flanterm_fb_char empty;
···
+
static void flanterm_fb_scroll(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
for (size_t i = (_ctx->scroll_top_margin + 1) * _ctx->cols;
i < _ctx->scroll_bottom_margin * _ctx->cols; i++) {
+
struct flanterm_fb_char *c;
+
struct flanterm_fb_queue_item *q = ctx->map[i];
···
// Clear the last line of the screen.
+
struct flanterm_fb_char empty;
···
+
static void flanterm_fb_clear(struct flanterm_context *_ctx, bool move) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
+
struct flanterm_fb_char empty;
···
+
static void flanterm_fb_set_cursor_pos(struct flanterm_context *_ctx, size_t x, size_t y) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
···
+
static void flanterm_fb_get_cursor_pos(struct flanterm_context *_ctx, size_t *x, size_t *y) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
*x = ctx->cursor_x >= _ctx->cols ? _ctx->cols - 1 : ctx->cursor_x;
*y = ctx->cursor_y >= _ctx->rows ? _ctx->rows - 1 : ctx->cursor_y;
+
static void flanterm_fb_move_character(struct flanterm_context *_ctx, size_t new_x, size_t new_y, size_t old_x, size_t old_y) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
if (old_x >= _ctx->cols || old_y >= _ctx->rows
|| new_x >= _ctx->cols || new_y >= _ctx->rows) {
···
size_t i = old_x + old_y * _ctx->cols;
+
struct flanterm_fb_char *c;
+
struct flanterm_fb_queue_item *q = ctx->map[i];
···
push_to_queue(_ctx, c, new_x, new_y);
+
static void flanterm_fb_set_text_fg(struct flanterm_context *_ctx, size_t fg) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_fg = ctx->ansi_colours[fg];
+
static void flanterm_fb_set_text_bg(struct flanterm_context *_ctx, size_t bg) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_bg = ctx->ansi_colours[bg];
+
static void flanterm_fb_set_text_fg_bright(struct flanterm_context *_ctx, size_t fg) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_fg = ctx->ansi_bright_colours[fg];
+
static void flanterm_fb_set_text_bg_bright(struct flanterm_context *_ctx, size_t bg) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_bg = ctx->ansi_bright_colours[bg];
+
static void flanterm_fb_set_text_fg_rgb(struct flanterm_context *_ctx, uint32_t fg) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
+
static void flanterm_fb_set_text_bg_rgb(struct flanterm_context *_ctx, uint32_t bg) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
+
static void flanterm_fb_set_text_fg_default(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_fg = ctx->default_fg;
+
static void flanterm_fb_set_text_bg_default(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_bg = 0xffffffff;
+
static void flanterm_fb_set_text_fg_default_bright(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_fg = ctx->default_fg_bright;
+
static void flanterm_fb_set_text_bg_default_bright(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
ctx->text_bg = ctx->default_bg_bright;
+
static void draw_cursor(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
if (ctx->cursor_x >= _ctx->cols || ctx->cursor_y >= _ctx->rows) {
···
size_t i = ctx->cursor_x + ctx->cursor_y * _ctx->cols;
+
struct flanterm_fb_char c;
+
struct flanterm_fb_queue_item *q = ctx->map[i];
···
+
static void flanterm_fb_double_buffer_flush(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
if (_ctx->cursor_enabled) {
for (size_t i = 0; i < ctx->queue_i; i++) {
+
struct flanterm_fb_queue_item *q = &ctx->queue[i];
size_t offset = q->y * _ctx->cols + q->x;
if (ctx->map[offset] == NULL) {
+
struct flanterm_fb_char *old = &ctx->grid[offset];
if (q->c.bg == old->bg && q->c.fg == old->fg) {
plot_char_fast(_ctx, old, &q->c, q->x, q->y);
···
+
static void flanterm_fb_raw_putchar(struct flanterm_context *_ctx, uint8_t c) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
if (ctx->cursor_x >= _ctx->cols && (ctx->cursor_y < _ctx->scroll_bottom_margin - 1 || _ctx->scroll_enabled)) {
if (ctx->cursor_y == _ctx->scroll_bottom_margin) {
+
flanterm_fb_scroll(_ctx);
if (ctx->cursor_y >= _ctx->cols) {
ctx->cursor_y = _ctx->cols - 1;
+
struct flanterm_fb_char ch;
push_to_queue(_ctx, &ch, ctx->cursor_x++, ctx->cursor_y);
+
static void flanterm_fb_full_refresh(struct flanterm_context *_ctx) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
for (size_t y = 0; y < ctx->height; y++) {
for (size_t x = 0; x < ctx->width; x++) {
···
+
static void flanterm_fb_deinit(struct flanterm_context *_ctx, void (*_free)(void *, size_t)) {
+
struct flanterm_fb_context *ctx = (void *)_ctx;
_free(ctx->font_bits, ctx->font_bits_size);
_free(ctx->font_bool, ctx->font_bool_size);
···
_free(ctx->queue, ctx->queue_size);
_free(ctx->map, ctx->map_size);
_free(ctx->canvas, ctx->canvas_size);
+
_free(ctx, sizeof(struct flanterm_fb_context));
+
struct flanterm_context *flanterm_fb_init(
void *(*_malloc)(size_t),
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
···
size_t font_scale_x, size_t font_scale_y,
+
struct flanterm_fb_context *ctx = _malloc(sizeof(struct flanterm_fb_context));
+
struct flanterm_context *_ctx = (void *)ctx;
+
memset(ctx, 0, sizeof(struct flanterm_fb_context));
if (ansi_colours != NULL) {
memcpy(ctx->ansi_colours, ansi_colours, sizeof(ctx->ansi_colours));
···
+
#define FONT_BYTES ((font_width * font_height * FLANTERM_FB_FONT_GLYPHS) / 8)
ctx->font_width = font_width;
···
ctx->font_width += font_spacing;
+
ctx->font_bool_size = FLANTERM_FB_FONT_GLYPHS * font_height * ctx->font_width * sizeof(bool);
ctx->font_bool = _malloc(ctx->font_bool_size);
+
for (size_t i = 0; i < FLANTERM_FB_FONT_GLYPHS; i++) {
uint8_t *glyph = &ctx->font_bits[i * font_height];
for (size_t y = 0; y < font_height; y++) {
···
ctx->offset_x = margin + ((ctx->width - margin * 2) % ctx->glyph_width) / 2;
ctx->offset_y = margin + ((ctx->height - margin * 2) % ctx->glyph_height) / 2;
+
ctx->grid_size = _ctx->rows * _ctx->cols * sizeof(struct flanterm_fb_char);
ctx->grid = _malloc(ctx->grid_size);
for (size_t i = 0; i < _ctx->rows * _ctx->cols; i++) {
···
ctx->grid[i].bg = ctx->text_bg;
+
ctx->queue_size = _ctx->rows * _ctx->cols * sizeof(struct flanterm_fb_queue_item);
ctx->queue = _malloc(ctx->queue_size);
memset(ctx->queue, 0, ctx->queue_size);
+
ctx->map_size = _ctx->rows * _ctx->cols * sizeof(struct flanterm_fb_queue_item *);
ctx->map = _malloc(ctx->map_size);
memset(ctx->map, 0, ctx->map_size);
···
+
_ctx->raw_putchar = flanterm_fb_raw_putchar;
+
_ctx->clear = flanterm_fb_clear;
+
_ctx->set_cursor_pos = flanterm_fb_set_cursor_pos;
+
_ctx->get_cursor_pos = flanterm_fb_get_cursor_pos;
+
_ctx->set_text_fg = flanterm_fb_set_text_fg;
+
_ctx->set_text_bg = flanterm_fb_set_text_bg;
+
_ctx->set_text_fg_bright = flanterm_fb_set_text_fg_bright;
+
_ctx->set_text_bg_bright = flanterm_fb_set_text_bg_bright;
+
_ctx->set_text_fg_rgb = flanterm_fb_set_text_fg_rgb;
+
_ctx->set_text_bg_rgb = flanterm_fb_set_text_bg_rgb;
+
_ctx->set_text_fg_default = flanterm_fb_set_text_fg_default;
+
_ctx->set_text_bg_default = flanterm_fb_set_text_bg_default;
+
_ctx->set_text_fg_default_bright = flanterm_fb_set_text_fg_default_bright;
+
_ctx->set_text_bg_default_bright = flanterm_fb_set_text_bg_default_bright;
+
_ctx->move_character = flanterm_fb_move_character;
+
_ctx->scroll = flanterm_fb_scroll;
+
_ctx->revscroll = flanterm_fb_revscroll;
+
_ctx->swap_palette = flanterm_fb_swap_palette;
+
_ctx->save_state = flanterm_fb_save_state;
+
_ctx->restore_state = flanterm_fb_restore_state;
+
_ctx->double_buffer_flush = flanterm_fb_double_buffer_flush;
+
_ctx->full_refresh = flanterm_fb_full_refresh;
+
_ctx->deinit = flanterm_fb_deinit;
+
flanterm_context_reinit(_ctx);
+
flanterm_fb_full_refresh(_ctx);