feat: Add basic support for MPD

pull/268/head
Minijackson 2019-04-16 16:34:37 +02:00
parent 85b6e6f709
commit 06aff70e2e
No known key found for this signature in database
GPG Key ID: FEA888C9F5D64F62
8 changed files with 235 additions and 1 deletions

View File

@ -14,6 +14,7 @@
- Memory - Memory
- Cpu load average - Cpu load average
- Temperature - Temperature
- MPD
- Custom scripts - Custom scripts
- Multiple output configuration - Multiple output configuration
- And much more customizations - And much more customizations
@ -49,6 +50,7 @@ libpulse [Pulseaudio module]
libnl [Network module] libnl [Network module]
sway [Sway modules] sway [Sway modules]
libdbusmenu-gtk3 [Tray module] libdbusmenu-gtk3 [Tray module]
libmpdclient [MPD module]
``` ```
Contributions welcome! - have fun :)<br> Contributions welcome! - have fun :)<br>

View File

@ -23,6 +23,9 @@
#ifdef HAVE_LIBPULSE #ifdef HAVE_LIBPULSE
#include "modules/pulseaudio.hpp" #include "modules/pulseaudio.hpp"
#endif #endif
#ifdef HAVE_LIBMPDCLIENT
#include "modules/mpd.hpp"
#endif
#include "modules/temperature.hpp" #include "modules/temperature.hpp"
#include "modules/custom.hpp" #include "modules/custom.hpp"

View File

@ -0,0 +1,39 @@
#pragma once
#include <thread>
#include <fmt/format.h>
#include <mpd/client.h>
#include "ALabel.hpp"
namespace waybar::modules {
class MPD : public ALabel {
public:
MPD(const std::string&, const Json::Value&);
auto update() -> void;
private:
std::thread worker();
void setLabel();
void tryConnect();
void checkErrors();
void fetchState();
void waitForEvent();
std::thread worker_;
using unique_connection = std::unique_ptr<mpd_connection, decltype(&mpd_connection_free)>;
using unique_status = std::unique_ptr<mpd_status, decltype(&mpd_status_free)>;
using unique_song = std::unique_ptr<mpd_song, decltype(&mpd_song_free)>;
// Not using unique_ptr since we don't manage the pointer
// (It's either nullptr, or from the config)
const char* server;
unique_connection connection_;
unique_status status_;
unique_song song_;
bool stopped_;
};
} // namespace waybar::modules

View File

@ -56,6 +56,7 @@ libnl = dependency('libnl-3.0', required: get_option('libnl'))
libnlgen = dependency('libnl-genl-3.0', required: get_option('libnl')) libnlgen = dependency('libnl-genl-3.0', required: get_option('libnl'))
libpulse = dependency('libpulse', required: get_option('pulseaudio')) libpulse = dependency('libpulse', required: get_option('pulseaudio'))
libudev = dependency('libudev', required: get_option('libudev')) libudev = dependency('libudev', required: get_option('libudev'))
libmpdclient = dependency('libmpdclient', required: get_option('mpd'))
src_files = files( src_files = files(
'src/factory.cpp', 'src/factory.cpp',
@ -107,6 +108,11 @@ if libudev.found()
src_files += 'src/modules/backlight.cpp' src_files += 'src/modules/backlight.cpp'
endif endif
if libmpdclient.found()
add_project_arguments('-DHAVE_LIBMPDCLIENT', language: 'cpp')
src_files += 'src/modules/mpd.cpp'
endif
subdir('protocol') subdir('protocol')
executable( executable(
@ -128,7 +134,8 @@ executable(
libnl, libnl,
libnlgen, libnlgen,
libpulse, libpulse,
libudev libudev,
libmpdclient
], ],
include_directories: [include_directories('include')], include_directories: [include_directories('include')],
install: true, install: true,

View File

@ -2,4 +2,5 @@ option('libnl', type: 'feature', value: 'auto', description: 'Enable libnl suppo
option('libudev', type: 'feature', value: 'auto', description: 'Enable libudev support for udev related features') option('libudev', type: 'feature', value: 'auto', description: 'Enable libudev support for udev related features')
option('pulseaudio', type: 'feature', value: 'auto', description: 'Enable support for pulseaudio') option('pulseaudio', type: 'feature', value: 'auto', description: 'Enable support for pulseaudio')
option('dbusmenu-gtk', type: 'feature', value: 'auto', description: 'Enable support for tray') option('dbusmenu-gtk', type: 'feature', value: 'auto', description: 'Enable support for tray')
option('mpd', type: 'feature', value: 'auto', description: 'Enable support for the Music Player Daemon')
option('out', type: 'string', value : '/', description: 'output prefix directory') option('out', type: 'string', value : '/', description: 'output prefix directory')

View File

@ -138,3 +138,16 @@ window#waybar.hidded {
background-color: #ecf0f1; background-color: #ecf0f1;
color: #2d3436; color: #2d3436;
} }
#mpd {
background-color: #ba2880;
}
#mpd.disconnected {
background: #f53c3c;
}
#mpd.stopped {
background: #90b1b1;
color: #2a5c45;
}

View File

@ -56,6 +56,11 @@ 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
#ifdef HAVE_LIBMPDCLIENT
if (ref == "mpd") {
return new waybar::modules::MPD(id, config_[name]);
}
#endif
if (ref == "temperature") { if (ref == "temperature") {
return new waybar::modules::Temperature(id, config_[name]); return new waybar::modules::Temperature(id, config_[name]);
} }

164
src/modules/mpd.cpp 100644
View File

@ -0,0 +1,164 @@
#include "modules/mpd.hpp"
#include <iostream>
waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config)
: ALabel(config, "{album} - {artist} - {title}", 2),
server(nullptr),
connection_(nullptr, &mpd_connection_free),
status_(nullptr, &mpd_status_free),
song_(nullptr, &mpd_song_free) {
label_.set_name("mpd");
if (!id.empty()) {
label_.get_style_context()->add_class(id);
}
if (!config["server"].isNull()) {
server = config["server"].asCString();
}
worker_ = worker();
}
auto waybar::modules::MPD::update() -> void {
tryConnect();
if (connection_ != nullptr) {
try {
fetchState();
} catch (std::exception e) {
stopped_ = true;
}
}
setLabel();
}
std::thread waybar::modules::MPD::worker() {
return std::thread([this] () {
while (true) {
if (connection_ == nullptr) {
// Retry periodically if no connection
update();
std::this_thread::sleep_for(std::chrono::seconds(2));
} else {
// Else, update on any event
waitForEvent();
update();
}
}
});
}
void waybar::modules::MPD::setLabel() {
if (connection_ == nullptr) {
label_.get_style_context()->add_class("disconnected");
// In the case connection closed while MPD is stopped
label_.get_style_context()->remove_class("stopped");
auto format = config_["format-disconnected"].isString() ?
config_["format-disconnected"].asString() : "disconnected";
label_.set_markup(format);
return;
} else {
label_.get_style_context()->remove_class("disconnected");
}
auto format = format_;
std::string artist, album_artist, album, title, date;
if (stopped_) {
format = config_["format-stopped"].isString() ?
config_["format-stopped"].asString() : "stopped";
label_.get_style_context()->add_class("stopped");
} else {
label_.get_style_context()->remove_class("stopped");
artist = mpd_song_get_tag(song_.get(), MPD_TAG_ARTIST, 0);
album_artist = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM_ARTIST, 0);
album = mpd_song_get_tag(song_.get(), MPD_TAG_ALBUM, 0);
title = mpd_song_get_tag(song_.get(), MPD_TAG_TITLE, 0);
date = mpd_song_get_tag(song_.get(), MPD_TAG_DATE, 0);
}
label_.set_markup(fmt::format(format,
fmt::arg("artist", artist),
fmt::arg("album-artist", album_artist),
fmt::arg("album", album),
fmt::arg("title", title),
fmt::arg("date", date)));
if (tooltipEnabled()) {
std::string tooltip_format;
tooltip_format = config_["tooltip-format"].isString() ?
config_["tooltip-format"].asString() : "MPD";
auto tooltip_text = fmt::format(tooltip_format,
fmt::arg("artist", artist),
fmt::arg("album-artist", album_artist),
fmt::arg("album", album),
fmt::arg("title", title),
fmt::arg("date", date));
label_.set_tooltip_text(tooltip_text);
}
}
void waybar::modules::MPD::tryConnect() {
if (connection_ != nullptr) {
return;
}
connection_ = unique_connection(
mpd_connection_new(server, config_["port"].asUInt(), 5'000),
&mpd_connection_free);
if (connection_ == nullptr) {
std::cerr << "Failed to connect to MPD" << std::endl;
return;
}
try {
checkErrors();
} catch (std::runtime_error e) {
std::cerr << "Failed to connect to MPD: " << e.what() << std::endl;
connection_.reset();
}
}
void waybar::modules::MPD::checkErrors() {
auto conn = connection_.get();
switch (mpd_connection_get_error(conn)) {
case MPD_ERROR_SUCCESS:
return;
case MPD_ERROR_CLOSED:
std::cerr << "Connection to MPD closed" << std::endl;
mpd_connection_clear_error(conn);
connection_.reset();
return;
default:
auto error_message = mpd_connection_get_error_message(conn);
mpd_connection_clear_error(conn);
throw std::runtime_error(std::string(error_message));
}
}
void waybar::modules::MPD::fetchState() {
status_ = unique_status(mpd_run_status(connection_.get()), &mpd_status_free);
checkErrors();
mpd_state state = mpd_status_get_state(status_.get());
checkErrors();
stopped_ = state == MPD_STATE_UNKNOWN || state == MPD_STATE_STOP;
mpd_send_current_song(connection_.get());
song_ = unique_song(mpd_recv_song(connection_.get()), &mpd_song_free);
mpd_response_finish(connection_.get());
checkErrors();
}
void waybar::modules::MPD::waitForEvent() {
auto conn = connection_.get();
mpd_run_idle_mask(conn, MPD_IDLE_PLAYER /* | MPD_IDLE_OPTIONS */);
checkErrors();
}