summarylogtreecommitdiffstats
path: root/htop-temperature.patch
blob: d6545a46eaa6550b9971f73ec43e266ade6882f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
diff --git a/CRT.c b/CRT.c
index 7776c4e..519749c 100644
--- a/CRT.c
+++ b/CRT.c
@@ -94,6 +94,9 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
       [METER_VALUE] = A_BOLD | ColorPair(Cyan,Black),
       [LED_COLOR] = ColorPair(Green,Black),
       [TASKS_RUNNING] = A_BOLD | ColorPair(Green,Black),
+      [TEMPERATURE_COOL] = A_DIM | ColorPair(Green,Black),
+      [TEMPERATURE_MEDIUM] = A_NORMAL | ColorPair(Yellow,Black),
+      [TEMPERATURE_HOT] = A_BOLD | ColorPair(Red,Black),
       [PROCESS] = A_NORMAL,
       [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
       [PROCESS_TAG] = A_BOLD | ColorPair(Yellow,Black),
@@ -163,6 +166,9 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
       [METER_VALUE] = A_BOLD,
       [LED_COLOR] = A_NORMAL,
       [TASKS_RUNNING] = A_BOLD,
+      [TEMPERATURE_COOL] = A_DIM,
+      [TEMPERATURE_MEDIUM] = A_NORMAL,
+      [TEMPERATURE_HOT] = A_BOLD,
       [PROCESS] = A_NORMAL,
       [PROCESS_SHADOW] = A_DIM,
       [PROCESS_TAG] = A_BOLD,
@@ -232,6 +238,9 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
       [METER_VALUE] = ColorPair(Black,White),
       [LED_COLOR] = ColorPair(Green,White),
       [TASKS_RUNNING] = ColorPair(Green,White),
+      [TEMPERATURE_COOL] = ColorPair(Green,White),
+      [TEMPERATURE_MEDIUM] = ColorPair(Yellow,White),
+      [TEMPERATURE_HOT] = ColorPair(Red,White),
       [PROCESS] = ColorPair(Black,White),
       [PROCESS_SHADOW] = A_BOLD | ColorPair(Black,White),
       [PROCESS_TAG] = ColorPair(White,Blue),
@@ -301,6 +310,9 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
       [METER_VALUE] = ColorPair(Black,Black),
       [LED_COLOR] = ColorPair(Green,Black),
       [TASKS_RUNNING] = ColorPair(Green,Black),
+      [TEMPERATURE_COOL] = ColorPair(Green,Black),
+      [TEMPERATURE_MEDIUM] = ColorPair(Yellow,Black),
+      [TEMPERATURE_HOT] = ColorPair(Red,Black),
       [PROCESS] = ColorPair(Black,Black),
       [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
       [PROCESS_TAG] = ColorPair(White,Blue),
@@ -370,6 +382,9 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
       [METER_VALUE] = A_BOLD | ColorPair(Cyan,Blue),
       [LED_COLOR] = ColorPair(Green,Blue),
       [TASKS_RUNNING] = A_BOLD | ColorPair(Green,Blue),
+      [TEMPERATURE_COOL] = A_DIM | ColorPair(Green,Blue),
+      [TEMPERATURE_MEDIUM] = A_NORMAL | ColorPair(Yellow,Blue),
+      [TEMPERATURE_HOT] = A_BOLD | ColorPair(Red,Blue),
       [PROCESS] = ColorPair(White,Blue),
       [PROCESS_SHADOW] = A_BOLD | ColorPair(Black,Blue),
       [PROCESS_TAG] = A_BOLD | ColorPair(Yellow,Blue),
@@ -439,6 +454,9 @@ int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
       [METER_VALUE] = ColorPair(Green,Black),
       [LED_COLOR] = ColorPair(Green,Black),
       [TASKS_RUNNING] = A_BOLD | ColorPair(Green,Black),
+      [TEMPERATURE_COOL] = A_DIM | ColorPair(Green,Black),
+      [TEMPERATURE_MEDIUM] = A_NORMAL | ColorPair(Yellow,Black),
+      [TEMPERATURE_HOT] = A_BOLD | ColorPair(Red,Black),
       [PROCESS] = ColorPair(Cyan,Black),
       [PROCESS_SHADOW] = A_BOLD | ColorPairGrayBlack,
       [PROCESS_TAG] = A_BOLD | ColorPair(Yellow,Black),
diff --git a/CRT.h b/CRT.h
index 8a5d6ac..8b56f2f 100644
--- a/CRT.h
+++ b/CRT.h
@@ -53,6 +53,9 @@ typedef enum ColorElements_ {
    UPTIME,
    BATTERY,
    TASKS_RUNNING,
+   TEMPERATURE_COOL,
+   TEMPERATURE_MEDIUM,
+   TEMPERATURE_HOT,
    SWAP,
    PROCESS,
    PROCESS_SHADOW,
diff --git a/Makefile.am b/Makefile.am
index 50fb586..5746a66 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -47,6 +47,7 @@ linux_platform_headers = \
 	linux/LinuxProcessList.h \
 	linux/LinuxCRT.h \
 	linux/Battery.h \
+	TemperatureMeter.h \
 	linux/PressureStallMeter.h \
 	zfs/ZfsArcMeter.h \
 	zfs/ZfsCompressedArcMeter.h \
@@ -55,7 +56,7 @@ linux_platform_headers = \
 if HTOP_LINUX
 AM_LDFLAGS += -rdynamic
 myhtopplatsources = linux/Platform.c linux/IOPriorityPanel.c \
-linux/LinuxProcess.c linux/LinuxProcessList.c linux/LinuxCRT.c linux/Battery.c \
+linux/LinuxProcess.c linux/LinuxProcessList.c linux/LinuxCRT.c linux/Battery.c TemperatureMeter.c \
 linux/PressureStallMeter.c \
 zfs/ZfsArcMeter.c zfs/ZfsCompressedArcMeter.c zfs/ZfsArcStats.c

diff --git a/TemperatureMeter.c b/TemperatureMeter.c
new file mode 100644
index 0000000..a19bccd
--- /dev/null
+++ b/TemperatureMeter.c
@@ -0,0 +1,103 @@
+/*
+htop - TemperatureMeter.c
+(C) 2013 Ralf Stemmer
+(C) 2014 Blair Bonnett
+(C) 2020 Maxim Kurnosenko
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "TemperatureMeter.h"
+
+#include "ProcessList.h"
+#include "CRT.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+/*{
+#include "Meter.h"
+}*/
+
+int TemperatureMeter_attributes[] = {
+   TEMPERATURE_COOL,
+   TEMPERATURE_MEDIUM,
+   TEMPERATURE_HOT,
+};
+
+static void TemperatureMeter_setValues(Meter* this, char* buffer, int len) {
+   ProcessList* pl = this->pl;
+   this->total = pl->totalTasks;
+   this->values[0] = pl->runningTasks;
+   snprintf(buffer, len, "%d/%d", (int) this->values[0], (int) this->total);
+}
+
+static void TemperatureMeter_display(Object* cast, RichString* out) {
+   (void) cast;
+   FILE *p;
+   p = popen("sensors", "r");
+   if(p == NULL) return;
+
+   int textColor   = CRT_colors[METER_TEXT];
+   int coolColor   = CRT_colors[TEMPERATURE_COOL];
+   int mediumColor = CRT_colors[TEMPERATURE_MEDIUM];
+   int hotColor    = CRT_colors[TEMPERATURE_HOT];
+
+   int read;
+   size_t len;
+   char *line = NULL;
+   char *entry = NULL;
+   char *tstart = NULL, *tend = NULL;
+   long  temperature;
+   while ((read = getline(&line, &len, p)) != -1) {
+      // contains this line a core-temperature?
+      entry = strstr(line, "Core ");
+      if (entry == NULL) continue;
+
+      // find the begin of the temperature value
+      tstart = strchr(entry, '+'); // no negative temperatures expected :)
+      if (tstart == NULL) continue;
+      tstart++; // jump over the '+'
+
+      // find the end of the temperature. Remember, it can be above 99°C ;)
+      tend = strchr(tstart, '.'); // just the integer
+      if (tend == NULL) continue;
+
+      // convert the string into an integer, this is necessary for further steps
+      temperature = strtol(tstart, &tend, 10);
+      if (temperature == LONG_MAX || temperature == LONG_MIN) continue;
+      if (tstart == tend) continue;
+
+      // choose the color for the temperature
+      int tempColor;
+      if      (temperature < 60) tempColor = coolColor;
+      else if (temperature < 70) tempColor = mediumColor;
+      else                       tempColor = hotColor;
+
+      // output the temperature
+      char buffer[30];
+      sprintf(buffer, "%ld", temperature);
+      RichString_append(out, tempColor, buffer);
+      RichString_append(out, textColor, "°C ");
+   }
+
+   free(line);
+   pclose(p);
+}
+
+MeterClass TemperatureMeter_class = {
+   .super = {
+      .extends = Class(Meter),
+      .display = TemperatureMeter_display,
+      .delete = Meter_delete,
+   },
+   .updateValues = TemperatureMeter_setValues,
+   .defaultMode = TEXT_METERMODE,
+   .total = 100.0,
+   .attributes = TemperatureMeter_attributes,
+   .name = "Temperature",
+   .uiName = "Temperature Sensors",
+   .caption = "Temperature: "
+};
diff --git a/TemperatureMeter.h b/TemperatureMeter.h
new file mode 100644
index 0000000..b158147
--- /dev/null
+++ b/TemperatureMeter.h
@@ -0,0 +1,20 @@
+/* Do not edit this file. It was automatically generated. */
+
+#ifndef HEADER_TemperatureMeter
+#define HEADER_TemperatureMeter
+/*
+htop - TemperatureMeter.h
+(C) 2013 Ralf Stemmer
+(C) 2014 Blair Bonnett
+(C) 2020 Maxim Kurnosenko
+Released under the GNU GPL, see the COPYING file
+in the source distribution for its full text.
+*/
+
+#include "Meter.h"
+
+extern int TemperatureMeter_attributes[];
+
+extern MeterClass TemperatureMeter_class;
+
+#endif
diff --git a/linux/Platform.c b/linux/Platform.c
index e82d8f8..02e3ede 100644
--- a/linux/Platform.c
+++ b/linux/Platform.c
@@ -25,6 +25,7 @@ in the source distribution for its full text.
 #include "zfs/ZfsArcMeter.h"
 #include "zfs/ZfsCompressedArcMeter.h"
 #include "LinuxProcess.h"
+#include "TemperatureMeter.h"

 #include <math.h>
 #include <assert.h>
@@ -108,6 +109,7 @@ MeterClass* Platform_meterTypes[] = {
    &MemoryMeter_class,
    &SwapMeter_class,
    &TasksMeter_class,
+   &TemperatureMeter_class,
    &UptimeMeter_class,
    &BatteryMeter_class,
    &HostnameMeter_class,