-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMpdClient.cpp
210 lines (194 loc) · 6.01 KB
/
MpdClient.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "MpdClient.h"
#include <cassert>
#include <iostream>
#include <mpd/client.h>
using namespace std;
#define CHUNK_SIZE 8192
MpdClient::MpdClient(Glib::RefPtr<Glib::MainLoop> ml) : MusicServerClient(ml) {
conn = mpd_connection_new(NULL, 0, 0);
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
handle_error("Connection Failure");
exit(EXIT_FAILURE);
}
attach_io_event_handler();
struct mpd_status *status;
status = mpd_run_status(conn);
if (status == NULL)
handle_error("MpdClient Error Getting Status");
else {
last_state = mpd_status_get_state(status);
mpd_status_free(status);
get_song_info(last_song_info, true);
}
start_idling();
}
MpdClient::~MpdClient() {
mpd_connection_free(conn);
}
int MpdClient::handle_error(string prefix) {
if (mpd_connection_get_error(conn) == MPD_ERROR_SUCCESS) {
fprintf(stdout, "%s: hmmmmm. no error, actually.\n", prefix.c_str());
} else {
fprintf(stdout, "%s: %s\n", prefix.c_str(), mpd_connection_get_error_message(conn));
}
return 0;
}
void MpdClient::get_song_info(song_info_t &info, bool get_albumart) {
struct mpd_song *song = mpd_run_current_song(conn);
if (song != NULL) {
info.id = mpd_song_get_id(song);
info.artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
info.album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0);
info.title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
if (get_albumart) {
string uri(mpd_song_get_uri(song));
int ret = 0;
int start_at = 0;
uint8_t buf[CHUNK_SIZE];
Glib::RefPtr<Gdk::PixbufLoader> loader = Gdk::PixbufLoader::create();
int done = 0;
while(!done) {
ret = mpd_run_readpicture(conn, uri.c_str(), start_at, (void*)buf, CHUNK_SIZE);
if (ret > 0) {
start_at += ret;
loader->write( buf, ret);
} else {
done = 1;
}
}
try {
loader->close();
info.albumart = loader->get_pixbuf();
} catch ( Glib::Error& e ) {
info.albumart = Gdk::Pixbuf::create(Gdk::Colorspace::COLORSPACE_RGB, 1, 8, 1,1);
info.albumart->fill(0);
}
}
mpd_song_free(song);
} else {
handle_error("get_song_info mpd_recv_song");
}
}
bool MpdClient::io_event_handler(Glib::IOCondition ioc) {
assert(Glib::IOCondition::IO_IN & ioc);
stop_idling();
song_info_t info;
get_song_info(info, false);
if (last_song_info.id != info.id) {
get_song_info(last_song_info, true);
song_change_signal.emit(last_song_info);
}
struct mpd_status *status;
status = mpd_run_status(conn);
if (status == NULL) {
handle_error("IO Event Handler Error Getting Status");
} else {
mpd_state new_state = mpd_status_get_state(status);
if ((new_state != last_state && last_state == MPD_STATE_STOP
&& new_state == MPD_STATE_PLAY)) {
song_change_signal.emit(last_song_info);
}
last_state = new_state;
mpd_status_free(status);
}
start_idling();
return true;
}
void MpdClient::attach_io_event_handler() {
Glib::RefPtr<Glib::MainContext> mc = ml->get_context();
Glib::RefPtr<Glib::IOSource> iosource = Glib::IOSource::create(
mpd_connection_get_fd(conn),
Glib::IOCondition::IO_IN);
sigc::slot<bool(Glib::IOCondition)> slot = sigc::mem_fun(this,& MpdClient::io_event_handler);
iosource->connect(slot);
iosource->attach(mc);
}
void MpdClient::stop_idling() {
mpd_run_noidle(conn);
}
void MpdClient::start_idling() {
if (!mpd_send_idle(conn)) {
handle_error("start_listening");
}
}
void MpdClient::state_to_name(mpd_state state, string &name) {
switch(state) {
case MPD_STATE_PAUSE:
name = "PAUSED";
break;
case MPD_STATE_PLAY:
name = "PLAYING";
break;
case MPD_STATE_STOP:
name = "STOPPED";
break;
default:
name = "UNKNOWN";
}
}
int MpdClient::next() {
struct mpd_status * status;
stop_idling();
status = mpd_run_status(conn);
if (status == NULL)
handle_error("Next Error Getting Status");
else {
if (mpd_status_get_state(status) == MPD_STATE_PLAY ||
mpd_status_get_state(status) == MPD_STATE_PAUSE) {
if (!mpd_run_next(conn)) {
handle_error("Next Error");
}
}
mpd_status_free(status);
}
start_idling();
return 0;
}
int MpdClient::previous() {
struct mpd_status * status;
stop_idling();
status = mpd_run_status(conn);
if (status == NULL)
handle_error("Previous Error Getting Status");
else {
if (mpd_status_get_state(status) == MPD_STATE_PLAY ||
mpd_status_get_state(status) == MPD_STATE_PAUSE) {
if (!mpd_run_previous(conn)) {
handle_error("Previous Error");
}
}
mpd_status_free(status);
}
start_idling();
return 0;
}
int MpdClient::pause() {
struct mpd_status * status;
stop_idling();
status = mpd_run_status(conn);
if (status == NULL) {
handle_error("Toggle Pause Error Getting Status");
} else {
if (mpd_status_get_state(status) == MPD_STATE_PLAY ||
mpd_status_get_state(status) == MPD_STATE_PAUSE) {
if (!mpd_run_toggle_pause(conn)) {
handle_error("Play/Pause Error");
}
} else {
if (!mpd_run_play(conn)) {
handle_error("Play Error");
}
}
mpd_status_free(status);
}
start_idling();
return 0;
}
int MpdClient::stop() {
stop_idling();
if (!mpd_run_stop(conn)) {
handle_error("Stop Error");
}
start_idling();
return 0;
}