Child: [b108df] (diff)

Download this file

mpdcli.cxx    137 lines (122 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Copyright (C) 2014 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <memory.h>
#include <iostream>
#include <mpd/connection.h>
#include <mpd/password.h>
#include <mpd/mixer.h>
#include <mpd/status.h>
#include "mpdcli.hxx"
using namespace std;
#define M_CONN ((struct mpd_connection *)m_conn)
MPDCli::MPDCli(const string& host, int port, const string& pass)
: m_premutevolume(0)
{
cerr << "MPDCli::MPDCli" << endl;
m_conn = mpd_connection_new(host.c_str(), port, 0);
if (m_conn == NULL) {
cerr << "mpd_connection_new failed. No memory?" << endl;
return;
}
if (mpd_connection_get_error(M_CONN) != MPD_ERROR_SUCCESS) {
cerr << "Mpd connection error: " <<
mpd_connection_get_error_message(M_CONN) << endl;
return;
}
if(!pass.empty()) {
if (!mpd_run_password(M_CONN, pass.c_str())) {
cerr << "Password wrong" << endl;
return;
}
}
m_ok = true;
updStatus();
}
MPDCli::~MPDCli()
{
if (m_conn)
mpd_connection_free(M_CONN);
}
bool MPDCli::setVolume(int volume, bool relative)
{
cerr << "setVolume: vol " << volume << " relative " << relative << endl;
if (volume == 0) {
if (relative) {
// Restore premute volume
m_stat.volume = m_premutevolume;
cerr << "Restoring premute" << endl;
} else {
updStatus();
if (m_stat.volume != 0) {
cerr << "Saving premute: " << m_stat.volume << endl;
m_premutevolume = m_stat.volume;
}
}
}
if (relative)
volume += m_stat.volume;
if (volume < 0)
volume = 0;
else if (volume > 100)
volume = 100;
if (!mpd_run_set_volume(M_CONN, volume)) {
cerr << "Mpd set_volume error: " <<
mpd_connection_get_error_message(M_CONN) << endl;
return false;
}
return true;
}
bool MPDCli::updStatus()
{
//cerr << "MPDCli::updStatus" << endl;
if (!m_ok) {
cerr << "MPDCli::updStatus: bad state" << endl;
return false;
}
memset(&m_stat, 0, sizeof(m_stat));
mpd_status *mpds = mpd_run_status(M_CONN);
if (mpds == 0) {
cerr << "MPDCli::updStatus: status failed" << endl;
return false;
}
m_stat.volume = mpd_status_get_volume(mpds);
m_stat.rept = mpd_status_get_repeat(mpds);
m_stat.random = mpd_status_get_random(mpds);
m_stat.single = mpd_status_get_single(mpds);
m_stat.consume = mpd_status_get_consume(mpds);
m_stat.qlen = mpd_status_get_queue_length(mpds);
m_stat.qvers = mpd_status_get_queue_version(mpds);
switch (mpd_status_get_state(mpds)) {
case MPD_STATE_UNKNOWN: m_stat.state = MpdStatus::MPDS_UNK;break;
case MPD_STATE_STOP: m_stat.state = MpdStatus::MPDS_STOP;break;
case MPD_STATE_PLAY: m_stat.state = MpdStatus::MPDS_PLAY;break;
case MPD_STATE_PAUSE: m_stat.state = MpdStatus::MPDS_PAUSE;break;
}
m_stat.crossfade = mpd_status_get_crossfade(mpds);
m_stat.mixrampdb = mpd_status_get_mixrampdb(mpds);
m_stat.mixrampdelay = mpd_status_get_mixrampdelay(mpds);
m_stat.songpos = mpd_status_get_song_pos(mpds);
m_stat.songid = mpd_status_get_song_id(mpds);
m_stat.songms = mpd_status_get_elapsed_ms(mpds);
m_stat.songlen = mpd_status_get_total_time(mpds);
m_stat.kbrate = mpd_status_get_kbit_rate(mpds);
const char *err = mpd_status_get_error(mpds);
if (err != 0)
m_stat.errormessage = err;
mpd_status_free(mpds);
return true;
}