Remove C-style casts, add -Wold-style-cast

This commit is contained in:
nobounce 2023-09-13 14:51:18 +02:00
parent 2c3ac4855d
commit be65d50b24
No known key found for this signature in database
GPG key ID: CC8B8C464BDC2F39
14 changed files with 213 additions and 209 deletions

View file

@ -88,7 +88,7 @@ endif()
# TODO: enable more warnings in coordination with upstream # TODO: enable more warnings in coordination with upstream
target_compile_options(btop PRIVATE target_compile_options(btop PRIVATE
-Wall -Wextra -Wpedantic -Wall -Wextra -Wpedantic -Wold-style-cast
-ftree-vectorize -fstack-clash-protection -ftree-vectorize -fstack-clash-protection
) )
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
@ -99,7 +99,7 @@ endif()
if(BTOP_PEDANTIC) if(BTOP_PEDANTIC)
target_compile_options(btop PRIVATE target_compile_options(btop PRIVATE
-Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wunused -Wshadow -Wnon-virtual-dtor -Wcast-align -Wunused
-Woverloaded-virtual -Wconversion -Wsign-conversion -Wdouble-promotion -Woverloaded-virtual -Wconversion -Wsign-conversion -Wdouble-promotion
-Wformat=2 -Wimplicit-fallthrough -Weffc++ -Wformat=2 -Wimplicit-fallthrough -Weffc++
) )

View file

@ -170,7 +170,7 @@ override GOODFLAGS := $(foreach flag,$(TESTFLAGS),$(strip $(shell echo "int main
#? Flags, Libraries and Includes #? Flags, Libraries and Includes
override REQFLAGS := -std=c++20 override REQFLAGS := -std=c++20
WARNFLAGS := -Wall -Wextra -pedantic WARNFLAGS := -Wall -Wextra -pedantic -Wold-style-cast
OPTFLAGS := -O2 -ftree-vectorize -flto=$(LTO) OPTFLAGS := -O2 -ftree-vectorize -flto=$(LTO)
LDCXXFLAGS := -pthread -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -D_FILE_OFFSET_BITS=64 $(GOODFLAGS) $(ADDFLAGS) LDCXXFLAGS := -pthread -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -D_FILE_OFFSET_BITS=64 $(GOODFLAGS) $(ADDFLAGS)
override CXXFLAGS += $(REQFLAGS) $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS) override CXXFLAGS += $(REQFLAGS) $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS)

View file

@ -56,7 +56,6 @@ using std::cout;
using std::flush; using std::flush;
using std::min; using std::min;
using std::string; using std::string;
using std::string_view;
using std::to_string; using std::to_string;
using std::vector; using std::vector;
@ -753,9 +752,9 @@ int main(int argc, char **argv) {
if (argc > 1) argumentParser(argc, argv); if (argc > 1) argumentParser(argc, argv);
//? Setup paths for config, log and user themes //? Setup paths for config, log and user themes
for (const auto& env : {"XDG_CONFIG_HOME", "HOME"}) { for (const std::string_view env : {"XDG_CONFIG_HOME", "HOME"}) {
if (std::getenv(env) != nullptr and access(std::getenv(env), W_OK) != -1) { if (std::getenv(env.data()) != nullptr and access(std::getenv(env.data()), W_OK) != -1) {
Config::conf_dir = fs::path(std::getenv(env)) / (((string)env == "HOME") ? ".config/btop" : "btop"); Config::conf_dir = fs::path(std::getenv(env.data())) / ((env.compare("HOME") == 0) ? ".config/btop" : "btop");
break; break;
} }
} }
@ -821,15 +820,15 @@ int main(int argc, char **argv) {
} }
//? Try to find and set a UTF-8 locale //? Try to find and set a UTF-8 locale
if (std::setlocale(LC_ALL, "") != nullptr and not s_contains((string)std::setlocale(LC_ALL, ""), ";") if (std::setlocale(LC_ALL, "") != nullptr and not s_contains(std::setlocale(LC_ALL, ""), ";")
and str_to_upper(s_replace((string)std::setlocale(LC_ALL, ""), "-", "")).ends_with("UTF8")) { and str_to_upper(s_replace(std::setlocale(LC_ALL, ""), "-", "")).ends_with("UTF8")) {
Logger::debug("Using locale " + (string)std::setlocale(LC_ALL, "")); Logger::debug("Using locale " + static_cast<string>(std::setlocale(LC_ALL, "")));
} }
else { else {
string found; string found;
bool set_failure{}; // defaults to false bool set_failure{}; // defaults to false
for (const auto loc_env : array{"LANG", "LC_ALL"}) { for (const auto loc_env : array{"LANG", "LC_ALL"}) {
if (std::getenv(loc_env) != nullptr and str_to_upper(s_replace((string)std::getenv(loc_env), "-", "")).ends_with("UTF8")) { if (std::getenv(loc_env) != nullptr and str_to_upper(s_replace(std::getenv(loc_env), "-", "")).ends_with("UTF8")) {
found = std::getenv(loc_env); found = std::getenv(loc_env);
if (std::setlocale(LC_ALL, found.c_str()) == nullptr) { if (std::setlocale(LC_ALL, found.c_str()) == nullptr) {
set_failure = true; set_failure = true;
@ -858,7 +857,7 @@ int main(int argc, char **argv) {
#ifdef __APPLE__ #ifdef __APPLE__
if (found.empty()) { if (found.empty()) {
CFLocaleRef cflocale = CFLocaleCopyCurrent(); CFLocaleRef cflocale = CFLocaleCopyCurrent();
CFStringRef id_value = (CFStringRef)CFLocaleGetValue(cflocale, kCFLocaleIdentifier); CFStringRef id_value = static_cast<CFStringRef>(CFLocaleGetValue(cflocale, kCFLocaleIdentifier));
auto loc_id = CFStringGetCStringPtr(id_value, kCFStringEncodingUTF8); auto loc_id = CFStringGetCStringPtr(id_value, kCFStringEncodingUTF8);
CFRelease(cflocale); CFRelease(cflocale);
std::string cur_locale = (loc_id != nullptr ? loc_id : ""); std::string cur_locale = (loc_id != nullptr ? loc_id : "");
@ -951,7 +950,7 @@ int main(int argc, char **argv) {
//? Calculate sizes of all boxes //? Calculate sizes of all boxes
Config::presetsValid(Config::getS("presets")); Config::presetsValid(Config::getS("presets"));
if (Global::arg_preset >= 0) { if (Global::arg_preset >= 0) {
Config::current_preset = min(Global::arg_preset, (int)Config::preset_list.size() - 1); Config::current_preset = min(Global::arg_preset, static_cast<int>(Config::preset_list.size()) - 1);
Config::apply_preset(Config::preset_list.at(Config::current_preset)); Config::apply_preset(Config::preset_list.at(Config::current_preset));
} }
@ -1020,7 +1019,7 @@ int main(int argc, char **argv) {
future_time = current_time; future_time = current_time;
//? Poll for input and process any input detected //? Poll for input and process any input detected
else if (Input::poll(min((uint64_t)1000, future_time - current_time))) { else if (Input::poll(min(static_cast<uint64_t>(1000), future_time - current_time))) {
if (not Runner::active) Config::unlock(); if (not Runner::active) Config::unlock();
if (Menu::active) Menu::process(Input::get()); if (Menu::active) Menu::process(Input::get());

View file

@ -208,7 +208,7 @@ namespace Draw {
return Fx::ul + " " + Fx::uul; return Fx::ul + " " + Fx::uul;
if (limit > 0 and ulen(text) + 1 > limit) { if (limit > 0 and ulen(text) + 1 > limit) {
try { try {
const size_t half = (size_t)round((double)limit / 2); const size_t half = round(limit / 2.0);
string first; string first;
if (upos + half > ulen(text)) if (upos + half > ulen(text))
@ -366,7 +366,7 @@ namespace Draw {
if (not cache.at(value).empty()) return cache.at(value); if (not cache.at(value).empty()) return cache.at(value);
auto& out = cache.at(value); auto& out = cache.at(value);
for (const int& i : iota(1, width + 1)) { for (const int& i : iota(1, width + 1)) {
int y = round((double)i * 100.0 / width); int y = round(i * 100.0 / width);
if (value >= y) if (value >= y)
out += Theme::g(color_gradient).at(invert ? 100 - y : y) + Symbols::meter; out += Theme::g(color_gradient).at(invert ? 100 - y : y) + Symbols::meter;
else { else {
@ -391,7 +391,7 @@ namespace Draw {
} }
//? Horizontal iteration over values in <data> //? Horizontal iteration over values in <data>
for (const int& i : iota(data_offset, (int)data.size())) { for (const int& i : iota(data_offset, static_cast<int>(data.size()))) {
// if (tty_mode and mult and i % 2 != 0) continue; // if (tty_mode and mult and i % 2 != 0) continue;
if (not tty_mode and mult) current = not current; if (not tty_mode and mult) current = not current;
if (i < 0) { if (i < 0) {
@ -415,7 +415,7 @@ namespace Draw {
else if (value <= cur_low) else if (value <= cur_low)
result[ai++] = clamp_min; result[ai++] = clamp_min;
else { else {
result[ai++] = clamp((int)round((float)(value - cur_low) * 4 / (cur_high - cur_low) + mod), clamp_min, 4); result[ai++] = clamp(static_cast<int>(round((value - cur_low) * 4.0 / (cur_high - cur_low) + mod)), clamp_min, 4);
} }
} }
//? Generate graph symbol from 5x5 2D vector //? Generate graph symbol from 5x5 2D vector
@ -462,7 +462,7 @@ namespace Draw {
if (max_value == 0 and offset > 0) max_value = 100; if (max_value == 0 and offset > 0) max_value = 100;
this->max_value = max_value; this->max_value = max_value;
const int value_width = (tty_mode ? data.size() : ceil((double)data.size() / 2)); const int value_width = (tty_mode ? data.size() : ceil(data.size() / 2.0));
int data_offset = (value_width > width) ? data.size() - width * (tty_mode ? 1 : 2) : 0; int data_offset = (value_width > width) ? data.size() - width * (tty_mode ? 1 : 2) : 0;
if (not tty_mode and (data.size() - data_offset) % 2 != 0) { if (not tty_mode and (data.size() - data_offset) % 2 != 0) {
@ -491,7 +491,7 @@ namespace Draw {
else if (graphs.at(current).at(i).at(0) == ' ') graphs.at(current).at(i).erase(0, 1); else if (graphs.at(current).at(i).at(0) == ' ') graphs.at(current).at(i).erase(0, 1);
else graphs.at(current).at(i).erase(0, 3); else graphs.at(current).at(i).erase(0, 3);
} }
this->_create(data, (int)data.size() - 1); this->_create(data, static_cast<int>(data.size()) - 1);
return out; return out;
} }
@ -544,7 +544,7 @@ namespace Cpu {
//* Redraw elements not needed to be updated every cycle //* Redraw elements not needed to be updated every cycle
if (redraw) { if (redraw) {
mid_line = (not single_graph and graph_up_field != graph_lo_field); mid_line = (not single_graph and graph_up_field != graph_lo_field);
graph_up_height = (single_graph ? height - 2 : ceil((double)(height - 2) / 2) - (mid_line and height % 2 != 0 ? 1 : 0)); graph_up_height = (single_graph ? height - 2 : ceil((height - 2.0) / 2.0) - (mid_line and height % 2 != 0 ? 1 : 0));
const int graph_low_height = height - 2 - graph_up_height - (mid_line ? 1 : 0); const int graph_low_height = height - 2 - graph_up_height - (mid_line ? 1 : 0);
const int button_y = cpu_bottom ? y + height - 1 : y; const int button_y = cpu_bottom ? y + height - 1 : y;
out += box; out += box;
@ -558,7 +558,7 @@ namespace Cpu {
const string update = to_string(Config::getI("update_ms")) + "ms"; const string update = to_string(Config::getI("update_ms")) + "ms";
out += Mv::to(button_y, x + width - update.size() - 8) + title_left + Fx::b + Theme::c("hi_fg") + "- " + Theme::c("title") + update out += Mv::to(button_y, x + width - update.size() - 8) + title_left + Fx::b + Theme::c("hi_fg") + "- " + Theme::c("title") + update
+ Theme::c("hi_fg") + " +" + Fx::ub + title_right; + Theme::c("hi_fg") + " +" + Fx::ub + title_right;
Input::mouse_mappings["-"] = {button_y, x + width - (int)update.size() - 7, 1, 2}; Input::mouse_mappings["-"] = {button_y, x + width - static_cast<int>(update.size()) - 7, 1, 2};
Input::mouse_mappings["+"] = {button_y, x + width - 5, 1, 2}; Input::mouse_mappings["+"] = {button_y, x + width - 5, 1, 2};
//? Graphs & meters //? Graphs & meters
@ -592,7 +592,7 @@ namespace Cpu {
temp_graphs.clear(); temp_graphs.clear();
temp_graphs.emplace_back(5, 1, "temp", cpu.temp.at(0), graph_symbol, false, false, cpu.temp_max, -23); temp_graphs.emplace_back(5, 1, "temp", cpu.temp.at(0), graph_symbol, false, false, cpu.temp_max, -23);
if (not hide_cores and b_column_size > 1) { if (not hide_cores and b_column_size > 1) {
for (const auto& i : iota((size_t)1, cpu.temp.size())) { for (const auto& i : iota(static_cast<size_t>(1), cpu.temp.size())) {
temp_graphs.emplace_back(5, 1, "temp", cpu.temp.at(i), graph_symbol, false, false, cpu.temp_max, -23); temp_graphs.emplace_back(5, 1, "temp", cpu.temp.at(i), graph_symbol, false, false, cpu.temp_max, -23);
} }
} }
@ -699,7 +699,7 @@ namespace Cpu {
out += Theme::c("div_line") + Symbols::v_line; out += Theme::c("div_line") + Symbols::v_line;
if ((++cy > ceil((double)Shared::coreCount / b_columns) or cy == b_height - 2) and n != Shared::coreCount - 1) { if ((++cy > ceil(static_cast<double>(Shared::coreCount) / b_columns) or cy == b_height - 2) and n != Shared::coreCount - 1) {
if (++cc >= b_columns) break; if (++cc >= b_columns) break;
cy = 1; cx = (b_width / b_columns) * cc; cy = 1; cx = (b_width / b_columns) * cc;
} }
@ -714,7 +714,7 @@ namespace Cpu {
else if (b_column_size == 1 or (b_column_size == 0 and show_temps)) { lavg_pre = "L"; } else if (b_column_size == 1 or (b_column_size == 0 and show_temps)) { lavg_pre = "L"; }
string lavg; string lavg;
for (const auto& val : cpu.load_avg) { for (const auto& val : cpu.load_avg) {
lavg += string(sep, ' ') + (lavg_pre.size() < 3 ? to_string((int)round(val)) : to_string(val).substr(0, 4)); lavg += string(sep, ' ') + (lavg_pre.size() < 3 ? to_string(static_cast<int>(round(val))) : to_string(val).substr(0, 4));
} }
string lavg_str = lavg_pre + lavg; string lavg_str = lavg_pre + lavg;
@ -804,8 +804,8 @@ namespace Mem {
unordered_flat_map<string, int> custom_speeds; unordered_flat_map<string, int> custom_speeds;
int half_height = 0; int half_height = 0;
if (io_mode) { if (io_mode) {
disks_io_h = max((int)floor((double)(height - 2 - (disk_ios * 2)) / max(1, disk_ios)), (io_graph_combined ? 1 : 2)); disks_io_h = max(static_cast<int>(floor((height - 2.0 - (disk_ios * 2.0)) / max(1, disk_ios))), (io_graph_combined ? 1 : 2));
half_height = ceil((double)disks_io_h / 2); half_height = ceil(disks_io_h / 2.0);
if (not Config::getS("io_graph_speeds").empty()) { if (not Config::getS("io_graph_speeds").empty()) {
auto split = ssplit(Config::getS("io_graph_speeds")); auto split = ssplit(Config::getS("io_graph_speeds"));
@ -893,7 +893,7 @@ namespace Mem {
if (title.empty()) title = capitalize(name); if (title.empty()) title = capitalize(name);
const string humanized = floating_humanizer(mem.stats.at(name)); const string humanized = floating_humanizer(mem.stats.at(name));
const int offset = max(0, divider.empty() ? 9 - (int)humanized.size() : 0); const int offset = max(0, divider.empty() ? 9 - static_cast<int>(humanized.size()) : 0);
const string graphics = (use_graphs ? mem_graphs.at(name)(mem.percent.at(name), redraw or data_same) : mem_meters.at(name)(mem.percent.at(name).back())); const string graphics = (use_graphs ? mem_graphs.at(name)(mem.percent.at(name), redraw or data_same) : mem_meters.at(name)(mem.percent.at(name).back()));
if (mem_size > 2) { if (mem_size > 2) {
out += Mv::to(y+1+cy, x+1+cx) + divider + title.substr(0, big_mem ? 10 : 5) + ":" out += Mv::to(y+1+cy, x+1+cx) + divider + title.substr(0, big_mem ? 10 : 5) + ":"
@ -928,7 +928,7 @@ namespace Mem {
+ trans(total) + Fx::ub; + trans(total) + Fx::ub;
if (big_disk) { if (big_disk) {
const string used_percent = to_string(disk.used_percent); const string used_percent = to_string(disk.used_percent);
out += Mv::to(y+1+cy, x+1+cx + round((double)disks_width / 2) - round((double)used_percent.size() / 2) - 1) + hu_div + used_percent + '%' + hu_div; out += Mv::to(y + 1 + cy, x + 1 + cx + round(disks_width / 2.0) - round(used_percent.size() / 2.0) - 1) + hu_div + used_percent + '%' + hu_div;
} }
out += Mv::to(y+2+cy++, x+1+cx) + (big_disk ? " IO% " : " IO " + Mv::l(2)) + Theme::c("inactive_fg") + graph_bg * (disks_width - 6) out += Mv::to(y+2+cy++, x+1+cx) + (big_disk ? " IO% " : " IO " + Mv::l(2)) + Theme::c("inactive_fg") + graph_bg * (disks_width - 6)
+ Mv::l(disks_width - 6) + io_graphs.at(mount + "_activity")(disk.io_activity, redraw or data_same) + Theme::c("main_fg"); + Mv::l(disks_width - 6) + io_graphs.at(mount + "_activity")(disk.io_activity, redraw or data_same) + Theme::c("main_fg");
@ -968,7 +968,7 @@ namespace Mem {
out += Mv::to(y+1+cy, x+1+cx) + divider + Theme::c("title") + Fx::b + uresize(disk.name, disks_width - 8) + Mv::to(y+1+cy, x+cx + disks_width - human_total.size()) out += Mv::to(y+1+cy, x+1+cx) + divider + Theme::c("title") + Fx::b + uresize(disk.name, disks_width - 8) + Mv::to(y+1+cy, x+cx + disks_width - human_total.size())
+ trans(human_total) + Fx::ub + Theme::c("main_fg"); + trans(human_total) + Fx::ub + Theme::c("main_fg");
if (big_disk and not human_io.empty()) if (big_disk and not human_io.empty())
out += Mv::to(y+1+cy, x+1+cx + round((double)disks_width / 2) - round((double)human_io.size() / 2) - 1) + hu_div + human_io + hu_div; out += Mv::to(y + 1 + cy, x + 1 + cx + round(disks_width / 2.0) - round(human_io.size() / 2.0) - 1) + hu_div + human_io + hu_div;
if (++cy > height - 3) break; if (++cy > height - 3) break;
if (show_io_stat and io_graphs.contains(mount + "_activity")) { if (show_io_stat and io_graphs.contains(mount + "_activity")) {
out += Mv::to(y+1+cy, x+1+cx) + (big_disk ? " IO% " : " IO " + Mv::l(2)) + Theme::c("inactive_fg") + graph_bg * (disks_width - 6) + Theme::g("available").at(clamp(disk.io_activity.back(), 50ll, 100ll)) out += Mv::to(y+1+cy, x+1+cx) + (big_disk ? " IO% " : " IO " + Mv::l(2)) + Theme::c("inactive_fg") + graph_bg * (disks_width - 6) + Theme::g("available").at(clamp(disk.io_activity.back(), 50ll, 100ll))
@ -1025,9 +1025,9 @@ namespace Net {
out.reserve(width * height); out.reserve(width * height);
const string title_left = Theme::c("net_box") + Fx::ub + Symbols::title_left; const string title_left = Theme::c("net_box") + Fx::ub + Symbols::title_left;
const string title_right = Theme::c("net_box") + Fx::ub + Symbols::title_right; const string title_right = Theme::c("net_box") + Fx::ub + Symbols::title_right;
const int i_size = min((int)selected_iface.size(), 10); const int i_size = min(static_cast<int>(selected_iface.size()), 10);
const long long down_max = (net_auto ? graph_max.at("download") : ((long long)(Config::getI("net_download")) << 20) / 8); const long long down_max = (net_auto ? graph_max.at("download") : (static_cast<long long>(Config::getI("net_download")) << 20) / 8);
const long long up_max = (net_auto ? graph_max.at("upload") : ((long long)(Config::getI("net_upload")) << 20) / 8); const long long up_max = (net_auto ? graph_max.at("upload") : (static_cast<long long>(Config::getI("net_upload")) << 20) / 8);
//* Redraw elements not needed to be updated every cycle //* Redraw elements not needed to be updated every cycle
if (redraw) { if (redraw) {
@ -1163,7 +1163,7 @@ namespace Proc {
} }
else if (cmd_key.starts_with("mousey")) { else if (cmd_key.starts_with("mousey")) {
int mouse_y = std::stoi(cmd_key.substr(6)); int mouse_y = std::stoi(cmd_key.substr(6));
start = clamp((int)round((double)mouse_y * (numpids - select_max - 2) / (select_max - 2)), 0, max(0, numpids - select_max)); start = clamp(static_cast<int>(round(mouse_y * (numpids - select_max - 2) / (select_max - 2.0))), 0, max(0, numpids - select_max));
} }
bool changed = false; bool changed = false;
@ -1269,8 +1269,8 @@ namespace Proc {
if (selected == 0) Input::mouse_mappings["enter"] = {d_y, d_x + d_width - 9, 1, 6}; if (selected == 0) Input::mouse_mappings["enter"] = {d_y, d_x + d_width - 9, 1, 6};
//? Labels //? Labels
const int item_fit = floor((double)(d_width - 2) / 10); const int item_fit = floor((d_width - 2) / 10.0);
const int item_width = floor((double)(d_width - 2) / min(item_fit, 8)); const int item_width = floor((d_width - 2.0) / min(item_fit, 8));
out += Mv::to(d_y + 1, d_x + 1) + Fx::b + Theme::c("title") out += Mv::to(d_y + 1, d_x + 1) + Fx::b + Theme::c("title")
+ cjust("Status:", item_width) + cjust("Status:", item_width)
+ cjust("Elapsed:", item_width); + cjust("Elapsed:", item_width);
@ -1288,7 +1288,7 @@ namespace Proc {
out += Theme::c("main_fg") + Fx::ub; out += Theme::c("main_fg") + Fx::ub;
const int cmd_size = ulen(detailed.entry.cmd, true); const int cmd_size = ulen(detailed.entry.cmd, true);
for (int num_lines = min(3, (int)ceil((double)cmd_size / (d_width - 5))), i = 0; i < num_lines; i++) { for (int num_lines = min(3, static_cast<int>(ceil(cmd_size / (d_width - 5.0)))), i = 0; i < num_lines; i++) {
out += Mv::to(d_y + 5 + (num_lines == 1 ? 1 : i), d_x + 3) out += Mv::to(d_y + 5 + (num_lines == 1 ? 1 : i), d_x + 3)
+ cjust(luresize(detailed.entry.cmd, cmd_size - (d_width - 5) * i, true), d_width - 5, true, true); + cjust(luresize(detailed.entry.cmd, cmd_size - (d_width - 5) * i, true), d_width - 5, true, true);
} }
@ -1378,8 +1378,8 @@ namespace Proc {
//? Draw details box if shown //? Draw details box if shown
if (show_detailed) { if (show_detailed) {
bool alive = detailed.status != "Dead"; bool alive = detailed.status != "Dead";
const int item_fit = floor((double)(d_width - 2) / 10); const int item_fit = floor((d_width - 2) / 10.0);
const int item_width = floor((double)(d_width - 2) / min(item_fit, 8)); const int item_width = floor((d_width - 2.0) / min(item_fit, 8));
//? Graph part of box //? Graph part of box
string cpu_str = (alive ? to_string(detailed.entry.cpu_p) : ""); string cpu_str = (alive ? to_string(detailed.entry.cpu_p) : "");
@ -1405,7 +1405,7 @@ namespace Proc {
if (item_fit >= 8) out += cjust(to_string(detailed.entry.p_nice), item_width); if (item_fit >= 8) out += cjust(to_string(detailed.entry.p_nice), item_width);
const double mem_p = (double)detailed.mem_bytes.back() * 100 / totalMem; const double mem_p = detailed.mem_bytes.back() * 100.0 / totalMem;
string mem_str = to_string(mem_p); string mem_str = to_string(mem_p);
mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4)); mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4));
out += Mv::to(d_y + 4, d_x + 1) + Theme::c("title") + Fx::b + rjust((item_fit > 4 ? "Memory: " : "M:") + mem_str + "% ", (d_width / 3) - 2) out += Mv::to(d_y + 4, d_x + 1) + Theme::c("title") + Fx::b + rjust((item_fit > 4 ? "Memory: " : "M:") + mem_str + "% ", (d_width / 3) - 2)
@ -1430,7 +1430,7 @@ namespace Proc {
if (p.filtered or (proc_tree and p.tree_index == plist.size()) or n++ < start) continue; if (p.filtered or (proc_tree and p.tree_index == plist.size()) or n++ < start) continue;
bool is_selected = (lc + 1 == selected); bool is_selected = (lc + 1 == selected);
if (is_selected) { if (is_selected) {
selected_pid = (int)p.pid; selected_pid = p.pid;
selected_name = p.name; selected_name = p.name;
selected_depth = p.depth; selected_depth = p.depth;
} }
@ -1464,7 +1464,7 @@ namespace Proc {
if (proc_colors) { if (proc_colors) {
end = Theme::c("main_fg") + Fx::ub; end = Theme::c("main_fg") + Fx::ub;
array<string, 3> colors; array<string, 3> colors;
for (int i = 0; int v : {(int)round(p.cpu_p), (int)round(p.mem * 100 / totalMem), (int)p.threads / 3}) { for (int i = 0; int v : {static_cast<int>(round(p.cpu_p)), static_cast<int>(round(p.mem * 100.0 / totalMem)), static_cast<int>(p.threads / 3)}) {
if (proc_gradient) { if (proc_gradient) {
int val = (min(v, 100) + 100) - calc * 100 / select_max; int val = (min(v, 100) + 100) - calc * 100 / select_max;
if (val < 100) colors[i++] = Theme::g("proc_color").at(max(0, val)); if (val < 100) colors[i++] = Theme::g("proc_color").at(max(0, val));
@ -1523,17 +1523,17 @@ namespace Proc {
} }
string mem_str = (mem_bytes ? floating_humanizer(p.mem, true) : ""); string mem_str = (mem_bytes ? floating_humanizer(p.mem, true) : "");
if (not mem_bytes) { if (not mem_bytes) {
double mem_p = clamp((double)p.mem * 100 / totalMem, 0.0, 100.0); double mem_p = clamp(p.mem * 100.0 / totalMem, 0.0, 100.0);
mem_str = to_string(mem_p); mem_str = to_string(mem_p);
if (mem_str.size() < 4) mem_str = "0"; if (mem_str.size() < 4) mem_str = "0";
else mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4)); else mem_str.resize((mem_p < 10 or mem_p >= 100 ? 3 : 4));
mem_str += '%'; mem_str += '%';
} }
out += (thread_size > 0 ? t_color + rjust(to_string(min(p.threads, (size_t)9999)), thread_size) + ' ' + end : "" ) out += (thread_size > 0 ? t_color + rjust(to_string(min(p.threads, static_cast<size_t>(9999))), thread_size) + ' ' + end : "" )
+ g_color + ljust((cmp_greater(p.user.size(), user_size) ? p.user.substr(0, user_size - 1) + '+' : p.user), user_size) + ' ' + g_color + ljust((cmp_greater(p.user.size(), user_size) ? p.user.substr(0, user_size - 1) + '+' : p.user), user_size) + ' '
+ m_color + rjust(mem_str, 5) + end + ' ' + m_color + rjust(mem_str, 5) + end + ' '
+ (is_selected ? "" : Theme::c("inactive_fg")) + (show_graphs ? graph_bg * 5: "") + (is_selected ? "" : Theme::c("inactive_fg")) + (show_graphs ? graph_bg * 5: "")
+ (p_graphs.contains(p.pid) ? Mv::l(5) + c_color + p_graphs.at(p.pid)({(p.cpu_p >= 0.1 and p.cpu_p < 5 ? 5ll : (long long)round(p.cpu_p))}, data_same) : "") + end + ' ' + (p_graphs.contains(p.pid) ? Mv::l(5) + c_color + p_graphs.at(p.pid)({(p.cpu_p >= 0.1 and p.cpu_p < 5 ? 5ll : llround(p.cpu_p))}, data_same) : "") + end + ' '
+ c_color + rjust(cpu_str, 4) + " " + end; + c_color + rjust(cpu_str, 4) + " " + end;
if (lc++ > height - 5) break; if (lc++ > height - 5) break;
} }
@ -1543,7 +1543,7 @@ namespace Proc {
//? Draw scrollbar if needed //? Draw scrollbar if needed
if (numpids > select_max) { if (numpids > select_max) {
const int scroll_pos = clamp((int)round((double)start * select_max / (numpids - select_max)), 0, height - 5); const int scroll_pos = clamp(static_cast<int>(round(static_cast<double>(start) * select_max / (numpids - select_max))), 0, height - 5);
out += Mv::to(y + 1, x + width - 2) + Fx::b + Theme::c("main_fg") + Symbols::up out += Mv::to(y + 1, x + width - 2) + Fx::b + Theme::c("main_fg") + Symbols::up
+ Mv::to(y + height - 2, x + width - 2) + Symbols::down + Mv::to(y + height - 2, x + width - 2) + Symbols::down
+ Mv::to(y + 2 + scroll_pos, x + width - 2) + ""; + Mv::to(y + 2 + scroll_pos, x + width - 2) + "";
@ -1551,8 +1551,8 @@ namespace Proc {
//? Current selection and number of processes //? Current selection and number of processes
string location = to_string(start + selected) + '/' + to_string(numpids); string location = to_string(start + selected) + '/' + to_string(numpids);
string loc_clear = Symbols::h_line * max((size_t)0, 9 - location.size()); string loc_clear = Symbols::h_line * max(static_cast<size_t>(0), 9 - location.size());
out += Mv::to(y + height - 1, x+width - 3 - max(9, (int)location.size())) + Fx::ub + Theme::c("proc_box") + loc_clear out += Mv::to(y + height - 1, x+width - 3 - max(9, static_cast<int>(location.size()))) + Fx::ub + Theme::c("proc_box") + loc_clear
+ Symbols::title_left_down + Theme::c("title") + Fx::b + location + Fx::ub + Theme::c("proc_box") + Symbols::title_right_down; + Symbols::title_left_down + Theme::c("title") + Fx::b + location + Fx::ub + Theme::c("proc_box") + Symbols::title_right_down;
//? Clear out left over graphs from dead processes at a regular interval //? Clear out left over graphs from dead processes at a regular interval
@ -1627,12 +1627,12 @@ namespace Draw {
if (Cpu::shown) { if (Cpu::shown) {
using namespace Cpu; using namespace Cpu;
bool show_temp = (Config::getB("check_temp") and got_sensors); bool show_temp = (Config::getB("check_temp") and got_sensors);
width = round((double)Term::width * width_p / 100); width = round(Term::width * width_p / 100.0);
height = max(8, (int)ceil((double)Term::height * (trim(boxes) == "cpu" ? 100 : height_p) / 100)); height = max(8, static_cast<int>(ceil(Term::height * (trim(boxes) == "cpu" ? 100 : height_p) / 100.0)));
x = 1; x = 1;
y = cpu_bottom ? Term::height - height + 1 : 1; y = cpu_bottom ? Term::height - height + 1 : 1;
b_columns = max(1, (int)ceil((double)(Shared::coreCount + 1) / (height - 5))); b_columns = max(1, static_cast<int>(ceil((Shared::coreCount + 1) / (height - 5.0))));
if (b_columns * (21 + 12 * show_temp) < width - (width / 3)) { if (b_columns * (21 + 12 * show_temp) < width - (width / 3)) {
b_column_size = 2; b_column_size = 2;
b_width = (21 + 12 * show_temp) * b_columns - (b_columns - 1); b_width = (21 + 12 * show_temp) * b_columns - (b_columns - 1);
@ -1650,10 +1650,10 @@ namespace Draw {
} }
if (b_column_size == 0) b_width = (8 + 6 * show_temp) * b_columns + 1; if (b_column_size == 0) b_width = (8 + 6 * show_temp) * b_columns + 1;
b_height = min(height - 2, (int)ceil((double)Shared::coreCount / b_columns) + 4); b_height = min(height - 2, static_cast<int>(ceil(static_cast<double>(Shared::coreCount) / b_columns)) + 4);
b_x = x + width - b_width - 1; b_x = x + width - b_width - 1;
b_y = y + ceil((double)(height - 2) / 2) - ceil((double)b_height / 2) + 1; b_y = y + ceil((height - 2) / 2.0) - ceil(b_height / 2.0) + 1;
box = createBox(x, y, width, height, Theme::c("cpu_box"), true, (cpu_bottom ? "" : "cpu"), (cpu_bottom ? "cpu" : ""), 1); box = createBox(x, y, width, height, Theme::c("cpu_box"), true, (cpu_bottom ? "" : "cpu"), (cpu_bottom ? "cpu" : ""), 1);
@ -1669,8 +1669,8 @@ namespace Draw {
auto swap_disk = Config::getB("swap_disk"); auto swap_disk = Config::getB("swap_disk");
auto mem_graphs = Config::getB("mem_graphs"); auto mem_graphs = Config::getB("mem_graphs");
width = round((double)Term::width * (Proc::shown ? width_p : 100) / 100); width = round(Term::width * (Proc::shown ? width_p : 100) / 100.0);
height = ceil((double)Term::height * (100 - Cpu::height_p * Cpu::shown - Net::height_p * Net::shown) / 100) + 1; height = ceil(Term::height * (100 - Cpu::height_p * Cpu::shown - Net::height_p * Net::shown) / 100.0) + 1;
if (height + Cpu::height > Term::height) height = Term::height - Cpu::height; if (height + Cpu::height > Term::height) height = Term::height - Cpu::height;
x = (proc_left and Proc::shown) ? Term::width - width + 1: 1; x = (proc_left and Proc::shown) ? Term::width - width + 1: 1;
if (mem_below_net and Net::shown) if (mem_below_net and Net::shown)
@ -1679,7 +1679,7 @@ namespace Draw {
y = cpu_bottom ? 1 : Cpu::height + 1; y = cpu_bottom ? 1 : Cpu::height + 1;
if (show_disks) { if (show_disks) {
mem_width = ceil((double)(width - 3) / 2); mem_width = ceil((width - 3) / 2.0);
mem_width += mem_width % 2; mem_width += mem_width % 2;
disks_width = width - mem_width - 2; disks_width = width - mem_width - 2;
divider = x + mem_width; divider = x + mem_width;
@ -1699,7 +1699,7 @@ namespace Draw {
if (mem_size == 1) mem_meter += 6; if (mem_size == 1) mem_meter += 6;
if (mem_graphs) { if (mem_graphs) {
graph_height = max(1, (int)round((double)((height - (has_swap and not swap_disk ? 2 : 1)) - (mem_size == 3 ? 2 : 1) * item_height) / item_height)); graph_height = max(1, static_cast<int>(round((height - (has_swap and not swap_disk ? 2 : 1)) - (mem_size == 3 ? 2 : 1) * item_height / static_cast<double>(item_height))));
if (graph_height > 1) mem_meter += 6; if (graph_height > 1) mem_meter += 6;
} }
else else
@ -1724,7 +1724,7 @@ namespace Draw {
//* Calculate and draw net box outlines //* Calculate and draw net box outlines
if (Net::shown) { if (Net::shown) {
using namespace Net; using namespace Net;
width = round((double)Term::width * (Proc::shown ? width_p : 100) / 100); width = round(Term::width * (Proc::shown ? width_p : 100) / 100.0);
height = Term::height - Cpu::height - Mem::height; height = Term::height - Cpu::height - Mem::height;
x = (proc_left and Proc::shown) ? Term::width - width + 1 : 1; x = (proc_left and Proc::shown) ? Term::width - width + 1 : 1;
if (mem_below_net and Mem::shown) if (mem_below_net and Mem::shown)
@ -1736,7 +1736,7 @@ namespace Draw {
b_height = (height > 10) ? 9 : height - 2; b_height = (height > 10) ? 9 : height - 2;
b_x = x + width - b_width - 1; b_x = x + width - b_width - 1;
b_y = y + ((height - 2) / 2) - b_height / 2 + 1; b_y = y + ((height - 2) / 2) - b_height / 2 + 1;
d_graph_height = round((double)(height - 2) / 2); d_graph_height = round((height - 2) / 2.0);
u_graph_height = height - 2 - d_graph_height; u_graph_height = height - 2 - d_graph_height;
box = createBox(x, y, width, height, Theme::c("net_box"), true, "net", "", 3); box = createBox(x, y, width, height, Theme::c("net_box"), true, "net", "", 3);

View file

@ -16,19 +16,21 @@ indent = tab
tab-size = 4 tab-size = 4
*/ */
#include <charconv>
#include <csignal>
#include <iostream> #include <iostream>
#include <ranges>
#include <vector>
#include <thread>
#include <mutex> #include <mutex>
#include <signal.h> #include <ranges>
#include <stdexcept>
#include <thread>
#include <vector>
#include "btop_input.hpp"
#include "btop_tools.hpp"
#include "btop_config.hpp" #include "btop_config.hpp"
#include "btop_shared.hpp"
#include "btop_menu.hpp"
#include "btop_draw.hpp" #include "btop_draw.hpp"
#include "btop_input.hpp"
#include "btop_menu.hpp"
#include "btop_shared.hpp"
#include "btop_tools.hpp"
using std::cin; using std::cin;
@ -190,8 +192,10 @@ namespace Input {
if (not key.empty()) { if (not key.empty()) {
try { try {
const auto delim = key_view.find(';'); const auto delim = key_view.find(';');
mouse_pos[0] = stoi((string)key_view.substr(0, delim)); auto result = std::from_chars(key_view.data(), key_view.data() + delim, mouse_pos[0]);
mouse_pos[1] = stoi((string)key_view.substr(delim + 1, key_view.find('M', delim))); if (result.ec == std::errc::invalid_argument) { throw std::invalid_argument(""); }
result = std::from_chars(key_view.data() + delim + 1, key_view.data() + key_view.find('M', delim + 1), mouse_pos[1]);
if (result.ec == std::errc::invalid_argument) { throw std::invalid_argument(""); }
} }
catch (const std::invalid_argument&) { mouse_event.clear(); } catch (const std::invalid_argument&) { mouse_event.clear(); }
catch (const std::out_of_range&) { mouse_event.clear(); } catch (const std::out_of_range&) { mouse_event.clear(); }
@ -271,7 +275,7 @@ namespace Input {
} }
else if (is_in(key, "p", "P") and Config::preset_list.size() > 1) { else if (is_in(key, "p", "P") and Config::preset_list.size() > 1) {
if (key == "p") { if (key == "p") {
if (++Config::current_preset >= (int)Config::preset_list.size()) Config::current_preset = 0; if (++Config::current_preset >= static_cast<int>(Config::preset_list.size())) Config::current_preset = 0;
} }
else { else {
if (--Config::current_preset < 0) Config::current_preset = Config::preset_list.size() - 1; if (--Config::current_preset < 0) Config::current_preset = Config::preset_list.size() - 1;
@ -507,12 +511,12 @@ namespace Input {
if (is_in(key, "b", "n")) { if (is_in(key, "b", "n")) {
atomic_wait(Runner::active); atomic_wait(Runner::active);
int c_index = v_index(Net::interfaces, Net::selected_iface); int c_index = v_index(Net::interfaces, Net::selected_iface);
if (c_index != (int)Net::interfaces.size()) { if (c_index != static_cast<int>(Net::interfaces.size())) {
if (key == "b") { if (key == "b") {
if (--c_index < 0) c_index = Net::interfaces.size() - 1; if (--c_index < 0) c_index = Net::interfaces.size() - 1;
} }
else if (key == "n") { else if (key == "n") {
if (++c_index == (int)Net::interfaces.size()) c_index = 0; if (++c_index == static_cast<int>(Net::interfaces.size())) c_index = 0;
} }
Net::selected_iface = Net::interfaces.at(c_index); Net::selected_iface = Net::interfaces.at(c_index);
Net::rescale = true; Net::rescale = true;

View file

@ -645,7 +645,7 @@ namespace Menu {
box_contents = Draw::createBox(x, y, width, height, Theme::c("hi_fg"), true, title) + Mv::d(1); box_contents = Draw::createBox(x, y, width, height, Theme::c("hi_fg"), true, title) + Mv::d(1);
for (const auto& line : content) { for (const auto& line : content) {
box_contents += Mv::save + Mv::r(max((size_t)0, (width / 2) - (Fx::uncolor(line).size() / 2) - 1)) + line + Mv::restore + Mv::d(1); box_contents += Mv::save + Mv::r(max(static_cast<size_t>(0), (width / 2) - (Fx::uncolor(line).size() / 2) - 1)) + line + Mv::restore + Mv::d(1);
} }
} }
@ -1034,7 +1034,7 @@ namespace Menu {
auto vim_keys = Config::getB("vim_keys"); auto vim_keys = Config::getB("vim_keys");
if (max_items == 0) { if (max_items == 0) {
for (const auto& cat : categories) { for (const auto& cat : categories) {
if ((int)cat.size() > max_items) max_items = cat.size(); if (static_cast<int>(cat.size()) > max_items) max_items = cat.size();
} }
} }
if (bg.empty()) { if (bg.empty()) {
@ -1112,7 +1112,7 @@ namespace Menu {
return Closed; return Closed;
} }
else if (mouse_x < x + 30 and mouse_y > y + 8) { else if (mouse_x < x + 30 and mouse_y > y + 8) {
auto m_select = ceil((double)(mouse_y - y - 8) / 2) - 1; auto m_select = ceil((mouse_y - y - 8) / 2.0) - 1;
if (selected != m_select) if (selected != m_select)
selected = m_select; selected = m_select;
else if (selPred.test(isEditable)) else if (selPred.test(isEditable))
@ -1154,11 +1154,11 @@ namespace Menu {
selected = 0; selected = 0;
} }
else if (key == "tab") { else if (key == "tab") {
if (++selected_cat >= (int)categories.size()) selected_cat = 0; if (++selected_cat >= static_cast<int>(categories.size())) selected_cat = 0;
page = selected = 0; page = selected = 0;
} }
else if (key == "shift_tab") { else if (key == "shift_tab") {
if (--selected_cat < 0) selected_cat = (int)categories.size() - 1; if (--selected_cat < 0) selected_cat = static_cast<int>(categories.size()) - 1;
page = selected = 0; page = selected = 0;
} }
else if (is_in(key, "1", "2", "3", "4", "5") or key.starts_with("select_cat_")) { else if (is_in(key, "1", "2", "3", "4", "5") or key.starts_with("select_cat_")) {
@ -1203,7 +1203,7 @@ namespace Menu {
auto& optList = optionsList.at(option).get(); auto& optList = optionsList.at(option).get();
int i = v_index(optList, Config::getS(option)); int i = v_index(optList, Config::getS(option));
if ((key == "right" or (vim_keys and key == "l")) and ++i >= (int)optList.size()) i = 0; if ((key == "right" or (vim_keys and key == "l")) and ++i >= static_cast<int>(optList.size())) i = 0;
else if ((key == "left" or (vim_keys and key == "h")) and --i < 0) i = optList.size() - 1; else if ((key == "left" or (vim_keys and key == "h")) and --i < 0) i = optList.size() - 1;
Config::set(option, optList.at(i)); Config::set(option, optList.at(i));
@ -1228,10 +1228,10 @@ namespace Menu {
Config::unlock(); Config::unlock();
auto& out = Global::overlay; auto& out = Global::overlay;
out = bg; out = bg;
item_height = min((int)categories[selected_cat].size(), (int)floor((double)(height - 4) / 2)); item_height = min(static_cast<int>(categories[selected_cat].size()), (height - 4) / 2);
pages = ceil((double)categories[selected_cat].size() / item_height); pages = ceil(static_cast<double>(categories[selected_cat].size()) / item_height);
if (page > pages - 1) page = pages - 1; if (page > pages - 1) page = pages - 1;
select_max = min(item_height - 1, (int)categories[selected_cat].size() - 1 - item_height * page); select_max = min(item_height - 1, static_cast<int>(categories[selected_cat].size()) - 1 - item_height * page);
if (selected > select_max) { if (selected > select_max) {
selected = select_max; selected = select_max;
} }
@ -1274,9 +1274,9 @@ namespace Menu {
} }
//? Option name and value //? Option name and value
auto cy = y+9; auto cy = y+9;
for (int c = 0, i = max(0, item_height * page); c++ < item_height and i < (int)categories[selected_cat].size(); i++) { for (int c = 0, i = max(0, item_height * page); c++ < item_height and i < static_cast<int>(categories[selected_cat].size()); i++) {
const auto& option = categories[selected_cat][i][0]; const auto& option = categories[selected_cat][i][0];
const auto& value = (option == "color_theme" ? (string) fs::path(Config::getS("color_theme")).stem() : Config::getAsString(option)); const auto& value = (option == "color_theme" ? fs::path(Config::getS("color_theme")).stem().string() : Config::getAsString(option));
out += Mv::to(cy++, x + 1) + (c-1 == selected ? Theme::c("selected_bg") + Theme::c("selected_fg") : Theme::c("title")) out += Mv::to(cy++, x + 1) + (c-1 == selected ? Theme::c("selected_bg") + Theme::c("selected_fg") : Theme::c("title"))
+ Fx::b + cjust(capitalize(s_replace(option, "_", " ")) + Fx::b + cjust(capitalize(s_replace(option, "_", " "))
@ -1307,7 +1307,7 @@ namespace Menu {
} }
if (not warnings.empty()) { if (not warnings.empty()) {
messageBox = msgBox{min(78, (int)ulen(warnings) + 10), msgBox::BoxTypes::OK, {uresize(warnings, 74)}, "warning"}; messageBox = msgBox{min(78, static_cast<int>(ulen(warnings)) + 10), msgBox::BoxTypes::OK, {uresize(warnings, 74)}, "warning"};
out += messageBox(); out += messageBox();
} }
@ -1348,10 +1348,10 @@ namespace Menu {
int retval = Changed; int retval = Changed;
if (redraw) { if (redraw) {
y = max(1, Term::height/2 - 4 - (int)(help_text.size() / 2)); y = max(1, Term::height / 2 - 4 - static_cast<int>(help_text.size()) / 2);
x = Term::width/2 - 39; x = Term::width / 2 - 39;
height = min(Term::height - 6, (int)help_text.size() + 3); height = min(Term::height - 6, static_cast<int>(help_text.size()) + 3);
pages = ceil((double)help_text.size() / (height - 3)); pages = ceil(help_text.size() / (height - 3.0));
page = 0; page = 0;
bg = Draw::banner_gen(y, 0, true); bg = Draw::banner_gen(y, 0, true);
bg += Draw::createBox(x, y + 6, 78, height, Theme::c("hi_fg"), true, "help"); bg += Draw::createBox(x, y + 6, 78, height, Theme::c("hi_fg"), true, "help");
@ -1379,7 +1379,7 @@ namespace Menu {
} }
auto cy = y+7; auto cy = y+7;
out += Mv::to(cy++, x + 1) + Theme::c("title") + Fx::b + cjust("Key:", 20) + "Description:"; out += Mv::to(cy++, x + 1) + Theme::c("title") + Fx::b + cjust("Key:", 20) + "Description:";
for (int c = 0, i = max(0, (height - 3) * page); c++ < height - 3 and i < (int)help_text.size(); i++) { for (int c = 0, i = max(0, (height - 3) * page); c++ < height - 3 and i < static_cast<int>(help_text.size()); i++) {
out += Mv::to(cy++, x + 1) + Theme::c("hi_fg") + Fx::b + cjust(help_text[i][0], 20) out += Mv::to(cy++, x + 1) + Theme::c("hi_fg") + Fx::b + cjust(help_text[i][0], 20)
+ Theme::c("main_fg") + Fx::ub + help_text[i][1]; + Theme::c("main_fg") + Fx::ub + help_text[i][1];
} }
@ -1426,7 +1426,7 @@ namespace Menu {
menuMask.set(SizeError); menuMask.set(SizeError);
} }
for (const auto& i : iota(0, (int)menuMask.size())) { for (const auto& i : iota(0, static_cast<int>(menuMask.size()))) {
if (menuMask.test(i)) currentMenu = i; if (menuMask.test(i)) currentMenu = i;
} }

View file

@ -93,7 +93,7 @@ namespace Proc {
for (auto& r : proc_vec) { for (auto& r : proc_vec) {
r.entry.get().tree_index = (collapsed or r.entry.get().filtered ? index_max : c_index++); r.entry.get().tree_index = (collapsed or r.entry.get().filtered ? index_max : c_index++);
if (not r.children.empty()) { if (not r.children.empty()) {
tree_sort(r.children, sorting, reverse, c_index, (collapsed or r.entry.get().collapsed or r.entry.get().tree_index == (size_t)index_max)); tree_sort(r.children, sorting, reverse, c_index, (collapsed or r.entry.get().collapsed or r.entry.get().tree_index == static_cast<size_t>(index_max)));
} }
} }
} }
@ -130,7 +130,7 @@ namespace Proc {
//? Try to find name of the binary file and append to program name if not the same //? Try to find name of the binary file and append to program name if not the same
if (cur_proc.short_cmd.empty() and not cur_proc.cmd.empty()) { if (cur_proc.short_cmd.empty() and not cur_proc.cmd.empty()) {
std::string_view cmd_view = cur_proc.cmd; std::string_view cmd_view = cur_proc.cmd;
cmd_view = cmd_view.substr((size_t)0, std::min(cmd_view.find(' '), cmd_view.size())); cmd_view = cmd_view.substr(0, std::min(cmd_view.find(' '), cmd_view.size()));
cmd_view = cmd_view.substr(std::min(cmd_view.find_last_of('/') + 1, cmd_view.size())); cmd_view = cmd_view.substr(std::min(cmd_view.find_last_of('/') + 1, cmd_view.size()));
cur_proc.short_cmd = string{cmd_view}; cur_proc.short_cmd = string{cmd_view};
} }

View file

@ -140,12 +140,12 @@ namespace Theme {
//* Convert 24-bit colors to 256 colors //* Convert 24-bit colors to 256 colors
int truecolor_to_256(const int& r, const int& g, const int& b) { int truecolor_to_256(const int& r, const int& g, const int& b) {
//? Use upper 232-255 greyscale values if the downscaled red, green and blue are the same value //? Use upper 232-255 greyscale values if the downscaled red, green and blue are the same value
if (const int red = round((double)r / 11); red == round((double)g / 11) and red == round((double)b / 11)) { if (r == g && g == b) {
return 232 + red; return 232 + round(r / 11.0);
} }
//? Else use 6x6x6 color cube to calculate approximate colors //? Else use 6x6x6 color cube to calculate approximate colors
else { else {
return round((double)r / 51) * 36 + round((double)g / 51) * 6 + round((double)b / 51) + 16; return lround(r / 51.0) * 36 + lround(g / 51.0) * 6 + lround(b / 51.0) + 16;
} }
} }
} }

View file

@ -118,7 +118,7 @@ namespace Term {
bool init() { bool init() {
if (not initialized) { if (not initialized) {
initialized = (bool)isatty(STDIN_FILENO); initialized = isatty(STDIN_FILENO) == 1;
if (initialized) { if (initialized) {
tcgetattr(STDIN_FILENO, &initial_settings); tcgetattr(STDIN_FILENO, &initial_settings);
current_tty = (ttyname(STDIN_FILENO) != nullptr ? static_cast<string>(ttyname(STDIN_FILENO)) : "unknown"); current_tty = (ttyname(STDIN_FILENO) != nullptr ? static_cast<string>(ttyname(STDIN_FILENO)) : "unknown");
@ -243,7 +243,7 @@ namespace Tools {
for (size_t x = 0, last_pos = 0, i = str.size() - 1; i > 0 ; i--) { for (size_t x = 0, last_pos = 0, i = str.size() - 1; i > 0 ; i--) {
if (wide and static_cast<unsigned char>(str.at(i)) > 0xef) { if (wide and static_cast<unsigned char>(str.at(i)) > 0xef) {
x += 2; x += 2;
last_pos = max((size_t)0, i - 1); last_pos = max(static_cast<size_t>(0), i - 1);
} }
else if ((static_cast<unsigned char>(str.at(i)) & 0xC0) != 0x80) { else if ((static_cast<unsigned char>(str.at(i)) & 0xC0) != 0x80) {
x++; x++;
@ -298,14 +298,14 @@ namespace Tools {
if (limit and ulen(str, wide) > x) if (limit and ulen(str, wide) > x)
return uresize(str, x, wide); return uresize(str, x, wide);
return str + string(max((int)(x - ulen(str)), 0), ' '); return str + string(max(static_cast<int>(x - ulen(str)), 0), ' ');
} }
else { else {
if (limit and str.size() > x) { if (limit and str.size() > x) {
str.resize(x); str.resize(x);
return str; return str;
} }
return str + string(max((int)(x - str.size()), 0), ' '); return str + string(max(static_cast<int>(x - str.size()), 0), ' ');
} }
} }
@ -314,14 +314,14 @@ namespace Tools {
if (limit and ulen(str, wide) > x) if (limit and ulen(str, wide) > x)
return uresize(str, x, wide); return uresize(str, x, wide);
return string(max((int)(x - ulen(str)), 0), ' ') + str; return string(max(static_cast<int>(x - ulen(str)), 0), ' ') + str;
} }
else { else {
if (limit and str.size() > x) { if (limit and str.size() > x) {
str.resize(x); str.resize(x);
return str; return str;
}; };
return string(max((int)(x - str.size()), 0), ' ') + str; return string(max(static_cast<int>(x - str.size()), 0), ' ') + str;
} }
} }
@ -330,14 +330,14 @@ namespace Tools {
if (limit and ulen(str, wide) > x) if (limit and ulen(str, wide) > x)
return uresize(str, x, wide); return uresize(str, x, wide);
return string(max((int)ceil((double)(x - ulen(str)) / 2), 0), ' ') + str + string(max((int)floor((double)(x - ulen(str)) / 2), 0), ' '); return string(max(static_cast<int>(ceil(static_cast<double>(x - ulen(str)) / 2)), 0), ' ') + str + string(max(static_cast<int>(floor((x - ulen(str)) / 2)), 0), ' ');
} }
else { else {
if (limit and str.size() > x) { if (limit and str.size() > x) {
str.resize(x); str.resize(x);
return str; return str;
} }
return string(max((int)ceil((double)(x - str.size()) / 2), 0), ' ') + str + string(max((int)floor((double)(x - str.size()) / 2), 0), ' '); return string(max(static_cast<int>(ceil((x - str.size()) / 2.0)), 0), ' ') + str + string(max(static_cast<int>(floor((x - str.size()) / 2.0)), 0), ' ');
} }
} }
@ -440,10 +440,10 @@ namespace Tools {
out = to_string(round(stod(out) * 10) / 10).substr(0,3); out = to_string(round(stod(out) * 10) / 10).substr(0,3);
} }
else if (f_pos != string::npos) { else if (f_pos != string::npos) {
out = to_string((int)round(stod(out))); out = to_string(round(stod(out)));
} }
if (out.size() > 3) { if (out.size() > 3) {
out = to_string((int)(out[0] - '0')) + ".0"; out = to_string((out[0] - '0')) + ".0";
start++; start++;
} }
out.push_back(units[start][0]); out.push_back(units[start][0]);
@ -515,11 +515,11 @@ namespace Tools {
if (scale == "celsius") if (scale == "celsius")
return {celsius, "°C"}; return {celsius, "°C"};
else if (scale == "fahrenheit") else if (scale == "fahrenheit")
return {(long long)round((double)celsius * 1.8 + 32), "°F"}; return {round(celsius * 1.8 + 32), "°F"};
else if (scale == "kelvin") else if (scale == "kelvin")
return {(long long)round((double)celsius + 273.15), "K "}; return {round(celsius + 273.15), "K "};
else if (scale == "rankine") else if (scale == "rankine")
return {(long long)round((double)celsius * 1.8 + 491.67), "°R"}; return {round(celsius * 1.8 + 491.67), "°R"};
return {0, ""}; return {0, ""};
} }

View file

@ -333,7 +333,7 @@ namespace Cpu {
//? If core mapping from cpuinfo was incomplete try to guess remainder, if missing completely, map 0-0 1-1 2-2 etc. //? If core mapping from cpuinfo was incomplete try to guess remainder, if missing completely, map 0-0 1-1 2-2 etc.
if (cmp_less(core_map.size(), Shared::coreCount)) { if (cmp_less(core_map.size(), Shared::coreCount)) {
if (Shared::coreCount % 2 == 0 and (long) core_map.size() == Shared::coreCount / 2) { if (Shared::coreCount % 2 == 0 and static_cast<long>(core_map.size()) == Shared::coreCount / 2) {
for (int i = 0, n = 0; i < Shared::coreCount / 2; i++) { for (int i = 0, n = 0; i < Shared::coreCount / 2; i++) {
if (std::cmp_greater_equal(n, core_sensors.size())) n = 0; if (std::cmp_greater_equal(n, core_sensors.size())) n = 0;
core_map[Shared::coreCount / 2 + i] = n++; core_map[Shared::coreCount / 2 + i] = n++;
@ -441,14 +441,14 @@ namespace Cpu {
core_old_totals.at(i) = totals; core_old_totals.at(i) = totals;
core_old_idles.at(i) = idles; core_old_idles.at(i) = idles;
cpu.core_percent.at(i).push_back(clamp((long long)round((double)(calc_totals - calc_idles) * 100 / calc_totals), 0ll, 100ll)); cpu.core_percent.at(i).push_back(clamp(llround((calc_totals - calc_idles) * 100.0 / calc_totals), 0ll, 100ll));
//? Reduce size if there are more values than needed for graph //? Reduce size if there are more values than needed for graph
if (cpu.core_percent.at(i).size() > 40) cpu.core_percent.at(i).pop_front(); if (cpu.core_percent.at(i).size() > 40) cpu.core_percent.at(i).pop_front();
} catch (const std::exception &e) { } catch (const std::exception &e) {
Logger::error("Cpu::collect() : " + (string)e.what()); Logger::error("Cpu::collect() : " + string{e.what()});
throw std::runtime_error("collect() : " + (string)e.what()); throw std::runtime_error("collect() : " + string{e.what()});
} }
} }
@ -458,7 +458,7 @@ namespace Cpu {
//? Populate cpu.cpu_percent with all fields from syscall //? Populate cpu.cpu_percent with all fields from syscall
for (int ii = 0; const auto &val : times_summed) { for (int ii = 0; const auto &val : times_summed) {
cpu.cpu_percent.at(time_names.at(ii)).push_back(clamp((long long)round((double)(val - cpu_old.at(time_names.at(ii))) * 100 / calc_totals), 0ll, 100ll)); cpu.cpu_percent.at(time_names.at(ii)).push_back(clamp(llround((val - cpu_old.at(time_names.at(ii))) * 100.0 / calc_totals), 0ll, 100ll));
cpu_old.at(time_names.at(ii)) = val; cpu_old.at(time_names.at(ii)) = val;
//? Reduce size if there are more values than needed for graph //? Reduce size if there are more values than needed for graph
@ -471,7 +471,7 @@ namespace Cpu {
cpu_old.at("idles") = global_idles; cpu_old.at("idles") = global_idles;
//? Total usage of cpu //? Total usage of cpu
cpu.cpu_percent.at("total").push_back(clamp((long long)round((double)(calc_totals - calc_idles) * 100 / calc_totals), 0ll, 100ll)); cpu.cpu_percent.at("total").push_back(clamp(llround((calc_totals - calc_idles) * 100.0 / calc_totals), 0ll, 100ll));
//? Reduce size if there are more values than needed for graph //? Reduce size if there are more values than needed for graph
while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front(); while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front();
@ -511,7 +511,7 @@ namespace Mem {
if (disk.io_read.empty()) { if (disk.io_read.empty()) {
disk.io_read.push_back(0); disk.io_read.push_back(0);
} else { } else {
disk.io_read.push_back(max((int64_t)0, (readBytes - disk.old_io.at(0)))); disk.io_read.push_back(max(static_cast<int64_t>(0), (readBytes - disk.old_io.at(0))));
} }
disk.old_io.at(0) = readBytes; disk.old_io.at(0) = readBytes;
while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front(); while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front();
@ -519,7 +519,7 @@ namespace Mem {
if (disk.io_write.empty()) { if (disk.io_write.empty()) {
disk.io_write.push_back(0); disk.io_write.push_back(0);
} else { } else {
disk.io_write.push_back(max((int64_t)0, (writeBytes - disk.old_io.at(1)))); disk.io_write.push_back(max(static_cast<int64_t>(0), (writeBytes - disk.old_io.at(1))));
} }
disk.old_io.at(1) = writeBytes; disk.old_io.at(1) = writeBytes;
while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front(); while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front();
@ -528,7 +528,7 @@ namespace Mem {
if (disk.io_activity.empty()) if (disk.io_activity.empty())
disk.io_activity.push_back(0); disk.io_activity.push_back(0);
else else
disk.io_activity.push_back(clamp((long)round((double)(disk.io_write.back() + disk.io_read.back()) / (1 << 20)), 0l, 100l)); disk.io_activity.push_back(clamp(lround(static_cast<double>(disk.io_write.back() + disk.io_read.back()) / (1 << 20)), 0l, 100l));
while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front(); while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front();
} }
@ -676,7 +676,7 @@ namespace Mem {
if (show_swap and mem.stats.at("swap_total") > 0) { if (show_swap and mem.stats.at("swap_total") > 0) {
for (const auto &name : swap_names) { for (const auto &name : swap_names) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / mem.stats.at("swap_total"))); mem.percent.at(name).push_back(round(mem.stats.at(name) * 100.0 / mem.stats.at("swap_total")));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) while (cmp_greater(mem.percent.at(name).size(), width * 2))
mem.percent.at(name).pop_front(); mem.percent.at(name).pop_front();
} }
@ -685,7 +685,7 @@ namespace Mem {
has_swap = false; has_swap = false;
//? Calculate percentages //? Calculate percentages
for (const auto &name : mem_names) { for (const auto &name : mem_names) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / Shared::totalMem)); mem.percent.at(name).push_back(round(mem.stats.at(name) * 100.0 / Shared::totalMem));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) while (cmp_greater(mem.percent.at(name).size(), width * 2))
mem.percent.at(name).pop_front(); mem.percent.at(name).pop_front();
} }
@ -772,7 +772,7 @@ namespace Mem {
disk.total = vfs.f_blocks * vfs.f_frsize; disk.total = vfs.f_blocks * vfs.f_frsize;
disk.free = vfs.f_bfree * vfs.f_frsize; disk.free = vfs.f_bfree * vfs.f_frsize;
disk.used = disk.total - disk.free; disk.used = disk.total - disk.free;
disk.used_percent = round((double)disk.used * 100 / disk.total); disk.used_percent = round(disk.used * 100.0 / disk.total);
disk.free_percent = 100 - disk.used_percent; disk.free_percent = 100 - disk.used_percent;
} }
@ -905,11 +905,11 @@ namespace Net {
char *lim = buf.get() + len; char *lim = buf.get() + len;
char *next = nullptr; char *next = nullptr;
for (next = buf.get(); next < lim;) { for (next = buf.get(); next < lim;) {
struct if_msghdr *ifm = (struct if_msghdr *)next; struct if_msghdr *ifm = reinterpret_cast<struct if_msghdr*>(next);
next += ifm->ifm_msglen; next += ifm->ifm_msglen;
struct if_data ifm_data = ifm->ifm_data; struct if_data ifm_data = ifm->ifm_data;
if (ifm->ifm_addrs & RTA_IFP) { if (ifm->ifm_addrs & RTA_IFP) {
struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifm + 1); struct sockaddr_dl *sdl = reinterpret_cast<struct sockaddr_dl*>(ifm + 1);
char iface[32]; char iface[32];
strncpy(iface, sdl->sdl_data, sdl->sdl_nlen); strncpy(iface, sdl->sdl_data, sdl->sdl_nlen);
iface[sdl->sdl_nlen] = 0; iface[sdl->sdl_nlen] = 0;
@ -931,11 +931,11 @@ namespace Net {
saved_stat.rollover += saved_stat.last; saved_stat.rollover += saved_stat.last;
saved_stat.last = 0; saved_stat.last = 0;
} }
if (cmp_greater((unsigned long long)saved_stat.rollover + (unsigned long long)val, numeric_limits<uint64_t>::max())) { if (cmp_greater(static_cast<unsigned long long>(saved_stat.rollover) + static_cast<unsigned long long>(val), numeric_limits<uint64_t>::max())) {
saved_stat.rollover = 0; saved_stat.rollover = 0;
saved_stat.last = 0; saved_stat.last = 0;
} }
saved_stat.speed = round((double)(val - saved_stat.last) / ((double)(new_timestamp - timestamp) / 1000)); saved_stat.speed = round(static_cast<double>(val - saved_stat.last) / (new_timestamp - timestamp) / 1000);
if (saved_stat.speed > saved_stat.top) saved_stat.top = saved_stat.speed; if (saved_stat.speed > saved_stat.top) saved_stat.top = saved_stat.speed;
if (saved_stat.offset > val + saved_stat.rollover) saved_stat.offset = 0; if (saved_stat.offset > val + saved_stat.rollover) saved_stat.offset = 0;
saved_stat.total = (val + saved_stat.rollover) - saved_stat.offset; saved_stat.total = (val + saved_stat.rollover) - saved_stat.offset;
@ -1012,7 +1012,7 @@ namespace Net {
const long long avg_speed = (net[selected_iface].bandwidth[dir].size() > 5 const long long avg_speed = (net[selected_iface].bandwidth[dir].size() > 5
? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0ll) / 5 ? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0ll) / 5
: net[selected_iface].stat[dir].speed); : net[selected_iface].stat[dir].speed);
graph_max[dir] = max(uint64_t(avg_speed * (sel == 0 ? 1.3 : 3.0)), (uint64_t)10 << 10); graph_max[dir] = max(uint64_t(avg_speed * (sel == 0 ? 1.3 : 3.0)), static_cast<uint64_t>(10) << 10);
max_count[dir][0] = max_count[dir][1] = 0; max_count[dir][0] = max_count[dir][1] = 0;
redraw = true; redraw = true;
if (net_sync) sync = true; if (net_sync) sync = true;
@ -1075,7 +1075,7 @@ namespace Proc {
//? Update cpu percent deque for process cpu graph //? Update cpu percent deque for process cpu graph
if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount; if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount;
detailed.cpu_percent.push_back(clamp((long long)round(detailed.entry.cpu_p), 0ll, 100ll)); detailed.cpu_percent.push_back(clamp(llround(detailed.entry.cpu_p), 0ll, 100ll));
while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front(); while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front();
//? Process runtime : current time - start time (both in unix time - seconds since epoch) //? Process runtime : current time - start time (both in unix time - seconds since epoch)
@ -1097,7 +1097,7 @@ namespace Proc {
detailed.memory = floating_humanizer(detailed.entry.mem); detailed.memory = floating_humanizer(detailed.entry.mem);
if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) { if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) {
detailed.first_mem = min((uint64_t)detailed.mem_bytes.back() * 2, Mem::get_totalMem()); detailed.first_mem = min(static_cast<uint64_t>(detailed.mem_bytes.back()) * 2, Mem::get_totalMem());
redraw = true; redraw = true;
} }
@ -1164,7 +1164,7 @@ namespace Proc {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
const struct kinfo_proc* kproc = &kprocs[i]; const struct kinfo_proc* kproc = &kprocs[i];
const size_t pid = (size_t)kproc->ki_pid; const size_t pid = kproc->ki_pid;
if (pid < 1) continue; if (pid < 1) continue;
found.push_back(pid); found.push_back(pid);
@ -1219,7 +1219,7 @@ namespace Proc {
new_proc.cpu_p = clamp((100.0 * kproc->ki_pctcpu / Shared::kfscale) * cmult, 0.0, 100.0 * Shared::coreCount); new_proc.cpu_p = clamp((100.0 * kproc->ki_pctcpu / Shared::kfscale) * cmult, 0.0, 100.0 * Shared::coreCount);
//? Process cumulative cpu usage since process start //? Process cumulative cpu usage since process start
new_proc.cpu_c = (double)(cpu_t * Shared::clkTck / 1'000'000) / max(1.0, timeNow - new_proc.cpu_s); new_proc.cpu_c = (cpu_t * Shared::clkTck / 1'000'000.0) / max(1.0, timeNow - new_proc.cpu_s);
//? Update cached value with latest cpu times //? Update cached value with latest cpu times
new_proc.cpu_t = cpu_t; new_proc.cpu_t = cpu_t;
@ -1334,7 +1334,7 @@ namespace Proc {
} }
} }
numpids = (int)current_procs.size() - filter_found; numpids = static_cast<int>(current_procs.size()) - filter_found;
return current_procs; return current_procs;
} }
} // namespace Proc } // namespace Proc

View file

@ -339,11 +339,11 @@ namespace Cpu {
const int64_t temp = stol(readfile(basepath / "temp", "0")) / 1000; const int64_t temp = stol(readfile(basepath / "temp", "0")) / 1000;
int64_t high, crit; int64_t high, crit;
for (int ii = 0; fs::exists(basepath / string("trip_point_" + to_string(ii) + "_temp")); ii++) { for (int ii = 0; fs::exists(basepath / ("trip_point_" + to_string(ii) + "_temp")); ii++) {
const string trip_type = readfile(basepath / string("trip_point_" + to_string(ii) + "_type")); const string trip_type = readfile(basepath / ("trip_point_" + to_string(ii) + "_type"));
if (not is_in(trip_type, "high", "critical")) continue; if (not is_in(trip_type, "high", "critical")) continue;
auto& val = (trip_type == "high" ? high : crit); auto& val = (trip_type == "high" ? high : crit);
val = stol(readfile(basepath / string("trip_point_" + to_string(ii) + "_temp"), "0")) / 1000; val = stol(readfile(basepath / ("trip_point_" + to_string(ii) + "_temp"), "0")) / 1000;
} }
if (high < 1) high = 80; if (high < 1) high = 80;
if (crit < 1) crit = 95; if (crit < 1) crit = 95;
@ -444,12 +444,12 @@ namespace Cpu {
throw std::runtime_error("Failed to read /sys/devices/system/cpu/cpufreq/policy and /proc/cpuinfo."); throw std::runtime_error("Failed to read /sys/devices/system/cpu/cpufreq/policy and /proc/cpuinfo.");
if (hz >= 1000) { if (hz >= 1000) {
if (hz >= 10000) cpuhz = to_string((int)round(hz / 1000)); // Future proof until we reach THz speeds :) if (hz >= 10000) cpuhz = to_string(round(hz / 1000)); // Future proof until we reach THz speeds :)
else cpuhz = to_string(round(hz / 100) / 10.0).substr(0, 3); else cpuhz = to_string(round(hz / 100) / 10.0).substr(0, 3);
cpuhz += " GHz"; cpuhz += " GHz";
} }
else if (hz > 0) else if (hz > 0)
cpuhz = to_string((int)round(hz)) + " MHz"; cpuhz = to_string(round(hz)) + " MHz";
} }
catch (const std::exception& e) { catch (const std::exception& e) {
@ -495,7 +495,7 @@ namespace Cpu {
//? If core mapping from cpuinfo was incomplete try to guess remainder, if missing completely, map 0-0 1-1 2-2 etc. //? If core mapping from cpuinfo was incomplete try to guess remainder, if missing completely, map 0-0 1-1 2-2 etc.
if (cmp_less(core_map.size(), Shared::coreCount)) { if (cmp_less(core_map.size(), Shared::coreCount)) {
if (Shared::coreCount % 2 == 0 and (long)core_map.size() == Shared::coreCount / 2) { if (Shared::coreCount % 2 == 0 and static_cast<long>(core_map.size()) == Shared::coreCount / 2) {
for (int i = 0, n = 0; i < Shared::coreCount / 2; i++) { for (int i = 0, n = 0; i < Shared::coreCount / 2; i++) {
if (std::cmp_greater_equal(n, core_sensors.size())) n = 0; if (std::cmp_greater_equal(n, core_sensors.size())) n = 0;
core_map[Shared::coreCount / 2 + i] = n++; core_map[Shared::coreCount / 2 + i] = n++;
@ -649,7 +649,7 @@ namespace Cpu {
if (not is_in(status, "charging", "full")) { if (not is_in(status, "charging", "full")) {
if (b.use_energy and not b.power_now.empty()) { if (b.use_energy and not b.power_now.empty()) {
try { try {
seconds = round((double)stoll(readfile(b.energy_now, "0")) / stoll(readfile(b.power_now, "1")) * 3600); seconds = round(static_cast<double>(stoll(readfile(b.energy_now, "0"))) / stoll(readfile(b.power_now, "1")) * 3600);
} }
catch (const std::invalid_argument&) { } catch (const std::invalid_argument&) { }
catch (const std::out_of_range&) { } catch (const std::out_of_range&) { }
@ -744,14 +744,14 @@ namespace Cpu {
cpu_old.at("idles") = idles; cpu_old.at("idles") = idles;
//? Total usage of cpu //? Total usage of cpu
cpu.cpu_percent.at("total").push_back(clamp((long long)round((double)(calc_totals - calc_idles) * 100 / calc_totals), 0ll, 100ll)); cpu.cpu_percent.at("total").push_back(clamp(llround((calc_totals - calc_idles) * 100.0 / calc_totals), 0ll, 100ll));
//? Reduce size if there are more values than needed for graph //? Reduce size if there are more values than needed for graph
while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front(); while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front();
//? Populate cpu.cpu_percent with all fields from stat //? Populate cpu.cpu_percent with all fields from stat
for (int ii = 0; const auto& val : times) { for (int ii = 0; const auto& val : times) {
cpu.cpu_percent.at(time_names.at(ii)).push_back(clamp((long long)round((double)(val - cpu_old.at(time_names.at(ii))) * 100 / calc_totals), 0ll, 100ll)); cpu.cpu_percent.at(time_names.at(ii)).push_back(clamp(llround((val - cpu_old.at(time_names.at(ii))) * 100.0 / calc_totals), 0ll, 100ll));
cpu_old.at(time_names.at(ii)) = val; cpu_old.at(time_names.at(ii)) = val;
//? Reduce size if there are more values than needed for graph //? Reduce size if there are more values than needed for graph
@ -774,7 +774,7 @@ namespace Cpu {
core_old_totals.at(i-1) = totals; core_old_totals.at(i-1) = totals;
core_old_idles.at(i-1) = idles; core_old_idles.at(i-1) = idles;
cpu.core_percent.at(i-1).push_back(clamp((long long)round((double)(calc_totals - calc_idles) * 100 / calc_totals), 0ll, 100ll)); cpu.core_percent.at(i-1).push_back(clamp(llround((calc_totals - calc_idles) * 100.0 / calc_totals), 0ll, 100ll));
} }
} }
@ -913,13 +913,13 @@ namespace Mem {
//? Calculate percentages //? Calculate percentages
for (const auto& name : mem_names) { for (const auto& name : mem_names) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / totalMem)); mem.percent.at(name).push_back(round(mem.stats.at(name) * 100.0 / totalMem));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) mem.percent.at(name).pop_front(); while (cmp_greater(mem.percent.at(name).size(), width * 2)) mem.percent.at(name).pop_front();
} }
if (show_swap and mem.stats.at("swap_total") > 0) { if (show_swap and mem.stats.at("swap_total") > 0) {
for (const auto& name : swap_names) { for (const auto& name : swap_names) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / mem.stats.at("swap_total"))); mem.percent.at(name).push_back(round(mem.stats.at(name) * 100.0 / mem.stats.at("swap_total")));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) mem.percent.at(name).pop_front(); while (cmp_greater(mem.percent.at(name).size(), width * 2)) mem.percent.at(name).pop_front();
} }
has_swap = true; has_swap = true;
@ -1031,12 +1031,13 @@ namespace Mem {
if (disks.at(mountpoint).name.empty()) disks.at(mountpoint).name = (mountpoint == "/" ? "root" : mountpoint); if (disks.at(mountpoint).name.empty()) disks.at(mountpoint).name = (mountpoint == "/" ? "root" : mountpoint);
string devname = disks.at(mountpoint).dev.filename(); string devname = disks.at(mountpoint).dev.filename();
int c = 0; int c = 0;
const std::string_view dev_stat_path {"sys/block/" + devname + "/stat"};
while (devname.size() >= 2) { while (devname.size() >= 2) {
if (fs::exists("/sys/block/" + devname + "/stat", ec) and access(string("/sys/block/" + devname + "/stat").c_str(), R_OK) == 0) { if (fs::exists(dev_stat_path, ec) and access(dev_stat_path.data(), R_OK) == 0) {
if (c > 0 and fs::exists("/sys/block/" + devname + '/' + disks.at(mountpoint).dev.filename().string() + "/stat", ec)) if (c > 0 and fs::exists("/sys/block/" + devname + '/' + disks.at(mountpoint).dev.filename().string() + "/stat", ec))
disks.at(mountpoint).stat = "/sys/block/" + devname + '/' + disks.at(mountpoint).dev.filename().string() + "/stat"; disks.at(mountpoint).stat = "/sys/block/" + devname + '/' + disks.at(mountpoint).dev.filename().string() + "/stat";
else else
disks.at(mountpoint).stat = "/sys/block/" + devname + "/stat"; disks.at(mountpoint).stat = dev_stat_path;
break; break;
//? Set ZFS stat filepath //? Set ZFS stat filepath
} else if (fstype == "zfs") { } else if (fstype == "zfs") {
@ -1091,7 +1092,7 @@ namespace Mem {
disk.total = vfs.f_blocks * vfs.f_frsize; disk.total = vfs.f_blocks * vfs.f_frsize;
disk.free = (free_priv ? vfs.f_bfree : vfs.f_bavail) * vfs.f_frsize; disk.free = (free_priv ? vfs.f_bfree : vfs.f_bavail) * vfs.f_frsize;
disk.used = disk.total - disk.free; disk.used = disk.total - disk.free;
disk.used_percent = round((double)disk.used * 100 / disk.total); disk.used_percent = round(disk.used * 100.0 / disk.total);
disk.free_percent = 100 - disk.used_percent; disk.free_percent = 100 - disk.used_percent;
} }
@ -1154,7 +1155,7 @@ namespace Mem {
if (disk.io_write.empty()) if (disk.io_write.empty())
disk.io_write.push_back(0); disk.io_write.push_back(0);
else else
disk.io_write.push_back(max((int64_t)0, (sectors_write - disk.old_io.at(1)))); disk.io_write.push_back(max(static_cast<int64_t>(0), sectors_write - disk.old_io.at(1)));
disk.old_io.at(1) = sectors_write; disk.old_io.at(1) = sectors_write;
while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front(); while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front();
@ -1169,14 +1170,14 @@ namespace Mem {
if (disk.io_read.empty()) if (disk.io_read.empty())
disk.io_read.push_back(0); disk.io_read.push_back(0);
else else
disk.io_read.push_back(max((int64_t)0, (sectors_read - disk.old_io.at(0)))); disk.io_read.push_back(max(static_cast<int64_t>(0), sectors_read - disk.old_io.at(0)));
disk.old_io.at(0) = sectors_read; disk.old_io.at(0) = sectors_read;
while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front(); while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front();
if (disk.io_activity.empty()) if (disk.io_activity.empty())
disk.io_activity.push_back(0); disk.io_activity.push_back(0);
else else
disk.io_activity.push_back(max((int64_t)0, (io_ticks - disk.old_io.at(2)))); disk.io_activity.push_back(max(static_cast<int64_t>(0), io_ticks - disk.old_io.at(2)));
disk.old_io.at(2) = io_ticks; disk.old_io.at(2) = io_ticks;
while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front(); while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front();
} else { } else {
@ -1185,7 +1186,7 @@ namespace Mem {
if (disk.io_read.empty()) if (disk.io_read.empty())
disk.io_read.push_back(0); disk.io_read.push_back(0);
else else
disk.io_read.push_back(max((int64_t)0, (sectors_read - disk.old_io.at(0)) * 512)); disk.io_read.push_back(max(static_cast<int64_t>(0), (sectors_read - disk.old_io.at(0)) * 512));
disk.old_io.at(0) = sectors_read; disk.old_io.at(0) = sectors_read;
while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front(); while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front();
@ -1194,7 +1195,7 @@ namespace Mem {
if (disk.io_write.empty()) if (disk.io_write.empty())
disk.io_write.push_back(0); disk.io_write.push_back(0);
else else
disk.io_write.push_back(max((int64_t)0, (sectors_write - disk.old_io.at(1)) * 512)); disk.io_write.push_back(max(static_cast<int64_t>(0), (sectors_write - disk.old_io.at(1)) * 512));
disk.old_io.at(1) = sectors_write; disk.old_io.at(1) = sectors_write;
while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front(); while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front();
@ -1203,7 +1204,7 @@ namespace Mem {
if (disk.io_activity.empty()) if (disk.io_activity.empty())
disk.io_activity.push_back(0); disk.io_activity.push_back(0);
else else
disk.io_activity.push_back(clamp((long)round((double)(io_ticks - disk.old_io.at(2)) / (uptime - old_uptime) / 10), 0l, 100l)); disk.io_activity.push_back(clamp(llround(static_cast<double>(io_ticks - disk.old_io.at(2)) / (uptime - old_uptime) / 10.0), 0ll, 100ll));
disk.old_io.at(2) = io_ticks; disk.old_io.at(2) = io_ticks;
while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front(); while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front();
} }
@ -1330,21 +1331,21 @@ namespace Mem {
if (disk.io_write.empty()) if (disk.io_write.empty())
disk.io_write.push_back(0); disk.io_write.push_back(0);
else else
disk.io_write.push_back(max((int64_t)0, (bytes_write_total - disk.old_io.at(1)))); disk.io_write.push_back(max(static_cast<int64_t>(0), bytes_write_total - disk.old_io.at(1)));
disk.old_io.at(1) = bytes_write_total; disk.old_io.at(1) = bytes_write_total;
while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front(); while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front();
if (disk.io_read.empty()) if (disk.io_read.empty())
disk.io_read.push_back(0); disk.io_read.push_back(0);
else else
disk.io_read.push_back(max((int64_t)0, (bytes_read_total - disk.old_io.at(0)))); disk.io_read.push_back(max(static_cast<int64_t>(0), bytes_read_total - disk.old_io.at(0)));
disk.old_io.at(0) = bytes_read_total; disk.old_io.at(0) = bytes_read_total;
while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front(); while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front();
if (disk.io_activity.empty()) if (disk.io_activity.empty())
disk.io_activity.push_back(0); disk.io_activity.push_back(0);
else else
disk.io_activity.push_back(max((int64_t)0, (io_ticks_total - disk.old_io.at(2)))); disk.io_activity.push_back(max(static_cast<int64_t>(0), (io_ticks_total - disk.old_io.at(2))));
disk.old_io.at(2) = io_ticks_total; disk.old_io.at(2) = io_ticks_total;
while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front(); while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front();
@ -1452,7 +1453,7 @@ namespace Net {
auto& bandwidth = net.at(iface).bandwidth.at(dir); auto& bandwidth = net.at(iface).bandwidth.at(dir);
uint64_t val{}; // defaults to 0 uint64_t val{}; // defaults to 0
try { val = (uint64_t)stoull(readfile(sys_file, "0")); } try { val = stoull(readfile(sys_file, "0")); }
catch (const std::invalid_argument&) {} catch (const std::invalid_argument&) {}
catch (const std::out_of_range&) {} catch (const std::out_of_range&) {}
@ -1461,11 +1462,11 @@ namespace Net {
saved_stat.rollover += saved_stat.last; saved_stat.rollover += saved_stat.last;
saved_stat.last = 0; saved_stat.last = 0;
} }
if (cmp_greater((unsigned long long)saved_stat.rollover + (unsigned long long)val, numeric_limits<uint64_t>::max())) { if (cmp_greater(static_cast<uint64_t>(saved_stat.rollover) + static_cast<uint64_t>(val), numeric_limits<uint64_t>::max())) {
saved_stat.rollover = 0; saved_stat.rollover = 0;
saved_stat.last = 0; saved_stat.last = 0;
} }
saved_stat.speed = round((double)(val - saved_stat.last) / ((double)(new_timestamp - timestamp) / 1000)); saved_stat.speed = round(static_cast<double>(val - saved_stat.last) / (new_timestamp - timestamp) / 1000.0);
if (saved_stat.speed > saved_stat.top) saved_stat.top = saved_stat.speed; if (saved_stat.speed > saved_stat.top) saved_stat.top = saved_stat.speed;
if (saved_stat.offset > val + saved_stat.rollover) saved_stat.offset = 0; if (saved_stat.offset > val + saved_stat.rollover) saved_stat.offset = 0;
saved_stat.total = (val + saved_stat.rollover) - saved_stat.offset; saved_stat.total = (val + saved_stat.rollover) - saved_stat.offset;
@ -1544,7 +1545,7 @@ namespace Net {
const long long avg_speed = (net[selected_iface].bandwidth[dir].size() > 5 const long long avg_speed = (net[selected_iface].bandwidth[dir].size() > 5
? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0ll) / 5 ? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0ll) / 5
: net[selected_iface].stat[dir].speed); : net[selected_iface].stat[dir].speed);
graph_max[dir] = max(uint64_t(avg_speed * (sel == 0 ? 1.3 : 3.0)), (uint64_t)10 << 10); graph_max[dir] = max(static_cast<uint64_t>(avg_speed * (sel == 0 ? 1.3 : 3.0)), static_cast<uint64_t>(10) << 10);
max_count[dir][0] = max_count[dir][1] = 0; max_count[dir][0] = max_count[dir][1] = 0;
redraw = true; redraw = true;
if (net_sync) sync = true; if (net_sync) sync = true;
@ -1602,7 +1603,7 @@ namespace Proc {
//? Update cpu percent deque for process cpu graph //? Update cpu percent deque for process cpu graph
if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount; if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount;
detailed.cpu_percent.push_back(clamp((long long)round(detailed.entry.cpu_p), 0ll, 100ll)); detailed.cpu_percent.push_back(clamp(llround(detailed.entry.cpu_p), 0ll, 100ll));
while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front(); while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front();
//? Process runtime //? Process runtime
@ -1651,7 +1652,7 @@ namespace Proc {
detailed.memory = floating_humanizer(detailed.entry.mem); detailed.memory = floating_humanizer(detailed.entry.mem);
} }
if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) { if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) {
detailed.first_mem = min((uint64_t)detailed.mem_bytes.back() * 2, Mem::get_totalMem()); detailed.first_mem = min(static_cast<uint64_t>(detailed.mem_bytes.back()) * 2, Mem::get_totalMem());
redraw = true; redraw = true;
} }
@ -1934,10 +1935,10 @@ namespace Proc {
} }
//? Process cpu usage since last update //? Process cpu usage since last update
new_proc.cpu_p = clamp(round(cmult * 1000 * (cpu_t - new_proc.cpu_t) / max((uint64_t)1, cputimes - old_cputimes)) / 10.0, 0.0, 100.0 * Shared::coreCount); new_proc.cpu_p = clamp(round(cmult * 1000 * (cpu_t - new_proc.cpu_t) / max(static_cast<uint64_t>(1), cputimes - old_cputimes)) / 10.0, 0.0, 100.0 * Shared::coreCount);
//? Process cumulative cpu usage since process start //? Process cumulative cpu usage since process start
new_proc.cpu_c = (double)cpu_t / max(1.0, (uptime * Shared::clkTck) - new_proc.cpu_s); new_proc.cpu_c = cpu_t / max(1.0, (uptime * Shared::clkTck) - new_proc.cpu_s);
//? Update cached value with latest cpu times //? Update cached value with latest cpu times
new_proc.cpu_t = cpu_t; new_proc.cpu_t = cpu_t;
@ -2051,7 +2052,7 @@ namespace Proc {
} }
} }
numpids = (int)current_procs.size() - filter_found; numpids = current_procs.size() - filter_found;
return current_procs; return current_procs;
} }

View file

@ -107,7 +107,7 @@ namespace Mem {
processor_info_array_t info_array; processor_info_array_t info_array;
mach_msg_type_number_t info_count; mach_msg_type_number_t info_count;
MachProcessorInfo() {} MachProcessorInfo() {}
virtual ~MachProcessorInfo() {vm_deallocate(mach_task_self(), (vm_address_t)info_array, (vm_size_t)sizeof(processor_info_array_t) * info_count);} virtual ~MachProcessorInfo() {vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(info_array), static_cast<vm_size_t>(sizeof(processor_info_array_t)) * info_count);}
}; };
namespace Shared { namespace Shared {
@ -349,7 +349,7 @@ namespace Cpu {
//? If core mapping from cpuinfo was incomplete try to guess remainder, if missing completely, map 0-0 1-1 2-2 etc. //? If core mapping from cpuinfo was incomplete try to guess remainder, if missing completely, map 0-0 1-1 2-2 etc.
if (cmp_less(core_map.size(), Shared::coreCount)) { if (cmp_less(core_map.size(), Shared::coreCount)) {
if (Shared::coreCount % 2 == 0 and (long) core_map.size() == Shared::coreCount / 2) { if (Shared::coreCount % 2 == 0 and static_cast<long>(core_map.size()) == Shared::coreCount / 2) {
for (int i = 0, n = 0; i < Shared::coreCount / 2; i++) { for (int i = 0, n = 0; i < Shared::coreCount / 2; i++) {
if (std::cmp_greater_equal(n, core_sensors.size())) n = 0; if (std::cmp_greater_equal(n, core_sensors.size())) n = 0;
core_map[Shared::coreCount / 2 + i] = n++; core_map[Shared::coreCount / 2 + i] = n++;
@ -411,17 +411,17 @@ namespace Cpu {
if (CFArrayGetCount(one_ps_descriptor())) { if (CFArrayGetCount(one_ps_descriptor())) {
CFDictionaryRef one_ps = IOPSGetPowerSourceDescription(ps_info(), CFArrayGetValueAtIndex(one_ps_descriptor(), 0)); CFDictionaryRef one_ps = IOPSGetPowerSourceDescription(ps_info(), CFArrayGetValueAtIndex(one_ps_descriptor(), 0));
has_battery = true; has_battery = true;
CFNumberRef remaining = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSTimeToEmptyKey)); CFNumberRef remaining = static_cast<CFNumberRef>(CFDictionaryGetValue(one_ps, CFSTR(kIOPSTimeToEmptyKey)));
int32_t estimatedMinutesRemaining; int32_t estimatedMinutesRemaining;
if (remaining) { if (remaining) {
CFNumberGetValue(remaining, kCFNumberSInt32Type, &estimatedMinutesRemaining); CFNumberGetValue(remaining, kCFNumberSInt32Type, &estimatedMinutesRemaining);
seconds = estimatedMinutesRemaining * 60; seconds = estimatedMinutesRemaining * 60;
} }
CFNumberRef charge = (CFNumberRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSCurrentCapacityKey)); CFNumberRef charge = static_cast<CFNumberRef>(CFDictionaryGetValue(one_ps, CFSTR(kIOPSCurrentCapacityKey)));
if (charge) { if (charge) {
CFNumberGetValue(charge, kCFNumberSInt32Type, &percent); CFNumberGetValue(charge, kCFNumberSInt32Type, &percent);
} }
CFBooleanRef charging = (CFBooleanRef)CFDictionaryGetValue(one_ps, CFSTR(kIOPSIsChargingKey)); CFBooleanRef charging = static_cast<CFBooleanRef>(CFDictionaryGetValue(one_ps, CFSTR(kIOPSIsChargingKey)));
if (charging) { if (charging) {
bool isCharging = CFBooleanGetValue(charging); bool isCharging = CFBooleanGetValue(charging);
if (isCharging) { if (isCharging) {
@ -460,7 +460,7 @@ namespace Cpu {
if (error != KERN_SUCCESS) { if (error != KERN_SUCCESS) {
Logger::error("Failed getting CPU load info"); Logger::error("Failed getting CPU load info");
} }
cpu_load_info = (processor_cpu_load_info_data_t *)info.info_array; cpu_load_info = reinterpret_cast<processor_cpu_load_info_data_t*>(info.info_array);
long long global_totals = 0; long long global_totals = 0;
long long global_idles = 0; long long global_idles = 0;
vector<long long> times_summed = {0, 0, 0, 0}; vector<long long> times_summed = {0, 0, 0, 0};
@ -490,14 +490,14 @@ namespace Cpu {
core_old_totals.at(i) = totals; core_old_totals.at(i) = totals;
core_old_idles.at(i) = idles; core_old_idles.at(i) = idles;
cpu.core_percent.at(i).push_back(clamp((long long)round((double)(calc_totals - calc_idles) * 100 / calc_totals), 0ll, 100ll)); cpu.core_percent.at(i).push_back(clamp(llround((calc_totals - calc_idles) * 100.0 / calc_totals), 0ll, 100ll));
//? Reduce size if there are more values than needed for graph //? Reduce size if there are more values than needed for graph
if (cpu.core_percent.at(i).size() > 40) cpu.core_percent.at(i).pop_front(); if (cpu.core_percent.at(i).size() > 40) cpu.core_percent.at(i).pop_front();
} catch (const std::exception &e) { } catch (const std::exception &e) {
Logger::error("Cpu::collect() : " + (string)e.what()); Logger::error("Cpu::collect() : " + string{e.what()});
throw std::runtime_error("collect() : " + (string)e.what()); throw std::runtime_error("collect() : " + string{e.what()});
} }
} }
@ -506,7 +506,7 @@ namespace Cpu {
//? Populate cpu.cpu_percent with all fields from syscall //? Populate cpu.cpu_percent with all fields from syscall
for (int ii = 0; const auto &val : times_summed) { for (int ii = 0; const auto &val : times_summed) {
cpu.cpu_percent.at(time_names.at(ii)).push_back(clamp((long long)round((double)(val - cpu_old.at(time_names.at(ii))) * 100 / calc_totals), 0ll, 100ll)); cpu.cpu_percent.at(time_names.at(ii)).push_back(clamp(llround((val - cpu_old.at(time_names.at(ii))) * 100.0 / calc_totals), 0ll, 100ll));
cpu_old.at(time_names.at(ii)) = val; cpu_old.at(time_names.at(ii)) = val;
//? Reduce size if there are more values than needed for graph //? Reduce size if there are more values than needed for graph
@ -519,7 +519,7 @@ namespace Cpu {
cpu_old.at("idles") = global_idles; cpu_old.at("idles") = global_idles;
//? Total usage of cpu //? Total usage of cpu
cpu.cpu_percent.at("total").push_back(clamp((long long)round((double)(calc_totals - calc_idles) * 100 / calc_totals), 0ll, 100ll)); cpu.cpu_percent.at("total").push_back(clamp(llround((calc_totals - calc_idles) * 100.0 / calc_totals), 0ll, 100ll));
//? Reduce size if there are more values than needed for graph //? Reduce size if there are more values than needed for graph
while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front(); while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front();
@ -555,7 +555,7 @@ namespace Mem {
} }
int64_t getCFNumber(CFDictionaryRef dict, const void *key) { int64_t getCFNumber(CFDictionaryRef dict, const void *key) {
CFNumberRef ref = (CFNumberRef)CFDictionaryGetValue(dict, key); CFNumberRef ref = static_cast<CFNumberRef>(CFDictionaryGetValue(dict, key));
if (ref) { if (ref) {
int64_t value; int64_t value;
CFNumberGetValue(ref, kCFNumberSInt64Type, &value); CFNumberGetValue(ref, kCFNumberSInt64Type, &value);
@ -565,7 +565,7 @@ namespace Mem {
} }
string getCFString(io_registry_entry_t volumeRef, CFStringRef key) { string getCFString(io_registry_entry_t volumeRef, CFStringRef key) {
CFStringRef bsdNameRef = (CFStringRef)IORegistryEntryCreateCFProperty(volumeRef, key, kCFAllocatorDefault, 0); CFStringRef bsdNameRef = static_cast<CFStringRef>(IORegistryEntryCreateCFProperty(volumeRef, key, kCFAllocatorDefault, 0));
if (bsdNameRef) { if (bsdNameRef) {
char buf[200]; char buf[200];
CFStringGetCString(bsdNameRef, buf, 200, kCFStringEncodingASCII); CFStringGetCString(bsdNameRef, buf, 200, kCFStringEncodingASCII);
@ -576,7 +576,7 @@ namespace Mem {
} }
bool isWhole(io_registry_entry_t volumeRef) { bool isWhole(io_registry_entry_t volumeRef) {
CFBooleanRef isWhole = (CFBooleanRef)IORegistryEntryCreateCFProperty(volumeRef, CFSTR("Whole"), kCFAllocatorDefault, 0); CFBooleanRef isWhole = static_cast<CFBooleanRef>(IORegistryEntryCreateCFProperty(volumeRef, CFSTR("Whole"), kCFAllocatorDefault, 0));
Boolean val = CFBooleanGetValue(isWhole); Boolean val = CFBooleanGetValue(isWhole);
CFRelease(isWhole); CFRelease(isWhole);
return bool(val); return bool(val);
@ -624,16 +624,16 @@ namespace Mem {
if (disks.contains(mountpoint)) { if (disks.contains(mountpoint)) {
auto& disk = disks.at(mountpoint); auto& disk = disks.at(mountpoint);
CFDictionaryRef properties; CFDictionaryRef properties;
IORegistryEntryCreateCFProperties(volumeRef, (CFMutableDictionaryRef *)&properties, kCFAllocatorDefault, 0); IORegistryEntryCreateCFProperties(volumeRef, const_cast<CFMutableDictionaryRef*>(&properties), kCFAllocatorDefault, 0);
if (properties) { if (properties) {
CFDictionaryRef statistics = (CFDictionaryRef)CFDictionaryGetValue(properties, CFSTR("Statistics")); CFDictionaryRef statistics = static_cast<CFDictionaryRef>(CFDictionaryGetValue(properties, CFSTR("Statistics")));
if (statistics) { if (statistics) {
disk_ios++; disk_ios++;
int64_t readBytes = getCFNumber(statistics, CFSTR("Bytes read from block device")); int64_t readBytes = getCFNumber(statistics, CFSTR("Bytes read from block device"));
if (disk.io_read.empty()) if (disk.io_read.empty())
disk.io_read.push_back(0); disk.io_read.push_back(0);
else else
disk.io_read.push_back(max((int64_t)0, (readBytes - disk.old_io.at(0)))); disk.io_read.push_back(max(static_cast<int64_t>(0), (readBytes - disk.old_io.at(0))));
disk.old_io.at(0) = readBytes; disk.old_io.at(0) = readBytes;
while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front(); while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front();
@ -641,7 +641,7 @@ namespace Mem {
if (disk.io_write.empty()) if (disk.io_write.empty())
disk.io_write.push_back(0); disk.io_write.push_back(0);
else else
disk.io_write.push_back(max((int64_t)0, (writeBytes - disk.old_io.at(1)))); disk.io_write.push_back(max(static_cast<int64_t>(0), (writeBytes - disk.old_io.at(1))));
disk.old_io.at(1) = writeBytes; disk.old_io.at(1) = writeBytes;
while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front(); while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front();
@ -649,7 +649,7 @@ namespace Mem {
if (disk.io_activity.empty()) if (disk.io_activity.empty())
disk.io_activity.push_back(0); disk.io_activity.push_back(0);
else else
disk.io_activity.push_back(clamp((long)round((double)(disk.io_write.back() + disk.io_read.back()) / (1 << 20)), 0l, 100l)); disk.io_activity.push_back(clamp(lround(static_cast<double>(disk.io_write.back() + disk.io_read.back()) / (1 << 20)), 0l, 100l));
while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front(); while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front();
} }
} }
@ -674,7 +674,7 @@ namespace Mem {
vm_statistics64 p; vm_statistics64 p;
mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT; mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT;
if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&p, &info_size) == 0) { if (host_statistics64(mach_host_self(), HOST_VM_INFO64, reinterpret_cast<host_info64_t>(&p), &info_size) == 0) {
mem.stats.at("free") = p.free_count * Shared::pageSize; mem.stats.at("free") = p.free_count * Shared::pageSize;
mem.stats.at("cached") = p.external_page_count * Shared::pageSize; mem.stats.at("cached") = p.external_page_count * Shared::pageSize;
mem.stats.at("used") = (p.active_count + p.inactive_count + p.wire_count) * Shared::pageSize; mem.stats.at("used") = (p.active_count + p.inactive_count + p.wire_count) * Shared::pageSize;
@ -693,7 +693,7 @@ namespace Mem {
if (show_swap and mem.stats.at("swap_total") > 0) { if (show_swap and mem.stats.at("swap_total") > 0) {
for (const auto &name : swap_names) { for (const auto &name : swap_names) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / mem.stats.at("swap_total"))); mem.percent.at(name).push_back(round(mem.stats.at(name) * 100.0 / mem.stats.at("swap_total")));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) while (cmp_greater(mem.percent.at(name).size(), width * 2))
mem.percent.at(name).pop_front(); mem.percent.at(name).pop_front();
} }
@ -702,7 +702,7 @@ namespace Mem {
has_swap = false; has_swap = false;
//? Calculate percentages //? Calculate percentages
for (const auto &name : mem_names) { for (const auto &name : mem_names) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / Shared::totalMem)); mem.percent.at(name).push_back(round(mem.stats.at(name) * 100.0 / Shared::totalMem));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) while (cmp_greater(mem.percent.at(name).size(), width * 2))
mem.percent.at(name).pop_front(); mem.percent.at(name).pop_front();
} }
@ -786,7 +786,7 @@ namespace Mem {
disk.total = vfs.f_blocks * vfs.f_frsize; disk.total = vfs.f_blocks * vfs.f_frsize;
disk.free = vfs.f_bfree * vfs.f_frsize; disk.free = vfs.f_bfree * vfs.f_frsize;
disk.used = disk.total - disk.free; disk.used = disk.total - disk.free;
disk.used_percent = round((double)disk.used * 100 / disk.total); disk.used_percent = round(disk.used * 100.0 / disk.total);
disk.free_percent = 100 - disk.used_percent; disk.free_percent = 100 - disk.used_percent;
} }
@ -917,11 +917,11 @@ namespace Net {
char *lim = buf.get() + len; char *lim = buf.get() + len;
char *next = nullptr; char *next = nullptr;
for (next = buf.get(); next < lim;) { for (next = buf.get(); next < lim;) {
struct if_msghdr *ifm = (struct if_msghdr *)next; struct if_msghdr *ifm = reinterpret_cast<struct if_msghdr*>(next);
next += ifm->ifm_msglen; next += ifm->ifm_msglen;
if (ifm->ifm_type == RTM_IFINFO2) { if (ifm->ifm_type == RTM_IFINFO2) {
struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm; struct if_msghdr2 *if2m = reinterpret_cast<struct if_msghdr2*>(ifm);
struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1); struct sockaddr_dl *sdl = reinterpret_cast<struct sockaddr_dl*>(if2m + 1);
char iface[32]; char iface[32];
strncpy(iface, sdl->sdl_data, sdl->sdl_nlen); strncpy(iface, sdl->sdl_data, sdl->sdl_nlen);
iface[sdl->sdl_nlen] = 0; iface[sdl->sdl_nlen] = 0;
@ -943,11 +943,11 @@ namespace Net {
saved_stat.rollover += saved_stat.last; saved_stat.rollover += saved_stat.last;
saved_stat.last = 0; saved_stat.last = 0;
} }
if (cmp_greater((unsigned long long)saved_stat.rollover + (unsigned long long)val, numeric_limits<uint64_t>::max())) { if (cmp_greater(static_cast<unsigned long long>(saved_stat.rollover) + static_cast<unsigned long long>(val), numeric_limits<uint64_t>::max())) {
saved_stat.rollover = 0; saved_stat.rollover = 0;
saved_stat.last = 0; saved_stat.last = 0;
} }
saved_stat.speed = round((double)(val - saved_stat.last) / ((double)(new_timestamp - timestamp) / 1000)); saved_stat.speed = round(static_cast<double>(val - saved_stat.last) / (new_timestamp - timestamp) / 1000);
if (saved_stat.speed > saved_stat.top) saved_stat.top = saved_stat.speed; if (saved_stat.speed > saved_stat.top) saved_stat.top = saved_stat.speed;
if (saved_stat.offset > val + saved_stat.rollover) saved_stat.offset = 0; if (saved_stat.offset > val + saved_stat.rollover) saved_stat.offset = 0;
saved_stat.total = (val + saved_stat.rollover) - saved_stat.offset; saved_stat.total = (val + saved_stat.rollover) - saved_stat.offset;
@ -1024,7 +1024,7 @@ namespace Net {
const long long avg_speed = (net[selected_iface].bandwidth[dir].size() > 5 const long long avg_speed = (net[selected_iface].bandwidth[dir].size() > 5
? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0ll) / 5 ? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0ll) / 5
: net[selected_iface].stat[dir].speed); : net[selected_iface].stat[dir].speed);
graph_max[dir] = max(uint64_t(avg_speed * (sel == 0 ? 1.3 : 3.0)), (uint64_t)10 << 10); graph_max[dir] = max(uint64_t(avg_speed * (sel == 0 ? 1.3 : 3.0)), static_cast<uint64_t>(10) << 10);
max_count[dir][0] = max_count[dir][1] = 0; max_count[dir][0] = max_count[dir][1] = 0;
redraw = true; redraw = true;
if (net_sync) sync = true; if (net_sync) sync = true;
@ -1087,7 +1087,7 @@ namespace Proc {
//? Update cpu percent deque for process cpu graph //? Update cpu percent deque for process cpu graph
if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount; if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount;
detailed.cpu_percent.push_back(clamp((long long)round(detailed.entry.cpu_p), 0ll, 100ll)); detailed.cpu_percent.push_back(clamp(llround(detailed.entry.cpu_p), 0ll, 100ll));
while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front(); while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front();
//? Process runtime : current time - start time (both in unix time - seconds since epoch) //? Process runtime : current time - start time (both in unix time - seconds since epoch)
@ -1109,14 +1109,14 @@ namespace Proc {
detailed.memory = floating_humanizer(detailed.entry.mem); detailed.memory = floating_humanizer(detailed.entry.mem);
if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) { if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) {
detailed.first_mem = min((uint64_t)detailed.mem_bytes.back() * 2, Mem::get_totalMem()); detailed.first_mem = min(static_cast<uint64_t>(detailed.mem_bytes.back()) * 2, Mem::get_totalMem());
redraw = true; redraw = true;
} }
while (cmp_greater(detailed.mem_bytes.size(), width)) detailed.mem_bytes.pop_front(); while (cmp_greater(detailed.mem_bytes.size(), width)) detailed.mem_bytes.pop_front();
rusage_info_current rusage; rusage_info_current rusage;
if (proc_pid_rusage(pid, RUSAGE_INFO_CURRENT, (void **)&rusage) == 0) { if (proc_pid_rusage(pid, RUSAGE_INFO_CURRENT, reinterpret_cast<void**>(&rusage)) == 0) {
// this fails for processes we don't own - same as in Linux // this fails for processes we don't own - same as in Linux
detailed.io_read = floating_humanizer(rusage.ri_diskio_bytesread); detailed.io_read = floating_humanizer(rusage.ri_diskio_bytesread);
detailed.io_write = floating_humanizer(rusage.ri_diskio_byteswritten); detailed.io_write = floating_humanizer(rusage.ri_diskio_byteswritten);
@ -1160,7 +1160,7 @@ namespace Proc {
if (error != KERN_SUCCESS) { if (error != KERN_SUCCESS) {
Logger::error("Failed getting CPU load info"); Logger::error("Failed getting CPU load info");
} }
cpu_load_info = (processor_cpu_load_info_data_t *)info.info_array; cpu_load_info = reinterpret_cast<processor_cpu_load_info_data_t*>(info.info_array);
cputimes = 0; cputimes = 0;
for (natural_t i = 0; i < cpu_count; i++) { for (natural_t i = 0; i < cpu_count; i++) {
cputimes += (cpu_load_info[i].cpu_ticks[CPU_STATE_USER] cputimes += (cpu_load_info[i].cpu_ticks[CPU_STATE_USER]
@ -1186,7 +1186,7 @@ namespace Proc {
size_t count = size / sizeof(struct kinfo_proc); size_t count = size / sizeof(struct kinfo_proc);
for (size_t i = 0; i < count; i++) { //* iterate over all processes in kinfo_proc for (size_t i = 0; i < count; i++) { //* iterate over all processes in kinfo_proc
struct kinfo_proc& kproc = processes.get()[i]; struct kinfo_proc& kproc = processes.get()[i];
const size_t pid = (size_t)kproc.kp_proc.p_pid; const size_t pid = static_cast<size_t>(kproc.kp_proc.p_pid);
if (pid < 1) continue; if (pid < 1) continue;
found.push_back(pid); found.push_back(pid);
@ -1211,7 +1211,7 @@ namespace Proc {
//? Get process arguments if possible, fallback to process path in case of failure //? Get process arguments if possible, fallback to process path in case of failure
if (Shared::arg_max > 0) { if (Shared::arg_max > 0) {
std::unique_ptr<char[]> proc_chars(new char[Shared::arg_max]); std::unique_ptr<char[]> proc_chars(new char[Shared::arg_max]);
int mib[] = {CTL_KERN, KERN_PROCARGS2, (int)pid}; int mib[] = {CTL_KERN, KERN_PROCARGS2, static_cast<int>(pid)};
size_t argmax = Shared::arg_max; size_t argmax = Shared::arg_max;
if (sysctl(mib, 3, proc_chars.get(), &argmax, nullptr, 0) == 0) { if (sysctl(mib, 3, proc_chars.get(), &argmax, nullptr, 0) == 0) {
int argc = 0; int argc = 0;
@ -1221,7 +1221,7 @@ namespace Proc {
if (size_t start_pos = proc_args.find_first_not_of('\0', null_pos); start_pos != string::npos) { if (size_t start_pos = proc_args.find_first_not_of('\0', null_pos); start_pos != string::npos) {
while (argc-- > 0 and null_pos != string::npos and cmp_less(new_proc.cmd.size(), 1000)) { while (argc-- > 0 and null_pos != string::npos and cmp_less(new_proc.cmd.size(), 1000)) {
null_pos = proc_args.find('\0', start_pos); null_pos = proc_args.find('\0', start_pos);
new_proc.cmd += (string)proc_args.substr(start_pos, null_pos - start_pos) + ' '; new_proc.cmd += static_cast<string>(proc_args.substr(start_pos, null_pos - start_pos)) + ' ';
start_pos = null_pos + 1; start_pos = null_pos + 1;
} }
} }
@ -1256,7 +1256,7 @@ namespace Proc {
new_proc.cpu_p = clamp(round(((cpu_t - new_proc.cpu_t) * Shared::machTck) / ((cputimes - old_cputimes) * Shared::clkTck)) * cmult / 1000.0, 0.0, 100.0 * Shared::coreCount); new_proc.cpu_p = clamp(round(((cpu_t - new_proc.cpu_t) * Shared::machTck) / ((cputimes - old_cputimes) * Shared::clkTck)) * cmult / 1000.0, 0.0, 100.0 * Shared::coreCount);
//? Process cumulative cpu usage since process start //? Process cumulative cpu usage since process start
new_proc.cpu_c = (double)(cpu_t * Shared::machTck) / (timeNow - new_proc.cpu_s); new_proc.cpu_c = static_cast<double>(cpu_t * Shared::machTck) / (timeNow - new_proc.cpu_s);
//? Update cached value with latest cpu times //? Update cached value with latest cpu times
new_proc.cpu_t = cpu_t; new_proc.cpu_t = cpu_t;
@ -1366,7 +1366,7 @@ namespace Proc {
} }
} }
numpids = (int)current_procs.size() - filter_found; numpids = static_cast<int>(current_procs.size()) - filter_found;
return current_procs; return current_procs;
} }
} // namespace Proc } // namespace Proc

View file

@ -83,7 +83,7 @@ long long Cpu::ThermalSensors::getSensors() {
if (matchingsrvs) { if (matchingsrvs) {
long count = CFArrayGetCount(matchingsrvs); long count = CFArrayGetCount(matchingsrvs);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
IOHIDServiceClientRef sc = (IOHIDServiceClientRef)CFArrayGetValueAtIndex(matchingsrvs, i); IOHIDServiceClientRef sc = reinterpret_cast<IOHIDServiceClientRef>(const_cast<void*>(CFArrayGetValueAtIndex(matchingsrvs, i)));
if (sc) { if (sc) {
CFStringRef name = IOHIDServiceClientCopyProperty(sc, CFSTR("Product")); // here we use ...CopyProperty CFStringRef name = IOHIDServiceClientCopyProperty(sc, CFSTR("Product")); // here we use ...CopyProperty
if (name) { if (name) {

View file

@ -29,7 +29,7 @@ static UInt32 _strtoul(char *str, int size, int base) {
if (base == 16) { if (base == 16) {
total += str[i] << (size - 1 - i) * 8; total += str[i] << (size - 1 - i) * 8;
} else { } else {
total += (unsigned char)(str[i] << (size - 1 - i) * 8); total += static_cast<unsigned char>(str[i] << (size - 1 - i) * 8);
} }
} }
return total; return total;
@ -38,10 +38,10 @@ static UInt32 _strtoul(char *str, int size, int base) {
static void _ultostr(char *str, UInt32 val) { static void _ultostr(char *str, UInt32 val) {
str[0] = '\0'; str[0] = '\0';
snprintf(str, 5, "%c%c%c%c", snprintf(str, 5, "%c%c%c%c",
(unsigned int)val >> 24, static_cast<unsigned int>(val) >> 24,
(unsigned int)val >> 16, static_cast<unsigned int>(val) >> 16,
(unsigned int)val >> 8, static_cast<unsigned int>(val) >> 8,
(unsigned int)val); static_cast<unsigned int>(val));
} }
namespace Cpu { namespace Cpu {
@ -77,7 +77,7 @@ namespace Cpu {
if (val.dataSize > 0) { if (val.dataSize > 0) {
if (strcmp(val.dataType, DATATYPE_SP78) == 0) { if (strcmp(val.dataType, DATATYPE_SP78) == 0) {
// convert sp78 value to temperature // convert sp78 value to temperature
int intValue = val.bytes[0] * 256 + (unsigned char)val.bytes[1]; int intValue = val.bytes[0] * 256 + static_cast<unsigned char>(val.bytes[1]);
return static_cast<long long>(intValue / 256.0); return static_cast<long long>(intValue / 256.0);
} }
} }
@ -93,7 +93,7 @@ namespace Cpu {
long long SMCConnection::getTemp(int core) { long long SMCConnection::getTemp(int core) {
char key[] = SMC_KEY_CPU_TEMP; char key[] = SMC_KEY_CPU_TEMP;
if (core >= 0) { if (core >= 0) {
if ((size_t)core > MaxIndexCount) { if (static_cast<size_t>(core) > MaxIndexCount) {
return -1; return -1;
} }
snprintf(key, 5, "TC%1cc", KeyIndexes[core]); snprintf(key, 5, "TC%1cc", KeyIndexes[core]);