From f667b739f0a0c713dc88d888dedc0e12bfe0b9ea Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Thu, 9 Jun 2016 13:42:03 +0200 Subject: [PATCH] task(mpd): Move connection settings to config Closes jaagr/lemonbuddy#16 --- CMakeLists.txt | 9 ------- README.md | 4 +++ include/config.hpp.cmake | 3 --- include/interfaces/mpd.hpp | 16 ++++++------ include/modules/mpd.hpp | 6 ++++- src/interfaces/mpd.cpp | 17 +++---------- src/modules/mpd.cpp | 51 ++++++++++++++++++++++---------------- 7 files changed, 52 insertions(+), 54 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e716217d..8caa94d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/README.md b/README.md index 42365bd1..d8bc2d5e 100644 --- a/README.md +++ b/README.md @@ -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 ~~~ diff --git a/include/config.hpp.cmake b/include/config.hpp.cmake index 412c6064..7b627f10 100644 --- a/include/config.hpp.cmake +++ b/include/config.hpp.cmake @@ -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@" diff --git a/include/interfaces/mpd.hpp b/include/interfaces/mpd.hpp index 8e047040..87737a5e 100644 --- a/include/interfaces/mpd.hpp +++ b/include/interfaces/mpd.hpp @@ -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 status); - void update(int event); + + void update(int event, std::unique_ptr& connection); void update_timer(); // unsigned get_total_time(); @@ -112,9 +115,9 @@ namespace mpd }; std::unique_ptr 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 &get(); + Connection(std::string host, int port, std::string password) + : host(host), password(password), port(port) {} void connect(); void disconnect(); diff --git a/include/modules/mpd.hpp b/include/modules/mpd.hpp index aeaf7aca..b765813b 100644 --- a/include/modules/mpd.hpp +++ b/include/modules/mpd.hpp @@ -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 PROGRESSBAR_THREAD_INTERVAL = 1s; @@ -53,7 +57,7 @@ namespace modules std::string toggle_on_color; std::string toggle_off_color; - std::shared_ptr mpd; + std::unique_ptr mpd; std::chrono::system_clock::time_point synced_at; float sync_interval = 0.5f; diff --git a/src/interfaces/mpd.cpp b/src/interfaces/mpd.cpp index 6da6b864..6b0e6605 100644 --- a/src/interfaces/mpd.cpp +++ b/src/interfaces/mpd.cpp @@ -10,15 +10,6 @@ namespace mpd { - std::shared_ptr conn; - std::shared_ptr &Connection::get() - { - if (!conn) - conn = std::make_shared(); - 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) { - 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()); diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp index 369f1065..025c6cf0 100644 --- a/src/modules/mpd.cpp +++ b/src/modules/mpd.cpp @@ -13,6 +13,13 @@ using namespace mpd; MpdModule::MpdModule(const std::string& name_) : EventModule(name_), icons(std::make_unique()) { + // Load configuration values {{{ + this->mpd_host = config::get(this->name(), "host", "127.0.0.1"); + this->mpd_port = config::get(this->name(), "port", 6600); + this->mpd_pass = config::get(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(this->mpd_host, this->mpd_port, this->mpd_pass); this->synced_at = std::chrono::system_clock::now(); this->sync_interval = config::get(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(); + auto mpd = std::make_unique(this->mpd_host, this->mpd_port, this->mpd_pass); mpd->connect();