Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)

Simplify #ifdef situation

mintsuki f62c969b f4287305

Changed files
+21 -32
backends
+19 -6
README.md
···
### Quick usage
To quickly set up and use a framebuffer Flanterm instance, it is possible to
-
use the `flanterm_fb_simple_init()` function as such:
```c
#include <flanterm/flanterm.h>
#include <flanterm/backends/fb.h>
-
struct flanterm_context *ft_ctx = flanterm_fb_simple_init(
-
framebuffer_ptr, framebuffer_width, framebuffer_height, framebuffer_pitch
-
);
```
-
Where `framebuffer_{ptr,width,height,pitch}` represent the corresponding info
-
about the framebuffer to use for this given instance.
To then print to the terminal instance, simply use the `flanterm_write()`
function on the given instance. For example:
···
### Quick usage
To quickly set up and use a framebuffer Flanterm instance, it is possible to
+
use the `flanterm_fb_init()` function as such:
```c
#include <flanterm/flanterm.h>
#include <flanterm/backends/fb.h>
+
struct flanterm_context *ft_ctx = flanterm_fb_init(
+
NULL,
+
NULL,
+
framebuffer_ptr, width, height, pitch,
+
red_mask_size, red_mask_shift,
+
green_mask_size, green_mask_shift,
+
blue_mask_size, blue_mask_shift,
+
NULL, NULL,
+
NULL, NULL,
+
NULL, NULL,
+
NULL, 0, 0, 1,
+
0, 0,
+
0
+
);
```
+
Where `framebuffer_ptr, width, height, pitch` and `{red,green,blue}_mask_{size,shift}`
+
represent the corresponding info about the framebuffer to use for this given instance.
+
+
The meaning of the other arguments can be found in `backends/fb.h`.
To then print to the terminal instance, simply use the `flanterm_write()`
function on the given instance. For example:
-13
backends/fb.c
···
0x00, 0x00, 0x00, 0x00
};
-
#ifdef FLANTERM_FB_SUPPORT_BPP
static inline __attribute__((always_inline)) uint32_t convert_colour(struct flanterm_context *_ctx, uint32_t colour) {
struct flanterm_fb_context *ctx = (void *)_ctx;
uint32_t r = (colour >> 16) & 0xff;
···
uint32_t b = colour & 0xff;
return (r << ctx->red_mask_shift) | (g << ctx->green_mask_shift) | (b << ctx->blue_mask_shift);
}
-
#else
-
#define convert_colour(CTX, COLOUR) (COLOUR)
-
#endif
static void flanterm_fb_save_state(struct flanterm_context *_ctx) {
struct flanterm_fb_context *ctx = (void *)_ctx;
···
void *(*_malloc)(size_t),
void (*_free)(void *, size_t),
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
-
#ifdef FLANTERM_FB_SUPPORT_BPP
uint8_t red_mask_size, uint8_t red_mask_shift,
uint8_t green_mask_size, uint8_t green_mask_shift,
uint8_t blue_mask_size, uint8_t blue_mask_shift,
-
#endif
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t *canvas,
#endif
···
}
}
-
#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
-
#endif
-
-
#ifdef FLANTERM_FB_SUPPORT_BPP
if (red_mask_size < 8 || red_mask_size != green_mask_size || red_mask_size != blue_mask_size) {
return NULL;
}
-
#endif
if (_malloc == NULL) {
#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
···
struct flanterm_context *_ctx = (void *)ctx;
memset(ctx, 0, sizeof(struct flanterm_fb_context));
-
#ifdef FLANTERM_FB_SUPPORT_BPP
ctx->red_mask_size = red_mask_size;
ctx->red_mask_shift = red_mask_shift + (red_mask_size - 8);
ctx->green_mask_size = green_mask_size;
ctx->green_mask_shift = green_mask_shift + (green_mask_size - 8);
ctx->blue_mask_size = blue_mask_size;
ctx->blue_mask_shift = blue_mask_shift + (blue_mask_size - 8);
-
#endif
if (ansi_colours != NULL) {
for (size_t i = 0; i < 8; i++) {
···
0x00, 0x00, 0x00, 0x00
};
static inline __attribute__((always_inline)) uint32_t convert_colour(struct flanterm_context *_ctx, uint32_t colour) {
struct flanterm_fb_context *ctx = (void *)_ctx;
uint32_t r = (colour >> 16) & 0xff;
···
uint32_t b = colour & 0xff;
return (r << ctx->red_mask_shift) | (g << ctx->green_mask_shift) | (b << ctx->blue_mask_shift);
}
static void flanterm_fb_save_state(struct flanterm_context *_ctx) {
struct flanterm_fb_context *ctx = (void *)_ctx;
···
void *(*_malloc)(size_t),
void (*_free)(void *, size_t),
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
uint8_t red_mask_size, uint8_t red_mask_shift,
uint8_t green_mask_size, uint8_t green_mask_shift,
uint8_t blue_mask_size, uint8_t blue_mask_shift,
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t *canvas,
#endif
···
}
}
if (red_mask_size < 8 || red_mask_size != green_mask_size || red_mask_size != blue_mask_size) {
return NULL;
}
if (_malloc == NULL) {
#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
···
struct flanterm_context *_ctx = (void *)ctx;
memset(ctx, 0, sizeof(struct flanterm_fb_context));
ctx->red_mask_size = red_mask_size;
ctx->red_mask_shift = red_mask_shift + (red_mask_size - 8);
ctx->green_mask_size = green_mask_size;
ctx->green_mask_shift = green_mask_shift + (green_mask_size - 8);
ctx->blue_mask_size = blue_mask_size;
ctx->blue_mask_shift = blue_mask_shift + (blue_mask_size - 8);
if (ansi_colours != NULL) {
for (size_t i = 0; i < 8; i++) {
+2 -13
backends/fb.h
···
size_t height;
size_t bpp;
-
#ifdef FLANTERM_FB_SUPPORT_BPP
uint8_t red_mask_size, red_mask_shift;
uint8_t green_mask_size, green_mask_shift;
uint8_t blue_mask_size, blue_mask_shift;
-
#endif
size_t font_bits_size;
uint8_t *font_bits;
···
};
struct flanterm_context *flanterm_fb_init(
void *(*_malloc)(size_t),
void (*_free)(void *, size_t),
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
-
#ifdef FLANTERM_FB_SUPPORT_BPP
uint8_t red_mask_size, uint8_t red_mask_shift,
uint8_t green_mask_size, uint8_t green_mask_shift,
uint8_t blue_mask_size, uint8_t blue_mask_shift,
-
#endif
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t *canvas,
#endif
···
#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
static inline struct flanterm_context *flanterm_fb_simple_init(
-
uint32_t *framebuffer, size_t width, size_t height, size_t pitch
-
#ifdef FLANTERM_FB_SUPPORT_BPP
-
,
uint8_t red_mask_size, uint8_t red_mask_shift,
uint8_t green_mask_size, uint8_t green_mask_shift,
uint8_t blue_mask_size, uint8_t blue_mask_shift
-
#endif
) {
return flanterm_fb_init(
NULL,
NULL,
framebuffer, width, height, pitch,
-
#ifdef FLANTERM_FB_SUPPORT_BPP
red_mask_size, red_mask_shift,
green_mask_size, green_mask_shift,
blue_mask_size, blue_mask_shift,
-
#endif
-
#ifndef FLANTERM_FB_DISABLE_CANVAS
-
NULL,
-
#endif
NULL, NULL,
NULL, NULL,
NULL, NULL,
···
size_t height;
size_t bpp;
uint8_t red_mask_size, red_mask_shift;
uint8_t green_mask_size, green_mask_shift;
uint8_t blue_mask_size, blue_mask_shift;
size_t font_bits_size;
uint8_t *font_bits;
···
};
struct flanterm_context *flanterm_fb_init(
+
// If _malloc and _free are nulled, use the bump allocated instance (1 use only).
void *(*_malloc)(size_t),
void (*_free)(void *, size_t),
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
uint8_t red_mask_size, uint8_t red_mask_shift,
uint8_t green_mask_size, uint8_t green_mask_shift,
uint8_t blue_mask_size, uint8_t blue_mask_shift,
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t *canvas,
#endif
···
#ifndef FLANTERM_FB_DISABLE_BUMP_ALLOC
static inline struct flanterm_context *flanterm_fb_simple_init(
+
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
uint8_t red_mask_size, uint8_t red_mask_shift,
uint8_t green_mask_size, uint8_t green_mask_shift,
uint8_t blue_mask_size, uint8_t blue_mask_shift
) {
return flanterm_fb_init(
NULL,
NULL,
framebuffer, width, height, pitch,
red_mask_size, red_mask_shift,
green_mask_size, green_mask_shift,
blue_mask_size, blue_mask_shift,
NULL, NULL,
NULL, NULL,
NULL, NULL,