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

task(mpd): Move connection settings to config

Closes jaagr/lemonbuddy#16
This commit is contained in:
Michael Carlberg 2016-06-09 13:42:03 +02:00
parent f19c11c3c8
commit f667b739f0
7 changed files with 52 additions and 54 deletions

View file

@ -42,15 +42,6 @@ if(ENABLE_ALSA)
CACHE STRING "Name of the ALSA soundcard driver")
endif()
if(ENABLE_MPD)
set(SETTING_MPD_HOST "127.0.0.1"
CACHE STRING "Address MPD is bound to")
set(SETTING_MPD_PASSWORD ""
CACHE STRING "Password required for authentication")
set(SETTING_MPD_PORT "6600"
CACHE STRING "Port MPD is bound to")
endif()
set(SETTING_CONNECTION_TEST_IP "8.8.8.8"
CACHE STRING "Address to ping when testing network connection")
set(SETTING_PATH_BACKLIGHT_VAL "/sys/class/backlight/%card%/brightness"

View file

@ -524,6 +524,10 @@ See [the bspwm module](#user-content-dependencies) for details on `label:dimmed`
[module/mpd]
type = internal/mpd
;host = 127.0.0.1
;port = 6600
;password = mypassword
; Seconds to sleep between progressbar/song timer sync
;interval = 0.5
~~~

View file

@ -9,9 +9,6 @@
#define BUILDER_SPACE_TOKEN "%__"
#define ALSA_SOUNDCARD "@SETTING_ALSA_SOUNDCARD@"
#define MPD_HOST "@SETTING_MPD_HOST@"
#define MPD_PASSWORD "@SETTING_MPD_PASSWORD@"
#define MPD_PORT @SETTING_MPD_PORT@
#define CONNECTION_TEST_IP "@SETTING_CONNECTION_TEST_IP@"
#define PATH_BACKLIGHT_VAL "@SETTING_PATH_BACKLIGHT_VAL@"
#define PATH_BACKLIGHT_MAX "@SETTING_PATH_BACKLIGHT_MAX@"

View file

@ -59,6 +59,8 @@ namespace mpd
}
};
class Connection;
struct Status
{
struct StatusDeleter
@ -88,7 +90,8 @@ namespace mpd
unsigned long elapsed_time_ms;
void set(std::unique_ptr<struct mpd_status, StatusDeleter> status);
void update(int event);
void update(int event, std::unique_ptr<Connection>& connection);
void update_timer();
// unsigned get_total_time();
@ -112,9 +115,9 @@ namespace mpd
};
std::unique_ptr<mpd_connection, ConnectionDeleter> connection;
std::string host = MPD_HOST;
std::string password = MPD_PASSWORD;
int port = MPD_PORT;
std::string host;
std::string password;
int port;
int timeout = 15;
bool mpd_command_list_active = false;
@ -127,9 +130,8 @@ namespace mpd
void check_errors();
public:
Connection() = default;
static std::shared_ptr<Connection> &get();
Connection(std::string host, int port, std::string password)
: host(host), password(password), port(port) {}
void connect();
void disconnect();

View file

@ -10,6 +10,10 @@ namespace modules
{
DefineModule(MpdModule, EventModule)
{
std::string mpd_host = "127.0.0.1";
std::string mpd_pass = "";
int mpd_port = 6600;
static const int PROGRESSBAR_THREAD_SYNC_COUNT = 10;
const std::chrono::duration<double> PROGRESSBAR_THREAD_INTERVAL = 1s;
@ -53,7 +57,7 @@ namespace modules
std::string toggle_on_color;
std::string toggle_off_color;
std::shared_ptr<mpd::Connection> mpd;
std::unique_ptr<mpd::Connection> mpd;
std::chrono::system_clock::time_point synced_at;
float sync_interval = 0.5f;

View file

@ -10,15 +10,6 @@
namespace mpd
{
std::shared_ptr<Connection> conn;
std::shared_ptr<Connection> &Connection::get()
{
if (!conn)
conn = std::make_shared<Connection>();
return conn;
}
// Base
void Connection::connect()
@ -40,7 +31,7 @@ namespace mpd
this->check_errors();
} catch(ClientError &e) {
this->disconnect();
throw &e;
throw e;
}
}
@ -284,15 +275,15 @@ namespace mpd
this->updated_at = std::chrono::system_clock::now();
}
void Status::update(int event)
void Status::update(int event, std::unique_ptr<Connection>& connection)
{
auto status = Connection::get()->get_status();
auto status = connection->get_status();
if (event & (MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS)) {
this->set(std::move(status->status));
this->elapsed_time_ms = this->elapsed_time * 1000;
this->song = Connection::get()->get_song();
this->song = connection->get_song();
auto mpd_state = mpd_status_get_state(this->status.get());

View file

@ -13,6 +13,13 @@ using namespace mpd;
MpdModule::MpdModule(const std::string& name_)
: EventModule(name_), icons(std::make_unique<drawtypes::IconMap>())
{
// Load configuration values {{{
this->mpd_host = config::get<std::string>(this->name(), "host", "127.0.0.1");
this->mpd_port = config::get<int>(this->name(), "port", 6600);
this->mpd_pass = config::get<std::string>(this->name(), "password", "");
// }}}
// Add formats and elements {{{
this->formatter->add(FORMAT_ONLINE, TAG_LABEL_SONG, {
TAG_BAR_PROGRESS, TAG_TOGGLE, TAG_LABEL_SONG, TAG_LABEL_TIME,
TAG_ICON_RANDOM, TAG_ICON_REPEAT, TAG_ICON_REPEAT_ONE, TAG_ICON_PREV,
@ -53,8 +60,11 @@ MpdModule::MpdModule(const std::string& name_)
if (this->formatter->has(TAG_BAR_PROGRESS)) {
this->bar_progress = drawtypes::get_config_bar(name(), get_tag_name(TAG_BAR_PROGRESS));
}
// }}}
// Sign up for stdin events {{{
register_command_handler(name());
// }}}
}
MpdModule::~MpdModule()
@ -65,18 +75,18 @@ MpdModule::~MpdModule()
void MpdModule::start()
{
this->mpd = mpd::Connection::get();
this->mpd = std::make_unique<mpd::Connection>(this->mpd_host, this->mpd_port, this->mpd_pass);
this->synced_at = std::chrono::system_clock::now();
this->sync_interval = config::get<float>(name(), "interval", this->sync_interval) * 1000;
try {
mpd->connect();
this->status = mpd->get_status();
this->status->update(-1);
this->mpd->connect();
this->status = this->mpd->get_status();
this->status->update(-1, this->mpd);
} catch (mpd::Exception &e) {
log_error(e.what());
mpd->disconnect();
this->mpd->disconnect();
}
this->EventModule::start();
@ -84,41 +94,40 @@ void MpdModule::start()
bool MpdModule::has_event()
{
auto &mpd = mpd::Connection::get();
bool has_event = false;
if (!mpd->connected()) {
if (!this->mpd->connected()) {
try {
mpd->connect();
this->mpd->connect();
} catch (mpd::Exception &e) {
get_logger()->debug(e.what());
}
if (!mpd->connected()) {
if (!this->mpd->connected()) {
std::this_thread::sleep_for(3s);
return false;
}
}
if (!this->status) {
this->status = mpd->get_status();
this->status->update(-1);
this->status = this->mpd->get_status();
this->status->update(-1, this->mpd);
}
try {
mpd->idle();
this->mpd->idle();
int idle_flags;
if ((idle_flags = mpd->noidle()) != 0) {
this->status->update(idle_flags);
if ((idle_flags = this->mpd->noidle()) != 0) {
this->status->update(idle_flags, this->mpd);
has_event = true;
} else if (this->status->state & mpd::PLAYING) {
this->status->update_timer();
}
} catch (mpd::Exception &e) {
log_error(e.what());
mpd->disconnect();
this->mpd->disconnect();
has_event = true;
}
@ -136,12 +145,12 @@ bool MpdModule::has_event()
bool MpdModule::update()
{
if (!mpd::Connection::get()->connected())
if (!this->mpd->connected())
return true;
if (!this->status)
try {
this->status = mpd::Connection::get()->get_status();
this->status = this->mpd->get_status();
} catch (mpd::Exception &e) {
log_trace(e.what());
}
@ -156,7 +165,7 @@ bool MpdModule::update()
elapsed_str = this->status->get_formatted_elapsed();
total_str = this->status->get_formatted_total();
song = mpd::Connection::get()->get_song();
song = this->mpd->get_song();
if (*song) {
artist = song->get_artist();
@ -165,7 +174,7 @@ bool MpdModule::update()
}
} catch (mpd::Exception &e) {
log_error(e.what());
mpd::Connection::get()->disconnect();
this->mpd->disconnect();
return true;
}
@ -193,7 +202,7 @@ bool MpdModule::update()
}
std::string MpdModule::get_format() {
return mpd::Connection::get()->connected() ? FORMAT_ONLINE : FORMAT_OFFLINE;
return this->mpd->connected() ? FORMAT_ONLINE : FORMAT_OFFLINE;
}
bool MpdModule::build(Builder *builder, const std::string& tag)
@ -253,7 +262,7 @@ bool MpdModule::handle_command(const std::string& cmd)
return false;
try {
auto mpd = std::make_shared<mpd::Connection>();
auto mpd = std::make_unique<mpd::Connection>(this->mpd_host, this->mpd_port, this->mpd_pass);
mpd->connect();