File: battery.hpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (121 lines) | stat: -rw-r--r-- 3,604 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once

#include "common.hpp"
#include "modules/meta/inotify_module.hpp"
#include "modules/meta/types.hpp"

POLYBAR_NS

namespace modules {
  class battery_module : public inotify_module<battery_module> {
   public:
    enum class state {
      NONE = 0,
      CHARGING,
      DISCHARGING,
      LOW,
      FULL,
    };

    enum class value {
      NONE = 0,
      ADAPTER,
      CAPACITY,
      CAPACITY_MAX,
      VOLTAGE,
      RATE,
    };

    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 */>>;
    using consumption_reader = mutex_wrapper<value_reader<string /* watts */>>;

   public:
    explicit battery_module(const bar_settings&, string, const config&);

    void start() override;
    void teardown();
    void idle();
    bool on_event(const inotify_event& event);
    string get_format() const;
    bool build(builder* builder, const string& tag) const;

    static constexpr auto TYPE = BATTERY_TYPE;

   protected:
    state current_state();
    int current_percentage();
    int clamp_percentage(int percentage, state state) const;
    string current_time();
    string current_consumption();
    void subthread();

   private:
    static constexpr const char* FORMAT_CHARGING{"format-charging"};
    static constexpr const char* FORMAT_DISCHARGING{"format-discharging"};
    static constexpr const char* FORMAT_FULL{"format-full"};
    static constexpr const char* FORMAT_LOW{"format-low"};

    static constexpr const char* TAG_ANIMATION_CHARGING{"<animation-charging>"};
    static constexpr const char* TAG_ANIMATION_DISCHARGING{"<animation-discharging>"};
    static constexpr const char* TAG_ANIMATION_LOW{"<animation-low>"};
    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>"};
    static constexpr const char* TAG_LABEL_LOW{"<label-low>"};

    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;
    unique_ptr<consumption_reader> m_consumption_reader;

    label_t m_label_charging;
    label_t m_label_discharging;
    label_t m_label_full;
    label_t m_label_low;
    animation_t m_animation_charging;
    animation_t m_animation_discharging;
    animation_t m_animation_low;
    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};

    int m_fullat{100};
    int m_lowat{10};
    string m_timeformat;
    size_t m_unchanged{SKIP_N_UNCHANGED};
    chrono::duration<double> m_interval{};
    chrono::steady_clock::time_point m_lastpoll;
    thread m_subthread;
  };
} // namespace modules

POLYBAR_NS_END