diff options
| author | Riley Beckett <rbeckettvt@gmail.com> | 2025-10-14 15:48:32 -0400 |
|---|---|---|
| committer | Riley Beckett <rbeckettvt@gmail.com> | 2025-10-14 15:48:32 -0400 |
| commit | 5fa96e59fa378af36e90cdd8ee741d164304e3b8 (patch) | |
| tree | 233787686f276f4ca77dc98d61d6efbd6435efb1 | |
| parent | 26a95fface3c47a870d88cc1abd174c708af0ffe (diff) | |
read stdin input
| -rw-r--r-- | font.c | 9 | ||||
| -rw-r--r-- | font.h | 2 | ||||
| -rw-r--r-- | main.c | 28 | ||||
| -rw-r--r-- | state.h | 8 |
4 files changed, 46 insertions, 1 deletions
@@ -1,5 +1,6 @@ #include <fontconfig/fontconfig.h> #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); + } +} @@ -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_ @@ -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) { @@ -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; @@ -32,6 +33,11 @@ typedef struct { } atlas_t; typedef struct { + char* text; + size_t pixel_len; +} item_t; + +typedef struct { // wayland struct wl_display* display; struct wl_registry* registry; @@ -73,6 +79,8 @@ typedef struct { bool running; + item_t* items; + } client_state; #endif // STATE_H_ |
