1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include "mpd_func.h"
6 
7 char * mpd_host;
8 int mpd_port;
9 char * mpd_password = NULL;
10 
11 mpd_Connection * connection = NULL;
12 mpd_Status * status = NULL;
13 
14 int MpdIsErrored() {
15 	if(!connection->error) return 0;
16 	if(connection->error==MPD_ERROR_UNKHOST ||
17 			connection->error==MPD_ERROR_SYSTEM ||
18 			connection->error==MPD_ERROR_NOTMPD) {
19 		fprintf(stderr,"%s\n",connection->errorStr);
20 		exit(-1);
21 	}
22 	else if(connection->error==MPD_ERROR_TIMEOUT ||
23 			connection->error==MPD_ERROR_SENDING ||
24 			connection->error==MPD_ERROR_CONNCLOSED ||
25 			connection->error==MPD_ERROR_NORESPONSE ||
26 			connection->error==MPD_ERROR_CONNPORT) {
27 		/*fprintf(stderr,"%s\n",connection->errorStr);
28 		fprintf(stderr,"resetting connection\n");*/
29 		mpd_closeConnection(connection);
30 		connection = NULL;
31 	}
32 	else {
33 		fprintf(stderr,"%s\n",connection->errorStr);
34 		connection->error = 0;
35 	}
36 	return 1;
37 }
38 
39 void MpdPlay() {
40 	if(status && status->state==MPD_STATUS_STATE_PAUSE) {
41 		MpdPause();
42 		return;
43 	}
44 
45 	mpd_sendPlayCommand(connection,MPD_PLAY_AT_BEGINNING);
46 	if(MpdIsErrored()) return;
47 	mpd_finishCommand(connection);
48 	if(MpdIsErrored()) return;
49 }
50 
51 void MpdStop() {
52 	mpd_sendStopCommand(connection);
53 	if(MpdIsErrored()) return;
54 	mpd_finishCommand(connection);
55 	if(MpdIsErrored()) return;
56 }
57 
58 void MpdPause() {
59 	mpd_sendPauseCommand(connection);
60 	if(MpdIsErrored()) return;
61 	mpd_finishCommand(connection);
62 	if(MpdIsErrored()) return;
63 }
64 
65 void MpdEject() {
66 }
67 
68 void MpdPrev() {
69 	mpd_sendPrevCommand(connection);
70 	if(MpdIsErrored()) return;
71 	mpd_finishCommand(connection);
72 	if(MpdIsErrored()) return;
73 }
74 
75 void MpdNext() {
76 	mpd_sendNextCommand(connection);
77 	if(MpdIsErrored()) return;
78 	mpd_finishCommand(connection);
79 	if(MpdIsErrored()) return;
80 }
81 
82 void MpdFastr() {
83 	if(!status || status->state==MPD_STATUS_STATE_STOP) return;
84 	mpd_sendSeekCommand(connection,status->song,status->elapsedTime-10);
85 	if(MpdIsErrored()) return;
86 	mpd_finishCommand(connection);
87 	if(MpdIsErrored()) return;
88 	MpdStatus();
89 }
90 
91 void MpdFastf() {
92 	if(!status || status->state==MPD_STATUS_STATE_STOP) return;
93 	mpd_sendSeekCommand(connection,status->song,status->elapsedTime+10);
94 	if(MpdIsErrored()) return;
95 	mpd_finishCommand(connection);
96 	if(MpdIsErrored()) return;
97 	MpdStatus();
98 }
99 
100 int MpdGetVolume() {
101 	if(status && status->volume!=MPD_STATUS_NO_VOLUME) {
102 		return status->volume;
103 	}
104 	return 0;
105 }
106 
107 void MpdToggleRandom() {
108 	mpd_sendRandomCommand(connection,!status->random);
109 	if(MpdIsErrored()) return;
110 	mpd_finishCommand(connection);
111 	if(MpdIsErrored()) return;
112 }
113 
114 void MpdToggleRepeat() {
115 	mpd_sendRepeatCommand(connection,!status->repeat);
116 	if(MpdIsErrored()) return;
117 	mpd_finishCommand(connection);
118 	if(MpdIsErrored()) return;
119 }
120 
121 int MpdStatus() {
122 	if(!connection) {
123 		connection = mpd_newConnection(mpd_host,mpd_port,10);
124 		if(MpdIsErrored()) return 0;
125 		if(mpd_password && strlen(mpd_password)) {
126 			mpd_sendPasswordCommand(connection,mpd_password);
127 			if(MpdIsErrored()) return 0;
128 			mpd_finishCommand(connection);
129 			if(MpdIsErrored()) return 0;
130 		}
131 	}
132 	if(status) mpd_freeStatus(status);
133 	status = mpd_getStatus(connection);
134 	if(MpdIsErrored()) return 0;
135 	mpd_finishCommand(connection);
136 	if(MpdIsErrored()) return 0;
137 	if(!status) return 0;
138 
139 	return 1;
140 }
141 
142 int MpdPauseStatus() {
143 	if(status) return (status->state == MPD_STATUS_STATE_PAUSE);
144 	return 0;
145 }
146 
147 int MpdRandomStatus() {
148 	if(status) return status->random;
149 	return 0;
150 }
151 
152 int MpdRepeatStatus() {
153 	if(status) return status->repeat;
154 	return 0;
155 }
156 
157 int MpdPlayStatus() {
158 	if(status) return (status->state == MPD_STATUS_STATE_PLAY);
159 	return 0;
160 }
161 
162 int MpdGetTrack() {
163 	if(status) return status->song;
164 	return 0;
165 }
166 
167 char *MpdGetTitle() {
168 	static char * ret = NULL;
169 	static unsigned long lastVersion = 0;
170 	static int lastSong = -1;
171 	mpd_InfoEntity * entity;
172 
173 
174 	if(!status || (status->state!=MPD_STATUS_STATE_PLAY &&
175 			status->state!=MPD_STATUS_STATE_PAUSE)) {
176 		if(ret) free(ret);
177 		lastVersion = 0;
178 		lastSong = -1;
179 		ret = strdup("");
180 		return ret;
181 	}
182 
183 	if(ret && lastVersion == status->playlist &&
184 			lastSong == status->song) {
185 		return ret;
186 	}
187 
188 	lastVersion = status->playlist;
189 	lastSong = status->song;
190 
191 	if(ret) free(ret);
192 
193 	mpd_sendPlaylistInfoCommand(connection,status->song);
194 	if(MpdIsErrored()) {
195 		ret = strdup("");
196 		return ret;
197 	}
198 	if((entity = mpd_getNextInfoEntity(connection))) {
199 		if(entity->info.song->artist && entity->info.song->title) {
200 			ret = malloc(strlen(entity->info.song->artist) +
201 					strlen(entity->info.song->title) + 4);
202 			sprintf(ret,"%s - %s",entity->info.song->artist,
203 					entity->info.song->title);
204 		}
205 		else ret = strdup(entity->info.song->file);
206 		mpd_freeInfoEntity(entity);
207 	}
208 	if(MpdIsErrored()) return ret;
209 	mpd_finishCommand(connection);
210 	if(MpdIsErrored()) return ret;
211 	return ret;
212 }
213 
214 int MpdGetTime() {
215 	if(status) return status->elapsedTime;
216 	return 0;
217 }
218 
219 void MpdSetVolume(int v) {
220 	if(!status || status->volume == MPD_STATUS_NO_VOLUME) return;
221 
222 	mpd_sendSetvolCommand(connection,v);
223 	if(connection->error) {
224 		fprintf(stderr,"%s\n",connection->errorStr);
225 	}
226 	mpd_finishCommand(connection);
227 	if(connection->error) {
228 		fprintf(stderr,"%s\n",connection->errorStr);
229 	}
230 	MpdStatus();
231 }
232