diff options
| author | iamcheeseman <[hidden email]> | 2026-03-02 19:00:42 -0500 |
|---|---|---|
| committer | iamcheeseman <[hidden email]> | 2026-03-02 19:00:42 -0500 |
| commit | b1df65a9325f95c09b85c2cd5483e198c909b4e3 (patch) | |
| tree | 63bbc75a30167d8532a3f6955978cc4448c4e261 /src | |
| parent | a996c83e1079f49830138858f6c54816102daeba (diff) | |
Add support for different bg/fg colors per comp
Diffstat (limited to 'src')
| -rw-r--r-- | src/bardata.c | 108 | ||||
| -rw-r--r-- | src/bardata.h | 8 | ||||
| -rw-r--r-- | src/tsar.c | 25 |
3 files changed, 86 insertions, 55 deletions
diff --git a/src/bardata.c b/src/bardata.c index b182997..f1495ea 100644 --- a/src/bardata.c +++ b/src/bardata.c @@ -19,6 +19,54 @@ size_t left_size = 0, right_size = 0, center_size = 0; int padding = 0; int comp_gap = 0; +void change_color(const char* name, color_t* xft_color) { + if (xft_color != NULL) { + XftColorFree( + display, + DefaultVisual(display, DefaultScreen(display)), + DefaultColormap(display, DefaultScreen(display)), + xft_color + ); + } + + XftColorAllocName( + display, + DefaultVisual(display, DefaultScreen(display)), + DefaultColormap(display, DefaultScreen(display)), + name, + xft_color + ); +} + +color_t parse_color(const char* col_str) { + color_t col = {0}; + + if (col_str[0] == '#') { + col_str++; + } + + col.pixel = strtol(col_str, NULL, 16); + col.color.red = (col.pixel >> 16) & 0xFF; + col.color.green = (col.pixel >> 8) & 0xFF; + col.color.blue = (col.pixel) & 0xFF; + col.color.alpha = 255; + + return col; +} + +void set_default_background(const char* name) { + change_color(name, &default_bg); + XSetWindowAttributes win_attr = { + .background_pixel = default_bg.pixel, + }; + XChangeWindowAttributes(display, win, CWBackPixel, &win_attr); +} + +void init_bardata(void) { + change_color("#FFFFFF", &default_fg); + set_default_background("#202020"); +} + comp_t *find_comp(char *name) { for (size_t i = 0; i < comps_size; i++) { if (strcmp(comps[i].name, name) == 0) { @@ -27,9 +75,11 @@ comp_t *find_comp(char *name) { } comp_t *comp = &comps[comps_size++]; + memset(comp, 0, sizeof(comp_t)); + strcpy(comp->name, name); - comp->fg = default_fg; - comp->bg = default_bg; + comp->fg = &default_fg; + comp->bg = &default_bg; return comp; } @@ -52,22 +102,6 @@ void add_component(comp_side_t side, char *name) { } } -color_t parse_color(const char* col_str) { - color_t col = {0}; - - if (col_str[0] == '#') { - col_str++; - } - - col.pixel = strtol(col_str, NULL, 16); - col.color.red = (col.pixel >> 16) & 0xFF; - col.color.green = (col.pixel >> 8) & 0xFF; - col.color.blue = (col.pixel) & 0xFF; - col.color.alpha = 255; - - return col; -} - void set_component(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { comp_t* comp = find_comp(argv[1]); @@ -78,8 +112,19 @@ void set_component(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { continue; } if (strcmp(argv[i], "-fg") == 0) { - const char* col_str = argv[++i]; - comp->fg = parse_color(col_str); + const char* col_str = argv[i + 1]; + comp->custom_fg = parse_color(col_str); + comp->fg = &comp->custom_fg; + change_color(argv[i + 1], comp->fg); + i++; + continue; + } + if (strcmp(argv[i], "-bg") == 0) { + const char* col_str = argv[i + 1]; + comp->custom_bg = parse_color(col_str); + comp->bg = &comp->custom_bg; + change_color(argv[i + 1], comp->bg); + i++; continue; } } @@ -108,7 +153,6 @@ void set_layout(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { } } - void set_config(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { char *var = argv[1]; if (strcmp(var, "-height") == 0) { @@ -133,29 +177,11 @@ void set_config(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) { return; } if (strcmp(var, "-background") == 0 || strcmp(var, "-bg") == 0) { - default_bg = parse_color(argv[2]); - XSetWindowAttributes win_attr = { - .background_pixel = default_bg.pixel, - }; - XChangeWindowAttributes(display, win, CWBackPixel, &win_attr); + set_default_background(argv[2]); return; } if (strcmp(var, "-foreground") == 0 || strcmp(var, "-fg") == 0) { - default_fg = parse_color(argv[2]); - XftColorFree( - display, - DefaultVisual(display, DefaultScreen(display)), - DefaultColormap(display, DefaultScreen(display)), - &xft_fg_color - ); - - XftColorAllocName( - display, - DefaultVisual(display, DefaultScreen(display)), - DefaultColormap(display, DefaultScreen(display)), - argv[2], - &xft_fg_color - ); + change_color(argv[2], &default_fg); return; } } diff --git a/src/bardata.h b/src/bardata.h index c2144bd..9ad0d97 100644 --- a/src/bardata.h +++ b/src/bardata.h @@ -18,7 +18,11 @@ typedef enum { typedef struct { char name[16]; char data[64]; - color_t fg, bg; + int flags; + color_t custom_fg, custom_bg; + // if there are custom colors, these point to the above fields. Otherwise, it + // will point to default_fg/bg. + color_t *fg, *bg; } comp_t; extern int fontsize; @@ -29,7 +33,7 @@ extern size_t left_size, right_size, center_size; extern int padding; extern int comp_gap; - +void init_bardata(void); void new_component(char *name); void set_component(char argv[MAX_ARGS][MAX_ARG_LEN], int argc); void set_layout(char argv[MAX_ARGS][MAX_ARG_LEN], int argc); @@ -17,7 +17,6 @@ Window win; GC gc; XftFont* font = NULL; -XftColor xft_fg_color; static int bar_width = 1000; static float width_ratio = 1; @@ -94,14 +93,6 @@ void init_x(void) { load_font("monospace:size=10"); - XftColorAllocName( - display, - DefaultVisual(display, DefaultScreen(display)), - DefaultColormap(display, DefaultScreen(display)), - "#FFFFFF", - &xft_fg_color - ); - gc = XCreateGC(display, win, 0, NULL); XSetForeground(display, gc, 0xFFFFFF); @@ -115,7 +106,13 @@ void deinit_x(void) { XDestroyWindow(display, win); } -void draw_text(const char* text, int x, int y) { +void draw_text( + const char* text, + int x, + int y, + const XftColor* fg_color, + const XftColor* bg_color +) { XftDraw* xft_draw = XftDrawCreate( display, win, @@ -123,8 +120,11 @@ void draw_text(const char* text, int x, int y) { DefaultColormap(display, DefaultScreen(display)) ); + size_t width = font->max_advance_width * utf8_strlen(text); + XftDrawRect(xft_draw, bg_color, x, y, width, bar_height); + int dy = y + (bar_height - font->height) / 2 + font->ascent; - XftDrawStringUtf8(xft_draw, &xft_fg_color, font, x, dy, (const FcChar8*)text, strlen(text)); + XftDrawStringUtf8(xft_draw, fg_color, font, x, dy, (const FcChar8*)text, strlen(text)); XftDrawDestroy(xft_draw); } @@ -143,7 +143,7 @@ void draw_comp_set(float pos_ratio, comp_t **set, int size) { for (int i = 0; i < size; i++) { comp_t *comp = set[i]; - draw_text(comp->data, x, y); + draw_text(comp->data, x, y, comp->fg, comp->bg); x += font->max_advance_width * utf8_strlen(comp->data); x += comp_gap; } @@ -152,6 +152,7 @@ void draw_comp_set(float pos_ratio, comp_t **set, int size) { int main(void) { init_pipe(); init_x(); + init_bardata(); while (true) { XEvent ev; |
