File: backlight.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 (82 lines) | stat: -rw-r--r-- 2,658 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
#pragma once

#include "components/config.hpp"
#include "modules/meta/inotify_module.hpp"
#include "modules/meta/types.hpp"
#include "settings.hpp"

POLYBAR_NS

namespace modules {
  /**
   * Reads value from `/sys/class/backlight/` to get a brightness value for some device.
   *
   * There are two file providing brightness values: `brightness` and `actual_brightness`.
   * The `actual_brightness` file is usually more reliable, but in some cases does not work (provides completely wrong
   * values, doesn't update) depending on kernel version, graphics driver, and/or graphics card.
   * Which file is used is controlled by the use-actual-brightness setting.
   *
   * The general issue with the `brightness` file seems to be that, while it does receive inotify events, the events it
   * receives are not for modification of the file and arrive just before the file is updated with a new value. The module
   * thus reads and displays an outdated brightness value. To compensate for this, the module periodically (controlled by
   * `poll-interval`) forces an update. By default, this is only enabled if the `backlight` file is used.
   */
  class backlight_module : public inotify_module<backlight_module> {
   public:
    struct brightness_handle {
      void filepath(const string& path);
      float read() const;

     private:
      string m_path;
    };

    string get_output();

    explicit backlight_module(const bar_settings&, string, const config&);

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

    static constexpr auto TYPE = BACKLIGHT_TYPE;

    static constexpr const char* EVENT_INC = "inc";
    static constexpr const char* EVENT_DEC = "dec";

   protected:
    void action_inc();
    void action_dec();

    void change_value(int value_mod);

   private:
    static constexpr auto TAG_LABEL = "<label>";
    static constexpr auto TAG_BAR = "<bar>";
    static constexpr auto TAG_RAMP = "<ramp>";

    ramp_t m_ramp;
    label_t m_label;
    progressbar_t m_progressbar;
    string m_path_backlight;
    float m_max_brightness{};
    bool m_scroll{false};
    int m_scroll_interval{5};
    bool m_use_actual_brightness{true};

    brightness_handle m_val;
    brightness_handle m_max;

    /**
     * Initial value set to a negative number so that any value read from the backlight file triggers an update during
     * the first read.
     * Otherwise, tokens may not be replaced
     */
    int m_percentage = -1;

    chrono::duration<double> m_interval{};
    chrono::steady_clock::time_point m_lastpoll;
  };
} // namespace modules

POLYBAR_NS_END