waybar/include/modules/battery.hpp

59 lines
1.3 KiB
C++
Raw Permalink Normal View History

2018-08-08 23:54:58 +02:00
#pragma once
#ifdef FILESYSTEM_EXPERIMENTAL
2018-11-22 15:47:23 +01:00
#include <experimental/filesystem>
#else
2018-11-22 15:47:23 +01:00
#include <filesystem>
#endif
2018-08-08 23:54:58 +02:00
#include <fmt/format.h>
2022-10-04 06:14:37 +02:00
#if defined(__linux__)
2018-08-13 14:05:13 +02:00
#include <sys/inotify.h>
#endif
2022-04-06 08:37:19 +02:00
2018-08-13 22:33:07 +02:00
#include <algorithm>
2019-04-18 17:52:00 +02:00
#include <fstream>
2019-05-19 01:44:45 +02:00
#include <string>
#include <vector>
2022-04-06 08:37:19 +02:00
2022-11-24 12:28:52 +01:00
#include "ALabel.hpp"
2019-04-18 17:52:00 +02:00
#include "util/sleeper_thread.hpp"
2018-08-08 23:54:58 +02:00
namespace waybar::modules {
#ifdef FILESYSTEM_EXPERIMENTAL
namespace fs = std::experimental::filesystem;
#else
2018-08-16 14:29:41 +02:00
namespace fs = std::filesystem;
#endif
2018-08-08 23:54:58 +02:00
2022-11-24 12:28:52 +01:00
class Battery : public ALabel {
2019-04-18 17:52:00 +02:00
public:
Battery(const std::string&, const Json::Value&);
2023-03-02 14:57:07 +01:00
virtual ~Battery();
auto update() -> void override;
2019-04-18 17:52:00 +02:00
private:
static inline const fs::path data_dir_ = "/sys/class/power_supply/";
2022-04-06 08:37:19 +02:00
void refreshBatteries();
void worker();
const std::string getAdapterStatus(uint8_t capacity) const;
2021-01-24 21:39:14 +01:00
const std::tuple<uint8_t, float, std::string, float> getInfos();
2022-04-06 08:37:19 +02:00
const std::string formatTimeRemaining(float hoursRemaining);
int global_watch;
std::map<fs::path, int> batteries_;
fs::path adapter_;
int battery_watch_fd_;
int global_watch_fd_;
std::mutex battery_list_mutex_;
std::string old_status_;
bool warnFirstTime_{true};
2022-04-06 08:37:19 +02:00
util::SleeperThread thread_;
util::SleeperThread thread_battery_update_;
util::SleeperThread thread_timer_;
2018-08-16 14:29:41 +02:00
};
2018-08-08 23:54:58 +02:00
2019-04-18 17:52:00 +02:00
} // namespace waybar::modules