From 5fa96e59fa378af36e90cdd8ee741d164304e3b8 Mon Sep 17 00:00:00 2001 From: Riley Beckett Date: Tue, 14 Oct 2025 15:48:32 -0400 Subject: read stdin input --- font.c | 9 +++++++++ font.h | 2 +- main.c | 28 ++++++++++++++++++++++++++++ state.h | 8 ++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/font.c b/font.c index ef95d79..533788c 100644 --- a/font.c +++ b/font.c @@ -1,5 +1,6 @@ #include #include "font.h" +#include "array.h" char* get_font(const char* font_name) { FcPattern* pattern = FcNameParse((const FcChar8*)font_name); @@ -76,6 +77,8 @@ void atlas_init(client_state* state) { metric->advance_y = (float)state->ft_face->glyph->advance.y / 64; metric->bitmap_width = state->ft_face->glyph->bitmap.width; metric->bitmap_height = state->ft_face->glyph->bitmap.rows; + metric->bearing_x = state->ft_face->glyph->bitmap_left; + metric->bearing_y = state->ft_face->glyph->bitmap_top; } } @@ -133,3 +136,9 @@ int atlas_get_strwidth(client_state* state, const char* str) { } return (int)ceil(width); } + +void atlas_calc_item_widths(client_state* state) { + array_for_all(item_t, item, state->items) { + item->pixel_len = atlas_get_strwidth(state, item->text); + } +} diff --git a/font.h b/font.h index 71c1a12..fe77e17 100644 --- a/font.h +++ b/font.h @@ -8,5 +8,5 @@ void atlas_init(client_state* state); void atlas_create_texture(client_state* state); int atlas_get_strwidth(client_state* state, const char* str); - +void atlas_calc_item_widths(client_state* state); #endif // FONT_H_ diff --git a/main.c b/main.c index 90c2257..433d14d 100644 --- a/main.c +++ b/main.c @@ -8,14 +8,38 @@ #include "graphics.h" #include "input_field.h" #include "font.h" +#include "array.h" void poll_events(client_state* state); +void read_stdin(client_state* state) { + ssize_t len = 0; + size_t a; + char* line = NULL; + while((len = getline(&line, &a, stdin)) != -1) { + if (line[len - 1] == '\n') { + line[len - 1] = '\0'; + len -= 1; + } + if (len != 0) { + array_add(state->items, (item_t){0}); + array_last(state->items) = (item_t){ + .text = line, + .pixel_len = 0 + }; + } + line = NULL; + } + free(line); +} + int main() { client_state state = {0}; state.running = true; state.input_buffer = array_new(char, 0); + read_stdin(&state); + // find font char* font = get_font("Monospace"); if (!freetype_init(&state, font)) { @@ -26,6 +50,8 @@ int main() { // create font atlas atlas_init(&state); + atlas_calc_item_widths(&state); + // start wayland state.display = wl_display_connect(NULL); if (!state.display) { @@ -56,6 +82,8 @@ int main() { poll_events(&state); render_frame(&state); } + + array_free(state.items); } void poll_events(client_state* state) { diff --git a/state.h b/state.h index 3089233..4e4b99a 100644 --- a/state.h +++ b/state.h @@ -21,6 +21,7 @@ typedef struct { float advance_x, advance_y; uint32_t bitmap_width, bitmap_height; + int32_t bearing_x, bearing_y; float texture_offset; float texture_size; } metric_t; @@ -31,6 +32,11 @@ typedef struct { metric_t metrics[128]; } atlas_t; +typedef struct { + char* text; + size_t pixel_len; +} item_t; + typedef struct { // wayland struct wl_display* display; @@ -73,6 +79,8 @@ typedef struct { bool running; + item_t* items; + } client_state; #endif // STATE_H_ -- cgit v1.2.3