diff --git a/src/core.cc b/src/core.cc index 4ffa2295..0bbf28f6 100644 --- a/src/core.cc +++ b/src/core.cc @@ -1804,6 +1804,27 @@ struct text_object *construct_text_object(char *s, const char *arg, } obj->callbacks.print = &print_nvidia_value; obj->callbacks.free = &free_nvidia; + END OBJ_ARG(nvidiabar, 0, "nvidiabar needs an argument") + if (scan_nvidia_args(obj, arg, BAR)) { + CRIT_ERR(obj, free_at_crash, "nvidiabar: invalid argument" + " specified: '%s'\n", arg); + } + obj->callbacks.barval = &get_nvidia_barval; + obj->callbacks.free = &free_nvidia; + END OBJ_ARG(nvidiagraph, 0, "nvidiagraph needs an argument") + if (scan_nvidia_args(obj, arg, GRAPH)) { + CRIT_ERR(obj, free_at_crash, "nvidiagraph: invalid argument" + " specified: '%s'\n", arg); + } + obj->callbacks.graphval = &get_nvidia_barval; + obj->callbacks.free = &free_nvidia; + END OBJ_ARG(nvidiagauge, 0, "nvidiagauge needs an argument") + if (scan_nvidia_args(obj, arg, GAUGE)) { + CRIT_ERR(obj, free_at_crash, "nvidiagauge: invalid argument" + " specified: '%s'\n", arg); + } + obj->callbacks.gaugeval = &get_nvidia_barval; + obj->callbacks.free = &free_nvidia; #endif /* BUILD_NVIDIA */ #ifdef BUILD_APCUPSD END OBJ_ARG(apcupsd, &update_apcupsd, "apcupsd needs arguments: ") diff --git a/src/nvidia.cc b/src/nvidia.cc index 3cb14a9e..3b2c08ec 100644 --- a/src/nvidia.cc +++ b/src/nvidia.cc @@ -56,6 +56,15 @@ * TEMP GPU ${nvidia gputemp}°C (${nvidia gputempthreshold}°C max.), SYS ${nvidia ambienttemp}°C * FAN ${nvidia fanspeed}% RPM (${nvidia fanlevel}%) * + * --==| NVIDIA Bars |==-- + * LOAD ${nvidiabar gpuutil} + * VRAM ${nvidiabar memutil} + * RAM ${nvidiabar membwutil} + * VIDEO ${nvidiabar videoutil} + * PCIe ${nvidiabar pcieutil} + * Fan ${nvidiabar fanlevel} + * TEMP ${nvidiabar gputemp} + * */ @@ -331,6 +340,31 @@ namespace { nvidia_display_setting nvidia_display; } +// Extract arguments for nvidiabar, etc, and run set_nvidia_type +int scan_nvidia_args (struct text_object *obj, const char *args, unsigned int special_t) { + const char *arg = args; + + switch (special_t) { + case BAR: + arg = scan_bar(obj, arg, 100); + break; + case GRAPH: + arg = scan_graph(obj, arg, 100); + break; + case GAUGE: + arg = scan_gauge(obj, arg, 100); + break; + default: + return 1; + } + + // Return error if no argument + // (sometimes scan_graph gets excited and eats the whole string! + if (!arg) return 1; + + return set_nvidia_type(obj, arg); +} + // Evaluate module parameters and prepare query int set_nvidia_type(struct text_object *obj, const char *arg) @@ -704,6 +738,45 @@ void print_nvidia_value(struct text_object *obj, char *p, int p_max_size) } +double get_nvidia_barval(struct text_object *obj) { + struct nvidia_s *nvs = static_cast(obj->data.opaque); + int temp1, temp2; + double value; + + // Assume failure + value = 0; + + // Convert query_result to a percentage + if (nvs != NULL) { + switch (nvs->attribute) { + case ATTR_UTILS_STRING: // one of the percentage utils (gpuutil, membwutil, videoutil and pcieutil) + value = get_nvidia_string_value(nvs->target, ATTR_UTILS_STRING, nvs->token, nvs->search); + break; + case ATTR_MEM_UTIL: // memutil + temp1 = get_nvidia_value(nvs->target, ATTR_MEM_USED); + temp2 = get_nvidia_value(nvs->target, ATTR_MEM_TOTAL); + value = ((float)temp1 * 100 / (float)temp2) + 0.5; + break; + case ATTR_FAN_LEVEL: // fanlevel + case ATTR_FAN_SPEED: // TODO warn user to use fanlevel if they use fanspeed + value = get_nvidia_value(nvs->target, ATTR_FAN_LEVEL); + break; + case ATTR_GPU_TEMP: // gputemp (calculate out of gputempthreshold) + temp1 = get_nvidia_value(nvs->target, ATTR_GPU_TEMP); + temp2 = get_nvidia_value(nvs->target, ATTR_GPU_TEMP_THRESHOLD); + value = ((float)temp1 * 100 / (float)temp2) + 0.5; + break; + // TODO: calculate gpufreq, memfreq, etc + // can use (val-min)÷(max-min)×100. Perhaps a helper function or macro + + // TODO: throw errors if unsupported args are used + } + } + + // Return the percentage + return value; +} + // Cleanup void free_nvidia(struct text_object *obj) diff --git a/src/nvidia.h b/src/nvidia.h index e60208e3..74bd43b9 100644 --- a/src/nvidia.h +++ b/src/nvidia.h @@ -32,8 +32,10 @@ #ifndef NVIDIA_CONKY_H #define NVIDIA_CONKY_H +int scan_nvidia_args (struct text_object *obj, const char *args, unsigned int special_t); int set_nvidia_type(struct text_object *, const char *); void print_nvidia_value(struct text_object *, char *, int); +double get_nvidia_barval(struct text_object *obj); void free_nvidia(struct text_object *); #endif