Stress_UI/src/main.cpp
somebody_master c505350e6d
Implementar CpuMon al completo
Signed-off-by: somebody_master <somebody_master@somebodyserver.mooo.com>
2021-05-12 01:41:06 +02:00

300 lines
8.6 KiB
C++
Executable File

#include "configFile.hpp"
#include "cpu_mon.hpp"
#include "textos.hpp"
#include "ui.hpp"
#include <locale.h>
#include <thread>
using namespace std;
// Constantes para el manejo de los diferentes menús
const uint8_t MENU_PRINCIPAL = 0;
const uint8_t MENU_INICIAR_STRESS = 1;
const uint8_t MENU_CONFIGURACION = 2;
const uint8_t SALIR = 3;
const uint8_t CONFIGURAR_TODO = 4;
const uint8_t CONFIGURAR_HILOS = 5;
const uint8_t CONFIGURAR_TIEMPO = 6;
const uint8_t CONFIGURAR_MENU_PRINCIPAL = 7;
const uint8_t MENU_ERROR = 8;
const uint8_t MENU_TITULO = 9;
const uint8_t PRIMER_INICIO = 10;
const vector<string> TITULO_PRINCIPAL = {
"____ ___ __ __ _____ ____ ___ ______ __ ____ _____ ______ "
"_______ ____ ",
"/ ___| / _ \\| \\/ | ____| __ ) / _ \\| _ \\ \\ / / / ___|| ____| _ "
"\\ \\ / / ____| _ \\ ",
"\\___ \\| | | | |\\/| | _| | _ \\| | | | | | \\ V / \\___ \\| _| | "
"|_) \\ \\ / /| _| | |_) |",
" ___) | |_| | | | | |___| |_) | |_| | |_| || | ___) | |___| _ < \\ V "
"/ | |___| _ < ",
"|____/ \\___/|_| |_|_____|____/ \\___/|____/ |_| |____/|_____|_| \\_\\ "
"\\_/ |_____|_| \\_\\",
"",
"_______________________________________________________________________",
};
void userInterface(const uint8_t *menu, Config *config);
void userInterface(const uint8_t *menu, vector<string> *mensaje,
Config *config);
void showMenu(vector<string> *menu_elems, const uint8_t *num_menu,
vector<string> *mensaje, Config *config);
void showMenu(vector<string> *menu_elems, const uint8_t *num_menu,
Config *config);
void monitorHilo(CpuMon monitor) { monitor.iniciarCPU_MON(); }
int main(int argc, char *argv[]) {
txt::inicializarIdioma(setlocale(LC_ALL, ""));
if (ui::initUI(&MENU_PRINCIPAL, &MENU_ERROR) != 0) {
return -1;
}
Config config = cf::openConfig();
if (config.getTiempo() == "") {
userInterface(&PRIMER_INICIO, &config);
} else {
userInterface(&MENU_PRINCIPAL, &config);
}
ui::closeUI();
return 0;
}
void userInterface(const uint8_t *menu, Config *config) {
vector<string> dummy = {""};
userInterface(menu, &dummy, config);
}
void userInterface(const uint8_t *num_menu, vector<string> *mensaje,
Config *config) {
switch (*num_menu) {
case CONFIGURAR_MENU_PRINCIPAL:
case MENU_PRINCIPAL: {
vector<string> elem_menu = {txt::g_menuPrincipal_1, txt::g_menuPrincipal_2,
txt::g_menuPrincipal_3};
showMenu(&elem_menu, &MENU_PRINCIPAL, mensaje, config);
break;
}
case MENU_INICIAR_STRESS: {
/*TODO
*Lleva al menú principal
*/
CpuMon monitor =
CpuMon(std::thread::hardware_concurrency(),
newwin(ui::g_maxLines - (TITULO_PRINCIPAL.size() + 4),
ui::g_maxCols / 2 - 1, TITULO_PRINCIPAL.size() + 4,
ui::g_maxCols - ui::g_maxCols / 2 - 1));
std::thread monHilo(monitorHilo, monitor);
sleep(10);
monitor.setEnfuncionamiento(false);
vector<string> elem_menu = {txt::g_menuPrincipal_1, txt::g_menuPrincipal_2,
txt::g_menuPrincipal_3};
showMenu(&elem_menu, &MENU_PRINCIPAL, mensaje, config);
break;
}
case MENU_CONFIGURACION: {
vector<string> elem_menu = {
txt::g_menuConfigurar_1, txt::g_menuConfigurar_2,
txt::g_menuConfigurar_3, txt::g_menuConfigurar_4};
showMenu(&elem_menu, num_menu, mensaje, config);
break;
}
case CONFIGURAR_TODO:
case PRIMER_INICIO: {
vector<string> textos_init = {txt::g_confTodo + cf::g_configFile, "", ""};
vector<string> elemens = {txt::g_confHilosInput, txt::g_confTiempoInput};
ui::showTopTitle(&TITULO_PRINCIPAL);
vector<string> entrada =
ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens,
TITULO_PRINCIPAL.size(), &MENU_ERROR);
Config config2 = Config(atoi(entrada[0].c_str()), entrada[1]);
config = &config2;
cf::saveConfig(config);
userInterface(&MENU_PRINCIPAL, mensaje, config);
break;
}
case CONFIGURAR_HILOS: {
vector<string> textos_init = {
txt::g_confHilos + to_string(config->getNumHilos()), "", ""};
vector<string> elemens = {txt::g_confHilosInput};
vector<string> entrada =
ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens,
TITULO_PRINCIPAL.size(), &MENU_ERROR);
config->setNumHilos(atoi(entrada[0].c_str()));
cf::saveConfig(config);
userInterface(&MENU_CONFIGURACION, mensaje, config);
break;
}
case CONFIGURAR_TIEMPO: {
vector<string> textos_init = {txt::g_confTiempo + config->getTiempo(), "",
""};
vector<string> elemens = {txt::g_confTiempoInput};
vector<string> entrada =
ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens,
TITULO_PRINCIPAL.size(), &MENU_ERROR);
config->setTiempo(entrada[0]);
cf::saveConfig(config);
userInterface(&MENU_CONFIGURACION, mensaje, config);
break;
}
case MENU_TITULO: {
/*TODO
*Lleva al menú principal
*/
vector<string> elem_menu = {txt::g_menuPrincipal_1, txt::g_menuPrincipal_2,
txt::g_menuPrincipal_3};
showMenu(&elem_menu, &MENU_PRINCIPAL, mensaje, config);
break;
}
case SALIR: {
break;
}
default: {
ui::g_mensaje = true;
*mensaje = {txt::g_errorGenerico_1, txt::g_errorGenerico_2};
userInterface(&MENU_PRINCIPAL, mensaje, config);
break;
}
}
}
void showMenu(vector<string> *menu_elems, const uint8_t *num_menu,
vector<string> *mensaje, Config *config) {
clear();
if (ui::g_mensaje) {
ui::showCenterMensaje(mensaje, &MENU_ERROR);
}
showMenu(menu_elems, num_menu, config);
}
void showMenu(vector<string> *menu_elems, const uint8_t *num_menu,
Config *config) {
size_t height = menu_elems->size();
attron(COLOR_PAIR(MENU_PRINCIPAL));
WINDOW *menuwin =
newwin(height + 2, ui::g_menuWith, TITULO_PRINCIPAL.size() + 4, 2);
box(menuwin, 0, 0);
refresh();
ui::showTopTitle(&TITULO_PRINCIPAL);
wrefresh(menuwin);
keypad(menuwin, true);
uint16_t choice = 0;
uint8_t highlight = 0;
switch (*num_menu) {
case MENU_PRINCIPAL: {
mvwprintw(menuwin, 0, 1, txt::g_menuPrincipal.c_str());
break;
}
case MENU_CONFIGURACION: {
mvwprintw(menuwin, 0, 1, txt::g_menuConfigurar.c_str());
break;
}
}
while (true) {
for (uint8_t i = 0; i < height; i++) {
if (i == highlight) {
wattron(menuwin, A_REVERSE);
}
if (menu_elems->at(i).length() < (ui::g_menuWith - 4)) {
for (uint8_t j = menu_elems->at(i).length(); j < (ui::g_menuWith - 4);
j++) {
menu_elems->at(i).append(" ");
}
// Por algún motivo los acentos y tildes cuentan como un caracter
// aparte, por lo que hay que añadir un espacio extra en esos
// casos. De momento la mejor solución es leer la cadena cada dos
// caracteres y ver si cuadra con alguna de las siguientes letras.
for (uint8_t j = 0; j < menu_elems->at(i).length(); j++) {
if (menu_elems->at(i).substr(j, 2).compare("á") == 0 |
menu_elems->at(i).substr(j, 2).compare("é") == 0 |
menu_elems->at(i).substr(j, 2).compare("í") == 0 |
menu_elems->at(i).substr(j, 2).compare("ó") == 0 |
menu_elems->at(i).substr(j, 2).compare("ú") == 0 |
menu_elems->at(i).substr(j, 2).compare("Á") == 0 |
menu_elems->at(i).substr(j, 2).compare("É") == 0 |
menu_elems->at(i).substr(j, 2).compare("Ó") == 0 |
menu_elems->at(i).substr(j, 2).compare("Ú") == 0 |
menu_elems->at(i).substr(j, 2).compare("ñ") == 0 |
menu_elems->at(i).substr(j, 2).compare("Ñ") == 0) {
menu_elems->at(i).append(" ");
}
}
}
mvwprintw(menuwin, i + 1, 2, menu_elems->at(i).c_str());
wattroff(menuwin, A_REVERSE);
}
choice = wgetch(menuwin);
switch (choice) {
case KEY_UP: {
if (highlight > 0) {
highlight--;
}
break;
}
case KEY_DOWN: {
if ((highlight + 1) < height) {
highlight++;
}
break;
}
default: {
break;
}
}
if (choice == 10) {
break;
}
}
switch (*num_menu) {
case MENU_PRINCIPAL: {
highlight++;
break;
}
case MENU_CONFIGURACION: {
highlight = highlight + 4;
break;
}
}
attroff(COLOR_PAIR(MENU_PRINCIPAL));
userInterface(&highlight, config);
}