1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00

feat(backlight): Add enable-scroll (#1957)

* backlight: enable changing via scroll

* squash! feedback

* Update src/modules/backlight.cpp

Co-Authored-By: Jérôme BOULMIER <jerome.boulmier@outlook.fr>

Co-authored-by: Jérôme BOULMIER <jerome.boulmier@outlook.fr>
This commit is contained in:
Gus Caplan 2020-01-15 07:32:17 -08:00 committed by Patrick Ziegler
parent 0dbcb28a2c
commit 068bf5a311
4 changed files with 89 additions and 14 deletions

View file

@ -1,13 +1,14 @@
#pragma once #pragma once
#include "components/config.hpp" #include "components/config.hpp"
#include "settings.hpp"
#include "modules/meta/inotify_module.hpp" #include "modules/meta/inotify_module.hpp"
#include "modules/meta/input_handler.hpp"
#include "settings.hpp"
POLYBAR_NS POLYBAR_NS
namespace modules { namespace modules {
class backlight_module : public inotify_module<backlight_module> { class backlight_module : public inotify_module<backlight_module>, public input_handler {
public: public:
struct brightness_handle { struct brightness_handle {
void filepath(const string& path); void filepath(const string& path);
@ -17,6 +18,8 @@ namespace modules {
string m_path; string m_path;
}; };
string get_output();
public: public:
explicit backlight_module(const bar_settings&, string); explicit backlight_module(const bar_settings&, string);
@ -24,20 +27,29 @@ namespace modules {
bool on_event(inotify_event* event); bool on_event(inotify_event* event);
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
protected:
bool input(string&& cmd);
private: private:
static constexpr auto TAG_LABEL = "<label>"; static constexpr auto TAG_LABEL = "<label>";
static constexpr auto TAG_BAR = "<bar>"; static constexpr auto TAG_BAR = "<bar>";
static constexpr auto TAG_RAMP = "<ramp>"; static constexpr auto TAG_RAMP = "<ramp>";
static constexpr const char* EVENT_SCROLLUP{"backlight+"};
static constexpr const char* EVENT_SCROLLDOWN{"backlight-"};
ramp_t m_ramp; ramp_t m_ramp;
label_t m_label; label_t m_label;
progressbar_t m_progressbar; progressbar_t m_progressbar;
string m_path_backlight;
float m_max_brightness;
bool m_scroll{false};
brightness_handle m_val; brightness_handle m_val;
brightness_handle m_max; brightness_handle m_max;
int m_percentage = 0; int m_percentage = 0;
}; };
} } // namespace modules
POLYBAR_NS_END POLYBAR_NS_END

View file

@ -104,6 +104,7 @@ namespace file_util {
bool exists(const string& filename); bool exists(const string& filename);
string pick(const vector<string>& filenames); string pick(const vector<string>& filenames);
string contents(const string& filename); string contents(const string& filename);
void write_contents(const string& filename, const string& contents);
bool is_fifo(const string& filename); bool is_fifo(const string& filename);
vector<string> glob(string pattern); vector<string> glob(string pattern);
const string expand(const string& path); const string expand(const string& path);
@ -112,6 +113,6 @@ namespace file_util {
decltype(auto) make_file_descriptor(Args&&... args) { decltype(auto) make_file_descriptor(Args&&... args) {
return factory_util::unique<file_descriptor>(forward<Args>(args)...); return factory_util::unique<file_descriptor>(forward<Args>(args)...);
} }
} } // namespace file_util
POLYBAR_NS_END POLYBAR_NS_END

View file

@ -3,9 +3,9 @@
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "drawtypes/progressbar.hpp" #include "drawtypes/progressbar.hpp"
#include "drawtypes/ramp.hpp" #include "drawtypes/ramp.hpp"
#include "utils/file.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "utils/file.hpp"
#include "utils/math.hpp"
POLYBAR_NS POLYBAR_NS
@ -27,6 +27,9 @@ namespace modules {
: inotify_module<backlight_module>(bar, move(name_)) { : inotify_module<backlight_module>(bar, move(name_)) {
auto card = m_conf.get(name(), "card"); auto card = m_conf.get(name(), "card");
// Get flag to check if we should add scroll handlers for changing value
m_scroll = m_conf.get(name(), "enable-scroll", m_scroll);
// Add formats and elements // Add formats and elements
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR, TAG_RAMP}); m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR, TAG_RAMP});
@ -41,17 +44,17 @@ namespace modules {
} }
// Build path to the sysfs folder the current/maximum brightness values are located // Build path to the sysfs folder the current/maximum brightness values are located
auto path_backlight = string_util::replace(PATH_BACKLIGHT, "%card%", card); m_path_backlight = string_util::replace(PATH_BACKLIGHT, "%card%", card);
/* /*
* amdgpu drivers set the actual_brightness in a different scale than [0, max_brightness] * amdgpu drivers set the actual_brightness in a different scale than [0, max_brightness]
* The only sensible way is to use the 'brightness' file instead * The only sensible way is to use the 'brightness' file instead
* Ref: https://github.com/Alexays/Waybar/issues/335 * Ref: https://github.com/Alexays/Waybar/issues/335
*/ */
auto path_backlight_val = path_backlight + "/" + (card == "amdgpu_bl0"? "brightness" : "actual_brightness"); auto path_backlight_val = m_path_backlight + "/" + (card == "amdgpu_bl0" ? "brightness" : "actual_brightness");
m_val.filepath(path_backlight_val); m_val.filepath(path_backlight_val);
m_max.filepath(path_backlight + "/max_brightness"); m_max.filepath(m_path_backlight + "/max_brightness");
// Add inotify watch // Add inotify watch
watch(path_backlight_val); watch(path_backlight_val);
@ -66,7 +69,8 @@ namespace modules {
m_log.trace("%s: %s", name(), event->filename); m_log.trace("%s: %s", name(), event->filename);
} }
m_percentage = static_cast<int>(m_val.read() / m_max.read() * 100.0f + 0.5f); m_max_brightness = m_max.read();
m_percentage = static_cast<int>(m_val.read() / m_max_brightness * 100.0f + 0.5f);
if (m_label) { if (m_label) {
m_label->reset_tokens(); m_label->reset_tokens();
@ -76,6 +80,25 @@ namespace modules {
return true; return true;
} }
string backlight_module::get_output() {
// Get the module output early so that
// the format prefix/suffix also gets wrapped
// with the cmd handlers
string output{module::get_output()};
if (m_scroll) {
m_builder->cmd(mousebtn::SCROLL_UP, EVENT_SCROLLUP);
m_builder->cmd(mousebtn::SCROLL_DOWN, EVENT_SCROLLDOWN);
}
m_builder->append(std::move(output));
m_builder->cmd_close();
m_builder->cmd_close();
return m_builder->flush();
}
bool backlight_module::build(builder* builder, const string& tag) const { bool backlight_module::build(builder* builder, const string& tag) const {
if (tag == TAG_BAR) { if (tag == TAG_BAR) {
builder->node(m_progressbar->output(m_percentage)); builder->node(m_progressbar->output(m_percentage));
@ -88,6 +111,33 @@ namespace modules {
} }
return true; return true;
} }
}
bool backlight_module::input(string&& cmd) {
double value_mod{0.0};
if (cmd == EVENT_SCROLLUP) {
value_mod = 5.0;
} else if (cmd == EVENT_SCROLLDOWN) {
value_mod = -5.0;
} else {
return false;
}
m_log.info("%s: Changing value by %f%", name(), value_mod);
try {
int rounded = math_util::cap<double>(m_percentage + value_mod, 0.0, 100.0) + 0.5;
int value = math_util::percentage_to_value<int>(rounded, m_max_brightness);
file_util::write_contents(m_path_backlight + "/brightness", to_string(value));
} catch (const exception& err) {
m_log.err(
"%s: Unable to change backlight value. Your system may require additional "
"configuration. Please read the module documentation.\n(reason: %s)",
name(), err.what());
}
return true;
}
} // namespace modules
POLYBAR_NS_END POLYBAR_NS_END

View file

@ -1,6 +1,9 @@
#include "utils/file.hpp"
#include <fcntl.h> #include <fcntl.h>
#include <glob.h> #include <glob.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
@ -8,7 +11,6 @@
#include "errors.hpp" #include "errors.hpp"
#include "utils/env.hpp" #include "utils/env.hpp"
#include "utils/file.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
POLYBAR_NS POLYBAR_NS
@ -202,6 +204,16 @@ namespace file_util {
} }
} }
/**
* Writes the contents of the given file
*/
void write_contents(const string& filename, const string& contents) {
std::ofstream out(filename, std::ofstream::out);
if (!(out << contents)) {
throw std::system_error(errno, std::system_category(), "failed to write to " + filename);
}
}
/** /**
* Checks if the given file is a named pipe * Checks if the given file is a named pipe
*/ */
@ -246,7 +258,7 @@ namespace file_util {
bool is_absolute = path.size() > 0 && path.at(0) == '/'; bool is_absolute = path.size() > 0 && path.at(0) == '/';
vector<string> p_exploded = string_util::split(path, '/'); vector<string> p_exploded = string_util::split(path, '/');
for (auto& section : p_exploded) { for (auto& section : p_exploded) {
switch(section[0]) { switch (section[0]) {
case '$': case '$':
section = env_util::get(section.substr(1)); section = env_util::get(section.substr(1));
break; break;
@ -262,6 +274,6 @@ namespace file_util {
} }
return ret; return ret;
} }
} } // namespace file_util
POLYBAR_NS_END POLYBAR_NS_END