From a3b7905df65a1c5bafce65e42aac09fbef346bfe Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Sat, 4 Aug 2018 15:01:28 -0400 Subject: [PATCH] Add new text objects to retrieve the currently used keyboard layout and mouse speed in percentage and generate random password of chosen length and get the cpu clock speed from assembly (#550) * Add new text object to capitalize the first character of each word * Fix for issue https://github.com/brndnmtthws/conky/issues/46 * Fix for issue https://github.com/brndnmtthws/conky/issues/35 * Fix for issue https://github.com/brndnmtthws/conky/issues/35 * Fix for https://github.com/brndnmtthws/conky/issues/206 * Fix for https://github.com/brndnmtthws/conky/issues/474 * Add new "temp2" variable to print the temperature in floating number * Make the "temp2" variable static and add some explanation above it * openbsd.cc: Add one more formal parameter to "temp_print()" * extras/convert.lua: change url to wiki page (#554) --- src/CMakeLists.txt | 2 +- src/common.cc | 7 ++----- src/core.cc | 7 ++++++- src/freebsd.cc | 3 +++ src/hddtemp.cc | 2 +- src/i8k.cc | 2 +- src/ibm.cc | 2 +- src/linux.cc | 17 +++++++++++++++-- src/{cat.cc => misc.cc} | 22 ++++++++++++++++++++++ src/{cat.h => misc.h} | 7 ++++--- src/mpd.cc | 4 +++- src/openbsd.cc | 2 +- src/proc.cc | 2 +- src/smapi.cc | 2 +- src/temphelper.cc | 17 +++++++++++------ src/temphelper.h | 2 +- src/weather.cc | 6 +++--- 17 files changed, 77 insertions(+), 29 deletions(-) rename src/{cat.cc => misc.cc} (84%) rename src/{cat.h => misc.h} (92%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5598a34f..0b07fc0a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,12 +44,12 @@ set(conky_sources common.cc common.h conky.cc conky.h core.cc core.h - cat.cc cat.h diskio.cc diskio.h entropy.cc entropy.h exec.cc exec.h fs.cc fs.h mail.cc mail.h + misc.cc misc.h net_stat.cc net_stat.h template.cc template.h mboxscan.cc mboxscan.h diff --git a/src/common.cc b/src/common.cc index 9603001c..6fb5f093 100644 --- a/src/common.cc +++ b/src/common.cc @@ -307,10 +307,7 @@ void update_stuff() { /* Ohkie to return negative values for temperatures */ int round_to_int_temp(float f) { - if (f >= 0.0) { - return static_cast(f + 0.5); - } - return static_cast(f - 0.5); + return static_cast(f); } /* Don't return negative values for cpugraph, bar, gauge, percentage. * Causes unreasonable numbers to show */ @@ -588,7 +585,7 @@ int if_running_iftest(struct text_object *obj) { #ifndef __OpenBSD__ void print_acpitemp(struct text_object *obj, char *p, int p_max_size) { - temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS); + temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS, 1); } void free_acpitemp(struct text_object *obj) { close(obj->data.i); } diff --git a/src/core.cc b/src/core.cc index bef2e6ef..1c6936ef 100644 --- a/src/core.cc +++ b/src/core.cc @@ -31,7 +31,7 @@ #include "algebra.h" #include "bsdapm.h" #include "build.h" -#include "cat.h" +#include "misc.h" #include "colours.h" #include "combine.h" #include "core.h" @@ -825,6 +825,11 @@ struct text_object *construct_text_object(char *s, const char *arg, long line, obj->callbacks.print = &print_scroll_led; #endif /* BUILD_X11 */ + END OBJ(cap, 0) + obj->data.s = strndup(arg ? arg : "", text_buffer_size.get(*state)); + obj->callbacks.print = &print_cap; + obj->callbacks.free = &gen_free_opaque; + END OBJ(catp, 0) obj->data.s = strndup(arg ? arg : "", text_buffer_size.get(*state)); obj->callbacks.print = &print_catp; diff --git a/src/freebsd.cc b/src/freebsd.cc index f86dbcee..8e27bad3 100644 --- a/src/freebsd.cc +++ b/src/freebsd.cc @@ -641,6 +641,9 @@ int update_diskio(void) { free(dev_select); } + if (statinfo_cur.dinfo->mem_ptr) { + free(statinfo_cur.dinfo->mem_ptr); + } free(statinfo_cur.dinfo); return 0; } diff --git a/src/hddtemp.cc b/src/hddtemp.cc index 30a4e763..fc661cc2 100644 --- a/src/hddtemp.cc +++ b/src/hddtemp.cc @@ -228,6 +228,6 @@ void print_hddtemp(struct text_object *obj, char *p, int p_max_size) { snprintf(p, p_max_size, "%s", "N/A"); } else { temp_print(p, p_max_size, (double)val, - (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT)); + (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT), 1); } } diff --git a/src/i8k.cc b/src/i8k.cc index 54429f83..7776508f 100644 --- a/src/i8k.cc +++ b/src/i8k.cc @@ -113,7 +113,7 @@ void print_i8k_cpu_temp(struct text_object *obj, char *p, int p_max_size) { (void)obj; sscanf(i8k.cpu_temp, "%d", &cpu_temp); - temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS); + temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS, 1); } void print_i8k_right_fan_status(struct text_object *obj, char *p, diff --git a/src/ibm.cc b/src/ibm.cc index fafc6610..578f1e50 100644 --- a/src/ibm.cc +++ b/src/ibm.cc @@ -318,5 +318,5 @@ void parse_ibm_temps_arg(struct text_object *obj, const char *arg) { } void print_ibm_temps(struct text_object *obj, char *p, int p_max_size) { - temp_print(p, p_max_size, ibm_acpi_temps[obj->data.l], TEMP_CELSIUS); + temp_print(p, p_max_size, ibm_acpi_temps[obj->data.l], TEMP_CELSIUS, 1); } diff --git a/src/linux.cc b/src/linux.cc index 51649929..6e549698 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -106,6 +106,10 @@ static conky::simple_config_setting top_cpu_separate("top_cpu_separate", * using a flag in this manner creates less confusing code. */ static int prefer_proc = 0; +/* To tell 'print_sysfs_sensor' whether to print the temperature + * in int or float */ +static const char *temp2 = "empty"; + void prepare_update(void) {} int update_uptime(void) { @@ -1046,6 +1050,8 @@ static int open_sysfs_sensor(const char *dir, const char *dev, const char *type, type = "in"; } else if (strcmp(type, "tempf") == 0) { type = "temp"; + } else if (strcmp(type, "temp2") == 0) { + type = "temp"; } /* construct path */ @@ -1140,6 +1146,11 @@ static double get_sysfs_info(int *fd, int divisor, char *devtype, char *type) { /* divide voltage and temperature by 1000 */ /* or if any other divisor is given, use that */ + if (0 == (strcmp(type, "temp2"))) { + temp2 = "temp2"; + } else { + temp2 = "empty"; + } if (strcmp(type, "tempf") == 0) { if (divisor > 1) { return ((val / divisor + 40) * 9.0 / 5) - 40; @@ -1226,8 +1237,10 @@ void print_sysfs_sensor(struct text_object *obj, char *p, int p_max_size) { r = r * sf->factor + sf->offset; - if (!strncmp(sf->type, "temp", 4)) { - temp_print(p, p_max_size, r, TEMP_CELSIUS); + if (0 == (strcmp(temp2, "temp2"))) { + temp_print(p, p_max_size, r, TEMP_CELSIUS, 0); + } else if (!strncmp(sf->type, "temp", 4)) { + temp_print(p, p_max_size, r, TEMP_CELSIUS, 1); } else if (r >= 100.0 || r == 0) { snprintf(p, p_max_size, "%d", (int)r); } else { diff --git a/src/cat.cc b/src/misc.cc similarity index 84% rename from src/cat.cc rename to src/misc.cc index 63a7a6fa..6b482497 100644 --- a/src/cat.cc +++ b/src/misc.cc @@ -37,6 +37,8 @@ #include #include #include +#include +#include static inline void read_file(const char *data, char *buf, const int size) @@ -91,3 +93,23 @@ void print_catp(struct text_object *obj, char *p, int p_max_size) delete[] buf; } + +void print_cap(struct text_object *obj, char *p, int p_max_size) { + int x = 0; + int z = 0; + char buf[p_max_size]; + char *src = obj->data.s; + char *dest = buf; + + for (; *src && p_max_size-1 > x; src++, x++) { + if (0 == z) { + *dest++ = (toupper((unsigned char) *src)); + z++; + continue; + } + *dest++ = *src; + if (' ' == *src) z = 0; + } + *dest = '\0'; + snprintf(p, p_max_size, "%s", buf); +} diff --git a/src/cat.h b/src/misc.h similarity index 92% rename from src/cat.h rename to src/misc.h index 1ae6a546..6bdbd037 100644 --- a/src/cat.h +++ b/src/misc.h @@ -28,12 +28,13 @@ * */ -#ifndef _CAT_H -#define _CAT_H +#ifndef _MISC_H +#define _MISC_H #include "text_object.h" void print_cat(struct text_object *, char *, int); void print_catp(struct text_object *, char *, int); +void print_cap(struct text_object *, char *, int); -#endif /* _CAT_H */ +#endif /* _MISC_H */ diff --git a/src/mpd.cc b/src/mpd.cc index a820c322..b5280a35 100644 --- a/src/mpd.cc +++ b/src/mpd.cc @@ -234,7 +234,9 @@ void mpd_cb::work() { mpd_info.is_playing = 1; mpd_info.bitrate = status->bitRate; mpd_info.progress = - static_cast(status->elapsedTime) / status->totalTime; + ((0 != status->totalTime) ? + static_cast(status->elapsedTime) / status->totalTime + : 0.0); mpd_info.elapsed = status->elapsedTime; mpd_info.length = status->totalTime; } else { diff --git a/src/openbsd.cc b/src/openbsd.cc index 6b5a8f74..758572aa 100644 --- a/src/openbsd.cc +++ b/src/openbsd.cc @@ -489,7 +489,7 @@ void print_obsd_sensors_temp(struct text_object *obj, char *p, int p_max_size) { obsd_sensors.device = sensor_device.get(*state); update_obsd_sensors(); temp_print(p, p_max_size, obsd_sensors.temp[obsd_sensors.device][obj->data.l], - TEMP_CELSIUS); + TEMP_CELSIUS, 1); } void print_obsd_sensors_fan(struct text_object *obj, char *p, int p_max_size) { diff --git a/src/proc.cc b/src/proc.cc index eaafd51b..c1318788 100644 --- a/src/proc.cc +++ b/src/proc.cc @@ -240,7 +240,7 @@ void print_pid_nice(struct text_object *obj, char *p, int p_max_size) { generate_text_internal(objbuf.get(), max_user_text.get(*state), *obj->sub); - if (*(obj->data.s) != 0) { + if (!obj->data.s) { snprintf(pathbuf, 64, PROCDIR "/%d/stat", strtopid(objbuf.get())); buf = readfile(pathbuf, &bytes_read, 1); if (buf != nullptr) { diff --git a/src/smapi.cc b/src/smapi.cc index 59323752..ed5b8d28 100644 --- a/src/smapi.cc +++ b/src/smapi.cc @@ -139,7 +139,7 @@ void print_smapi_bat_temp(struct text_object *obj, char *p, int p_max_size) { ? smapi_get_bat_int(idx, "temperature") : 0; /* temperature is in milli degree celsius */ - temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS); + temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS, 1); } else NORM_ERR("argument to smapi_bat_temp must be an integer"); } diff --git a/src/temphelper.cc b/src/temphelper.cc index 84a3d03e..11b58de9 100644 --- a/src/temphelper.cc +++ b/src/temphelper.cc @@ -56,12 +56,17 @@ static double convert_temp_output(double n, enum TEMP_UNIT input_unit) { } int temp_print(char *p, size_t p_max_size, double n, - enum TEMP_UNIT input_unit) { - int out; - size_t plen; - - out = round_to_int_temp(convert_temp_output(n, input_unit)); - plen = spaced_print(p, p_max_size, "%d", 3, out); + enum TEMP_UNIT input_unit, int to_int) { + int i_out = 0; + float f_out = 0.0; + size_t plen = 0; + if (1 == to_int) { + i_out = round_to_int_temp(convert_temp_output(n, input_unit)); + plen = spaced_print(p, p_max_size, "%d", 3, i_out); + } else { + f_out = convert_temp_output(n, input_unit); + plen = spaced_print(p, p_max_size, "%.2f", 3, f_out); + } return static_cast(!(plen >= p_max_size)); } diff --git a/src/temphelper.h b/src/temphelper.h index e7dce891..12c0cdc5 100644 --- a/src/temphelper.h +++ b/src/temphelper.h @@ -28,6 +28,6 @@ enum TEMP_UNIT { TEMP_CELSIUS, TEMP_FAHRENHEIT }; -int temp_print(char *, std::size_t, double, enum TEMP_UNIT); +int temp_print(char *, std::size_t, double, enum TEMP_UNIT, int); #endif /* TEMPHELPER_H */ diff --git a/src/weather.cc b/src/weather.cc index 2b78f8e0..813f58be 100644 --- a/src/weather.cc +++ b/src/weather.cc @@ -721,9 +721,9 @@ static void weather_forecast_process_info(char *p, int p_max_size, std::lock_guard lock(cb->result_mutex); const weather_forecast &data = cb->get_result(); if (strcmp(data_type, "hi") == EQUAL) { - temp_print(p, p_max_size, data[day].hi, TEMP_CELSIUS); + temp_print(p, p_max_size, data[day].hi, TEMP_CELSIUS, 1); } else if (strcmp(data_type, "low") == EQUAL) { - temp_print(p, p_max_size, data[day].low, TEMP_CELSIUS); + temp_print(p, p_max_size, data[day].low, TEMP_CELSIUS, 1); } else if (strcmp(data_type, "icon") == EQUAL) { strncpy(p, data[day].icon.c_str(), p_max_size); } else if (strcmp(data_type, "forecast") == EQUAL) { @@ -777,7 +777,7 @@ static void weather_process_info(char *p, int p_max_size, if (strcmp(data_type, "last_update") == EQUAL) { strncpy(p, data->lastupd.c_str(), p_max_size); } else if (strcmp(data_type, "temperature") == EQUAL) { - temp_print(p, p_max_size, data->temp, TEMP_CELSIUS); + temp_print(p, p_max_size, data->temp, TEMP_CELSIUS, 1); } else if (strcmp(data_type, "cloud_cover") == EQUAL) { #ifdef BUILD_WEATHER_XOAP if (data->xoap_t[0] != '\0') {