状态栏绘制添加图标和色彩设置

This commit is contained in:
2025-09-01 00:56:45 +08:00
parent 1b0ed3b9b8
commit 91950dc2f4
14 changed files with 244 additions and 29 deletions

View File

@@ -13,6 +13,7 @@
#include <xcb/xproto.h>
#include "action.h"
#include "app.h"
#include "config.h"
#include "event-adapter.h"
#include "layout.h"
@@ -30,6 +31,8 @@ static void set_wallpaper_by_index(uint32_t index, wallpaper_config_t *config,
cairo_t *cr);
static wallpaper_config_t *parse_wallpaper_config(void);
static void free_wallpaper_config(wallpaper_config_t *wallpaper_config);
static void init_icons(void);
static void clean_icons(void);
static void init_color_set(void);
static void parse_color(const char *hex, color_t *color);
static void init_monitor_bar(void);
@@ -82,6 +85,7 @@ static monitor_t *monitors = NULL;
static monitor_t *current_monitor = NULL;
static cairo_t *wallpaper_cr = NULL;
static wallpaper_config_t *wallpaper_config = NULL;
static icons_t *icons = NULL;
static color_set_t *color_set = NULL;
static status_t *status = NULL;
static GMainLoop *loop = NULL;
@@ -94,6 +98,7 @@ static uint32_t tag_x_pad = tag_x_padding;
static uint32_t layout_symbol_x_pad = layout_symbol_x_padding;
static uint32_t client_name_x_pad = client_name_x_padding;
static uint32_t status_x_pad = status_x_padding;
static uint32_t status_icon_pad = status_icon_padding;
static uint32_t bar_height = 0;
static uint32_t border_px = border_width;
@@ -253,7 +258,12 @@ static void print_color_set(color_set_t *set) {
print_color("== client name color:", &set->client_name_color);
print_color("== active client name bg:", &set->active_client_name_bg);
print_color("== active client name color:", &set->active_client_name_color);
print_color("== status color:", &set->status_color);
print_color("== status net recv color:", &set->status_net_recv_color);
print_color("== status net send color:", &set->status_net_send_color);
print_color("== status volume color:", &set->status_volume_color);
print_color("== status memory color:", &set->status_memory_color);
print_color("== status cpu color:", &set->status_cpu_color);
print_color("== status datetime color:", &set->status_datetime_color);
print_color("== border color:", &set->border_color);
print_color("== active border color:", &set->active_border_color);
}
@@ -489,6 +499,66 @@ void free_wallpaper_config(wallpaper_config_t *wallpaper_config) {
free(wallpaper_config);
}
void init_icons(void) {
typedef struct {
const char *img_path;
cairo_surface_t **icon;
uint32_t **pixels;
} icon_path_surface_map_t;
if (icons) return;
icons = ecalloc(1, sizeof(icons_t));
icon_path_surface_map_t map[] = {
{NET_DOWN_ICON, (cairo_surface_t **)&icons->net_down,
&icons->net_down_pixels},
{NET_UP_ICON, (cairo_surface_t **)&icons->net_up, &icons->net_up_pixels},
{SPEAKER_ICON, (cairo_surface_t **)&icons->volume, &icons->volume_pixels},
{MEM_ICON, (cairo_surface_t **)&icons->memory, &icons->memory_pixels},
{CPU_ICON, (cairo_surface_t **)&icons->cpu, &icons->cpu_pixels},
{CLOCK_ICON, (cairo_surface_t **)&icons->clock, &icons->clock_pixels},
};
for (int i = 0; i < LENGTH(map); i++) {
const char *path = map[i].img_path;
uint32_t **pixels_addr = map[i].pixels;
cairo_surface_t **surface_addr = map[i].icon;
if (path && strlen(path)) {
*pixels_addr =
generate_icon_pixels(path, bar_height, bar_height, status_icon_pad);
cairo_format_t format = CAIRO_FORMAT_ARGB32;
int stride = cairo_format_stride_for_width(format, bar_height);
cairo_surface_t *surface = cairo_image_surface_create_for_data(
(uint8_t *)*pixels_addr, format, bar_height, bar_height, stride);
if (cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS) {
*surface_addr = surface;
} else {
cairo_surface_destroy(surface);
}
}
}
}
void clean_icons(void) {
if (!icons) return;
if (icons->net_down) cairo_surface_destroy(icons->net_down);
if (icons->net_up) cairo_surface_destroy(icons->net_up);
if (icons->volume) cairo_surface_destroy(icons->volume);
if (icons->memory) cairo_surface_destroy(icons->memory);
if (icons->cpu) cairo_surface_destroy(icons->cpu);
if (icons->clock) cairo_surface_destroy(icons->clock);
if (icons->net_down_pixels) free(icons->net_down);
if (icons->net_up_pixels) free(icons->net_up);
if (icons->volume_pixels) free(icons->volume);
if (icons->memory_pixels) free(icons->memory);
if (icons->cpu_pixels) free(icons->cpu);
if (icons->clock_pixels) free(icons->clock);
free(icons);
icons = NULL;
}
void init_color_set(void) {
color_set = ecalloc(1, sizeof(color_set_t));
char *bar_background = NULL;
@@ -501,7 +571,12 @@ void init_color_set(void) {
char *client_name = NULL;
char *active_client_name_background = NULL;
char *active_client_name = NULL;
char *status = NULL;
char *status_net_recv = NULL;
char *status_net_send = NULL;
char *status_volume = NULL;
char *status_memory = NULL;
char *status_cpu = NULL;
char *status_datetime = NULL;
char *border = NULL;
char *active_border = NULL;
@@ -520,7 +595,17 @@ void init_color_set(void) {
&active_client_name_background, active_client_name_bg);
get_xresource_string("zswm.color.active_client_name_color",
&active_client_name, active_client_name_color);
get_xresource_string("zswm.color.status", &status, status_color);
get_xresource_string("zswm.color.status_net_recv", &status_net_recv,
status_net_recv_color);
get_xresource_string("zswm.color.status_net_send", &status_net_send,
status_net_send_color);
get_xresource_string("zswm.color.status_volume", &status_volume,
status_volume_color);
get_xresource_string("zswm.color.status_memory", &status_memory,
status_memory_color);
get_xresource_string("zswm.color.status_cpu", &status_cpu, status_cpu_color);
get_xresource_string("zswm.color.status_datetime", &status_datetime,
status_datetime_color);
get_xresource_string("zswm.color.border", &border, border_color);
get_xresource_string("zswm.color.active_border", &active_border,
active_border_color);
@@ -535,7 +620,12 @@ void init_color_set(void) {
parse_color(client_name, &color_set->client_name_color);
parse_color(active_client_name_background, &color_set->active_client_name_bg);
parse_color(active_client_name, &color_set->active_client_name_color);
parse_color(status, &color_set->status_color);
parse_color(status_net_recv, &color_set->status_net_recv_color);
parse_color(status_net_send, &color_set->status_net_send_color);
parse_color(status_volume, &color_set->status_volume_color);
parse_color(status_memory, &color_set->status_memory_color);
parse_color(status_cpu, &color_set->status_cpu_color);
parse_color(status_datetime, &color_set->status_datetime_color);
parse_color(border, &color_set->border_color);
parse_color(active_border, &color_set->active_border_color);
@@ -549,7 +639,12 @@ void init_color_set(void) {
free(client_name);
free(active_client_name_background);
free(active_client_name);
free(status);
free(status_net_recv);
free(status_net_send);
free(status_volume);
free(status_memory);
free(status_cpu);
free(status_datetime);
free(border);
free(active_border);
}
@@ -602,6 +697,7 @@ void get_xresource_config(void) {
get_xresource_uint32("zswm.layout_symbol_x_padding", &layout_symbol_x_pad);
get_xresource_uint32("zswm.client_name_x_padding", &client_name_x_pad);
get_xresource_uint32("zswm.status_x_padding", &status_x_pad);
get_xresource_uint32("zswm.status_icon_padding", &status_icon_pad);
get_xresource_uint32("zswm.border_width", &border_px);
logger("dpi: %u, font family: %s, font size: %u\n", dpi, font_family,
@@ -619,8 +715,8 @@ void draw_all_monitor_bars(void) {
}
void draw_monitor_bar(monitor_t *m) {
draw_bar(m, m == current_monitor, status, color_set, bar_height, tag_x_pad,
layout_symbol_x_pad, client_name_x_pad, status_x_pad);
draw_bar(m, m == current_monitor, status, icons, color_set, bar_height,
tag_x_pad, layout_symbol_x_pad, client_name_x_pad, status_x_pad);
flush_xcb_connection();
}
@@ -1294,6 +1390,8 @@ void cleanup(bool will_restart) {
free(color_set);
color_set = NULL;
clean_icons();
free(font_family);
font_family = NULL;
@@ -1317,6 +1415,7 @@ int main(int argc, char *argv[]) {
init_monitor_bar();
change_window_cursor(get_root_window(), cursor_normal);
init_wallpaper();
init_icons();
init_status(g_main_loop_get_context(loop), update_status);
draw_all_monitor_bars();