ccb08359009ff880587e52c25d665cf24cbeac3f
1 /*
2 * $Id: mpc.c,v 1.5 2004/03/17 23:19:21 kalle Exp $
3 *
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <time.h>
10 #include <string.h>
11 #include <glib.h>
13 #include "config.h"
14 #include "support.h"
15 #include "libmpdclient.h"
16 #include "mpc.h"
17 #include "options.h"
19 void
20 mpc_update_song(mpd_client_t *c)
21 {
22 mpd_InfoEntity *entity;
24 if( c->song )
25 {
26 mpd_freeSong(c->song);
27 c->song = NULL;
28 }
30 mpd_sendPlaylistInfoCommand(c->connection, c->status->song);
31 while( (entity=mpd_getNextInfoEntity(c->connection)) )
32 {
33 mpd_Song *song = entity->info.song;
35 if(c->connection->error)
36 {
37 fprintf(stderr,"error: %s\n",c->connection->errorStr);
38 exit(EXIT_FAILURE);
39 }
40 if(entity->type!=MPD_INFO_ENTITY_TYPE_SONG) {
41 mpd_freeInfoEntity(entity);
42 fprintf(stderr,
43 "error: type != MPD_INFO_ENTITY_TYPE_SONG [%d]\n",
44 entity->type);
45 exit(EXIT_FAILURE);
46 }
47 c->song = mpd_songDup(song);
48 mpd_freeInfoEntity(entity);
49 }
50 }
52 int
53 mpc_close(mpd_client_t *c)
54 {
55 if( c->connection )
56 mpd_closeConnection(c->connection);
57 if( c->cwd )
58 free( c->cwd );
60 return 0;
61 }
63 mpd_client_t *
64 mpc_connect(char *host, int port)
65 {
66 mpd_Connection *connection;
67 mpd_client_t *c;
69 connection = mpd_newConnection(host, port, 10);
70 if( connection==NULL )
71 {
72 fprintf(stderr, "mpd_newConnection to %s:%d failed!\n", host, port);
73 exit(EXIT_FAILURE);
74 }
76 c = malloc(sizeof(mpd_client_t));
77 memset(c, 0, sizeof(mpd_client_t));
78 c->connection = connection;
79 c->cwd = strdup("");
81 return c;
82 }
84 int
85 mpc_error(mpd_client_t *c)
86 {
87 if( c == NULL || c->connection == NULL )
88 return 1;
89 if( c->connection->error )
90 return 1;
92 return 0;
93 }
95 char *
96 mpc_error_str(mpd_client_t *c)
97 {
98 if( c == NULL || c->connection == NULL )
99 return "Not connected";
101 if( c->connection && c->connection->errorStr )
102 return c->connection->errorStr;
104 return NULL;
105 }
109 int
110 mpc_free_playlist(mpd_client_t *c)
111 {
112 GList *list;
114 if( c==NULL || c->playlist==NULL )
115 return -1;
117 list=g_list_first(c->playlist);
119 while( list!=NULL )
120 {
121 mpd_Song *song = (mpd_Song *) list->data;
123 mpd_freeSong(song);
124 list=list->next;
125 }
126 g_list_free(c->playlist);
127 c->playlist=NULL;
128 c->playlist_length=0;
130 return 0;
131 }
133 int
134 mpc_update_playlist(mpd_client_t *c)
135 {
136 mpd_InfoEntity *entity;
138 // fprintf(stderr, "mpc_update_playlist(): status->playlist = %d\n", c->status->playlist);
140 if( c->playlist )
141 mpc_free_playlist(c);
143 c->playlist_length=0;
144 mpd_sendPlaylistInfoCommand(c->connection,-1);
145 while( (entity=mpd_getNextInfoEntity(c->connection)) )
146 {
147 if(entity->type==MPD_INFO_ENTITY_TYPE_SONG)
148 {
149 mpd_Song *song = mpd_songDup(entity->info.song);
151 c->playlist = g_list_append(c->playlist, (gpointer) song);
152 c->playlist_length++;
153 }
154 mpd_freeInfoEntity(entity);
155 }
156 c->playlist_id = c->status->playlist;
157 c->playlist_updated = 1;
158 c->song_id = -1;
160 return 0;
161 }
163 int
164 mpc_playlist_get_song_index(mpd_client_t *c, char *filename)
165 {
166 GList *list = c->playlist;
167 int i=0;
169 while( list )
170 {
171 mpd_Song *song = (mpd_Song *) list->data;
172 if( strcmp(song->file, filename ) == 0 )
173 return i;
174 list=list->next;
175 i++;
176 }
177 return -1;
178 }
180 mpd_Song *
181 mpc_playlist_get_song(mpd_client_t *c, int n)
182 {
183 return (mpd_Song *) g_list_nth_data(c->playlist, n);
184 }
186 char *
187 mpc_get_song_name(mpd_Song *song)
188 {
189 if( song->title )
190 {
191 if( song->artist )
192 {
193 static char buf[512];
195 snprintf(buf, 512, "%s - %s", song->artist, song->title);
196 return utf8(buf);
197 }
198 else
199 {
200 return utf8(song->title);
201 }
202 }
203 return utf8(basename(song->file));
204 }
206 int
207 mpc_update(mpd_client_t *c)
208 {
209 if( c->status )
210 {
211 mpd_freeStatus(c->status);
212 }
214 c->status = mpd_getStatus(c->connection);
216 // if( c->playlist == NULL || c->playlist_id!=c->status->playlist )
217 if( c->playlist_id!=c->status->playlist )
218 mpc_update_playlist(c);
220 // if( c->song == NULL || c->status->song != c->song_id )
221 if( c->status->song != c->song_id )
222 {
223 c->song = mpc_playlist_get_song(c, c->status->song);
224 c->song_id = c->status->song;
225 c->song_updated = 1;
226 }
228 return 0;
229 }
236 int
237 mpc_free_filelist(mpd_client_t *c)
238 {
239 GList *list;
241 if( c==NULL || c->filelist==NULL )
242 return -1;
244 list=g_list_first(c->filelist);
246 while( list!=NULL )
247 {
248 filelist_entry_t *entry = list->data;
250 if( entry->entity )
251 mpd_freeInfoEntity(entry->entity);
252 free(entry);
253 list=list->next;
254 }
255 g_list_free(c->filelist);
256 c->filelist=NULL;
257 c->filelist_length=0;
259 return 0;
260 }
264 int
265 mpc_update_filelist(mpd_client_t *c)
266 {
267 mpd_InfoEntity *entity;
269 if( c->filelist )
270 mpc_free_filelist(c);
272 c->filelist_length=0;
274 // mpd_sendListallCommand(conn,"");
275 mpd_sendLsInfoCommand(c->connection, c->cwd);
277 if( c->cwd && c->cwd[0] )
278 {
279 /* add a dummy entry for ./.. */
280 filelist_entry_t *entry = malloc(sizeof(filelist_entry_t));
281 memset(entry, 0, sizeof(filelist_entry_t));
282 entry->entity = NULL;
283 c->filelist = g_list_append(c->filelist, (gpointer) entry);
284 c->filelist_length++;
285 }
287 while( (entity=mpd_getNextInfoEntity(c->connection)) )
288 {
289 filelist_entry_t *entry = malloc(sizeof(filelist_entry_t));
291 memset(entry, 0, sizeof(filelist_entry_t));
292 entry->entity = entity;
293 c->filelist = g_list_append(c->filelist, (gpointer) entry);
294 c->filelist_length++;
295 }
297 c->filelist_updated = 1;
299 mpd_finishCommand(c->connection);
301 mpc_filelist_set_selected(c);
303 return 0;
304 }
306 int
307 mpc_filelist_set_selected(mpd_client_t *c)
308 {
309 GList *list = c->filelist;
311 while( list )
312 {
313 filelist_entry_t *entry = list->data;
314 mpd_InfoEntity *entity = entry->entity ;
316 if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
317 {
318 mpd_Song *song = entity->info.song;
320 if( mpc_playlist_get_song_index(c, song->file) >= 0 )
321 entry->selected = 1;
322 else
323 entry->selected = 0;
324 }
326 list=list->next;
327 }
328 return 0;
329 }