From a22baf4ba2ab78dfe0c5bbd74c9f6fef5aa35f5d Mon Sep 17 00:00:00 2001 From: iamcheeseman <[hidden email]> Date: Sun, 1 Mar 2026 12:18:17 -0500 Subject: Allow changing values at runtime --- src/bardata.c | 15 +++++++++++---- src/tsar.c | 34 +++++++++++++++++++++++----------- src/tsar.h | 6 ++++++ 3 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 src/tsar.h (limited to 'src') diff --git a/src/bardata.c b/src/bardata.c index 73b0cc2..6369aa3 100644 --- a/src/bardata.c +++ b/src/bardata.c @@ -6,6 +6,7 @@ #include #include "pipe.h" +#include "tsar.h" int fontsize = 16; color_t default_fg, default_bg; @@ -72,10 +73,16 @@ void set_layout(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { void set_var(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { char *var = argv[1]; - if (strcmp(var, "height")) { - bar_height = atoi(argv[2]); + if (strcmp(var, "height") == 0) { + int new_bar_height = atoi(argv[2]); + if (new_bar_height <= 0) { + fprintf(stderr, "invalid bar height %d\n", new_bar_height); + return; + } + bar_height = new_bar_height; } - if (strcmp(var, "fontsize")) { - fontsize = atoi(argv[2]); + if (strcmp(var, "font") == 0) { + // fontsize = atoi(argv[2]); + load_font(argv[2]); } } diff --git a/src/tsar.c b/src/tsar.c index 869d73c..a4a0025 100644 --- a/src/tsar.c +++ b/src/tsar.c @@ -9,15 +9,15 @@ #include #include "pipe.h" +#include "bardata.h" Display* display; Window win; GC gc; -XftFont* font; +XftFont* font = NULL; XftColor fg_color; -static int height = 17; static int width = 1000; static float width_ratio = 1; @@ -34,7 +34,17 @@ void die(const char* fmt, ...) { exit(err_code); } -void draw_bar(void) { +void load_font(const char* font_name) { + XftFont* new_font = XftFontOpenName(display, DefaultScreen(display), font_name); + if (new_font == NULL) { + fprintf(stderr, "cannot open font %s\n", font_name); + return; + } + + if (font != NULL) { + XftFontClose(display, font); + } + font = new_font; } void init_x(void) { @@ -56,7 +66,7 @@ void init_x(void) { display, DefaultRootWindow(display), 0, 0, - width, height, + width, bar_height, 0, CopyFromParent, InputOutput, @@ -67,11 +77,7 @@ void init_x(void) { XMapRaised(display, win); - gc = XCreateGC(display, win, 0, NULL); - XSetForeground(display, gc, 0xFFFFFF); - - const char font_name[] = "CommitMono:style=Regular:antialias=true"; - font = XftFontOpenName(display, DefaultScreen(display), font_name); + load_font("monospace:size=10"); XftColorAllocName( display, @@ -81,6 +87,9 @@ void init_x(void) { &fg_color ); + gc = XCreateGC(display, win, 0, NULL); + XSetForeground(display, gc, 0xFFFFFF); + XFlush(display); } @@ -98,7 +107,7 @@ void draw_text(const char* text, int x, int y) { DefaultColormap(display, DefaultScreen(display)) ); - int dy = y + (height - font->height) / 2 + font->ascent; + int dy = y + (bar_height - font->height) / 2 + font->ascent; XftDrawString8(xft_draw, &fg_color, font, x, dy, (const FcChar8*)text, strlen(text)); XftDrawDestroy(xft_draw); @@ -110,10 +119,13 @@ int main(void) { while (true) { XEvent ev; - XNextEvent(display, &ev); + while (XPending(display)) + XNextEvent(display, &ev); XClearWindow(display, win); + XResizeWindow(display, win, width, bar_height); + draw_text("Hello, world!", 0, 0); XFlush(display); diff --git a/src/tsar.h b/src/tsar.h new file mode 100644 index 0000000..1c9e60b --- /dev/null +++ b/src/tsar.h @@ -0,0 +1,6 @@ +#ifndef TSAR_H +#define TSAR_H + +void load_font(const char* font_name); + +#endif -- cgit v1.3-2-g0d8e