Skip to content

Commit

Permalink
GTK4: Migration pulseaudio
Browse files Browse the repository at this point in the history
Signed-off-by: Viktar Lukashonak <[email protected]>
  • Loading branch information
LukashonakV committed Apr 8, 2024
1 parent bbc916d commit ee777d8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
12 changes: 3 additions & 9 deletions include/modules/pulseaudio.hpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
#pragma once

#include <fmt/format.h>

#include <algorithm>
#include <array>
#include <memory>

#include "ALabel.hpp"
#include "util/audio_backend.hpp"

namespace waybar::modules {

class Pulseaudio : public ALabel {
class Pulseaudio final : public ALabel {
public:
Pulseaudio(const std::string&, const Json::Value&);
virtual ~Pulseaudio() = default;
auto update() -> void override;

private:
bool handleScroll(GdkEventScroll* e) override;
bool handleScroll(double dx, double dy) override;
const std::vector<std::string> getPulseIcon() const;

std::shared_ptr<util::AudioBackend> backend = nullptr;
std::shared_ptr<util::AudioBackend> backend{nullptr};
};

} // namespace waybar::modules
17 changes: 16 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ libudev = dependency('libudev', required: get_option('libudev'))
xkbregistry = dependency('xkbregistry')
libnl = dependency('libnl-3.0', required: get_option('libnl'))
libnlgen = dependency('libnl-genl-3.0', required: get_option('libnl'))
libpulse = dependency('libpulse', required: get_option('pulseaudio'))

gtk_layer_shell = dependency('gtk4-layer-shell-0',
version : ['>=1.0.2'],
Expand Down Expand Up @@ -281,6 +282,19 @@ if not get_option('logind').disabled()
)
endif

if libpulse.found()
add_project_arguments('-DHAVE_LIBPULSE', language: 'cpp')
src_files += files(
'src/modules/pulseaudio.cpp',
'src/modules/pulseaudio_slider.cpp',
'src/util/audio_backend.cpp',
)
man_files += files(
'man/waybar-pulseaudio.5.scd',
'man/waybar-pulseaudio-slider.5.scd',
)
endif

subdir('protocol')

executable(
Expand All @@ -300,7 +314,8 @@ executable(
xkbregistry,
cava,
libnl,
libnlgen
libnlgen,
libpulse
],
include_directories: inc_dirs,
install: true,
Expand Down
4 changes: 2 additions & 2 deletions src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ gtk4 todo
if (ref == "keyboard-state") {
return new waybar::modules::KeyboardState(id, bar_, config_[name]);
}
#endif
#endif*/
#ifdef HAVE_LIBPULSE
if (ref == "pulseaudio") {
return new waybar::modules::Pulseaudio(id, config_[name]);
}
if (ref == "pulseaudio/slider") {
return new waybar::modules::PulseaudioSlider(id, config_[name]);
}
#endif
#endif/*
#ifdef HAVE_LIBMPDCLIENT
if (ref == "mpd") {
return new waybar::modules::MPD(id, config_[name]);
Expand Down
26 changes: 13 additions & 13 deletions src/modules/dwl/tags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace waybar::modules::dwl {
/* dwl stuff */
wl_array tags, layouts;

static uint num_tags = 0;
static uint num_tags{0};

void toggle_visibility(void *data, zdwl_ipc_output_v2 *zdwl_output_v2) {
// Intentionally empty
Expand Down Expand Up @@ -88,8 +88,8 @@ Tags::Tags(const std::string &id, const waybar::Bar &bar, const Json::Value &con
bar_(bar),
box_{bar.orientation, 0},
output_status_{nullptr} {
struct wl_display *display = Client::inst()->wl_display;
struct wl_registry *registry = wl_display_get_registry(display);
struct wl_display *display{Client::inst()->wl_display};
struct wl_registry *registry{wl_display_get_registry(display)};
wl_registry_add_listener(registry, &registry_listener_impl, this);
wl_display_roundtrip(display);

Expand All @@ -109,26 +109,26 @@ Tags::Tags(const std::string &id, const waybar::Bar &bar, const Json::Value &con
box_.get_style_context()->add_class(MODULE_CLASS);

// Default to 9 tags, cap at 32
const uint32_t num_tags =
config["num-tags"].isUInt() ? std::min<uint32_t>(32, config_["num-tags"].asUInt()) : 9;
const uint32_t num_tags{
config["num-tags"].isUInt() ? std::min<uint32_t>(32, config_["num-tags"].asUInt()) : 9};

std::vector<std::string> tag_labels(num_tags);
for (uint32_t tag = 0; tag < num_tags; ++tag) {
for (uint32_t tag{0}; tag < num_tags; ++tag) {
tag_labels[tag] = std::to_string(tag + 1);
}
const Json::Value custom_labels = config["tag-labels"];
const Json::Value custom_labels{config["tag-labels"]};
if (custom_labels.isArray() && !custom_labels.empty()) {
for (uint32_t tag = 0; tag < std::min(num_tags, custom_labels.size()); ++tag) {
for (uint32_t tag{0}; tag < std::min(num_tags, custom_labels.size()); ++tag) {
tag_labels[tag] = custom_labels[tag].asString();
}
}

uint32_t i = 1;
uint32_t i{1};
for (const auto &tag_label : tag_labels) {
Gtk::Button &button{buttons_.emplace_back(tag_label)};
auto controlClick{clickControls_.emplace_back(Gtk::GestureClick::create())};
controlClick->set_propagation_phase(Gtk::PropagationPhase::TARGET);
controlClick->set_button(0);
controlClick->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
controlClick->set_button(1u);
button.add_controller(controlClick);
button.set_has_frame(false);
box_.prepend(button);
Expand All @@ -142,7 +142,7 @@ Tags::Tags(const std::string &id, const waybar::Bar &bar, const Json::Value &con
i <<= 1;
}

struct wl_output *output = gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj());
struct wl_output *output{gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj())};
output_status_ = zdwl_ipc_manager_v2_get_output(status_manager_, output);
zdwl_ipc_output_v2_add_listener(output_status_, &output_status_listener_impl, this);

Expand Down Expand Up @@ -171,7 +171,7 @@ void Tags::handle_button_press(int n_press, double dx, double dy, uint32_t tag,

void Tags::handle_view_tags(uint32_t tag, uint32_t state, uint32_t clients, uint32_t focused) {
// First clear all occupied state
auto &button = buttons_[tag];
auto &button{buttons_[tag]};
if (clients) {
button.get_style_context()->add_class("occupied");
} else {
Expand Down
53 changes: 28 additions & 25 deletions src/modules/pulseaudio.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#include "modules/pulseaudio.hpp"

waybar::modules::Pulseaudio::Pulseaudio(const std::string &id, const Json::Value &config)
: ALabel(config, "pulseaudio", id, "{volume}%") {
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &Pulseaudio::handleScroll));
#include <fmt/format.h>

namespace waybar::modules {

Pulseaudio::Pulseaudio(const std::string &id, const Json::Value &config)
: ALabel(config, "pulseaudio", id, "{volume}%", 60, false, false, true) {
backend = util::AudioBackend::getInstance([this] { this->dp.emit(); });
backend->setIgnoredSinks(config_["ignored-sinks"]);
}

bool waybar::modules::Pulseaudio::handleScroll(GdkEventScroll *e) {
bool Pulseaudio::handleScroll(double dx, double dy) {
// change the pulse volume only when no user provided
// events are configured
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) {
return AModule::handleScroll(e);
return AModule::handleScroll(dx, dy);
}
auto dir = AModule::getScrollDir(e);
auto dir{AModule::getScrollDir(controllScroll_->get_current_event())};
if (dir == SCROLL_DIR::NONE) {
return true;
}
int max_volume = 100;
double step = 1;
int max_volume{100};
double step{1};
// isDouble returns true for integers as well, just in case
if (config_["scroll-step"].isDouble()) {
step = config_["scroll-step"].asDouble();
Expand All @@ -29,21 +30,21 @@ bool waybar::modules::Pulseaudio::handleScroll(GdkEventScroll *e) {
max_volume = config_["max-volume"].asInt();
}

auto change_type = (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::RIGHT)
auto change_type{(dir == SCROLL_DIR::UP || dir == SCROLL_DIR::RIGHT)
? util::ChangeType::Increase
: util::ChangeType::Decrease;
: util::ChangeType::Decrease};

backend->changeVolume(change_type, step, max_volume);
return true;
}

static const std::array<std::string, 9> ports = {
static const std::array<std::string, 9> ports{
"headphone", "speaker", "hdmi", "headset", "hands-free", "portable", "car", "hifi", "phone",
};

const std::vector<std::string> waybar::modules::Pulseaudio::getPulseIcon() const {
std::vector<std::string> res = {backend->getCurrentSinkName(), backend->getDefaultSourceName()};
std::string nameLC = backend->getSinkPortName() + backend->getFormFactor();
std::vector<std::string> res{backend->getCurrentSinkName(), backend->getDefaultSourceName()};
std::string nameLC{backend->getSinkPortName() + backend->getFormFactor()};
std::transform(nameLC.begin(), nameLC.end(), nameLC.begin(), ::tolower);
for (auto const &port : ports) {
if (nameLC.find(port) != std::string::npos) {
Expand All @@ -54,12 +55,12 @@ const std::vector<std::string> waybar::modules::Pulseaudio::getPulseIcon() const
return res;
}

auto waybar::modules::Pulseaudio::update() -> void {
auto format = format_;
auto Pulseaudio::update() -> void {
auto format{format_};
std::string tooltip_format;
auto sink_volume = backend->getSinkVolume();
auto sink_volume{backend->getSinkVolume()};
if (!alt_) {
std::string format_name = "format";
std::string format_name{"format"};
if (backend->isBluetooth()) {
format_name = format_name + "-bluetooth";
label_.get_style_context()->add_class("bluetooth");
Expand All @@ -78,15 +79,15 @@ auto waybar::modules::Pulseaudio::update() -> void {
label_.get_style_context()->remove_class("muted");
label_.get_style_context()->remove_class("sink-muted");
}
auto state = getState(sink_volume, true);
auto state{getState(sink_volume, true)};
if (!state.empty() && config_[format_name + "-" + state].isString()) {
format = config_[format_name + "-" + state].asString();
} else if (config_[format_name].isString()) {
format = config_[format_name].asString();
}
}
// TODO: find a better way to split source/sink
std::string format_source = "{volume}%";
std::string format_source{"{volume}%"};
if (backend->getSourceMuted()) {
label_.get_style_context()->add_class("source-muted");
if (config_["format-source-muted"].isString()) {
Expand All @@ -99,15 +100,15 @@ auto waybar::modules::Pulseaudio::update() -> void {
}
}

auto source_volume = backend->getSourceVolume();
auto sink_desc = backend->getSinkDesc();
auto source_desc = backend->getSourceDesc();
auto source_volume{backend->getSourceVolume()};
auto sink_desc{backend->getSinkDesc()};
auto source_desc{backend->getSourceDesc()};

format_source = fmt::format(fmt::runtime(format_source), fmt::arg("volume", source_volume));
auto text = fmt::format(
auto text{fmt::format(
fmt::runtime(format), fmt::arg("desc", sink_desc), fmt::arg("volume", sink_volume),
fmt::arg("format_source", format_source), fmt::arg("source_volume", source_volume),
fmt::arg("source_desc", source_desc), fmt::arg("icon", getIcon(sink_volume, getPulseIcon())));
fmt::arg("source_desc", source_desc), fmt::arg("icon", getIcon(sink_volume, getPulseIcon())))};
if (text.empty()) {
label_.hide();
} else {
Expand All @@ -133,3 +134,5 @@ auto waybar::modules::Pulseaudio::update() -> void {
// Call parent update
ALabel::update();
}

} // namespace waybar::modules

0 comments on commit ee777d8

Please sign in to comment.