diff --git a/src/battery.cpp b/src/battery.cpp new file mode 100644 index 00000000..1eae8fe6 --- /dev/null +++ b/src/battery.cpp @@ -0,0 +1,32 @@ +#include "battery.h" + +void BatteryStats::findFiles(){ + FILE *file; + file = fopen("/sys/class/power_supply/BAT1/current_now", "r"); + powerMap["current_now"] = {file, 0}; + file = fopen("/sys/class/power_supply/BAT1/voltage_now", "r"); + powerMap["voltage_now"] = {file, 0}; + // file = fopen("/sys/class/power_supply/BAT1/charge_now", "r"); + // powerMap["charge_now"] = {file, 0}; + // file = fopen("/sys/class/power_supply/BAT1/charge_full", "r"); + // powerMap["charge_full"] = {file, 0}; + + files_fetched = true; +} + +void BatteryStats::update(){ + if (!files_fetched) + findFiles(); + + for(auto &pair : powerMap){ + if(pair.second.file) { + rewind(pair.second.file); + fflush(pair.second.file); + fscanf(pair.second.file, "%f", &pair.second.value); + pair.second.value /= 1000000; + } + } + current_watt = powerMap["current_now"].value * powerMap["voltage_now"].value; +} + +BatteryStats Battery_Stats; \ No newline at end of file diff --git a/src/battery.h b/src/battery.h new file mode 100644 index 00000000..42cc52a5 --- /dev/null +++ b/src/battery.h @@ -0,0 +1,21 @@ +#pragma once +#include "overlay.h" +#include "overlay_params.h" +#include +#include +#include + +class BatteryStats{ + public: + void findFiles(); + void update(); + bool files_fetched = false; + struct powerStruct{ + FILE *file = nullptr; + float value; + }; + std::unordered_map powerMap; + float current_watt = 0; +}; + +extern BatteryStats Battery_Stats; diff --git a/src/meson.build b/src/meson.build index b7e31db9..6fd11df1 100644 --- a/src/meson.build +++ b/src/meson.build @@ -79,6 +79,7 @@ if is_unixy 'elfhacks.cpp', 'real_dlsym.cpp', 'pci_ids.cpp', + 'battery.cpp', ) opengl_files = files( diff --git a/src/overlay.cpp b/src/overlay.cpp index 0c6c2ac7..8c57b079 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -9,6 +9,7 @@ #include "timing.hpp" #include "mesa/util/macros.h" #include "string_utils.h" +#include "battery.h" #ifdef HAVE_DBUS float g_overflow = 50.f /* 3333ms * 0.5 / 16.6667 / 2 (to edge and back) */; #endif @@ -21,6 +22,7 @@ std::vector graph_data; void update_hw_info(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID) { + Battery_Stats.update(); if (params.enabled[OVERLAY_PARAM_ENABLED_cpu_stats] || logger->is_active()) { cpuStats.UpdateCPUData(); #ifdef __gnu_linux__