From ec8c94dc43db11c79ef58b212cacfadec09bb59c Mon Sep 17 00:00:00 2001 From: Jack Jamison Date: Fri, 17 Oct 2025 18:21:51 -0400 Subject: lines argument --- args.c | 26 ++++++++++++++++++++++++++ args.h | 8 ++++++++ font.c | 2 +- graphics.c | 10 +++------- layer_surface.c | 25 ++++++++++++++++++++----- main.c | 4 +++- state.h | 6 +++++- 7 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 args.c create mode 100644 args.h diff --git a/args.c b/args.c new file mode 100644 index 0000000..5fe2f89 --- /dev/null +++ b/args.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +#include "args.h" + +void parse_args(client_state* state, int argc, char* argv[]) { + static struct option long_opts[] = { + {"lines", required_argument, 0, 'l'}, + {0, 0, 0, 0} + }; + + int opt; + int long_index = 0; + while ((opt = getopt_long(argc, argv, "l:", long_opts, &long_index)) != -1) { + switch (opt) { + case 'l': + state->lines = atoi(optarg); + break; + default: + fprintf(stderr, "Usage: NOT WHAT YOU TYPED\n"); + break; + } + } +} diff --git a/args.h b/args.h new file mode 100644 index 0000000..604bfda --- /dev/null +++ b/args.h @@ -0,0 +1,8 @@ +#ifndef ARGS_H_ +#define ARGS_H_ + +#include "state.h" + +void parse_args(client_state* state, int argc, char* argv[]); + +#endif diff --git a/font.c b/font.c index 50fe3d0..dd65a6a 100644 --- a/font.c +++ b/font.c @@ -61,7 +61,7 @@ bool freetype_init(client_state* state, const char* file) { return false; } state->load_flags = FT_LOAD_RENDER | FT_LOAD_TARGET_(FT_RENDER_MODE_SDF); - state->line_height = (float)(state->ft_face->size->metrics.height >> 6); + state->line_height = state->ft_face->size->metrics.height >> 6; return true; } diff --git a/graphics.c b/graphics.c index f8b85ea..d4fcb10 100644 --- a/graphics.c +++ b/graphics.c @@ -128,19 +128,15 @@ void render_frame(client_state *state) { glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // draw text + // bind vertex and texture glBindVertexArray(state->input_buffer_text.vao); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, state->atlas.texture); + // bind shader and draw glUseProgram(state->text_shader); glUniform2f(state->screen_size_uniform, state->width, state->height); - glUniform2f(state->offset_uniform, 0.0f, 0.0f); - glDrawElements(GL_TRIANGLES, state->input_buffer_text.num_elements, GL_UNSIGNED_INT, 0); - - // draw second text a line higher - glUniform2f(state->offset_uniform, 0.0f, state->line_height); + glUniform2f(state->offset_uniform, 0.0f, state->line_height * (state->lines + 0.15f)); glDrawElements(GL_TRIANGLES, state->input_buffer_text.num_elements, GL_UNSIGNED_INT, 0); // present screen diff --git a/layer_surface.c b/layer_surface.c index eb23da9..017ac8a 100644 --- a/layer_surface.c +++ b/layer_surface.c @@ -1,18 +1,33 @@ #include "./state.h" +#include "array.h" struct zwlr_layer_surface_v1_listener layer_surface_listener; void create_surface(client_state* state) { + // figure out anchor + uint32_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | + ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + if (state->lines > 0) { + anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; + } + + // figure out size + uint32_t desired_width = 0; + uint32_t desired_height = state->line_height; + if (state->lines > 0) { + desired_width = 600; // figure out later + desired_height = (state->lines + 1) * state->line_height; + } + + // create surface state->surface = wl_compositor_create_surface(state->compositor); state->layer_surface = zwlr_layer_shell_v1_get_layer_surface( state->layer_shell, state->surface, NULL, ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swenu"); zwlr_layer_surface_v1_add_listener(state->layer_surface, &layer_surface_listener, state); - zwlr_layer_surface_v1_set_size(state->layer_surface, 600, 400); - zwlr_layer_surface_v1_set_anchor(state->layer_surface, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | - ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | - ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | - ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); + zwlr_layer_surface_v1_set_size(state->layer_surface, desired_width, desired_height); + zwlr_layer_surface_v1_set_anchor(state->layer_surface, anchor); zwlr_layer_surface_v1_set_exclusive_zone(state->layer_surface, -1); zwlr_layer_surface_v1_set_margin(state->layer_surface, 0, 0, 0, 0); zwlr_layer_surface_v1_set_keyboard_interactivity(state->layer_surface, ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE); diff --git a/main.c b/main.c index 9ec04d1..794bb2a 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include "input_field.h" #include "font.h" #include "array.h" +#include "args.h" void poll_events(client_state* state); @@ -30,11 +31,12 @@ void read_stdin(client_state* state) { free(line); } -int main() { +int main(int argc, char* argv[]) { client_state state = {0}; state.running = true; state.input_buffer = array_new(char, 0); + parse_args(&state, argc, argv); read_stdin(&state); // find font diff --git a/state.h b/state.h index e3640fd..242970e 100644 --- a/state.h +++ b/state.h @@ -41,6 +41,7 @@ typedef struct { typedef struct { char* text; size_t pixel_len; + text_buffer_t text_buffer; } item_t; typedef struct { @@ -84,7 +85,10 @@ typedef struct { FT_Face ft_face; FT_Int32 load_flags; atlas_t atlas; - float line_height; + uint32_t line_height; + + // arguments + int lines; // state char* input_buffer; -- cgit v1.2.3