From: Phil Sutter Date: Sun, 8 Nov 2009 18:01:42 +0000 (+0100) Subject: smapi: outsource printing code X-Git-Url: http://vcs.maemo.org/git/?a=commitdiff_plain;h=ea22f212529dd40dcfedc75db906f94d4c18d4ad;hp=bb35c252845d7d942d1f3021e349d8fef4642d73;p=monky smapi: outsource printing code --- diff --git a/src/conky.c b/src/conky.c index 9e7e73a..2377b26 100644 --- a/src/conky.c +++ b/src/conky.c @@ -2367,12 +2367,7 @@ void generate_text_internal(char *p, int p_max_size, } #ifdef IBM OBJ(smapi) { - char *s; - if(obj->data.s) { - s = smapi_get_val(obj->data.s); - snprintf(p, p_max_size, "%s", s); - free(s); - } + print_smapi(obj, p, p_max_size); } OBJ(if_smapi_bat_installed) { int idx; @@ -2384,41 +2379,17 @@ void generate_text_internal(char *p, int p_max_size, NORM_ERR("argument to if_smapi_bat_installed must be an integer"); } OBJ(smapi_bat_perc) { - int idx, val; - if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) { - val = smapi_bat_installed(idx) ? - smapi_get_bat_int(idx, "remaining_percent") : 0; - percent_print(p, p_max_size, val); - } else - NORM_ERR("argument to smapi_bat_perc must be an integer"); + print_smapi_bat_perc(obj, p, p_max_size); } OBJ(smapi_bat_temp) { - int idx, val; - if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) { - val = smapi_bat_installed(idx) ? - smapi_get_bat_int(idx, "temperature") : 0; - /* temperature is in milli degree celsius */ - temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS); - } else - NORM_ERR("argument to smapi_bat_temp must be an integer"); + print_smapi_bat_temp(obj, p, p_max_size); } OBJ(smapi_bat_power) { - int idx, val; - if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) { - val = smapi_bat_installed(idx) ? - smapi_get_bat_int(idx, "power_now") : 0; - /* power_now is in mW, set to W with one digit precision */ - snprintf(p, p_max_size, "%.1f", ((double)val / 1000)); - } else - NORM_ERR("argument to smapi_bat_power must be an integer"); + print_smapi_bat_power(obj, p, p_max_size); } #ifdef X11 OBJ(smapi_bat_bar) { - if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i)) - new_bar(obj, p, (int) - (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100)); - else - new_bar(obj, p, 0); + print_smapi_bat_bar(obj, p, p_max_size); } #endif /* X11 */ #endif /* IBM */ diff --git a/src/smapi.c b/src/smapi.c index aa2ea2f..d1a89a0 100644 --- a/src/smapi.c +++ b/src/smapi.c @@ -124,3 +124,62 @@ char *smapi_get_val(const char *args) return smapi_get_str(str); } + +void print_smapi(struct text_object *obj, char *p, int p_max_size) +{ + char *s; + + if (!obj->data.s) + return; + + s = smapi_get_val(obj->data.s); + snprintf(p, p_max_size, "%s", s); + free(s); +} + +void print_smapi_bat_perc(struct text_object *obj, char *p, int p_max_size) +{ + int idx, val; + if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) { + val = smapi_bat_installed(idx) ? + smapi_get_bat_int(idx, "remaining_percent") : 0; + percent_print(p, p_max_size, val); + } else + NORM_ERR("argument to smapi_bat_perc must be an integer"); +} + +void print_smapi_bat_temp(struct text_object *obj, char *p, int p_max_size) +{ + int idx, val; + if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) { + val = smapi_bat_installed(idx) ? + smapi_get_bat_int(idx, "temperature") : 0; + /* temperature is in milli degree celsius */ + temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS); + } else + NORM_ERR("argument to smapi_bat_temp must be an integer"); +} + +void print_smapi_bat_power(struct text_object *obj, char *p, int p_max_size) +{ + int idx, val; + if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) { + val = smapi_bat_installed(idx) ? + smapi_get_bat_int(idx, "power_now") : 0; + /* power_now is in mW, set to W with one digit precision */ + snprintf(p, p_max_size, "%.1f", ((double)val / 1000)); + } else + NORM_ERR("argument to smapi_bat_power must be an integer"); +} + +void print_smapi_bat_bar(struct text_object *obj, char *p, int p_max_size) +{ + if (!p_max_size) + return; + + if (obj->data.i >= 0 && smapi_bat_installed(obj->data.i)) + new_bar(obj, p, (int) + (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100)); + else + new_bar(obj, p, 0); +} diff --git a/src/smapi.h b/src/smapi.h index 6b1986c..70a73d2 100644 --- a/src/smapi.h +++ b/src/smapi.h @@ -36,4 +36,10 @@ char *smapi_get_bat_str(int, const char *); int smapi_get_bat_int(int, const char *); char *smapi_get_bat_val(const char *); +void print_smapi(struct text_object *, char *, int); +void print_smapi_bat_perc(struct text_object *, char *, int); +void print_smapi_bat_temp(struct text_object *, char *, int); +void print_smapi_bat_power(struct text_object *, char *, int); +void print_smapi_bat_bar(struct text_object *, char *, int); + #endif /* _SMAPI_H */