Switch to unified view

a/upmpd/mpdcli.cxx b/upmpd/mpdcli.cxx
...
...
35
35
36
MPDCli::MPDCli(const string& host, int port, const string& pass)
36
MPDCli::MPDCli(const string& host, int port, const string& pass)
37
    : m_conn(0), m_premutevolume(0), 
37
    : m_conn(0), m_premutevolume(0), 
38
      m_host(host), m_port(port), m_password(pass)
38
      m_host(host), m_port(port), m_password(pass)
39
{
39
{
40
    cerr << "MPDCli::MPDCli" << endl;
41
    if (!openconn()) {
40
    if (!openconn()) {
42
        return;
41
        return;
43
    }
42
    }
44
    m_ok = true;
43
    m_ok = true;
45
    updStatus();
44
    updStatus();
...
...
72
        if (!mpd_run_password(M_CONN, m_password.c_str())) {
71
        if (!mpd_run_password(M_CONN, m_password.c_str())) {
73
            cerr << "Password wrong" << endl;
72
            cerr << "Password wrong" << endl;
74
            return false;
73
            return false;
75
        }
74
        }
76
    }
75
    }
76
    mpd_run_consume(M_CONN, true);
77
    return true;
77
    return true;
78
}
78
}
79
79
80
bool MPDCli::showError(const string& who)
80
bool MPDCli::showError(const string& who)
81
{
81
{
82
    if (!ok()) {
82
    if (!ok()) {
83
        cerr << "MPDCli::showError: bad state" << endl;
83
        cerr << "MPDCli::showError: bad state" << endl;
84
        return false;
84
        return false;
85
    }
85
    }
86
86
87
    int error = mpd_connection_get_error(M_CONN);
88
    if (error == MPD_ERROR_SUCCESS)
89
        return false;
87
    cerr << who << " failed: " <<  mpd_connection_get_error_message(M_CONN);
90
    cerr << who << " failed: " <<  mpd_connection_get_error_message(M_CONN);
88
    int error = mpd_connection_get_error(M_CONN);
89
    if (error == MPD_ERROR_SERVER) {
91
    if (error == MPD_ERROR_SERVER) {
90
        cerr << " server error: " << mpd_connection_get_server_error(M_CONN) ;
92
        cerr << " server error: " << mpd_connection_get_server_error(M_CONN) ;
91
    }
93
    }
92
    cerr << endl;
94
    cerr << endl;
93
    if (error == MPD_ERROR_CLOSED)
95
    if (error == MPD_ERROR_CLOSED)
...
...
111
        cerr << "MPDCli::updStatus: bad state" << endl;
113
        cerr << "MPDCli::updStatus: bad state" << endl;
112
        return false;
114
        return false;
113
    }
115
    }
114
116
115
    mpd_status *mpds = 0;
117
    mpd_status *mpds = 0;
116
    RETRY_CMD(mpds = mpd_run_status(M_CONN));
118
    mpds = mpd_run_status(M_CONN);
119
    if (mpds == 0) {
120
        openconn();
121
        mpds = mpd_run_status(M_CONN);
122
        cerr << "MPDCli::updStatus: can't get status" << endl;
123
        return false;
124
    }
117
125
118
    m_stat.volume = mpd_status_get_volume(mpds);
126
    m_stat.volume = mpd_status_get_volume(mpds);
119
    m_stat.rept = mpd_status_get_repeat(mpds);
127
    m_stat.rept = mpd_status_get_repeat(mpds);
120
    m_stat.random = mpd_status_get_random(mpds);
128
    m_stat.random = mpd_status_get_random(mpds);
121
    m_stat.single = mpd_status_get_single(mpds);
129
    m_stat.single = mpd_status_get_single(mpds);
...
...
203
211
204
bool MPDCli::setVolume(int volume, bool relative)
212
bool MPDCli::setVolume(int volume, bool relative)
205
{
213
{
206
    if (!ok()) {
214
    if (!ok()) {
207
        return false;
215
        return false;
216
    }
217
    // Can't set volume if not active
218
    if (!(m_stat.state== MpdStatus::MPDS_PLAY) &&
219
        !(m_stat.state == MpdStatus::MPDS_PAUSE)) {
220
        cerr << "MPDCli::setVolume: not active" << endl;
221
        return true;
208
    }
222
    }
209
    cerr << "setVolume: vol " << volume << " relative " << relative << endl;
223
    cerr << "setVolume: vol " << volume << " relative " << relative << endl;
210
    if (volume == 0) {
224
    if (volume == 0) {
211
        if (relative) {
225
        if (relative) {
212
            // Restore premute volume
226
            // Restore premute volume
...
...
227
    if (volume < 0)
241
    if (volume < 0)
228
        volume = 0;
242
        volume = 0;
229
    else if (volume > 100)
243
    else if (volume > 100)
230
        volume = 100;
244
        volume = 100;
231
    
245
    
232
    for (int i = 0; i < 2; i++) {
233
        if (mpd_run_set_volume(M_CONN, volume))
246
    RETRY_CMD(mpd_run_set_volume(M_CONN, volume));
234
            break;
247
    m_stat.volume = volume;
235
        if (i == 1 || !showError("MPDCli::updStatus"))
248
    return true;
236
            return false;
237
    }
238
239
    return updStatus();
240
}
249
}
241
250
242
int MPDCli::getVolume()
251
int MPDCli::getVolume()
243
{
252
{
244
    if (!updStatus())
253
    if (!updStatus())
...
...
253
        return false;
262
        return false;
254
    RETRY_CMD(mpd_run_toggle_pause(M_CONN));
263
    RETRY_CMD(mpd_run_toggle_pause(M_CONN));
255
    return true;
264
    return true;
256
}
265
}
257
266
258
bool MPDCli::play()
267
bool MPDCli::play(int pos)
259
{
268
{
260
    cerr << "MPDCli::play" << endl;
269
    cerr << "MPDCli::play(pos=" << pos << ")" << endl;
261
    if (!ok())
270
    if (!ok())
262
        return false;
271
        return false;
272
    if (pos >= 0) {
273
        RETRY_CMD(mpd_run_play_pos(M_CONN, (unsigned int)pos));
274
    } else {
263
    RETRY_CMD(mpd_run_play(M_CONN));
275
        RETRY_CMD(mpd_run_play(M_CONN));
276
    }
264
    return true;
277
    return true;
265
}
278
}
266
bool MPDCli::stop()
279
bool MPDCli::stop()
267
{
280
{
268
    cerr << "MPDCli::stop" << endl;
281
    cerr << "MPDCli::stop" << endl;
269
    if (!ok())
282
    if (!ok())
270
        return false;
283
        return false;
271
    RETRY_CMD(mpd_run_stop(M_CONN));
284
    RETRY_CMD(mpd_run_stop(M_CONN));
272
    return true;
285
    return true;
273
}
286
}
287
bool MPDCli::seek(int seconds)
288
{
289
    if (!updStatus())
290
        return -1;
291
    cerr << "MPDCli::seek: pos:"<<m_stat.songpos<<" seconds: "<< seconds<<endl;
292
    RETRY_CMD(mpd_run_seek_pos(M_CONN, m_stat.songpos, (unsigned int)seconds));
293
    return true;
294
}
295
274
bool MPDCli::next()
296
bool MPDCli::next()
275
{
297
{
276
    cerr << "MPDCli::next" << endl;
298
    cerr << "MPDCli::next" << endl;
277
    if (!ok())
299
    if (!ok())
278
        return false;
300
        return false;
...
...
326
    if (id < 0) {
348
    if (id < 0) {
327
        showError("MPDCli::run_add_id");
349
        showError("MPDCli::run_add_id");
328
        return -1;
350
        return -1;
329
    }
351
    }
330
    return id;
352
    return id;
353
}
354
bool MPDCli::deleteId(int id)
355
{
356
    cerr << "MPDCli::deleteId " << id << endl;
357
    if (!ok())
358
        return -1;
359
360
    RETRY_CMD(mpd_run_delete_id(M_CONN, (unsigned)id));
361
    return false;
362
}
363
bool MPDCli::statId(int id)
364
{
365
    cerr << "MPDCli::statId " << id << endl;
366
    if (!ok())
367
        return -1;
368
369
    mpd_song *song = mpd_run_get_queue_song_id(M_CONN, (unsigned)id);
370
    if (song) {
371
        mpd_song_free(song);
372
        return true;
373
    }
374
    return false;
331
}
375
}
332
int MPDCli::curpos()
376
int MPDCli::curpos()
333
{
377
{
334
    if (!updStatus())
378
    if (!updStatus())
335
        return -1;
379
        return -1;