mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Add temperature module
This commit is contained in:
parent
cd13180199
commit
7ae549dc9e
@ -23,6 +23,7 @@
|
|||||||
#ifdef HAVE_LIBPULSE
|
#ifdef HAVE_LIBPULSE
|
||||||
#include "modules/pulseaudio.hpp"
|
#include "modules/pulseaudio.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
#include "modules/temperature.hpp"
|
||||||
#include "modules/custom.hpp"
|
#include "modules/custom.hpp"
|
||||||
|
|
||||||
namespace waybar {
|
namespace waybar {
|
||||||
|
23
include/modules/temperature.hpp
Normal file
23
include/modules/temperature.hpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
#include <fstream>
|
||||||
|
#include "util/sleeper_thread.hpp"
|
||||||
|
#include "ALabel.hpp"
|
||||||
|
|
||||||
|
namespace waybar::modules {
|
||||||
|
|
||||||
|
class Temperature : public ALabel {
|
||||||
|
public:
|
||||||
|
Temperature(const std::string&, const Json::Value&);
|
||||||
|
~Temperature() = default;
|
||||||
|
auto update() -> void;
|
||||||
|
private:
|
||||||
|
std::tuple<uint16_t, uint16_t> getTemperature();
|
||||||
|
bool isCritical(uint16_t);
|
||||||
|
|
||||||
|
std::string file_path_;
|
||||||
|
waybar::util::SleeperThread thread_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -66,6 +66,7 @@ src_files = files(
|
|||||||
'src/modules/custom.cpp',
|
'src/modules/custom.cpp',
|
||||||
'src/modules/cpu.cpp',
|
'src/modules/cpu.cpp',
|
||||||
'src/modules/idle_inhibitor.cpp',
|
'src/modules/idle_inhibitor.cpp',
|
||||||
|
'src/modules/temperature.cpp',
|
||||||
'src/main.cpp',
|
'src/main.cpp',
|
||||||
'src/bar.cpp',
|
'src/bar.cpp',
|
||||||
'src/client.cpp'
|
'src/client.cpp'
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
// Choose the order of the modules
|
// Choose the order of the modules
|
||||||
"modules-left": ["sway/workspaces", "sway/mode", "custom/spotify"],
|
"modules-left": ["sway/workspaces", "sway/mode", "custom/spotify"],
|
||||||
"modules-center": ["sway/window"],
|
"modules-center": ["sway/window"],
|
||||||
"modules-right": ["idle_inhibitor", "pulseaudio", "network", "cpu", "memory", "backlight", "battery", "battery#bat2", "clock", "tray"],
|
"modules-right": ["idle_inhibitor", "pulseaudio", "network", "cpu", "memory", "temperature", "backlight", "battery", "battery#bat2", "clock", "tray"],
|
||||||
// Modules configuration
|
// Modules configuration
|
||||||
// "sway/workspaces": {
|
// "sway/workspaces": {
|
||||||
// "disable-scroll": true,
|
// "disable-scroll": true,
|
||||||
@ -47,6 +47,13 @@
|
|||||||
"memory": {
|
"memory": {
|
||||||
"format": "{}% "
|
"format": "{}% "
|
||||||
},
|
},
|
||||||
|
"temperature": {
|
||||||
|
// "thermal-zone": 2,
|
||||||
|
// "hwmon-path": "/sys/class/hwmon/hwmon2/temp1_input",
|
||||||
|
// "critical-threshold": 80,
|
||||||
|
// "format-critical": "{temperatureC}°C ",
|
||||||
|
"format": "{temperatureC}°C "
|
||||||
|
},
|
||||||
"backlight": {
|
"backlight": {
|
||||||
// "device": "acpi_video1",
|
// "device": "acpi_video1",
|
||||||
"format": "{percent}% {icon}",
|
"format": "{percent}% {icon}",
|
||||||
|
@ -30,7 +30,7 @@ window#waybar {
|
|||||||
border-bottom: 3px solid white;
|
border-bottom: 3px solid white;
|
||||||
}
|
}
|
||||||
|
|
||||||
#clock, #battery, #cpu, #memory, #backlight, #network, #pulseaudio, #custom-spotify, #tray, #mode, #idle_inhibitor {
|
#clock, #battery, #cpu, #memory, #temperature, #backlight, #network, #pulseaudio, #custom-spotify, #tray, #mode, #idle_inhibitor {
|
||||||
padding: 0 10px;
|
padding: 0 10px;
|
||||||
margin: 0 5px;
|
margin: 0 5px;
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,9 @@ waybar::IModule* waybar::Factory::makeModule(const std::string &name) const
|
|||||||
return new waybar::modules::Pulseaudio(id, config_[name]);
|
return new waybar::modules::Pulseaudio(id, config_[name]);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
if (ref == "temperature") {
|
||||||
|
return new waybar::modules::Temperature(id, config_[name]);
|
||||||
|
}
|
||||||
if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) {
|
if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) {
|
||||||
return new waybar::modules::Custom(ref.substr(7), config_[name]);
|
return new waybar::modules::Custom(ref.substr(7), config_[name]);
|
||||||
}
|
}
|
||||||
|
60
src/modules/temperature.cpp
Normal file
60
src/modules/temperature.cpp
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include "modules/temperature.hpp"
|
||||||
|
|
||||||
|
waybar::modules::Temperature::Temperature(const std::string& id, const Json::Value& config)
|
||||||
|
: ALabel(config, "{temperatureC}°C", 10)
|
||||||
|
{
|
||||||
|
if (config_["hwmon-path"].isString()) {
|
||||||
|
file_path_ = config_["hwmon-path"].asString();
|
||||||
|
} else {
|
||||||
|
auto zone =
|
||||||
|
config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 1;
|
||||||
|
file_path_ = fmt::format("/sys/class/thermal/thermal_zone{}/temp", zone);
|
||||||
|
}
|
||||||
|
|
||||||
|
label_.set_name("temperature");
|
||||||
|
if (!id.empty()) {
|
||||||
|
label_.get_style_context()->add_class(id);
|
||||||
|
}
|
||||||
|
thread_ = [this] {
|
||||||
|
dp.emit();
|
||||||
|
thread_.sleep_for(interval_);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto waybar::modules::Temperature::update() -> void
|
||||||
|
{
|
||||||
|
auto [temperature_c, temperature_f] = getTemperature();
|
||||||
|
auto critical = isCritical(temperature_c);
|
||||||
|
auto format = format_;
|
||||||
|
if (critical) {
|
||||||
|
format =
|
||||||
|
config_["format-critical"].isString() ? config_["format-critical"].asString() : format;
|
||||||
|
label_.get_style_context()->add_class("critical");
|
||||||
|
} else {
|
||||||
|
label_.get_style_context()->remove_class("critical");
|
||||||
|
}
|
||||||
|
label_.set_markup(fmt::format(format,
|
||||||
|
fmt::arg("temperatureC", temperature_c), fmt::arg("temperatureF", temperature_f)));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<uint16_t, uint16_t> waybar::modules::Temperature::getTemperature()
|
||||||
|
{
|
||||||
|
std::ifstream temp(file_path_);
|
||||||
|
if (!temp.is_open()) {
|
||||||
|
throw std::runtime_error("Can't open " + file_path_);
|
||||||
|
}
|
||||||
|
std::string line;
|
||||||
|
if (temp.good()) {
|
||||||
|
getline(temp, line);
|
||||||
|
}
|
||||||
|
temp.close();
|
||||||
|
auto temperature_c = std::strtol(line.c_str(), nullptr, 10) / 1000.0;
|
||||||
|
auto temperature_f = temperature_c * 1.8 + 32;
|
||||||
|
std::tuple<uint16_t, uint16_t> temperatures(std::round(temperature_c), std::round(temperature_f));
|
||||||
|
return temperatures;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool waybar::modules::Temperature::isCritical(uint16_t temperature_c)
|
||||||
|
{
|
||||||
|
return config_["critical-threshold"].isInt() && temperature_c >= config_["critical-threshold"].asInt();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user