2016-05-30 23:58:58 -04:00
|
|
|
#pragma once
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
#include "common.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "modules/meta/inotify_module.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
namespace modules {
|
2016-12-21 02:38:44 -05:00
|
|
|
class battery_module : public inotify_module<battery_module> {
|
|
|
|
public:
|
|
|
|
enum class state {
|
|
|
|
NONE = 0,
|
|
|
|
CHARGING,
|
|
|
|
DISCHARGING,
|
2020-12-02 09:55:13 -05:00
|
|
|
LOW,
|
2016-12-21 02:38:44 -05:00
|
|
|
FULL,
|
|
|
|
};
|
2016-11-19 09:42:31 -05:00
|
|
|
|
2016-12-21 02:38:44 -05:00
|
|
|
enum class value {
|
|
|
|
NONE = 0,
|
|
|
|
ADAPTER,
|
|
|
|
CAPACITY,
|
|
|
|
CAPACITY_MAX,
|
|
|
|
VOLTAGE,
|
|
|
|
RATE,
|
|
|
|
};
|
2016-11-19 09:42:31 -05:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
template <typename ReturnType>
|
|
|
|
struct value_reader {
|
|
|
|
using return_type = ReturnType;
|
|
|
|
|
|
|
|
explicit value_reader() = default;
|
|
|
|
explicit value_reader(function<ReturnType()>&& fn) : m_fn(forward<decltype(fn)>(fn)) {}
|
|
|
|
|
|
|
|
ReturnType read() const {
|
|
|
|
return m_fn();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const function<ReturnType()> m_fn;
|
|
|
|
};
|
|
|
|
|
|
|
|
using state_reader = mutex_wrapper<value_reader<bool /* is_charging */>>;
|
|
|
|
using capacity_reader = mutex_wrapper<value_reader<int /* percentage */>>;
|
|
|
|
using rate_reader = mutex_wrapper<value_reader<unsigned long /* seconds */>>;
|
2017-02-16 16:35:43 -05:00
|
|
|
using consumption_reader = mutex_wrapper<value_reader<string /* watts */>>;
|
2016-12-30 22:20:46 -05:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
public:
|
2016-12-21 02:00:09 -05:00
|
|
|
explicit battery_module(const bar_settings&, string);
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2021-01-04 04:38:43 -05:00
|
|
|
void start() override;
|
2016-11-02 15:22:45 -04:00
|
|
|
void teardown();
|
2016-11-03 07:56:33 -04:00
|
|
|
void idle();
|
2016-11-02 15:22:45 -04:00
|
|
|
bool on_event(inotify_event* event);
|
|
|
|
string get_format() const;
|
2016-11-25 07:55:15 -05:00
|
|
|
bool build(builder* builder, const string& tag) const;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2020-05-15 13:59:08 -04:00
|
|
|
static constexpr auto TYPE = "internal/battery";
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
protected:
|
2016-12-30 22:20:46 -05:00
|
|
|
state current_state();
|
2019-05-14 09:01:54 -04:00
|
|
|
int current_percentage();
|
|
|
|
int clamp_percentage(int percentage, state state) const;
|
2016-11-19 09:42:31 -05:00
|
|
|
string current_time();
|
2017-02-16 16:35:43 -05:00
|
|
|
string current_consumption();
|
2016-11-02 15:22:45 -04:00
|
|
|
void subthread();
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
private:
|
2016-12-30 22:20:46 -05:00
|
|
|
static constexpr const char* FORMAT_CHARGING{"format-charging"};
|
|
|
|
static constexpr const char* FORMAT_DISCHARGING{"format-discharging"};
|
|
|
|
static constexpr const char* FORMAT_FULL{"format-full"};
|
2020-12-02 09:55:13 -05:00
|
|
|
static constexpr const char* FORMAT_LOW{"format-low"};
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
static constexpr const char* TAG_ANIMATION_CHARGING{"<animation-charging>"};
|
2018-04-23 14:24:02 -04:00
|
|
|
static constexpr const char* TAG_ANIMATION_DISCHARGING{"<animation-discharging>"};
|
2020-12-02 09:55:13 -05:00
|
|
|
static constexpr const char* TAG_ANIMATION_LOW{"<animation-low>"};
|
2016-12-30 22:20:46 -05:00
|
|
|
static constexpr const char* TAG_BAR_CAPACITY{"<bar-capacity>"};
|
|
|
|
static constexpr const char* TAG_RAMP_CAPACITY{"<ramp-capacity>"};
|
|
|
|
static constexpr const char* TAG_LABEL_CHARGING{"<label-charging>"};
|
|
|
|
static constexpr const char* TAG_LABEL_DISCHARGING{"<label-discharging>"};
|
|
|
|
static constexpr const char* TAG_LABEL_FULL{"<label-full>"};
|
2020-12-02 09:55:13 -05:00
|
|
|
static constexpr const char* TAG_LABEL_LOW{"<label-low>"};
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-30 22:20:46 -05:00
|
|
|
static const size_t SKIP_N_UNCHANGED{3_z};
|
|
|
|
|
|
|
|
unique_ptr<state_reader> m_state_reader;
|
|
|
|
unique_ptr<capacity_reader> m_capacity_reader;
|
|
|
|
unique_ptr<rate_reader> m_rate_reader;
|
2017-02-16 16:35:43 -05:00
|
|
|
unique_ptr<consumption_reader> m_consumption_reader;
|
2016-11-19 09:42:31 -05:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
label_t m_label_charging;
|
|
|
|
label_t m_label_discharging;
|
|
|
|
label_t m_label_full;
|
2020-12-02 09:55:13 -05:00
|
|
|
label_t m_label_low;
|
2016-12-30 22:20:46 -05:00
|
|
|
animation_t m_animation_charging;
|
2018-04-23 14:24:02 -04:00
|
|
|
animation_t m_animation_discharging;
|
2020-12-02 09:55:13 -05:00
|
|
|
animation_t m_animation_low;
|
2016-12-30 22:20:46 -05:00
|
|
|
progressbar_t m_bar_capacity;
|
|
|
|
ramp_t m_ramp_capacity;
|
|
|
|
|
|
|
|
string m_fstate;
|
|
|
|
string m_fcapnow;
|
|
|
|
string m_fcapfull;
|
|
|
|
string m_frate;
|
|
|
|
string m_fvoltage;
|
|
|
|
|
|
|
|
state m_state{state::DISCHARGING};
|
|
|
|
int m_percentage{0};
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 09:42:31 -05:00
|
|
|
int m_fullat{100};
|
2020-12-02 09:55:13 -05:00
|
|
|
int m_lowat{10};
|
2016-12-30 22:20:46 -05:00
|
|
|
string m_timeformat;
|
|
|
|
size_t m_unchanged{SKIP_N_UNCHANGED};
|
2016-12-21 02:00:09 -05:00
|
|
|
chrono::duration<double> m_interval{};
|
2016-11-03 07:56:33 -04:00
|
|
|
chrono::system_clock::time_point m_lastpoll;
|
2016-12-30 22:20:46 -05:00
|
|
|
thread m_subthread;
|
2016-05-19 10:41:06 -04:00
|
|
|
};
|
2020-05-15 13:59:08 -04:00
|
|
|
} // namespace modules
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|