feat(mpd): play/pause on click & stop on right-click

pull/268/head
Minijackson 2019-04-17 16:16:39 +02:00
parent 07dab2baec
commit 80a12d0238
No known key found for this signature in database
GPG Key ID: FEA888C9F5D64F62
2 changed files with 37 additions and 1 deletions

View File

@ -23,6 +23,8 @@ class MPD : public ALabel {
void fetchState(); void fetchState();
void waitForEvent(); void waitForEvent();
bool handlePlayPause(GdkEventButton* const&);
std::thread worker_; std::thread worker_;
using unique_connection = std::unique_ptr<mpd_connection, decltype(&mpd_connection_free)>; using unique_connection = std::unique_ptr<mpd_connection, decltype(&mpd_connection_free)>;
@ -35,6 +37,9 @@ class MPD : public ALabel {
const unsigned port_; const unsigned port_;
unique_connection connection_; unique_connection connection_;
// Use two connections because the main one will be blocking most of the time.
// Used to send play/pause events and poll elapsed time.
unique_connection alternate_connection_;
unique_status status_; unique_status status_;
mpd_state state_; mpd_state state_;
unique_song song_; unique_song song_;

View File

@ -7,6 +7,7 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config)
server_(nullptr), server_(nullptr),
port_(config["port"].asUInt()), port_(config["port"].asUInt()),
connection_(nullptr, &mpd_connection_free), connection_(nullptr, &mpd_connection_free),
alternate_connection_(nullptr, &mpd_connection_free),
status_(nullptr, &mpd_status_free), status_(nullptr, &mpd_status_free),
song_(nullptr, &mpd_song_free) { song_(nullptr, &mpd_song_free) {
label_.set_name("mpd"); label_.set_name("mpd");
@ -19,6 +20,10 @@ waybar::modules::MPD::MPD(const std::string& id, const Json::Value &config)
} }
worker_ = worker(); worker_ = worker();
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event().connect(
sigc::mem_fun(*this, &MPD::handlePlayPause));
} }
auto waybar::modules::MPD::update() -> void { auto waybar::modules::MPD::update() -> void {
@ -184,8 +189,14 @@ void waybar::modules::MPD::tryConnect() {
mpd_connection_new(server_, port_, 5'000), mpd_connection_new(server_, port_, 5'000),
&mpd_connection_free); &mpd_connection_free);
if (connection_ == nullptr) { alternate_connection_ = unique_connection(
mpd_connection_new(server_, port_, 5'000),
&mpd_connection_free);
if (connection_ == nullptr || alternate_connection_ == nullptr) {
std::cerr << "Failed to connect to MPD" << std::endl; std::cerr << "Failed to connect to MPD" << std::endl;
connection_.reset();
alternate_connection_.reset();
return; return;
} }
@ -194,6 +205,7 @@ void waybar::modules::MPD::tryConnect() {
} catch (std::runtime_error e) { } catch (std::runtime_error e) {
std::cerr << "Failed to connect to MPD: " << e.what() << std::endl; std::cerr << "Failed to connect to MPD: " << e.what() << std::endl;
connection_.reset(); connection_.reset();
alternate_connection_.reset();
} }
} }
@ -208,6 +220,7 @@ void waybar::modules::MPD::checkErrors() {
std::cerr << "Connection to MPD closed" << std::endl; std::cerr << "Connection to MPD closed" << std::endl;
mpd_connection_clear_error(conn); mpd_connection_clear_error(conn);
connection_.reset(); connection_.reset();
alternate_connection_.reset();
return; return;
default: default:
auto error_message = mpd_connection_get_error_message(conn); auto error_message = mpd_connection_get_error_message(conn);
@ -233,3 +246,21 @@ void waybar::modules::MPD::waitForEvent() {
mpd_run_idle_mask(conn, static_cast<mpd_idle>(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST)); mpd_run_idle_mask(conn, static_cast<mpd_idle>(MPD_IDLE_PLAYER | MPD_IDLE_OPTIONS | MPD_IDLE_PLAYLIST));
checkErrors(); checkErrors();
} }
bool waybar::modules::MPD::handlePlayPause(GdkEventButton* const& e) {
if (e->type == GDK_2BUTTON_PRESS || e->type == GDK_3BUTTON_PRESS || alternate_connection_ == nullptr) {
return false;
}
if (e->button == 1) {
if (stopped_) {
mpd_run_play(alternate_connection_.get());
} else {
mpd_run_toggle_pause(alternate_connection_.get());
}
} else if (e->button == 3) {
mpd_run_stop(alternate_connection_.get());
}
return true;
}