Skip to content

Commit 3ff3a68

Browse files
committed
FlexMeter: Add FlexMeter functionality
FlexMeter provides functionality which will allow users to make custom meters without need of rebuilding every time htop binary and adding source to the project. It can be used to print some device status, free disk space CPU or other specific temeraturer, fan RPM and many more. Everything that can be fetched from linux shell with one line result can be printer. For fething information can be used anything from shell, python, precompiled binary or simply reading file located somewhere in file system. New meter will appear uppon restart of htop in list with available meters. Configuration folder location where metes should be placed: - /home/$USER/.config/htop/FlexMeter/ On start folder will be created if does not exist, together with template file .Template in same folder. Note: Files starting with '.' (.Template for examlpe) are ignored Meter Example: File name : Template name=<NAME SHOWN IN AvailableMeters> command=<COMMAND WHICH WILL BE EXECUTED> type=<METER TYPE FOR NO ONLY "TEXT_METER"> caption="CAPTION TEXT SHOWN IN THE BEGGINING OF THE METER" According to this implementation 30 Flex meter can be added Currently they have hardcoded limit of 30 meter in addition to all that already exist. Signed-off-by: Stoyan Bogdanov <[email protected]>
1 parent 4b3dfa2 commit 3ff3a68

File tree

10 files changed

+256
-5
lines changed

10 files changed

+256
-5
lines changed

CRT.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
129129
[FAILED_READ] = A_BOLD | ColorPair(Red, Black),
130130
[PAUSED] = A_BOLD | ColorPair(Yellow, Cyan),
131131
[UPTIME] = A_BOLD | ColorPair(Cyan, Black),
132+
[FLEX] = A_BOLD | ColorPair(Cyan, Black),
132133
[BATTERY] = A_BOLD | ColorPair(Cyan, Black),
133134
[LARGE_NUMBER] = A_BOLD | ColorPair(Red, Black),
134135
[METER_SHADOW] = A_BOLD | ColorPairGrayBlack,
@@ -247,6 +248,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
247248
[FAILED_READ] = A_BOLD,
248249
[PAUSED] = A_BOLD | A_REVERSE,
249250
[UPTIME] = A_BOLD,
251+
[FLEX] = A_BOLD,
250252
[BATTERY] = A_BOLD,
251253
[LARGE_NUMBER] = A_BOLD,
252254
[METER_SHADOW] = A_DIM,
@@ -365,6 +367,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
365367
[FAILED_READ] = ColorPair(Red, White),
366368
[PAUSED] = A_BOLD | ColorPair(Yellow, Cyan),
367369
[UPTIME] = ColorPair(Yellow, White),
370+
[FLEX] = ColorPair(Yellow, White),
368371
[BATTERY] = ColorPair(Yellow, White),
369372
[LARGE_NUMBER] = ColorPair(Red, White),
370373
[METER_SHADOW] = ColorPair(Blue, White),
@@ -483,6 +486,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
483486
[FAILED_READ] = ColorPair(Red, Black),
484487
[PAUSED] = A_BOLD | ColorPair(Yellow, Cyan),
485488
[UPTIME] = ColorPair(Yellow, Black),
489+
[FLEX] = ColorPair(Yellow, Black),
486490
[BATTERY] = ColorPair(Yellow, Black),
487491
[LARGE_NUMBER] = ColorPair(Red, Black),
488492
[METER_SHADOW] = A_BOLD | ColorPairGrayBlack,
@@ -601,6 +605,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
601605
[FAILED_READ] = A_BOLD | ColorPair(Red, Blue),
602606
[PAUSED] = A_BOLD | ColorPair(Yellow, Cyan),
603607
[UPTIME] = A_BOLD | ColorPair(Yellow, Blue),
608+
[FLEX] = A_BOLD | ColorPair(Yellow, Blue),
604609
[BATTERY] = A_BOLD | ColorPair(Yellow, Blue),
605610
[LARGE_NUMBER] = A_BOLD | ColorPair(Red, Blue),
606611
[METER_SHADOW] = ColorPair(Cyan, Blue),
@@ -719,6 +724,7 @@ static int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = {
719724
[FAILED_READ] = A_BOLD | ColorPair(Red, Black),
720725
[PAUSED] = A_BOLD | ColorPair(Yellow, Green),
721726
[UPTIME] = ColorPair(Green, Black),
727+
[FLEX] = ColorPair(Green, Black),
722728
[BATTERY] = ColorPair(Green, Black),
723729
[LARGE_NUMBER] = A_BOLD | ColorPair(Red, Black),
724730
[METER_SHADOW] = A_BOLD | ColorPairGrayBlack,

CRT.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ typedef enum ColorElements_ {
154154
DYNAMIC_MAGENTA,
155155
DYNAMIC_YELLOW,
156156
DYNAMIC_WHITE,
157+
FLEX,
157158
LAST_COLORELEMENT
158159
} ColorElements;
159160

FlexMeter.c

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
htop - FlexMeter.c
3+
(C) 2024 Stoyan Bogdanov
4+
Released under the GNU GPLv2+, see the COPYING file
5+
in the source distribution for its full text.
6+
*/
7+
8+
#include <dirent.h>
9+
#include <pwd.h>
10+
#include <sys/time.h>
11+
#include <sys/types.h>
12+
#include <time.h>
13+
#include <unistd.h>
14+
15+
#include "CRT.h"
16+
#include "config.h"
17+
#include "FlexMeter.h"
18+
#include "Object.h"
19+
20+
21+
#define FLEX_CFG_FOLDER ".config/htop/FlexMeter"
22+
23+
typedef struct {
24+
char* name;
25+
char* command;
26+
char* type;
27+
char* caption;
28+
char* uiName;
29+
} _flex_meter;
30+
31+
_flex_meter meter_list[METERS_LIST_SIZE];
32+
33+
static int meters_count = 0;
34+
35+
static const int DateMeter_attributes[] = {
36+
FLEX
37+
};
38+
39+
MeterClass* FlexMeter_class = NULL;
40+
41+
static int check_for_meters(void);
42+
43+
static bool parse_input(_flex_meter *meter, char* line)
44+
{
45+
switch(line[0])
46+
{
47+
case 'n':
48+
if (String_startsWith(line, "name=")) {
49+
xAsprintf(&meter->uiName, "Flex: %s", line + 5);
50+
}
51+
break;
52+
case 'c':
53+
if (String_startsWith(line, "command=")) {
54+
meter->command = xStrdup(line + 8);
55+
} else if (String_startsWith(line, "caption=")) {
56+
meter->caption = xStrdup(line + 8);
57+
}
58+
break;
59+
case 't':
60+
if (String_startsWith(line, "type=")) {
61+
meter->type = xStrdup(line + 6);
62+
}
63+
break;
64+
default:
65+
return false;
66+
}
67+
68+
return true;
69+
}
70+
71+
static bool load_config(_flex_meter *meter, char* file)
72+
{
73+
bool ret = false;
74+
FILE* fp = fopen(file, "r");
75+
76+
if (fp != NULL) {
77+
char* buff;
78+
while ((buff = String_readLine(fp)) != NULL) {
79+
ret = parse_input(meter, buff);
80+
if (!ret) {
81+
break;
82+
}
83+
}
84+
free(buff);
85+
buff = NULL;
86+
}
87+
88+
fclose(fp);
89+
return ret;
90+
}
91+
92+
static int check_for_meters(void)
93+
{
94+
char* path;
95+
struct dirent* dir;
96+
struct passwd* pw = getpwuid(getuid());
97+
const char* homedir = pw->pw_dir;
98+
char* home = NULL;
99+
bool ret;
100+
101+
xAsprintf(&home, "%s/%s", homedir, FLEX_CFG_FOLDER);
102+
struct stat fileStat;
103+
104+
if (stat(home, &fileStat) < 0) {
105+
return -1;
106+
}
107+
108+
uint32_t uid = getuid();
109+
110+
if ((fileStat.st_uid == uid) && S_ISDIR(fileStat.st_mode) && ((fileStat.st_mode & 0777) == 0700)) {
111+
DIR* d = opendir(home);
112+
if (d) {
113+
while ((dir = readdir(d)) != NULL) {
114+
if ( dir->d_name[0] == '.') {
115+
/* We are ignoring all files starting with . like ".Template"
116+
* and "." ".." directories
117+
*/
118+
continue;
119+
}
120+
121+
meter_list[meters_count].name = xStrdup(dir->d_name);
122+
xAsprintf(&path, "%s/%s", home, dir->d_name);
123+
124+
if (access(path, R_OK | W_OK | X_OK) == 0) {
125+
ret = load_config(&meter_list[meters_count], path);
126+
127+
if (ret && (meters_count < MAX_METERS_COUNT)) {
128+
meters_count++;
129+
}
130+
}
131+
if (path != NULL) {
132+
free(path);
133+
path=NULL;
134+
}
135+
}
136+
closedir(d);
137+
}
138+
}
139+
140+
if (home != NULL) {
141+
free(home);
142+
home = NULL;
143+
}
144+
return meters_count;
145+
}
146+
147+
static void FlexMeter_updateValues(Meter* this)
148+
{
149+
for (size_t i = 0 ; i < (size_t)meters_count; i++) {
150+
if (this->m_ptr == &FlexMeter_class[i] ) {
151+
char* buff = NULL;
152+
int ret = -1;
153+
if (meter_list[i].command) {
154+
FILE* fd = popen(meter_list[i].command, "r");
155+
if (fd) {
156+
buff = String_readLine(fd);
157+
ret = pclose(fd);
158+
}
159+
}
160+
161+
if (buff && !ret) {
162+
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", buff);
163+
} else {
164+
// Once fail, free command pointer and every time print Error message
165+
if (meter_list[i].command != NULL) {
166+
free(meter_list[i].command);
167+
meter_list[i].command = NULL;
168+
}
169+
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", "[ERR] Check command");
170+
}
171+
}
172+
}
173+
}
174+
175+
const MeterClass FlexMeter_class_template = {
176+
.super = {
177+
.extends = Class(Meter),
178+
.delete = Meter_delete
179+
},
180+
.updateValues = FlexMeter_updateValues,
181+
.defaultMode = TEXT_METERMODE,
182+
.maxItems = 1,
183+
.total = 100,
184+
.attributes = DateMeter_attributes,
185+
.name = NULL,
186+
.uiName = NULL,
187+
.caption = NULL,
188+
};
189+
190+
int load_flex_modules(void)
191+
{
192+
size_t meters_num = check_for_meters();
193+
if (!FlexMeter_class && meters_num > 0) {
194+
FlexMeter_class = (MeterClass*) xCalloc(meters_num, sizeof(MeterClass));
195+
for (size_t i = 0 ; i < meters_num; i++) {
196+
memcpy(&FlexMeter_class[i], &FlexMeter_class_template, sizeof(MeterClass));
197+
FlexMeter_class[i].name = (const char*) xStrdup(meter_list[i].name);
198+
FlexMeter_class[i].uiName = (const char*) xStrdup(meter_list[i].uiName);
199+
FlexMeter_class[i].caption = (const char*) xStrdup(meter_list[i].caption);
200+
}
201+
}
202+
return meters_num;
203+
}

FlexMeter.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef HEADER_FlexMeter
2+
#define HEADER_FlexMeter
3+
/*
4+
htop - FlexMeter.c
5+
(C) 2024 Stoyan Bogdanov
6+
Released under the GNU GPLv2+, see the COPYING file
7+
in the source distribution for its full text.
8+
*/
9+
#include <stdbool.h>
10+
11+
#include "Meter.h"
12+
13+
14+
#define METERS_LIST_SIZE 30
15+
16+
#define MAX_METERS_COUNT METERS_LIST_SIZE-1
17+
18+
extern MeterClass *FlexMeter_class ;
19+
20+
int load_flex_modules(void);
21+
22+
#endif

Header.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ in the source distribution for its full text.
2020
#include "CRT.h"
2121
#include "CPUMeter.h"
2222
#include "DynamicMeter.h"
23+
#include "FlexMeter.h"
2324
#include "Macros.h"
2425
#include "Object.h"
2526
#include "Platform.h"
2627
#include "ProvideCurses.h"
2728
#include "Settings.h"
2829
#include "XUtils.h"
2930

30-
3131
Header* Header_new(Machine* host, HeaderLayout hLayout) {
3232
Header* this = xCalloc(1, sizeof(Header));
3333
this->columns = xMallocArray(HeaderLayout_getColumns(hLayout), sizeof(Vector*));
@@ -121,6 +121,14 @@ void Header_populateFromSettings(Header* this) {
121121
const Settings* settings = this->host->settings;
122122
Header_setLayout(this, settings->hLayout);
123123

124+
int num = load_flex_modules();
125+
int platform_size = 0;
126+
127+
for (platform_size = 0; Platform_meterTypes[platform_size] != NULL; platform_size++);
128+
for (int i = 0; i < num; i++) Platform_meterTypes[platform_size+i]=FlexMeter_class+i;
129+
130+
Platform_meterTypes[platform_size+num]=NULL;
131+
124132
Header_forEachColumn(this, col) {
125133
const MeterColumnSetting* colSettings = &settings->hColumns[col];
126134
Vector_prune(this->columns[col]);

Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ myhtopsources = \
5252
DynamicScreen.c \
5353
EnvScreen.c \
5454
FileDescriptorMeter.c \
55+
FlexMeter.c \
5556
FunctionBar.c \
5657
Hashtable.c \
5758
Header.c \
@@ -93,6 +94,7 @@ myhtopsources = \
9394
Vector.c \
9495
XUtils.c
9596

97+
9698
myhtopheaders = \
9799
Action.h \
98100
Affinity.h \
@@ -118,6 +120,7 @@ myhtopheaders = \
118120
DynamicScreen.h \
119121
EnvScreen.h \
120122
FileDescriptorMeter.h \
123+
FlexMeter.h \
121124
FunctionBar.h \
122125
Hashtable.h \
123126
Header.h \
@@ -165,6 +168,7 @@ myhtopheaders = \
165168
Vector.h \
166169
XUtils.h
167170

171+
168172
# Linux
169173
# -----
170174

Meter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ Meter* Meter_new(const Machine* host, unsigned int param, const MeterClass* type
377377
this->values = type->maxItems ? xCalloc(type->maxItems, sizeof(double)) : NULL;
378378
this->total = type->total;
379379
this->caption = xStrdup(type->caption);
380+
this->m_ptr = type;
380381
if (Meter_initFn(this)) {
381382
Meter_init(this);
382383
}

Meter.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ typedef struct MeterClass_ {
6767
const MeterModeId defaultMode;
6868
const double total;
6969
const int* const attributes;
70-
const char* const name; /* internal name of the meter, must not contain any space */
71-
const char* const uiName; /* display name in header setup menu */
72-
const char* const caption; /* prefix in the actual header */
70+
const char* name; /* internal name of the meter, must not contain any space */
71+
const char* uiName; /* display name in header setup menu */
72+
const char* caption; /* prefix in the actual header */
7373
const char* const description; /* optional meter description in header setup menu */
7474
const uint8_t maxItems;
7575
const bool isMultiColumn; /* whether the meter draws multiple sub-columns (defaults to false) */
@@ -102,8 +102,9 @@ typedef struct GraphData_ {
102102
struct Meter_ {
103103
Object super;
104104
Meter_Draw draw;
105-
const Machine* host;
106105

106+
const Machine* host;
107+
const MeterClass* m_ptr;
107108
char* caption;
108109
MeterModeId mode;
109110
unsigned int param;

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ AM_INIT_AUTOMAKE([-Wall std-options subdir-objects])
2525

2626
# ----------------------------------------------------------------------
2727

28+
AC_DEFINE([MAX_PLATFORM_METERS], [100], [Set max meters for number])
2829

2930
# ----------------------------------------------------------------------
3031
# Checks for platform.

docs/FlexMeter/Template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name=template
2+
command=echo "`uptime`"
3+
type=TEXT_METERMODE
4+
caption="UPTIME"

0 commit comments

Comments
 (0)