-
-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
10 changed files
with
256 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* | ||
htop - FlexMeter.c | ||
(C) 2024 Stoyan Bogdanov | ||
Released under the GNU GPLv2+, see the COPYING file | ||
in the source distribution for its full text. | ||
*/ | ||
|
||
#include <dirent.h> | ||
#include <pwd.h> | ||
#include <sys/time.h> | ||
#include <sys/types.h> | ||
#include <time.h> | ||
#include <unistd.h> | ||
|
||
#include "CRT.h" | ||
#include "config.h" | ||
#include "FlexMeter.h" | ||
#include "Object.h" | ||
|
||
|
||
#define FLEX_CFG_FOLDER ".config/htop/FlexMeter" | ||
|
||
typedef struct { | ||
char* name; | ||
char* command; | ||
char* type; | ||
char* caption; | ||
char* uiName; | ||
} _flex_meter; | ||
|
||
_flex_meter meter_list[METERS_LIST_SIZE]; | ||
|
||
static int meters_count = 0; | ||
|
||
static const int DateMeter_attributes[] = { | ||
FLEX | ||
}; | ||
|
||
MeterClass* FlexMeter_class = NULL; | ||
|
||
static int check_for_meters(void); | ||
|
||
static bool parse_input(_flex_meter *meter, char* line) | ||
{ | ||
switch(line[0]) | ||
{ | ||
case 'n': | ||
if (String_startsWith(line, "name=")) { | ||
xAsprintf(&meter->uiName, "Flex: %s", line + 5); | ||
} | ||
break; | ||
case 'c': | ||
if (String_startsWith(line, "command=")) { | ||
meter->command = xStrdup(line + 8); | ||
} else if (String_startsWith(line, "caption=")) { | ||
meter->caption = xStrdup(line + 8); | ||
} | ||
break; | ||
case 't': | ||
if (String_startsWith(line, "type=")) { | ||
meter->type = xStrdup(line + 6); | ||
} | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static bool load_config(_flex_meter *meter, char* file) | ||
{ | ||
bool ret = false; | ||
FILE* fp = fopen(file, "r"); | ||
|
||
if (fp != NULL) { | ||
char* buff; | ||
while ((buff = String_readLine(fp)) != NULL) { | ||
ret = parse_input(meter, buff); | ||
if (!ret) { | ||
break; | ||
} | ||
} | ||
free(buff); | ||
buff = NULL; | ||
} | ||
|
||
fclose(fp); | ||
return ret; | ||
} | ||
|
||
static int check_for_meters(void) | ||
{ | ||
char* path; | ||
struct dirent* dir; | ||
struct passwd* pw = getpwuid(getuid()); | ||
const char* homedir = pw->pw_dir; | ||
char* home = NULL; | ||
bool ret; | ||
|
||
xAsprintf(&home, "%s/%s", homedir, FLEX_CFG_FOLDER); | ||
struct stat fileStat; | ||
|
||
if (stat(home, &fileStat) < 0) { | ||
return -1; | ||
} | ||
|
||
uint32_t uid = getuid(); | ||
|
||
if ((fileStat.st_uid == uid) && S_ISDIR(fileStat.st_mode) && ((fileStat.st_mode & 0777) == 0700)) { | ||
DIR* d = opendir(home); | ||
if (d) { | ||
while ((dir = readdir(d)) != NULL) { | ||
if ( dir->d_name[0] == '.') { | ||
/* We are ignoring all files starting with . like ".Template" | ||
* and "." ".." directories | ||
*/ | ||
continue; | ||
} | ||
|
||
meter_list[meters_count].name = xStrdup(dir->d_name); | ||
xAsprintf(&path, "%s/%s", home, dir->d_name); | ||
|
||
if (access(path, R_OK | W_OK | X_OK) == 0) { | ||
ret = load_config(&meter_list[meters_count], path); | ||
|
||
if (ret && (meters_count < MAX_METERS_COUNT)) { | ||
meters_count++; | ||
} | ||
} | ||
if (path != NULL) { | ||
free(path); | ||
path=NULL; | ||
} | ||
} | ||
closedir(d); | ||
} | ||
} | ||
|
||
if (home != NULL) { | ||
free(home); | ||
home = NULL; | ||
} | ||
return meters_count; | ||
} | ||
|
||
static void FlexMeter_updateValues(Meter* this) | ||
{ | ||
for (size_t i = 0 ; i < (size_t)meters_count; i++) { | ||
if (this->m_ptr == &FlexMeter_class[i] ) { | ||
char* buff = NULL; | ||
int ret = -1; | ||
if (meter_list[i].command) { | ||
FILE* fd = popen(meter_list[i].command, "r"); | ||
if (fd) { | ||
buff = String_readLine(fd); | ||
ret = pclose(fd); | ||
} | ||
} | ||
|
||
if (buff && !ret) { | ||
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", buff); | ||
} else { | ||
// Once fail, free command pointer and every time print Error message | ||
if (meter_list[i].command != NULL) { | ||
free(meter_list[i].command); | ||
meter_list[i].command = NULL; | ||
} | ||
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", "[ERR] Check command"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const MeterClass FlexMeter_class_template = { | ||
.super = { | ||
.extends = Class(Meter), | ||
.delete = Meter_delete | ||
}, | ||
.updateValues = FlexMeter_updateValues, | ||
.defaultMode = TEXT_METERMODE, | ||
.maxItems = 1, | ||
.total = 100, | ||
.attributes = DateMeter_attributes, | ||
.name = NULL, | ||
.uiName = NULL, | ||
.caption = NULL, | ||
}; | ||
|
||
int load_flex_modules(void) | ||
{ | ||
size_t meters_num = check_for_meters(); | ||
if (!FlexMeter_class && meters_num > 0) { | ||
FlexMeter_class = (MeterClass*) xCalloc(meters_num, sizeof(MeterClass)); | ||
for (size_t i = 0 ; i < meters_num; i++) { | ||
memcpy(&FlexMeter_class[i], &FlexMeter_class_template, sizeof(MeterClass)); | ||
FlexMeter_class[i].name = (const char*) xStrdup(meter_list[i].name); | ||
FlexMeter_class[i].uiName = (const char*) xStrdup(meter_list[i].uiName); | ||
FlexMeter_class[i].caption = (const char*) xStrdup(meter_list[i].caption); | ||
} | ||
} | ||
return meters_num; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef HEADER_FlexMeter | ||
#define HEADER_FlexMeter | ||
/* | ||
htop - FlexMeter.c | ||
(C) 2024 Stoyan Bogdanov | ||
Released under the GNU GPLv2+, see the COPYING file | ||
in the source distribution for its full text. | ||
*/ | ||
#include <stdbool.h> | ||
|
||
#include "Meter.h" | ||
|
||
|
||
#define METERS_LIST_SIZE 30 | ||
|
||
#define MAX_METERS_COUNT METERS_LIST_SIZE-1 | ||
|
||
extern MeterClass *FlexMeter_class ; | ||
|
||
int load_flex_modules(void); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name=template | ||
command=echo "`uptime`" | ||
type=TEXT_METERMODE | ||
caption="UPTIME" |