From 17d45419803e04913b835acedad878d4625a1bce Mon Sep 17 00:00:00 2001 From: Alessandro Toia Date: Wed, 13 Sep 2023 18:12:08 -0700 Subject: [PATCH] device: switch gamepad_battery to device_battery and allow to enable mouse or gamepad or both --- src/device.cpp | 201 +++++++++++++++++++++++++++++++++++++++++ src/device.h | 24 +++++ src/gamepad.cpp | 186 -------------------------------------- src/gamepad.h | 23 ----- src/hud_elements.cpp | 28 +++--- src/hud_elements.h | 2 +- src/meson.build | 2 +- src/overlay.cpp | 10 +- src/overlay_params.cpp | 4 +- src/overlay_params.h | 5 +- 10 files changed, 251 insertions(+), 234 deletions(-) create mode 100644 src/device.cpp create mode 100644 src/device.h delete mode 100644 src/gamepad.cpp delete mode 100644 src/gamepad.h diff --git a/src/device.cpp b/src/device.cpp new file mode 100644 index 00000000..2558a983 --- /dev/null +++ b/src/device.cpp @@ -0,0 +1,201 @@ +#include "device.h" +#include +#include +#include +#include + +namespace fs = ghc::filesystem; +using namespace std; +std::vector device_data; +std::vector list; +bool device_found = false; +bool check_gamepad = false; +bool check_mouse = false; +int device_count = 0; +int xbox_count = 0; +int ds4_count = 0; +int ds5_count = 0; +int switch_count = 0; +int bitdo_count = 0; +int logi_count = 0; //Logitech devices, mice & keyboards etc. + +std::string xbox_paths [2]{"gip","xpadneo"}; + +static bool operator<(const device_batt& a, const device_batt& b) +{ + return a.name < b.name; +} + + +void device_update(const struct overlay_params& params){ + fs::path path("/sys/class/power_supply"); + list.clear(); + xbox_count = 0; + ds4_count = 0; + ds5_count = 0; + switch_count = 0; + bitdo_count = 0; + for (auto &p : fs::directory_iterator(path)) { + string fileName = p.path().filename(); +//Gamepads + if (std::find(params.device_battery.begin(), params.device_battery.end(), "gamepad") != params.device_battery.end()){ + check_gamepad = true; + //CHECK XONE AND XPADNEO DEVICES + for (string n : xbox_paths ) { + if (fileName.find(n) != std::string::npos) { + list.push_back(p.path()); + device_found = true; + xbox_count += 1; + } + } + //CHECK FOR DUAL SHOCK 4 DEVICES + if (fileName.find("sony_controller") != std::string::npos) { + list.push_back(p.path()); + device_found = true; + ds4_count +=1 ; + } + if (fileName.find("ps-controller") != std::string::npos) { + list.push_back(p.path()); + device_found = true; + ds5_count +=1 ; + } + //CHECK FOR NINTENDO SWITCH DEVICES + if (fileName.find("nintendo_switch_controller") != std::string::npos) { + list.push_back(p.path()); + device_found = true; + switch_count += 1; + } + //CHECK * BITDO DEVICES + if (fileName.find("hid-e4") != std::string::npos) { + list.push_back(p.path()); + device_found = true; + bitdo_count += 1; + } + } +// Mice and Keyboards + //CHECK LOGITECH DEVICES + if (std::find(params.device_battery.begin(), params.device_battery.end(), "mouse") != params.device_battery.end()) { + check_mouse = true; + if (fileName.find("hidpp_battery") != std::string::npos) { + list.push_back(p.path()); + device_found = true; + } + } + } +} + + +void device_info () { + device_count = 0; + device_data.clear(); + //gamepad counters + int xbox_counter = 0; + int ds4_counter = 0; + int ds5_counter = 0; + int switch_counter = 0; + int bitdo_counter = 0; + + for (auto &path : list ) { + //Set devices paths + std::string capacity = path + "/capacity"; + std::string capacity_level = path + "/capacity_level"; + std::string status = path + "/status"; + std::string model = path + "/model_name"; + std::ifstream input_capacity(capacity); + std::ifstream input_capacity_level(capacity_level); + std::ifstream input_status(status); + std::ifstream device_name(model); + std::string line; + + device_data.push_back(device_batt()); + +// GAMEPADS + //Xone and xpadneo devices + if (check_gamepad == true) { + if (path.find("gip") != std::string::npos || path.find("xpadneo") != std::string::npos) { + if (xbox_count == 1 ) + device_data[device_count].name = "XBOX PAD"; + else + device_data[device_count].name = "XBOX PAD-" + to_string(xbox_counter + 1); + xbox_counter++; + } + //DualShock 4 devices + if (path.find("sony_controller") != std::string::npos) { + if (ds4_count == 1) + device_data[device_count].name = "DS4 PAD"; + else + device_data[device_count].name = "DS4 PAD-" + to_string(ds4_counter + 1); + ds4_counter++; + } + //DualSense 5 devices + //Dual Shock 4 added to hid-playstation in Linux 6.2 + if (path.find("ps-controller") != std::string::npos) { + if (ds5_count == 1) + device_data[device_count].name = "DS4/5 PAD"; + else + device_data[device_count].name = "DS4/5 PAD-" + to_string(ds5_counter + 1); + ds5_counter++; + } + //Nintendo Switch devices + if (path.find("nintendo_switch_controller") != std::string::npos) { + if (switch_count == 1) + device_data[device_count].name = "SWITCH PAD"; + else + device_data[device_count].name = "SWITCH PAD-" + to_string(switch_counter + 1); + switch_counter++; + } + //8bitdo devices + if (path.find("hid-e4") != std::string::npos) { + if (bitdo_count == 1) + device_data[device_count].name = "8BITDO PAD"; + else + device_data[device_count].name = "8BITDO PAD-" + to_string(bitdo_counter + 1); + bitdo_counter++; + } + } +// MICE AND KEYBOARDS + //Logitech Devices + if (check_mouse == true) { + if (path.find("hidpp_battery") != std::string::npos) { + if (std::getline(device_name, line)) { + device_data[device_count].name = line; + } + } + } + + //Get device charging status + if (std::getline(input_status, line)) { + if (line == "Charging" || line == "Full") + device_data[device_count].is_charging = true; + } + //Get device Battery + if (fs::exists(capacity)) { + if (std::getline(input_capacity, line)) { + device_data[device_count].battery_percent = line; + device_data[device_count].report_percent = true; + switch(std::stoi(line)) { + case 0 ... 25: + device_data[device_count].battery = "Low"; + break; + case 26 ... 49: + device_data[device_count].battery = "Normal"; + break; + case 50 ... 74: + device_data[device_count].battery = "High"; + break; + case 75 ... 100: + device_data[device_count].battery = "Full"; + break; + } + } + } + else { + if (std::getline(input_capacity_level, line)) { + device_data[device_count].battery = line; + } + } + std::sort(device_data.begin(), device_data.end()); + device_count += 1; + + } +} diff --git a/src/device.h b/src/device.h new file mode 100644 index 00000000..f7d87036 --- /dev/null +++ b/src/device.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef MANGOHUD_DEVICE_H +#define MANGOHUD_DEVICE_H +#include +#include +#include "overlay_params.h" +struct overlay_params; +struct device_batt { + std::string battery; + std::string name; + bool report_percent; + std::string battery_percent; + bool is_charging; +}; + +extern std::vector device_data; + +extern bool device_found; +extern int device_count; +void device_update(const overlay_params& params); +void device_info(); + + +#endif // MANGOHUD_DEVICE_H diff --git a/src/gamepad.cpp b/src/gamepad.cpp deleted file mode 100644 index 69df03a6..00000000 --- a/src/gamepad.cpp +++ /dev/null @@ -1,186 +0,0 @@ -#include "gamepad.h" -#include -#include -#include -#include - -namespace fs = ghc::filesystem; -using namespace std; -std::vector gamepad_data; -std::vector list; -bool gamepad_found = false; -int gamepad_count = 0; -int xbox_count = 0; -int ds4_count = 0; -int ds5_count = 0; -int switch_count = 0; -int bitdo_count = 0; -int logi_count = 0; //Logitech devices, mice & keyboards etc. - -std::string xbox_paths [2]{"gip","xpadneo"}; - -static bool operator<(const gamepad& a, const gamepad& b) -{ - return a.name < b.name; -} - - -void gamepad_update(){ - fs::path path("/sys/class/power_supply"); - list.clear(); - xbox_count = 0; - ds4_count = 0; - ds5_count = 0; - switch_count = 0; - bitdo_count = 0; - for (auto &p : fs::directory_iterator(path)) { - string fileName = p.path().filename(); -//Gamepads - //CHECK XONE AND XPADNEO DEVICES - for (string n : xbox_paths ) { - if (fileName.find(n) != std::string::npos) { - list.push_back(p.path()); - gamepad_found = true; - xbox_count += 1; - } - } - //CHECK FOR DUAL SHOCK 4 DEVICES - if (fileName.find("sony_controller") != std::string::npos) { - list.push_back(p.path()); - gamepad_found = true; - ds4_count +=1 ; - } - if (fileName.find("ps-controller") != std::string::npos) { - list.push_back(p.path()); - gamepad_found = true; - ds5_count +=1 ; - } - //CHECK FOR NINTENDO SWITCH DEVICES - if (fileName.find("nintendo_switch_controller") != std::string::npos) { - list.push_back(p.path()); - gamepad_found = true; - switch_count += 1; - } - //CHECK * BITDO DEVICES - if (fileName.find("hid-e4") != std::string::npos) { - list.push_back(p.path()); - gamepad_found = true; - bitdo_count += 1; - } -// Mice and Keyboards - //CHECK LOGITECH DEVICES - if (fileName.find("hidpp_battery") != std::string::npos) { - list.push_back(p.path()); - gamepad_found = true; - } - } -} - - -void gamepad_info () { - gamepad_count = 0; - gamepad_data.clear(); - //gamepad counters - int xbox_counter = 0; - int ds4_counter = 0; - int ds5_counter = 0; - int switch_counter = 0; - int bitdo_counter = 0; - - for (auto &path : list ) { - //Set devices paths - std::string capacity = path + "/capacity"; - std::string capacity_level = path + "/capacity_level"; - std::string status = path + "/status"; - std::string model = path + "/model_name"; - std::ifstream input_capacity(capacity); - std::ifstream input_capacity_level(capacity_level); - std::ifstream input_status(status); - std::ifstream device_name(model); - std::string line; - - gamepad_data.push_back(gamepad()); - - //Xone and xpadneo devices - if (path.find("gip") != std::string::npos || path.find("xpadneo") != std::string::npos) { - if (xbox_count == 1 ) - gamepad_data[gamepad_count].name = "XBOX PAD"; - else - gamepad_data[gamepad_count].name = "XBOX PAD-" + to_string(xbox_counter + 1); - xbox_counter++; - } - //DualShock 4 devices - if (path.find("sony_controller") != std::string::npos) { - if (ds4_count == 1) - gamepad_data[gamepad_count].name = "DS4 PAD"; - else - gamepad_data[gamepad_count].name = "DS4 PAD-" + to_string(ds4_counter + 1); - ds4_counter++; - } - //DualSense 5 devices - //Dual Shock 4 added to hid-playstation in Linux 6.2 - if (path.find("ps-controller") != std::string::npos) { - if (ds5_count == 1) - gamepad_data[gamepad_count].name = "DS4/5 PAD"; - else - gamepad_data[gamepad_count].name = "DS4/5 PAD-" + to_string(ds5_counter + 1); - ds5_counter++; - } - //Nintendo Switch devices - if (path.find("nintendo_switch_controller") != std::string::npos) { - if (switch_count == 1) - gamepad_data[gamepad_count].name = "SWITCH PAD"; - else - gamepad_data[gamepad_count].name = "SWITCH PAD-" + to_string(switch_counter + 1); - switch_counter++; - } - //8bitdo devices - if (path.find("hid-e4") != std::string::npos) { - if (bitdo_count == 1) - gamepad_data[gamepad_count].name = "8BITDO PAD"; - else - gamepad_data[gamepad_count].name = "8BITDO PAD-" + to_string(bitdo_counter + 1); - bitdo_counter++; - } - //Logitech Devices - if (path.find("hidpp_battery") != std::string::npos) { - if (std::getline(device_name, line)) { - gamepad_data[gamepad_count].name = line; - } - } - //Get device charging status - if (std::getline(input_status, line)) { - if (line == "Charging" || line == "Full") - gamepad_data[gamepad_count].is_charging = true; - } - //Get device Battery - if (fs::exists(capacity)) { - if (std::getline(input_capacity, line)) { - gamepad_data[gamepad_count].battery_percent = line; - gamepad_data[gamepad_count].report_percent = true; - switch(std::stoi(line)) { - case 0 ... 25: - gamepad_data[gamepad_count].battery = "Low"; - break; - case 26 ... 49: - gamepad_data[gamepad_count].battery = "Normal"; - break; - case 50 ... 74: - gamepad_data[gamepad_count].battery = "High"; - break; - case 75 ... 100: - gamepad_data[gamepad_count].battery = "Full"; - break; - } - } - } - else { - if (std::getline(input_capacity_level, line)) { - gamepad_data[gamepad_count].battery = line; - } - } - std::sort(gamepad_data.begin(), gamepad_data.end()); - gamepad_count += 1; - - } -} diff --git a/src/gamepad.h b/src/gamepad.h deleted file mode 100644 index 8839d0c7..00000000 --- a/src/gamepad.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#ifndef MANGOHUD_GAMEPAD_H -#define MANGOHUD_GAMEPAD_H -#include -#include - -struct gamepad { - std::string battery; - std::string name; - bool report_percent; - std::string battery_percent; - bool is_charging; -}; - -extern std::vector gamepad_data; - -extern bool gamepad_found; -extern int gamepad_count; -void gamepad_update(); -void gamepad_info(); - - -#endif // MANGOHUD_GAMEPAD_H diff --git a/src/hud_elements.cpp b/src/hud_elements.cpp index a5a8cc99..3fb3d731 100644 --- a/src/hud_elements.cpp +++ b/src/hud_elements.cpp @@ -8,7 +8,7 @@ #include "hud_elements.h" #include "logging.h" #include "battery.h" -#include "gamepad.h" +#include "device.h" #include "cpu.h" #include "gpu.h" #include "memory.h" @@ -1029,23 +1029,23 @@ void HudElements::gamescope_frame_timing(){ } } -void HudElements::gamepad_battery() +void HudElements::device_battery() { #ifdef __linux__ - if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_gamepad_battery]) { - if (gamepad_found) { - for (int i = 0; i < gamepad_count; i++) { - std::string battery = gamepad_data[i].battery; - std::string name = gamepad_data[i].name; - std::string battery_percent = gamepad_data[i].battery_percent; - bool report_percent = gamepad_data[i].report_percent; - bool charging = gamepad_data[i].is_charging; + if (!HUDElements.params->device_battery.empty()) { + if (device_found) { + for (int i = 0; i < device_count; i++) { + std::string battery = device_data[i].battery; + std::string name = device_data[i].name; + std::string battery_percent = device_data[i].battery_percent; + bool report_percent = device_data[i].report_percent; + bool charging = device_data[i].is_charging; ImguiNextColumnFirstItem(); ImGui::PushFont(HUDElements.sw_stats->font1); HUDElements.TextColored(HUDElements.colors.engine, "%s", name.c_str()); ImguiNextColumnOrNewRow(); - if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_gamepad_battery_icon]) { + if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_device_battery_icon]) { if (charging) right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%s", ICON_FK_USB); else { @@ -1313,7 +1313,7 @@ void HudElements::sort_elements(const std::pair& optio if (param == "fps_only") { ordered_functions.push_back({fps_only, value}); } if (param == "fsr") { ordered_functions.push_back({gamescope_fsr, value}); } if (param == "debug") { ordered_functions.push_back({gamescope_frame_timing, value}); } - if (param == "gamepad_battery") { ordered_functions.push_back({gamepad_battery, value}); } + if (param == "device_battery") { ordered_functions.push_back({device_battery, value}); } if (param == "frame_count") { ordered_functions.push_back({frame_count, value}); } if (param == "fan") { ordered_functions.push_back({fan, value}); } if (param == "throttling_status") { ordered_functions.push_back({throttling_status, value}); } @@ -1390,8 +1390,8 @@ void HudElements::legacy_elements(){ ordered_functions.push_back({show_fps_limit, value}); if (params->enabled[OVERLAY_PARAM_ENABLED_resolution]) ordered_functions.push_back({resolution, value}); - if (params->enabled[OVERLAY_PARAM_ENABLED_gamepad_battery]) - ordered_functions.push_back({gamepad_battery, value}); + if (!params->device_battery.empty() ) + ordered_functions.push_back({device_battery, value}); if (params->enabled[OVERLAY_PARAM_ENABLED_media_player]) ordered_functions.push_back({media_player, value}); if (params->enabled[OVERLAY_PARAM_ENABLED_exec_name]) diff --git a/src/hud_elements.h b/src/hud_elements.h index 677e892e..4cd4bdc7 100644 --- a/src/hud_elements.h +++ b/src/hud_elements.h @@ -70,7 +70,7 @@ class HudElements{ static void fps_only(); static void gamescope_fsr(); static void gamescope_frame_timing(); - static void gamepad_battery(); + static void device_battery(); static void frame_count(); static void fan(); static void throttling_status(); diff --git a/src/meson.build b/src/meson.build index 35f2dd04..537ce63c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -72,7 +72,7 @@ if is_unixy 'pci_ids.cpp', 'battery.cpp', 'control.cpp', - 'gamepad.cpp', + 'device.cpp', 'amdgpu.cpp', 'intel.cpp' ) diff --git a/src/overlay.cpp b/src/overlay.cpp index ea810b81..e6a057cf 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -17,7 +17,7 @@ #include "fcat.h" #include "mesa/util/macros.h" #include "battery.h" -#include "gamepad.h" +#include "device.h" #include "string_utils.h" #include "file_utils.h" #include "pci_ids.h" @@ -136,10 +136,10 @@ void update_hw_info(const struct overlay_params& params, uint32_t vendorID) #ifdef __linux__ if (params.enabled[OVERLAY_PARAM_ENABLED_battery]) Battery_Stats.update(); - if (params.enabled[OVERLAY_PARAM_ENABLED_gamepad_battery]) { - gamepad_update(); - if (gamepad_found) { - gamepad_info(); + if (!params.device_battery.empty()) { + device_update(params); + if (device_found) { + device_info(); } } if (params.enabled[OVERLAY_PARAM_ENABLED_ram] || params.enabled[OVERLAY_PARAM_ENABLED_swap] || logger->is_active()) diff --git a/src/overlay_params.cpp b/src/overlay_params.cpp index ef423019..43c09fdb 100644 --- a/src/overlay_params.cpp +++ b/src/overlay_params.cpp @@ -477,6 +477,7 @@ parse_gl_size_query(const char *str) #define parse_fsr_steam_sharpness(s) parse_float(s) #define parse_text_outline_color(s) parse_color(s) #define parse_text_outline_thickness(s) parse_float(s) +#define parse_device_battery(s) parse_str_tokenize(s) static bool parse_help(const char *str) @@ -639,8 +640,7 @@ static void set_param_defaults(struct overlay_params *params){ params->enabled[OVERLAY_PARAM_ENABLED_legacy_layout] = true; params->enabled[OVERLAY_PARAM_ENABLED_frametime] = true; params->enabled[OVERLAY_PARAM_ENABLED_fps_only] = false; - params->enabled[OVERLAY_PARAM_ENABLED_gamepad_battery] = false; - params->enabled[OVERLAY_PARAM_ENABLED_gamepad_battery_icon] = false; + params->enabled[OVERLAY_PARAM_ENABLED_device_battery_icon] = false; params->enabled[OVERLAY_PARAM_ENABLED_throttling_status] = false; params->enabled[OVERLAY_PARAM_ENABLED_fcat] = false; params->enabled[OVERLAY_PARAM_ENABLED_horizontal_stretch] = true; diff --git a/src/overlay_params.h b/src/overlay_params.h index 582b4450..4cc9b697 100644 --- a/src/overlay_params.h +++ b/src/overlay_params.h @@ -81,8 +81,7 @@ typedef unsigned long KeySym; OVERLAY_PARAM_BOOL(fsr) \ OVERLAY_PARAM_BOOL(mangoapp_steam) \ OVERLAY_PARAM_BOOL(debug) \ - OVERLAY_PARAM_BOOL(gamepad_battery) \ - OVERLAY_PARAM_BOOL(gamepad_battery_icon) \ + OVERLAY_PARAM_BOOL(device_battery_icon) \ OVERLAY_PARAM_BOOL(hide_fsr_sharpness) \ OVERLAY_PARAM_BOOL(fan) \ OVERLAY_PARAM_BOOL(throttling_status) \ @@ -184,6 +183,7 @@ typedef unsigned long KeySym; OVERLAY_PARAM_CUSTOM(text_outline_color) \ OVERLAY_PARAM_CUSTOM(text_outline_thickness) \ OVERLAY_PARAM_CUSTOM(fps_text) \ + OVERLAY_PARAM_CUSTOM(device_battery) \ enum overlay_param_position { LAYER_POSITION_TOP_LEFT, @@ -303,6 +303,7 @@ struct overlay_params { size_t font_params_hash; unsigned text_outline_color; float text_outline_thickness; + std::vector device_battery; }; const extern char *overlay_param_names[];