From 6b2f4cd20cc94d722518f0fd89c5a1e7002daf5a Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 2 Nov 2021 21:20:26 +0100 Subject: [PATCH] cpup_usage: param struct --- i3status.c | 13 ++++++++++++- include/i3status.h | 15 ++++++++++++++- src/print_cpu_usage.c | 25 +++++++++++++------------ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/i3status.c b/i3status.c index 174ee99..b96fa1d 100644 --- a/i3status.c +++ b/i3status.c @@ -854,7 +854,18 @@ int main(int argc, char *argv[]) { CASE_SEC("cpu_usage") { SEC_OPEN_MAP("cpu_usage"); - print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_above_threshold"), cfg_getstr(sec, "format_above_degraded_threshold"), cfg_getstr(sec, "path"), cfg_getfloat(sec, "max_threshold"), cfg_getfloat(sec, "degraded_threshold")); + cpu_usage_ctx_t ctx = { + .json_gen = json_gen, + .buf = buffer, + .buflen = sizeof(buffer), + .format = cfg_getstr(sec, "format"), + .format_above_threshold = cfg_getstr(sec, "format_above_threshold"), + .format_above_degraded_threshold = cfg_getstr(sec, "format_above_degraded_threshold"), + .path = cfg_getstr(sec, "path"), + .max_threshold = cfg_getfloat(sec, "max_threshold"), + .degraded_threshold = cfg_getfloat(sec, "degraded_threshold"), + }; + print_cpu_usage(&ctx); SEC_CLOSE_MAP; } diff --git a/include/i3status.h b/include/i3status.h index 469a302..ff869ff 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -324,7 +324,20 @@ typedef struct { void print_path_exists(path_exists_ctx_t *ctx); void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, const char *format_above_threshold, int); -void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const char *format_above_degraded_threshold, const char *path, const float max_threshold, const float degraded_threshold); + +typedef struct { + yajl_gen json_gen; + char *buf; + const size_t buflen; + const char *format; + const char *format_above_threshold; + const char *format_above_degraded_threshold; + const char *path; + const float max_threshold; + const float degraded_threshold; +} cpu_usage_ctx_t; + +void print_cpu_usage(cpu_usage_ctx_t *ctx); typedef struct { yajl_gen json_gen; diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index abf3481..7e505ea 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -58,13 +58,14 @@ static struct cpu_usage *curr_cpus = NULL; * percentage. * */ -void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const char *format_above_degraded_threshold, const char *path, const float max_threshold, const float degraded_threshold) { - const char *selected_format = format; +void print_cpu_usage(cpu_usage_ctx_t *ctx) { + const char *selected_format = ctx->format; const char *walk; - char *outwalk = buffer; + char *outwalk = ctx->buf; struct cpu_usage curr_all = {0, 0, 0, 0, 0}; int diff_idle, diff_total, diff_usage; bool colorful_output = false; +#define json_gen ctx->json_gen #if defined(__linux__) @@ -79,9 +80,9 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const } memcpy(curr_cpus, prev_cpus, cpu_count * sizeof(struct cpu_usage)); - FILE *f = fopen(path, "r"); + FILE *f = fopen(ctx->path, "r"); if (f == NULL) { - fprintf(stderr, "i3status: open %s: %s\n", path, strerror(errno)); + fprintf(stderr, "i3status: open %s: %s\n", ctx->path, strerror(errno)); goto error; } curr_cpu_count = get_nprocs(); @@ -160,16 +161,16 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const goto error; #endif - if (diff_usage >= max_threshold) { + if (diff_usage >= ctx->max_threshold) { START_COLOR("color_bad"); colorful_output = true; - if (format_above_threshold != NULL) - selected_format = format_above_threshold; - } else if (diff_usage >= degraded_threshold) { + if (ctx->format_above_threshold != NULL) + selected_format = ctx->format_above_threshold; + } else if (diff_usage >= ctx->degraded_threshold) { START_COLOR("color_degraded"); colorful_output = true; - if (format_above_degraded_threshold != NULL) - selected_format = format_above_degraded_threshold; + if (ctx->format_above_degraded_threshold != NULL) + selected_format = ctx->format_above_degraded_threshold; } for (walk = selected_format; *walk != '\0'; walk++) { @@ -210,7 +211,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const if (colorful_output) END_COLOR; - OUTPUT_FULL_TEXT(buffer); + OUTPUT_FULL_TEXT(ctx->buf); return; error: OUTPUT_FULL_TEXT("cant read cpu usage");