btop/src/btop_config.h

205 lines
5 KiB
C
Raw Normal View History

2021-05-09 00:56:48 +12:00
/* Copyright 2021 Aristocratos (jakob@qvantnet.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
indent = tab
tab-size = 4
*/
#ifndef _btop_config_included_
2021-06-06 11:41:36 +12:00
#define _btop_config_included_
2021-05-09 00:56:48 +12:00
#include <string>
#include <vector>
2021-05-29 12:32:36 +12:00
#include <robin_hood.h>
#include <filesystem>
2021-05-09 00:56:48 +12:00
2021-05-09 06:37:36 +12:00
#include <btop_tools.h>
2021-05-29 12:32:36 +12:00
using std::string, std::vector, robin_hood::unordered_flat_map;
namespace fs = std::filesystem;
using namespace Tools;
2021-05-09 06:37:36 +12:00
2021-05-15 04:54:37 +12:00
//* Functions and variables for reading and writing the btop config file
namespace Config {
namespace {
2021-05-29 12:32:36 +12:00
fs::path conf_dir;
fs::path conf_file;
2021-06-06 11:41:36 +12:00
atomic<bool> locked (false);
atomic<bool> writelock (false);
2021-06-10 05:47:49 +12:00
unordered_flat_map<string, bool> changed;
2021-05-15 04:54:37 +12:00
2021-05-29 12:32:36 +12:00
unordered_flat_map<string, string> strings = {
2021-05-15 04:54:37 +12:00
{"color_theme", "Default"},
{"shown_boxes", "cpu mem net proc"},
{"proc_sorting", "cpu lazy"},
{"cpu_graph_upper", "total"},
{"cpu_graph_lower", "total"},
{"cpu_sensor", "Auto"},
{"temp_scale", "celsius"},
{"draw_clock", "%X"},
{"custom_cpu_name", ""},
{"disks_filter", ""},
{"io_graph_speeds", ""},
{"net_download", "10M"},
{"net_upload", "10M"},
{"net_iface", ""},
{"log_level", "WARNING"}
};
2021-06-06 11:41:36 +12:00
unordered_flat_map<string, string> stringsTmp;
2021-05-29 12:32:36 +12:00
unordered_flat_map<string, bool> bools = {
2021-05-15 04:54:37 +12:00
{"theme_background", true},
{"truecolor", true},
{"proc_reversed", false},
{"proc_tree", false},
{"proc_colors", true},
{"proc_gradient", true},
{"proc_per_core", false},
{"proc_mem_bytes", true},
{"cpu_invert_lower", true},
{"cpu_single_graph", false},
{"show_uptime", true},
{"check_temp", true},
{"show_coretemp", true},
{"show_cpu_freq", true},
{"background_update", true},
{"update_check", true},
{"mem_graphs", true},
{"show_swap", true},
{"swap_disk", true},
{"show_disks", true},
{"only_physical", true},
{"use_fstab", false},
{"show_io_stat", true},
{"io_mode", false},
{"io_graph_combined", false},
{"net_color_fixed", false},
{"net_auto", true},
{"net_sync", false},
{"show_battery", true},
{"show_init", false}
};
2021-06-06 11:41:36 +12:00
unordered_flat_map<string, bool> boolsTmp;
2021-05-29 12:32:36 +12:00
unordered_flat_map<string, int> ints = {
2021-05-15 04:54:37 +12:00
{"update_ms", 2000},
{"proc_update_mult", 2},
{"tree_depth", 3}
};
2021-06-06 11:41:36 +12:00
unordered_flat_map<string, int> intsTmp;
bool _locked(){
atomic_wait(writelock);
return locked.load();
}
}
2021-05-09 00:56:48 +12:00
2021-06-10 05:47:49 +12:00
//* Return bool config value <name>
const bool& getB(string name){
2021-05-09 00:56:48 +12:00
return bools.at(name);
}
2021-06-10 05:47:49 +12:00
//* Return integer config value <name>
const int& getI(string name){
2021-05-09 00:56:48 +12:00
return ints.at(name);
}
2021-06-10 05:47:49 +12:00
//* Return string config value <name>
const string& getS(string name){
2021-05-09 00:56:48 +12:00
return strings.at(name);
}
2021-05-15 04:54:37 +12:00
//* Set config value <name> to bool <value>
2021-06-05 11:41:24 +12:00
void set(string name, bool value){
if (_locked()) boolsTmp.insert_or_assign(name, value);
2021-06-06 11:41:36 +12:00
else bools.at(name) = value;
2021-06-10 05:47:49 +12:00
changed.insert_or_assign(name, true);
2021-05-15 04:54:37 +12:00
}
//* Set config value <name> to int <value>
2021-06-05 11:41:24 +12:00
void set(string name, int value){
if (_locked()) intsTmp.insert_or_assign(name, value);
2021-05-15 04:54:37 +12:00
ints.at(name) = value;
2021-06-10 05:47:49 +12:00
changed.insert_or_assign(name, true);
2021-05-15 04:54:37 +12:00
}
//* Set config value <name> to string <value>
2021-06-05 11:41:24 +12:00
void set(string name, string value){
if (_locked()) stringsTmp.insert_or_assign(name, value);
2021-06-06 11:41:36 +12:00
else strings.at(name) = value;
2021-06-10 05:47:49 +12:00
changed.insert_or_assign(name, true);
2021-05-15 04:54:37 +12:00
}
2021-06-06 11:41:36 +12:00
//* Flip config bool <name>
2021-06-05 11:41:24 +12:00
void flip(string name){
2021-06-06 11:41:36 +12:00
if (_locked()) {
if (boolsTmp.contains(name)) boolsTmp.at(name) = !boolsTmp.at(name);
else boolsTmp.insert_or_assign(name, (!bools.at(name)));
2021-06-06 11:41:36 +12:00
}
else bools.at(name) = !bools.at(name);
2021-06-10 05:47:49 +12:00
changed.insert_or_assign(name, true);
2021-06-06 11:41:36 +12:00
}
//* Wait if locked then lock config and cache changes until unlock
void lock(){
atomic_wait_set(locked);
2021-06-06 11:41:36 +12:00
}
//* Unlock config and write any cached values to config
void unlock(){
atomic_wait_set(writelock);
2021-06-06 11:41:36 +12:00
if (stringsTmp.size() > 0) {
for (auto& item : stringsTmp){
strings.at(item.first) = item.second;
}
stringsTmp.clear();
2021-06-06 11:41:36 +12:00
}
if (intsTmp.size() > 0) {
for (auto& item : intsTmp){
ints.at(item.first) = item.second;
}
intsTmp.clear();
2021-06-06 11:41:36 +12:00
}
if (boolsTmp.size() > 0) {
for (auto& item : boolsTmp){
bools.at(item.first) = item.second;
}
boolsTmp.clear();
2021-06-06 11:41:36 +12:00
}
writelock.store(false);
locked.store(false);
2021-06-05 11:41:24 +12:00
}
2021-06-02 08:36:36 +12:00
void load(){
if (conf_file.empty()) return;
2021-05-15 04:54:37 +12:00
}
2021-06-10 05:47:49 +12:00
void write(){
if (conf_file.empty() || changed.empty()) return;
if (Logger::loglevel > 3) {
string items;
for (auto item : Config::changed) {
items += item.first + ", ";
}
items.pop_back(); items.pop_back();
Logger::debug("Writing out new config values for: " + items);
}
}
}
2021-05-09 00:56:48 +12:00
#endif