i3status: Allow cpu temperature to be specified in celsius, fahrenheit, or kelvin
Patch status: rejected
Patch by Matt Schwager
To apply this patch, use:
curl http://cr.i3wm.org/patch/438/raw.patch | git am
b/i3status.c
17 |
@@ -282,6 +282,7 @@ int main(int argc, char *argv[]) { |
18 |
cfg_opt_t temp_opts[] = { |
19 |
CFG_STR("format", "%degrees C", CFGF_NONE), |
20 |
CFG_STR("path", NULL, CFGF_NONE), |
21 |
+ CFG_STR("scale", NULL, CFGF_NONE), |
22 |
CFG_INT("max_threshold", 75, CFGF_NONE), |
23 |
CFG_CUSTOM_COLOR_OPTS, |
24 |
CFG_END() |
25 |
@@ -539,7 +540,7 @@ int main(int argc, char *argv[]) { |
26 |
|
27 |
CASE_SEC_TITLE("cpu_temperature") { |
28 |
SEC_OPEN_MAP("cpu_temperature"); |
29 |
- print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "max_threshold")); |
30 |
+ print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "max_threshold"), cfg_getstr(sec, "scale")); |
31 |
SEC_CLOSE_MAP; |
32 |
} |
33 |
|
b/include/i3status.h
38 |
@@ -153,7 +153,7 @@ const char *get_ip_addr(); |
39 |
void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down); |
40 |
void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format); |
41 |
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format); |
42 |
-void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int); |
43 |
+void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int, const char *scale); |
44 |
void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format); |
45 |
void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down); |
46 |
void print_load(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold); |
b/man/i3status.man
51 |
@@ -283,7 +283,9 @@ colored red. The low_threshold type can be of threshold_type "time" or |
52 |
|
53 |
Gets the temperature of the given thermal zone. It is possible to |
54 |
define a max_threshold that will color the temperature red in case the |
55 |
-specified thermal zone is getting too hot. Defaults to 75 degrees C. |
56 |
+specified thermal zone is getting too hot. Defaults to 75 degrees C. The scale |
57 |
+argument can be defined as F or K to get the temperature in Fahrenheit or Kelvin |
58 |
+respectively. |
59 |
|
60 |
*Example order*: +cpu_temperature 0+ |
61 |
|
62 |
@@ -293,6 +295,8 @@ specified thermal zone is getting too hot. Defaults to 75 degrees C. |
63 |
|
64 |
*Example path*: +/sys/devices/platform/coretemp.0/temp1_input+ |
65 |
|
66 |
+*Example scale*: +F+ |
67 |
+ |
68 |
=== CPU Usage |
69 |
|
70 |
Gets the percentual CPU usage from +/proc/stat+ (Linux) or +sysctl(3)+ (FreeBSD/OpenBSD). |
b/src/print_cpu_temperature.c
75 |
@@ -36,13 +36,19 @@ |
76 |
#define MUKTOC(v) ((v - 273150000) / 1000000.0) |
77 |
#endif |
78 |
|
79 |
+long celsius_to_fahrenheit(long temp) { |
80 |
+ return (long) ((9.0/5.0 * (double) temp) + 32.0); |
81 |
+} |
82 |
+long celsius_to_kelvin(long temp) { |
83 |
+ return temp + 273; |
84 |
+} |
85 |
|
86 |
/* |
87 |
* Reads the CPU temperature from /sys/class/thermal/thermal_zone%d/temp (or |
88 |
* the user provided path) and returns the temperature in degree celcius. |
89 |
* |
90 |
*/ |
91 |
-void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) { |
92 |
+void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold, const char *scale) { |
93 |
char *outwalk = buffer; |
94 |
#ifdef THERMAL_ZONE |
95 |
const char *walk; |
96 |
@@ -72,11 +78,20 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const |
97 |
if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0) |
98 |
*(outwalk++) = '?'; |
99 |
else { |
100 |
- if ((temp/1000) >= max_threshold) { |
101 |
+ temp /= 1000; |
102 |
+ if (scale) { |
103 |
+ if (!strncmp(scale, "F", sizeof(char))) { |
104 |
+ temp = celsius_to_fahrenheit(temp); |
105 |
+ } |
106 |
+ if (!strncmp(scale, "K", sizeof(char))) { |
107 |
+ temp = celsius_to_kelvin(temp); |
108 |
+ } |
109 |
+ } |
110 |
+ if ((temp) >= max_threshold) { |
111 |
START_COLOR("color_bad"); |
112 |
colorful_output = true; |
113 |
} |
114 |
- outwalk += sprintf(outwalk, "%ld", (temp/1000)); |
115 |
+ outwalk += sprintf(outwalk, "%ld", (temp)); |
116 |
if (colorful_output) { |
117 |
END_COLOR; |
118 |
colorful_output = false; |