From 3a2b04de1ef9ff745e1a7e3103ced25b22ab4f53 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Sat, 10 Apr 2021 13:22:45 +0200 Subject: [PATCH] [v0.97] Add loop option as suggested by #75 --- src/common/args.c | 22 ++++++++++---- src/common/args.h | 2 ++ src/common/main.c | 1 + src/common/printer.c | 70 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/common/args.c b/src/common/args.c index e9239bc..b54b52a 100644 --- a/src/common/args.c +++ b/src/common/args.c @@ -20,6 +20,7 @@ static const char *SYTLES_STR_LIST[] = { struct args_struct { bool debug_flag; bool help_flag; + bool loop_flag; bool raw_flag; bool verbose_flag; bool version_flag; @@ -30,6 +31,7 @@ struct args_struct { const char args_chr[] = { /* [ARG_CHAR_STYLE] = */ 's', /* [ARG_CHAR_COLOR] = */ 'c', + /* [ARG_CHAR_LOPP] = */ 'l', /* [ARG_CHAR_HELP] = */ 'h', /* [ARG_CHAR_RAW] = */ 'r', /* [ARG_CHAR_DEBUG] = */ 'd', @@ -40,6 +42,7 @@ const char args_chr[] = { const char *args_str[] = { /* [ARG_CHAR_STYLE] = */ "style", /* [ARG_CHAR_COLOR] = */ "color", + /* [ARG_CHAR_LOOP] = */ "loop", /* [ARG_CHAR_HELP] = */ "help", /* [ARG_CHAR_RAW] = */ "raw", /* [ARG_CHAR_DEBUG] = */ "debug", @@ -65,12 +68,16 @@ bool show_version() { return args.version_flag; } +bool loop_mode() { + return args.loop_flag; +} + bool show_debug() { return args.debug_flag; } bool show_raw() { - return args.raw_flag; + return args.raw_flag; } bool verbose_enabled() { @@ -190,13 +197,13 @@ char* build_short_options() { memset(str, 0, sizeof(char) * (len*2 + 1)); #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_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION]); + c[ARG_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION], c[ARG_LOOP]); #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_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION]); + c[ARG_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION], c[ARG_LOOP]); #endif return str; @@ -210,6 +217,7 @@ bool parse_args(int argc, char* argv[]) { bool color_flag = false; args.debug_flag = false; args.raw_flag = false; + args.loop_flag = false; args.verbose_flag = false; args.help_flag = false; 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] }, #endif {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_VERSION], no_argument, 0, args_chr[ARG_VERSION] }, {0, 0, 0, 0} @@ -255,6 +264,9 @@ bool parse_args(int argc, char* argv[]) { } break; } + else if(opt == args_chr[ARG_LOOP]) { + args.loop_flag = true; + } else if(opt == args_chr[ARG_HELP]) { args.help_flag = true; } diff --git a/src/common/args.h b/src/common/args.h index 3614718..0a71363 100644 --- a/src/common/args.h +++ b/src/common/args.h @@ -29,6 +29,7 @@ enum { enum { ARG_STYLE, ARG_COLOR, + ARG_LOOP, ARG_HELP, ARG_RAW, ARG_DEBUG, @@ -44,6 +45,7 @@ extern const char *args_str[]; int max_arg_str_length(); bool parse_args(int argc, char* argv[]); bool show_help(); +bool loop_mode(); bool show_raw(); bool show_debug(); bool show_version(); diff --git a/src/common/main.c b/src/common/main.c index 473bfdd..3aa225e 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -27,6 +27,7 @@ void print_help(char *argv[]) { 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 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 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 diff --git a/src/common/printer.c b/src/common/printer.c index 19993a2..586c7ca 100644 --- a/src/common/printer.c +++ b/src/common/printer.c @@ -1,3 +1,12 @@ +#ifdef _WIN32 + #define NOMINMAX + #include +#else + #define _POSIX_C_SOURCE 199309L + #include +#endif + +#include #include #include #include @@ -17,11 +26,6 @@ #include "../arm/soc.h" #endif -#ifdef _WIN32 -#define NOMINMAX -#include -#endif - #define max(a,b) (((a)>(b))?(a):(b)) #define MAX_ATTRIBUTES 100 @@ -118,6 +122,24 @@ struct ascii { STYLE style; }; +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) { art->attributes[art->n_attributes_set]->value = value; art->attributes[art->n_attributes_set]->type = type; @@ -408,9 +430,36 @@ void print_ascii_x86(struct ascii* art, uint32_t la, void (*callback_print_algor printf("\n"); } -void print_ascii(struct ascii* art) { +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; +} + +bool print_ascii(struct ascii* art) { uint32_t longest_attribute = longest_attribute_length(art); - + if(art->vendor == CPU_VENDOR_INTEL) print_ascii_x86(art, longest_attribute, &print_algorithm_intel); else if(art->vendor == CPU_VENDOR_AMD) @@ -418,7 +467,12 @@ void print_ascii(struct ascii* art) { else { printBug("Invalid CPU vendor: %d\n", art->vendor); } - + + if(loop_mode()) { + return run_loop_mode(); + } + + return true; } bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) {