|
| 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 "config.h" |
| 9 | +#include "FlexMeter.h" |
| 10 | +#include <time.h> |
| 11 | +#include <sys/time.h> |
| 12 | +#include <dirent.h> |
| 13 | +#include "CRT.h" |
| 14 | +#include "Object.h" |
| 15 | + |
| 16 | +#include <unistd.h> |
| 17 | +#include <sys/types.h> |
| 18 | +#include <pwd.h> |
| 19 | + |
| 20 | +#define FLEX_CFG_FOLDER ".config/htop/FlexMeter" |
| 21 | + |
| 22 | +typedef struct { |
| 23 | + char* name; |
| 24 | + char* command; |
| 25 | + char* type; |
| 26 | + char* caption; |
| 27 | + char* uiName; |
| 28 | +}_flex_meter; |
| 29 | + |
| 30 | +_flex_meter meter_list[30]; |
| 31 | + |
| 32 | +int meter_list_idx=0; |
| 33 | +static int meters_count=0; |
| 34 | + |
| 35 | +static const int DateMeter_attributes[] = { |
| 36 | + FLEX |
| 37 | +}; |
| 38 | + |
| 39 | +MeterClass * FlexMeter_class = NULL; |
| 40 | + |
| 41 | +int check_for_meters(void); |
| 42 | + |
| 43 | +static int parse_input(char * line) |
| 44 | +{ |
| 45 | + for (int i = strlen(line);i>0;i--) |
| 46 | + { |
| 47 | + if ((line[i]=='\r') || (line[i]=='\n')) |
| 48 | + { |
| 49 | + line[i]='\0'; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (!strncmp(line,"name=",5)) |
| 54 | + { |
| 55 | + meter_list[meters_count].uiName = xStrdup(line+5); |
| 56 | + } |
| 57 | + else if (!strncmp(line,"command=",7)) |
| 58 | + { |
| 59 | + meter_list[meters_count].command = xStrdup(line+8); |
| 60 | + } |
| 61 | + else if (!strncmp(line,"caption=",7)) |
| 62 | + { |
| 63 | + meter_list[meters_count].caption = xStrdup(line+8); |
| 64 | + } |
| 65 | + else if (!strncmp(line,"type=",5)) |
| 66 | + { |
| 67 | + meter_list[meters_count].type = xStrdup(line+6); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +static int load_config(char * file) |
| 78 | +{ |
| 79 | + int ret=0; |
| 80 | + char buff[2048]; |
| 81 | + memset(buff,0,2048); |
| 82 | + FILE *fp = fopen( file , "r"); |
| 83 | + while( NULL != fgets(buff, 2048, fp) ) |
| 84 | + { |
| 85 | + parse_input(buff); |
| 86 | + memset(buff,0,2048); |
| 87 | + } |
| 88 | + |
| 89 | + fclose(fp); |
| 90 | + return ret; |
| 91 | +} |
| 92 | + |
| 93 | +int check_for_meters(void) |
| 94 | +{ |
| 95 | + char path[400]; // full path |
| 96 | + DIR *d; |
| 97 | + struct dirent *dir; |
| 98 | + struct passwd *pw = getpwuid(getuid()); |
| 99 | + const char *homedir = pw->pw_dir; |
| 100 | + |
| 101 | + // path to home folder 1 for zero 1 for slash |
| 102 | + char * home = (char * ) xCalloc(1,(strlen(homedir) + strlen(FLEX_CFG_FOLDER) + 2)); |
| 103 | + |
| 104 | + sprintf(home,"%s/%s",homedir,FLEX_CFG_FOLDER); |
| 105 | + |
| 106 | + d = opendir(home); |
| 107 | + if (d) { |
| 108 | + while ((dir = readdir(d)) != NULL) { |
| 109 | + if ( dir->d_name[0]!='.' && strcmp(".",dir->d_name) && strcmp("..",dir->d_name)) |
| 110 | + { |
| 111 | + meter_list[meters_count].name = xStrdup(dir->d_name); |
| 112 | + memset(path,0,80); |
| 113 | + sprintf(path,"%s/%s",home,dir->d_name); |
| 114 | + if( 0 != load_config(path) ) |
| 115 | + { |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + if ( meters_count < MAX_METERS_COUNT ) |
| 120 | + { |
| 121 | + meters_count++; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + break; // go out we reach the limit |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + closedir(d); |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + if (0 == mkdir(home,0777)) |
| 134 | + { |
| 135 | + char template [4][40] = |
| 136 | + { |
| 137 | + "name=template", |
| 138 | + "command=echo \"`uptime`\"", |
| 139 | + "type=TEXT_METERMODE", |
| 140 | + "caption=\"UPTIME\"" |
| 141 | + }; |
| 142 | + |
| 143 | + char template_filename[500]; |
| 144 | + sprintf(template_filename,"%s/%s",home,".Template"); |
| 145 | + FILE * fp = fopen(template_filename,"w"); |
| 146 | + if (fp != NULL ) |
| 147 | + { |
| 148 | + for (int i=0; i<4; i++) |
| 149 | + { |
| 150 | + fprintf(fp,"%s\n",template[i]); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + free(home); |
| 157 | + return meters_count; |
| 158 | +} |
| 159 | + |
| 160 | +static void FlexMeter_updateValues(Meter* this) |
| 161 | +{ |
| 162 | + for (int i =0 ; i < meters_count; i++) |
| 163 | + { |
| 164 | + if (this->m_ptr == &FlexMeter_class[i] ) |
| 165 | + { |
| 166 | + char buff[256]={0}; |
| 167 | + int ret = -1; |
| 168 | + memset(buff,0,256); |
| 169 | + if ( meter_list[i].command[0] != 0 ) |
| 170 | + { |
| 171 | + FILE* fd = popen(meter_list[i].command, "r"); |
| 172 | + if (fd) |
| 173 | + { |
| 174 | + if (fgets(buff, 256, fd) == NULL) |
| 175 | + ret = pclose(fd); |
| 176 | + else |
| 177 | + ret = pclose(fd); |
| 178 | + } |
| 179 | + |
| 180 | + if( (buff[0] != 0) && (ret == 0) ) |
| 181 | + { |
| 182 | + int l = strlen(buff); |
| 183 | + buff[l-1] = '\0'; |
| 184 | + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s",buff); |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", "Read CMD ERR"); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +const MeterClass FlexMeter_class_template = { |
| 196 | + .super = { |
| 197 | + .extends = Class(Meter), |
| 198 | + .delete = Meter_delete |
| 199 | + }, |
| 200 | + .updateValues = FlexMeter_updateValues, |
| 201 | + .defaultMode = TEXT_METERMODE, |
| 202 | + .maxItems = 1, |
| 203 | + .total = 100, |
| 204 | + .attributes = DateMeter_attributes, |
| 205 | + .name = NULL, |
| 206 | + .uiName = NULL, |
| 207 | + .caption = NULL, |
| 208 | +}; |
| 209 | + |
| 210 | +int load_flex_modules(void) |
| 211 | +{ |
| 212 | + uint8_t meters_num = check_for_meters(); |
| 213 | + if (FlexMeter_class == NULL && meters_num!=0) |
| 214 | + { |
| 215 | + FlexMeter_class = (MeterClass*) xCalloc(meters_num,sizeof(MeterClass)); |
| 216 | + for (uint8_t i=0 ; i<meters_num;i++) |
| 217 | + { |
| 218 | + memcpy(&FlexMeter_class[i],&FlexMeter_class_template,sizeof(MeterClass)); |
| 219 | + |
| 220 | + FlexMeter_class[i].name = (const char *) xStrdup(meter_list[i].name); |
| 221 | + FlexMeter_class[i].uiName = (const char *) xStrdup(meter_list[i].uiName); |
| 222 | + FlexMeter_class[i].caption = (const char *) xStrdup(meter_list[i].caption); |
| 223 | + } |
| 224 | + } |
| 225 | + return (int)meters_num; |
| 226 | +} |
0 commit comments