From e91398d77b75e407c0eca8c19a93b8473860b3ac Mon Sep 17 00:00:00 2001 From: Bob van Loosen Date: Wed, 19 Dec 2012 22:47:03 +0100 Subject: [PATCH] added: now playing --- src/bitvis.cpp | 5 +- src/bitvis.h | 2 + src/mpdclient.cpp | 129 ++++++++-------------------------------------- src/mpdclient.h | 16 ++---- 4 files changed, 31 insertions(+), 121 deletions(-) diff --git a/src/bitvis.cpp b/src/bitvis.cpp index 5cdfde9..43bfea8 100644 --- a/src/bitvis.cpp +++ b/src/bitvis.cpp @@ -54,6 +54,9 @@ CBitVis::CBitVis(int argc, char *argv[]) m_nrlines = 48 - m_fontheight - 1; m_scrolloffset = 0; + + m_mpdclient = new CMpdClient("music.bitlair", 6600); + m_mpdclient->StartThread(); } CBitVis::~CBitVis() @@ -331,7 +334,7 @@ void CBitVis::SendData(int64_t time) //add an empty line data.SetData(text, m_nrcolumns / 4, true); - SetText(text, "Wat? No money? Hier! Suck a cock!"); + SetText(text, m_mpdclient->CurrentSong().c_str()); data.SetData(text, sizeof(text), true); uint8_t end[10]; diff --git a/src/bitvis.h b/src/bitvis.h index 399cf8a..5e2b400 100644 --- a/src/bitvis.h +++ b/src/bitvis.h @@ -25,6 +25,7 @@ #include "jackclient.h" #include "fft.h" #include "util/tcpsocket.h" +#include "mpdclient.h" class CBitVis { @@ -53,6 +54,7 @@ class CBitVis float m_decay; int m_fontheight; int m_scrolloffset; + CMpdClient* m_mpdclient; struct peak { diff --git a/src/mpdclient.cpp b/src/mpdclient.cpp index 6719d04..85697ce 100644 --- a/src/mpdclient.cpp +++ b/src/mpdclient.cpp @@ -27,35 +27,18 @@ void CMpdClient::Process() { if (!OpenSocket()) { - CLock lock(m_condition); - m_commands.clear(); + m_currentsong.clear(); continue; } } - CLock lock(m_condition); - if (m_commands.empty()) - { - if (!m_condition.Wait(10000000)) - { - lock.Leave(); - if (!Ping()) - { - m_socket.Close(); - lock.Enter(); - m_commands.clear(); - continue; - } - } - } - lock.Leave(); - - if (!ProcessCommands()) + if (!GetCurrentSong()) { + m_currentsong.clear(); m_socket.Close(); - lock.Enter(); - m_commands.clear(); } + + USleep(1000000); } } @@ -81,72 +64,18 @@ bool CMpdClient::OpenSocket() } } -bool CMpdClient::ProcessCommands() -{ - CLock lock(m_condition); - while (!m_commands.empty()) - { - ECMD cmd = m_commands.front(); - m_commands.pop_front(); - lock.Leave(); - - int volume; - if (!GetVolume(volume)) - return false; - - int newvolume = volume; - if (cmd == CMD_VOLUP) - newvolume += 5; - else if (cmd == CMD_VOLDOWN) - newvolume -= 5; - - newvolume = Clamp(newvolume, 0, 100); - printf("Setting volume from %i to %i\n", volume, newvolume); - - if (!SetVolume(newvolume)) - return false; - - lock.Enter(); - } - - return true; -} - -bool CMpdClient::Ping() +bool CMpdClient::GetCurrentSong() { CTcpData data; - data.SetData("ping\n"); - + data.SetData("currentsong\n"); if (m_socket.Write(data) != SUCCESS) { printf("Error writing socket: %s\n", m_socket.GetError().c_str()); return false; } - m_socket.SetTimeout(0); - int returnv; - while((returnv = m_socket.Read(data)) == SUCCESS); - - m_socket.SetTimeout(10000000); - - if (returnv == FAIL) - { - printf("Error reading socket: %s\n", m_socket.GetError().c_str()); - return false; - } - - return true; -} - -bool CMpdClient::GetVolume(int& volume) -{ - CTcpData data; - data.SetData("status\n"); - if (m_socket.Write(data) != SUCCESS) - { - printf("Error writing socket: %s\n", m_socket.GetError().c_str()); - return false; - } + string artist; + string title; data.Clear(); while(1) @@ -166,10 +95,18 @@ bool CMpdClient::GetVolume(int& volume) break; string word; - if (GetWord(line, word) && word == "volume:") + if (GetWord(line, word)) { - if (GetWord(line, word) && StrToInt(word, volume) && volume >= 0 && volume <= 100) - return true; + if (word == "Artist:") + artist = line.substr(1); + else if (word == "Title:") + title = line.substr(1); + } + + if (!artist.empty() && !title.empty()) + { + m_currentsong = artist + " - " + title; + return true; } } } @@ -177,31 +114,9 @@ bool CMpdClient::GetVolume(int& volume) return false; } -bool CMpdClient::SetVolume(int volume) -{ - CTcpData data; - data.SetData(string("setvol ") + ToString(volume) + "\n"); - - if (m_socket.Write(data) != SUCCESS) - { - printf("Error writing socket: %s\n", m_socket.GetError().c_str()); - return false; - } - - return true; -} - -void CMpdClient::VolumeUp() +std::string CMpdClient::CurrentSong() { CLock lock(m_condition); - m_commands.push_back(CMD_VOLUP); - m_condition.Signal(); -} - -void CMpdClient::VolumeDown() -{ - CLock lock(m_condition); - m_commands.push_back(CMD_VOLDOWN); - m_condition.Signal(); + return m_currentsong; } diff --git a/src/mpdclient.h b/src/mpdclient.h index 18ece4f..b9df02d 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -8,12 +8,6 @@ #include "util/condition.h" #include "util/tcpsocket.h" -enum ECMD -{ - CMD_VOLUP, - CMD_VOLDOWN -}; - class CMpdClient : public CThread { public: @@ -21,21 +15,17 @@ class CMpdClient : public CThread ~CMpdClient(); virtual void Process(); - void VolumeUp(); - void VolumeDown(); + std::string CurrentSong(); private: bool OpenSocket(); - bool Ping(); - bool ProcessCommands(); - bool GetVolume(int& volume); - bool SetVolume(int volume); + bool GetCurrentSong(); int m_port; std::string m_address; CTcpClientSocket m_socket; - std::deque m_commands; CCondition m_condition; + std::string m_currentsong; };