Waybar/src/modules/jack.cpp

117 lines
3.3 KiB
C++
Raw Normal View History

#include "modules/jack.hpp"
2022-07-20 02:30:42 +00:00
namespace waybar::modules {
JACK::JACK(const std::string &id, const Json::Value &config)
: ALabel(config, "jack", id, "{load}%", 1) {
2022-08-07 19:29:42 +00:00
running_ = false;
client_ = NULL;
thread_ = [this] {
dp.emit();
thread_.sleep_for(interval_);
};
}
2022-07-20 02:30:42 +00:00
std::string JACK::JACKState() {
2022-08-11 21:26:27 +00:00
std::lock_guard<std::mutex> lock(mutex_);
2022-08-07 19:29:42 +00:00
if (running_) {
load_ = jack_cpu_load(client_);
return state_;
}
xruns_ = 0;
load_ = 0;
bufsize_ = 0;
samplerate_ = 0;
if (client_) {
2022-08-11 19:49:24 +00:00
jack_client_close(client_);
2022-08-07 19:29:42 +00:00
client_ = NULL;
}
client_ = jack_client_open("waybar", JackNoStartServer, NULL);
2022-08-11 21:26:27 +00:00
if (!client_) return "disconnected";
if (config_["realtime"].isBool() && !config_["realtime"].asBool()) {
pthread_t jack_thread = jack_client_thread_id(client_);
2022-08-11 21:26:27 +00:00
jack_drop_real_time_scheduling(jack_thread);
}
2022-08-11 21:26:27 +00:00
bufsize_ = jack_get_buffer_size(client_);
samplerate_ = jack_get_sample_rate(client_);
jack_set_buffer_size_callback(client_, bufSizeCallback, this);
jack_set_xrun_callback(client_, xrunCallback, this);
jack_on_shutdown(client_, shutdownCallback, this);
if (jack_activate(client_)) return "disconnected";
running_ = true;
return "connected";
}
2022-07-20 02:30:42 +00:00
auto JACK::update() -> void {
std::string format;
2022-08-11 19:49:24 +00:00
std::string state = JACKState();
2022-08-07 19:29:42 +00:00
float latency = 1000 * (float)bufsize_ / (float)samplerate_;
2022-07-20 01:49:56 +00:00
if (label_.get_style_context()->has_class("xrun")) {
label_.get_style_context()->remove_class("xrun");
state = "connected";
}
2022-07-20 01:49:56 +00:00
if (label_.get_style_context()->has_class(state_))
label_.get_style_context()->remove_class(state_);
2022-08-07 19:29:42 +00:00
label_.get_style_context()->add_class(state);
state_ = state;
if (config_["format-" + state].isString()) {
format = config_["format-" + state].asString();
} else if (config_["format"].isString()) {
format = config_["format"].asString();
2022-07-20 01:49:56 +00:00
} else
format = "DSP {load}%";
2022-08-07 19:29:42 +00:00
label_.set_markup(fmt::format(format, fmt::arg("load", std::round(load_)),
2022-07-20 01:49:56 +00:00
fmt::arg("bufsize", bufsize_), fmt::arg("samplerate", samplerate_),
fmt::arg("latency", fmt::format("{:.2f}", latency)),
fmt::arg("xruns", xruns_)));
if (tooltipEnabled()) {
std::string tooltip_format = "{bufsize}/{samplerate} {latency}ms";
2022-07-20 01:49:56 +00:00
if (config_["tooltip-format"].isString()) tooltip_format = config_["tooltip-format"].asString();
2022-07-20 01:53:32 +00:00
label_.set_tooltip_text(fmt::format(
2022-08-07 19:29:42 +00:00
tooltip_format, fmt::arg("load", std::round(load_)), fmt::arg("bufsize", bufsize_),
2022-07-20 01:53:32 +00:00
fmt::arg("samplerate", samplerate_), fmt::arg("latency", fmt::format("{:.2f}", latency)),
fmt::arg("xruns", xruns_)));
}
// Call parent update
ALabel::update();
}
2022-02-12 06:52:51 +00:00
2022-07-20 02:30:42 +00:00
int JACK::bufSize(unsigned int size) {
2022-02-18 07:13:43 +00:00
bufsize_ = size;
2022-02-12 06:52:51 +00:00
return size;
}
2022-07-20 02:30:42 +00:00
int JACK::xrun() {
2022-02-18 07:13:43 +00:00
xruns_ += 1;
state_ = "xrun";
2022-02-12 06:52:51 +00:00
return 0;
}
2022-07-20 02:30:42 +00:00
void JACK::shutdown() {
2022-08-11 21:26:27 +00:00
std::lock_guard<std::mutex> lock(mutex_);
2022-08-07 19:29:42 +00:00
running_ = false;
2022-02-18 07:13:43 +00:00
}
2022-07-20 02:30:42 +00:00
} // namespace waybar::modules
2022-02-18 07:13:43 +00:00
int bufSizeCallback(unsigned int size, void *obj) {
2022-07-20 01:49:56 +00:00
return static_cast<waybar::modules::JACK *>(obj)->bufSize(size);
2022-02-18 07:13:43 +00:00
}
2022-07-20 01:49:56 +00:00
int xrunCallback(void *obj) { return static_cast<waybar::modules::JACK *>(obj)->xrun(); }
2022-02-18 07:13:43 +00:00
2022-07-20 01:49:56 +00:00
void shutdownCallback(void *obj) { return static_cast<waybar::modules::JACK *>(obj)->shutdown(); }