From 67a674e8507c25ac77737cbab3ea7b9731902171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Oliver=20Pito=C5=88=C3=A1k?= Date: Tue, 16 Jan 2024 19:02:20 +0100 Subject: [PATCH 1/4] btop now reacts to SIGUSR1 by reloading config --- src/btop.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/btop.cpp b/src/btop.cpp index 736854f..33c1255 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -362,7 +362,12 @@ void _signal_handler(const int sig) { term_resize(); break; case SIGUSR1: - // Input::poll interrupt + vector warnings; + Config::load(Config::conf_file, warnings); + Theme::setTheme(); + Draw::banner_gen(0, 0, false, true); + Draw::calcSizes(); + Runner::run("all", false, true); break; } } From e047f88bd5261bca1bb3d59c60b8b4368d3573ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Oliver=20Pito=C5=88=C3=A1k?= Date: Wed, 17 Jan 2024 16:41:58 +0100 Subject: [PATCH 2/4] SIGUSR2 signal now reloads the config --- src/btop.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/btop.cpp b/src/btop.cpp index 33c1255..94bf8a9 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -362,6 +362,9 @@ void _signal_handler(const int sig) { term_resize(); break; case SIGUSR1: + // Input::poll interrupt + break; + case SIGUSR2: vector warnings; Config::load(Config::conf_file, warnings); Theme::setTheme(); @@ -1040,6 +1043,7 @@ int main(int argc, char **argv) { std::signal(SIGCONT, _signal_handler); std::signal(SIGWINCH, _signal_handler); std::signal(SIGUSR1, _signal_handler); + std::signal(SIGUSR2, _signal_handler); sigset_t mask; sigemptyset(&mask); From fb994b69ebff8cc5f937ea8b0a587b5a5b0c6c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Oliver=20Pito=C5=88=C3=A1k?= Date: Wed, 17 Jan 2024 17:27:18 +0100 Subject: [PATCH 3/4] added ctrl+r shortcut to reload config from disk --- src/btop_input.cpp | 7 +++++-- src/btop_menu.cpp | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/btop_input.cpp b/src/btop_input.cpp index fbbae58..6bfe9d8 100644 --- a/src/btop_input.cpp +++ b/src/btop_input.cpp @@ -41,6 +41,7 @@ namespace Input { //* Map for translating key codes to readable values const std::unordered_map Key_escapes = { {"\033", "escape"}, + {"\x12", "ctrl_r"}, {"\n", "enter"}, {" ", "space"}, {"\x7f", "backspace"}, @@ -258,8 +259,10 @@ namespace Input { Draw::calcSizes(); Runner::run("all", false, true); return; - } - else + } else if (is_in(key, "ctrl_r")) { + kill(getpid(), SIGUSR2); + return; + } else keep_going = true; if (not keep_going) return; diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index fb119aa..b7f44bc 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -177,6 +177,7 @@ namespace Menu { {"F2, o", "Shows options."}, {"F1, ?, h", "Shows this window."}, {"ctrl + z", "Sleep program and put in background."}, + {"ctrl + r", "Reloads config file from disk."}, {"q, ctrl + c", "Quits program."}, {"+, -", "Add/Subtract 100ms to/from update timer."}, {"Up, Down", "Select in process list."}, From 1670e1db7987957062761e86f0eb35455177b74c Mon Sep 17 00:00:00 2001 From: aristocratos Date: Sun, 11 Feb 2024 17:42:51 +0100 Subject: [PATCH 4/4] Reuse code from init, properly log warnings and move execution in to main loop --- src/btop.cpp | 79 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/src/btop.cpp b/src/btop.cpp index a3eaa3c..3527d75 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -109,6 +109,7 @@ namespace Global { atomic should_sleep (false); atomic _runner_started (false); atomic init_conf (false); + atomic reload_conf (false); bool arg_tty{}; bool arg_low_color{}; @@ -366,16 +367,37 @@ void _signal_handler(const int sig) { // Input::poll interrupt break; case SIGUSR2: - vector warnings; - Config::load(Config::conf_file, warnings); - Theme::setTheme(); - Draw::banner_gen(0, 0, false, true); - Draw::calcSizes(); - Runner::run("all", false, true); + Global::reload_conf = true; + Input::interrupt(); break; } } +//* Config init +void init_config(){ + atomic_lock lck(Global::init_conf); + vector load_warnings; + Config::load(Config::conf_file, load_warnings); + Config::set("lowcolor", (Global::arg_low_color ? true : not Config::getB("truecolor"))); + + static bool first_init = true; + + if (Global::debug and first_init) { + Logger::set("DEBUG"); + Logger::debug("Running in DEBUG mode!"); + } + else Logger::set(Config::getS("log_level")); + + static string log_level; + if (const string current_level = Config::getS("log_level"); log_level != current_level) { + log_level = current_level; + Logger::info("Logger set to " + (Global::debug ? "DEBUG" : log_level)); + } + + for (const auto& err_str : load_warnings) Logger::warning(err_str); + first_init = false; +} + //* Manages secondary thread for collection and drawing of boxes namespace Runner { atomic active (false); @@ -904,22 +926,7 @@ int main(int argc, char **argv) { } //? Config init - { - atomic_lock lck(Global::init_conf); - vector load_warnings; - Config::load(Config::conf_file, load_warnings); - Config::set("lowcolor", (Global::arg_low_color ? true : not Config::getB("truecolor"))); - - if (Global::debug) { - Logger::set("DEBUG"); - Logger::debug("Starting in DEBUG mode!"); - } - else Logger::set(Config::getS("log_level")); - - Logger::info("Logger set to " + (Global::debug ? "DEBUG" : Config::getS("log_level"))); - - for (const auto& err_str : load_warnings) Logger::warning(err_str); - } + init_config(); //? Try to find and set a UTF-8 locale if (std::setlocale(LC_ALL, "") != nullptr and not s_contains((string)std::setlocale(LC_ALL, ""), ";") @@ -1096,9 +1103,27 @@ int main(int argc, char **argv) { try { while (not true not_eq not false) { //? Check for exceptions in secondary thread and exit with fail signal if true - if (Global::thread_exception) clean_quit(1); - else if (Global::should_quit) clean_quit(0); - else if (Global::should_sleep) { Global::should_sleep = false; _sleep(); } + if (Global::thread_exception) { + clean_quit(1); + } + else if (Global::should_quit) { + clean_quit(0); + } + else if (Global::should_sleep) { + Global::should_sleep = false; + _sleep(); + } + //? Hot reload config from CTRL + R or SIGUSR2 + else if (Global::reload_conf) { + Global::reload_conf = false; + if (Runner::active) Runner::stop(); + Config::unlock(); + init_config(); + Theme::updateThemes(); + Theme::setTheme(); + Draw::banner_gen(0, 0, false, true); + Global::resized = true; + } //? Make sure terminal size hasn't changed (in case of SIGWINCH not working properly) term_resize(Global::resized); @@ -1133,9 +1158,9 @@ int main(int argc, char **argv) { update_ms = Config::getI("update_ms"); future_time = time_ms() + update_ms; } - else if (future_time - current_time > update_ms) + else if (future_time - current_time > update_ms) { future_time = current_time; - + } //? Poll for input and process any input detected else if (Input::poll(min((uint64_t)1000, future_time - current_time))) { if (not Runner::active) Config::unlock();