Compare commits

...

4 Commits
riscv ... feat

Author SHA1 Message Date
Dr-Noob
2c4a2a0253 [v0.97] Automatically adjust text to the width of the terminal, as suggested by #65 2021-04-10 15:41:44 +02:00
Dr-Noob
61cd0783ac [v0.97] Make loop option work on ARM too 2021-04-10 14:33:45 +02:00
Dr-Noob
5242adf4ac [v0.97] Fix compilation in macOS 2021-04-10 13:34:28 +02:00
Dr-Noob
3a2b04de1e [v0.97] Add loop option as suggested by #75 2021-04-10 13:22:45 +02:00
6 changed files with 155 additions and 31 deletions

View File

@@ -20,6 +20,7 @@ static const char *SYTLES_STR_LIST[] = {
struct args_struct { struct args_struct {
bool debug_flag; bool debug_flag;
bool help_flag; bool help_flag;
bool loop_flag;
bool raw_flag; bool raw_flag;
bool verbose_flag; bool verbose_flag;
bool version_flag; bool version_flag;
@@ -30,6 +31,7 @@ struct args_struct {
const char args_chr[] = { const char args_chr[] = {
/* [ARG_CHAR_STYLE] = */ 's', /* [ARG_CHAR_STYLE] = */ 's',
/* [ARG_CHAR_COLOR] = */ 'c', /* [ARG_CHAR_COLOR] = */ 'c',
/* [ARG_CHAR_LOPP] = */ 'l',
/* [ARG_CHAR_HELP] = */ 'h', /* [ARG_CHAR_HELP] = */ 'h',
/* [ARG_CHAR_RAW] = */ 'r', /* [ARG_CHAR_RAW] = */ 'r',
/* [ARG_CHAR_DEBUG] = */ 'd', /* [ARG_CHAR_DEBUG] = */ 'd',
@@ -40,6 +42,7 @@ const char args_chr[] = {
const char *args_str[] = { const char *args_str[] = {
/* [ARG_CHAR_STYLE] = */ "style", /* [ARG_CHAR_STYLE] = */ "style",
/* [ARG_CHAR_COLOR] = */ "color", /* [ARG_CHAR_COLOR] = */ "color",
/* [ARG_CHAR_LOOP] = */ "loop",
/* [ARG_CHAR_HELP] = */ "help", /* [ARG_CHAR_HELP] = */ "help",
/* [ARG_CHAR_RAW] = */ "raw", /* [ARG_CHAR_RAW] = */ "raw",
/* [ARG_CHAR_DEBUG] = */ "debug", /* [ARG_CHAR_DEBUG] = */ "debug",
@@ -65,6 +68,10 @@ bool show_version() {
return args.version_flag; return args.version_flag;
} }
bool loop_mode() {
return args.loop_flag;
}
bool show_debug() { bool show_debug() {
return args.debug_flag; return args.debug_flag;
} }
@@ -190,13 +197,13 @@ char* build_short_options() {
memset(str, 0, sizeof(char) * (len*2 + 1)); memset(str, 0, sizeof(char) * (len*2 + 1));
#ifdef ARCH_X86 #ifdef ARCH_X86
sprintf(str, "%c:%c:%c%c%c%c%c", sprintf(str, "%c:%c:%c%c%c%c%c%c",
c[ARG_STYLE], c[ARG_COLOR], c[ARG_HELP], c[ARG_RAW], c[ARG_STYLE], c[ARG_COLOR], c[ARG_HELP], c[ARG_RAW],
c[ARG_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION]); c[ARG_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION], c[ARG_LOOP]);
#else #else
sprintf(str, "%c:%c:%c%c%c%c", sprintf(str, "%c:%c:%c%c%c%c%c",
c[ARG_STYLE], c[ARG_COLOR], c[ARG_HELP], c[ARG_STYLE], c[ARG_COLOR], c[ARG_HELP],
c[ARG_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION]); c[ARG_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION], c[ARG_LOOP]);
#endif #endif
return str; return str;
@@ -210,6 +217,7 @@ bool parse_args(int argc, char* argv[]) {
bool color_flag = false; bool color_flag = false;
args.debug_flag = false; args.debug_flag = false;
args.raw_flag = false; args.raw_flag = false;
args.loop_flag = false;
args.verbose_flag = false; args.verbose_flag = false;
args.help_flag = false; args.help_flag = false;
args.style = STYLE_EMPTY; args.style = STYLE_EMPTY;
@@ -223,6 +231,7 @@ bool parse_args(int argc, char* argv[]) {
{args_str[ARG_RAW], no_argument, 0, args_chr[ARG_RAW] }, {args_str[ARG_RAW], no_argument, 0, args_chr[ARG_RAW] },
#endif #endif
{args_str[ARG_DEBUG], no_argument, 0, args_chr[ARG_DEBUG] }, {args_str[ARG_DEBUG], no_argument, 0, args_chr[ARG_DEBUG] },
{args_str[ARG_LOOP], no_argument, 0, args_chr[ARG_LOOP] },
{args_str[ARG_VERBOSE], no_argument, 0, args_chr[ARG_VERBOSE] }, {args_str[ARG_VERBOSE], no_argument, 0, args_chr[ARG_VERBOSE] },
{args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] }, {args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] },
{0, 0, 0, 0} {0, 0, 0, 0}
@@ -255,6 +264,9 @@ bool parse_args(int argc, char* argv[]) {
} }
break; break;
} }
else if(opt == args_chr[ARG_LOOP]) {
args.loop_flag = true;
}
else if(opt == args_chr[ARG_HELP]) { else if(opt == args_chr[ARG_HELP]) {
args.help_flag = true; args.help_flag = true;
} }

View File

@@ -29,6 +29,7 @@ enum {
enum { enum {
ARG_STYLE, ARG_STYLE,
ARG_COLOR, ARG_COLOR,
ARG_LOOP,
ARG_HELP, ARG_HELP,
ARG_RAW, ARG_RAW,
ARG_DEBUG, ARG_DEBUG,
@@ -44,6 +45,7 @@ extern const char *args_str[];
int max_arg_str_length(); int max_arg_str_length();
bool parse_args(int argc, char* argv[]); bool parse_args(int argc, char* argv[]);
bool show_help(); bool show_help();
bool loop_mode();
bool show_raw(); bool show_raw();
bool show_debug(); bool show_debug();
bool show_version(); bool show_version();

View File

@@ -68,3 +68,7 @@ void set_log_level(bool verbose) {
int max(int a, int b) { int max(int a, int b) {
return a > b ? a : b; return a > b ? a : b;
} }
int min(int a, int b) {
return a < b ? a : b;
}

View File

@@ -8,5 +8,6 @@ void printWarn(const char *fmt, ...);
void printErr(const char *fmt, ...); void printErr(const char *fmt, ...);
void printBug(const char *fmt, ...); void printBug(const char *fmt, ...);
int max(int a, int b); int max(int a, int b);
int min(int a, int b);
#endif #endif

View File

@@ -27,6 +27,7 @@ void print_help(char *argv[]) {
printf("Options: \n"); printf("Options: \n");
printf(" -%c, --%s %*s Set the color scheme (by default, cpufetch uses the system color scheme)\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), ""); printf(" -%c, --%s %*s Set the color scheme (by default, cpufetch uses the system color scheme)\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), "");
printf(" -%c, --%s %*s Set the style of CPU art\n", c[ARG_STYLE], t[ARG_STYLE], (int) (max_len-strlen(t[ARG_STYLE])), ""); printf(" -%c, --%s %*s Set the style of CPU art\n", c[ARG_STYLE], t[ARG_STYLE], (int) (max_len-strlen(t[ARG_STYLE])), "");
printf(" -%c, --%s %*s Runs cpufetch in a loop\n", c[ARG_LOOP], t[ARG_LOOP], (int) (max_len-strlen(t[ARG_LOOP])), "");
#ifdef ARCH_X86 #ifdef ARCH_X86
printf(" -%c, --%s %*s Prints CPU model and cpuid levels (debug purposes)\n", c[ARG_DEBUG], t[ARG_DEBUG], (int) (max_len-strlen(t[ARG_DEBUG])), ""); printf(" -%c, --%s %*s Prints CPU model and cpuid levels (debug purposes)\n", c[ARG_DEBUG], t[ARG_DEBUG], (int) (max_len-strlen(t[ARG_DEBUG])), "");
#elif ARCH_ARM #elif ARCH_ARM

View File

@@ -1,3 +1,15 @@
#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#else
#ifdef __linux__
#define _POSIX_C_SOURCE 199309L
#endif
#include <sys/ioctl.h>
#include <unistd.h>
#endif
#include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@@ -17,11 +29,6 @@
#include "../arm/soc.h" #include "../arm/soc.h"
#endif #endif
#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#endif
#define max(a,b) (((a)>(b))?(a):(b)) #define max(a,b) (((a)>(b))?(a):(b))
#define MAX_ATTRIBUTES 100 #define MAX_ATTRIBUTES 100
@@ -118,6 +125,29 @@ struct ascii {
STYLE style; STYLE style;
}; };
struct terminal {
int w;
int h;
};
volatile sig_atomic_t loop = 1;
#ifdef _WIN32
BOOL WINAPI loop_mode_handler(DWORD signum) {
if (signum == CTRL_C_EVENT) {
loop = 0;
}
return TRUE;
}
#else
void loop_mode_handler(int signum) {
if(signum == SIGINT) {
loop = 0;
}
}
#endif
void setAttribute(struct ascii* art, int type, char* value) { void setAttribute(struct ascii* art, int type, char* value) {
art->attributes[art->n_attributes_set]->value = value; art->attributes[art->n_attributes_set]->value = value;
art->attributes[art->n_attributes_set]->type = type; art->attributes[art->n_attributes_set]->type = type;
@@ -345,6 +375,33 @@ uint32_t longest_attribute_length(struct ascii* art) {
return max; return max;
} }
bool run_loop_mode() {
#ifdef _WIN32
if (!SetConsoleCtrlHandler(loop_mode_handler, TRUE)) {
printErr("SetConsoleCtrlHandler failed");
return false;
}
#else
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = loop_mode_handler;
if(sigaction(SIGINT, &act, NULL) == -1) {
perror("sigaction");
return false;
}
#endif
while (loop) {
#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#endif
}
return true;
}
#ifdef ARCH_X86 #ifdef ARCH_X86
void print_algorithm_intel(struct ascii* art, int n, bool* flag) { void print_algorithm_intel(struct ascii* art, int n, bool* flag) {
for(int i=0; i < LINE_SIZE; i++) { for(int i=0; i < LINE_SIZE; i++) {
@@ -382,46 +439,59 @@ void print_algorithm_amd(struct ascii* art, int n, bool* flag) {
} }
} }
void print_ascii_x86(struct ascii* art, uint32_t la, void (*callback_print_algorithm)(struct ascii* art, int i, bool* flag)) { void print_ascii_x86(struct ascii* art, struct terminal* term, uint32_t la, void (*callback_print_algorithm)(struct ascii* art, int i, bool* flag)) {
int attr_to_print = 0; int attr_to_print = 0;
int attr_type; int attr_type;
char* attr_value; char* attr_value;
int attr_print_len;
uint32_t attr_space_left;
uint32_t space_right; uint32_t space_right;
uint32_t space_up = (NUMBER_OF_LINES - art->n_attributes_set)/2; uint32_t space_up = (NUMBER_OF_LINES - art->n_attributes_set)/2;
uint32_t space_down = NUMBER_OF_LINES - art->n_attributes_set - space_up; uint32_t space_down = NUMBER_OF_LINES - art->n_attributes_set - space_up;
bool flag = false; bool flag = false;
bool print_ascii_art;
if(term->w - LINE_SIZE - 1 - (int) la > 0) {
print_ascii_art = true;
attr_space_left = term->w - LINE_SIZE - 1 - la;
}
else {
print_ascii_art = false;
attr_space_left = term->w - 1 - la;
}
printf("\n"); printf("\n");
for(uint32_t n=0;n<NUMBER_OF_LINES;n++) { for(uint32_t n=0;n<NUMBER_OF_LINES;n++) {
callback_print_algorithm(art, n, &flag); if(print_ascii_art) callback_print_algorithm(art, n, &flag);
if(n > space_up-1 && n < NUMBER_OF_LINES-space_down) { if(n > space_up-1 && n < NUMBER_OF_LINES-space_down) {
attr_type = art->attributes[attr_to_print]->type; attr_type = art->attributes[attr_to_print]->type;
attr_value = art->attributes[attr_to_print]->value; attr_value = art->attributes[attr_to_print]->value;
attr_to_print++; attr_to_print++;
attr_print_len = min(strlen(attr_value), attr_space_left);
attr_print_len = max(attr_print_len, 0);
space_right = 1 + (la - strlen(ATTRIBUTE_FIELDS[attr_type])); space_right = 1 + (la - strlen(ATTRIBUTE_FIELDS[attr_type]));
printf("%s%s%s%*s%s%s%s\n", art->color1_text, ATTRIBUTE_FIELDS[attr_type], art->reset, space_right, "", art->color2_text, attr_value, art->reset); printf("%s%s%s%*s%s%.*s%s\n", art->color1_text, ATTRIBUTE_FIELDS[attr_type], art->reset, space_right, "", art->color2_text, attr_print_len, attr_value, art->reset);
} }
else printf("\n"); else printf("\n");
} }
printf("\n"); printf("\n");
} }
void print_ascii(struct ascii* art) { void print_ascii(struct ascii* art, struct terminal* term) {
uint32_t longest_attribute = longest_attribute_length(art); uint32_t longest_attribute = longest_attribute_length(art);
if(art->vendor == CPU_VENDOR_INTEL) if(art->vendor == CPU_VENDOR_INTEL)
print_ascii_x86(art, longest_attribute, &print_algorithm_intel); print_ascii_x86(art, term, longest_attribute, &print_algorithm_intel);
else if(art->vendor == CPU_VENDOR_AMD) else if(art->vendor == CPU_VENDOR_AMD)
print_ascii_x86(art, longest_attribute, &print_algorithm_amd); print_ascii_x86(art, term, longest_attribute, &print_algorithm_amd);
else { else {
printBug("Invalid CPU vendor: %d\n", art->vendor); printBug("Invalid CPU vendor: %d\n", art->vendor);
} }
} }
bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) { bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs, struct terminal* term) {
struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs); struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs);
if(art == NULL) if(art == NULL)
return false; return false;
@@ -436,7 +506,6 @@ bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
char* avx = get_str_avx(cpu); char* avx = get_str_avx(cpu);
char* fma = get_str_fma(cpu); char* fma = get_str_fma(cpu);
char* l1i = get_str_l1i(cpu->cach); char* l1i = get_str_l1i(cpu->cach);
char* l1d = get_str_l1d(cpu->cach); char* l1d = get_str_l1d(cpu->cach);
char* l2 = get_str_l2(cpu->cach); char* l2 = get_str_l2(cpu->cach);
@@ -474,7 +543,11 @@ bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
return false; return false;
} }
print_ascii(art); print_ascii(art, term);
if(loop_mode()) {
return run_loop_mode();
}
free(manufacturing_process); free(manufacturing_process);
free(max_frequency); free(max_frequency);
@@ -542,7 +615,7 @@ void print_algorithm_arm(struct ascii* art, int n) {
} }
} }
void print_ascii_arm(struct ascii* art, uint32_t la, void (*callback_print_algorithm)(struct ascii* art, int n)) { void print_ascii_arm(struct ascii* art, struct terminal* term, uint32_t la, void (*callback_print_algorithm)(struct ascii* art, int n)) {
int attr_to_print = 0; int attr_to_print = 0;
int attr_type; int attr_type;
char* attr_value; char* attr_value;
@@ -601,18 +674,18 @@ void print_ascii(struct ascii* art) {
uint32_t longest_attribute = longest_attribute_length(art); uint32_t longest_attribute = longest_attribute_length(art);
if(art->vendor == SOC_VENDOR_SNAPDRAGON || art->vendor == SOC_VENDOR_MEDIATEK || art->vendor == SOC_VENDOR_KIRIN || art->vendor == SOC_VENDOR_BROADCOM) if(art->vendor == SOC_VENDOR_SNAPDRAGON || art->vendor == SOC_VENDOR_MEDIATEK || art->vendor == SOC_VENDOR_KIRIN || art->vendor == SOC_VENDOR_BROADCOM)
print_ascii_arm(art, longest_attribute, &print_algorithm_snapd_mtk); print_ascii_arm(art, term, longest_attribute, &print_algorithm_snapd_mtk);
else if(art->vendor == SOC_VENDOR_EXYNOS) else if(art->vendor == SOC_VENDOR_EXYNOS)
print_ascii_arm(art, longest_attribute, &print_algorithm_samsung); print_ascii_arm(art, term, longest_attribute, &print_algorithm_samsung);
else { else {
if(art->vendor != SOC_VENDOR_UNKNOWN) if(art->vendor != SOC_VENDOR_UNKNOWN)
printWarn("Invalid SOC vendor: %d\n", art->vendor); printWarn("Invalid SOC vendor: %d\n", art->vendor);
print_ascii_arm(art, longest_attribute, &print_algorithm_arm); print_ascii_arm(art, term, longest_attribute, &print_algorithm_arm);
} }
} }
bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) { bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs, struct terminal* term) {
struct ascii* art = set_ascii(get_soc_vendor(cpu->soc), s, cs); struct ascii* art = set_ascii(get_soc_vendor(cpu->soc), s, cs);
if(art == NULL) if(art == NULL)
return false; return false;
@@ -680,7 +753,11 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
if(cpu->hv->present) if(cpu->hv->present)
setAttribute(art, ATTRIBUTE_HYPERVISOR, cpu->hv->hv_name); setAttribute(art, ATTRIBUTE_HYPERVISOR, cpu->hv->hv_name);
print_ascii(art); print_ascii(art, term);
if(loop_mode()) {
return run_loop_mode();
}
free(manufacturing_process); free(manufacturing_process);
free(pp); free(pp);
@@ -697,6 +774,31 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
} }
#endif #endif
struct terminal* get_terminal_size() {
struct terminal* term = malloc(sizeof(struct terminal));
memset(term, 0, sizeof(struct terminal));
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) == 0) {
printWarn("GetConsoleScreenBufferInfo failed");
return term;
}
term->w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
term->h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
#else
struct winsize w;
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
perror("ioctl");
return term;
}
term->h = w.ws_row;
term->w = w.ws_col;
#endif
return term;
}
bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs) { bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
// Sanity check of ASCII arts // Sanity check of ASCII arts
int len = sizeof(ASCII_ARRAY) / sizeof(ASCII_ARRAY[0]); int len = sizeof(ASCII_ARRAY) / sizeof(ASCII_ARRAY[0]);
@@ -708,9 +810,11 @@ bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
} }
} }
struct terminal* term = get_terminal_size();
#ifdef ARCH_X86 #ifdef ARCH_X86
return print_cpufetch_x86(cpu, s, cs); return print_cpufetch_x86(cpu, s, cs, term);
#elif ARCH_ARM #elif ARCH_ARM
return print_cpufetch_arm(cpu, s, cs); return print_cpufetch_arm(cpu, s, cs, term);
#endif #endif
} }