/******************** Song ingo retrival ************************************/ static int getSong(void) { mpd_Status *status; mpd_InfoEntity *info; /* Get status */ mpd_sendStatusCommand(D.conn); if (error()) return 0; status = mpd_getStatus(D.conn); if (error()) return 0; pdie_on(!status, "get status"); mpd_nextListOkCommand(D.conn); if (error()) { mpd_freeStatus(status); return 0; } /* Copy status */ D.cur.state = status->state; D.cur.songid = status->songid; D.cur.pos = status->elapsedTime; D.cur.len = status->totalTime; mpd_freeStatus(status); /* Same song, return */ if (D.cur.songid == D.old.songid) { return 1; } if (D.info) { mpd_freeInfoEntity(D.info); D.info = 0; } /* Get song */ mpd_sendCurrentSongCommand(D.conn); if (error()) { return 0; } info = mpd_getNextInfoEntity(D.conn); if (error()) { return 0; } if (!info) { return D.cur.state == MPD_STATUS_STATE_PLAY || D.cur.state == MPD_STATUS_STATE_PAUSE; } if (info->type != MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeInfoEntity(info); return 0; } D.info = info; return 1; }
/* ================= MPD_Status - show info about currently playing song ================= */ void MPD_Status(void) { mpd_Status *status; mpd_InfoEntity *entity = NULL; mpd_Song *song = 0; char title[MP3_MAXSONGTITLE]; if(!MP3_Status()) return; mpd_sendStatusCommand(conn); if( (status = mpd_getStatus(conn)) == NULL) { Com_Printf("MPD: %s\n", conn->errorStr); mpd_finishCommand(conn); return; } mpd_finishCommand(conn); if( status->state == MPD_STATUS_STATE_STOP) { /* Playback stopped */ Com_Printf("MPD: not playing\n"); mpd_freeStatus(status); return; } if( status->state == MPD_STATUS_STATE_PLAY || MPD_STATUS_STATE_PAUSE ) { mpd_sendCurrentSongCommand(conn); while((entity = mpd_getNextInfoEntity(conn))) { if(entity->type != MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeInfoEntity(entity); continue; } song = entity->info.song; if(song->title == NULL || song->artist == NULL) Com_sprintf(title, sizeof(title), "%s [%i:%02i]", (song->title) ? song->title : song->file, status->totalTime / 60, status->totalTime % 60); else Com_sprintf(title, sizeof(title), "%s - %s [%i:%02i]", song->artist, song->title, status->totalTime / 60, status->totalTime % 60); COM_MakePrintable(title); Com_Printf("%s\n", title); mpd_freeInfoEntity(entity); break; } mpd_finishCommand(conn); } mpd_freeStatus(status); }
void mpdGetSongTitle() { mpd_InfoEntity * entity; mpd_sendCommandListOkBegin(conn); mpd_sendStatusCommand(conn); mpd_sendCurrentSongCommand(conn); mpd_sendCommandListEnd(conn); if((status = mpd_getStatus(conn))==NULL) { fprintf(stderr,"%s\n",conn->errorStr); mpd_closeConnection(conn); } songname = (char *)malloc(100); mpd_nextListOkCommand(conn); while((entity = mpd_getNextInfoEntity(conn))) { mpd_Song * song = entity->info.song; if(entity->type!=MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeInfoEntity(entity); continue; } if(song->artist) { strcpy(songname, " ["); strcat(songname, song->artist); strcat(songname, " - "); } if(song->album) { strcat(songname, song->album); strcat(songname, " - "); } if(song->title) { songname = strcat(songname, song->title); songname = strcat(songname, "] "); } mpd_freeInfoEntity(entity); } if(conn->error) { fprintf(stderr,"%s\n",conn->errorStr); mpd_closeConnection(conn); } mpd_finishCommand(conn); if(conn->error) { fprintf(stderr,"%s\n",conn->errorStr); mpd_closeConnection(conn); } }
static char *MPD_SongTitle ( void ) { mpd_Status *status; mpd_InfoEntity *entity = NULL; mpd_Song *song = 0; static char title[MP3_MAXSONGTITLE]; title[0] = 0; if(!MP3_Status()) return title; mpd_sendCommandListOkBegin(conn); mpd_sendStatusCommand(conn); mpd_sendCurrentSongCommand(conn); mpd_sendCommandListEnd(conn); if( (status = mpd_getStatus(conn)) == NULL) { return title; } mpd_nextListOkCommand(conn); while((entity = mpd_getNextInfoEntity(conn))) { if(entity->type != MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeInfoEntity(entity); continue; } song = entity->info.song; if(song->title == NULL || song->artist == NULL) Com_sprintf(title, sizeof(title), "%s [%i:%02i]", song->file, status->totalTime / 60, status->totalTime % 60); else Com_sprintf(title, sizeof(title), "%s - %s [%i:%02i]", song->artist, song->title, status->totalTime / 60, status->totalTime % 60); COM_MakePrintable(title); mpd_freeInfoEntity(entity); break; } mpd_freeStatus(status); mpd_finishCommand(conn); return title; }
int cmd_current(mpd_unused int argc, mpd_unused char ** argv, mpd_Connection *conn) { mpd_Status * status; mpd_InfoEntity * entity; mpd_sendCommandListOkBegin(conn); printErrorAndExit(conn); mpd_sendStatusCommand(conn); printErrorAndExit(conn); mpd_sendCurrentSongCommand(conn); printErrorAndExit(conn); mpd_sendCommandListEnd(conn); printErrorAndExit(conn); status = mpd_getStatus(conn); printErrorAndExit(conn); if (status->state == MPD_STATUS_STATE_PLAY || status->state == MPD_STATUS_STATE_PAUSE) { mpd_nextListOkCommand(conn); printErrorAndExit(conn); while((entity = mpd_getNextInfoEntity(conn))) { struct mpd_song *song = entity->info.song; if(entity->type!=MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeInfoEntity(entity); continue; } pretty_print_song(song); printf("\n"); mpd_freeInfoEntity(entity); break; } printErrorAndExit(conn); mpd_finishCommand(conn); printErrorAndExit(conn); } mpd_freeStatus(status); return 0; }
static void *update_mpd_thread(void *arg) { static mpd_Connection *conn = NULL; mpd_Status *status; mpd_InfoEntity *entity; timed_thread *me = *(timed_thread **)arg; const char *emptystr = ""; while (1) { if (!conn) conn = mpd_newConnection(mpd_host, mpd_port, 10); if (*mpd_password) { mpd_sendPasswordCommand(conn, mpd_password); mpd_finishCommand(conn); } timed_thread_lock(me); if (conn->error || conn == NULL) { NORM_ERR("MPD error: %s\n", conn->errorStr); mpd_closeConnection(conn); conn = 0; clear_mpd(); mpd_info.status = "MPD not responding"; timed_thread_unlock(me); if (timed_thread_test(me, 0)) { timed_thread_exit(me); } continue; } mpd_sendStatusCommand(conn); if ((status = mpd_getStatus(conn)) == NULL) { NORM_ERR("MPD error: %s\n", conn->errorStr); mpd_closeConnection(conn); conn = 0; clear_mpd(); mpd_info.status = "MPD not responding"; timed_thread_unlock(me); if (timed_thread_test(me, 0)) { timed_thread_exit(me); } continue; } mpd_finishCommand(conn); if (conn->error) { // fprintf(stderr, "%s\n", conn->errorStr); mpd_closeConnection(conn); conn = 0; timed_thread_unlock(me); if (timed_thread_test(me, 0)) { timed_thread_exit(me); } continue; } mpd_info.vol = status->volume; if (status->random == 0) { mpd_info.random = "Off"; } else if (status->random == 1) { mpd_info.random = "On"; } else { mpd_info.random = ""; } if (status->repeat == 0) { mpd_info.repeat = "Off"; } else if (status->repeat == 1) { mpd_info.repeat = "On"; } else { mpd_info.repeat = ""; } /* if (status->error) { printf("error: %s\n", status->error); } */ switch (status->state) { case MPD_STATUS_STATE_PLAY: mpd_info.status = "Playing"; break; case MPD_STATUS_STATE_STOP: mpd_info.status = "Stopped"; break; case MPD_STATUS_STATE_PAUSE: mpd_info.status = "Paused"; break; default: mpd_info.status = ""; clear_mpd(); break; } if (status->state == MPD_STATUS_STATE_PLAY || status->state == MPD_STATUS_STATE_PAUSE) { mpd_info.is_playing = 1; mpd_info.bitrate = status->bitRate; mpd_info.progress = (float) status->elapsedTime / status->totalTime; mpd_info.elapsed = status->elapsedTime; mpd_info.length = status->totalTime; } else { mpd_info.progress = 0; mpd_info.is_playing = 0; mpd_info.elapsed = 0; } if (conn->error) { // fprintf(stderr, "%s\n", conn->errorStr); mpd_closeConnection(conn); conn = 0; timed_thread_unlock(me); if (timed_thread_test(me, 0)) { timed_thread_exit(me); } continue; } mpd_sendCurrentSongCommand(conn); while ((entity = mpd_getNextInfoEntity(conn))) { mpd_Song *song = entity->info.song; if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeInfoEntity(entity); continue; } #define SONGSET(x) { \ free(mpd_info.x); \ if(song->x) \ mpd_info.x = strmdup(song->x); \ else \ mpd_info.x = strmdup(emptystr); \ } SONGSET(artist); SONGSET(albumartist); SONGSET(album); SONGSET(title); SONGSET(date); SONGSET(track); SONGSET(name); SONGSET(file); #undef SONGSET if (entity != NULL) { mpd_freeInfoEntity(entity); entity = NULL; } } mpd_finishCommand(conn); if (conn->error) { // fprintf(stderr, "%s\n", conn->errorStr); mpd_closeConnection(conn); conn = 0; timed_thread_unlock(me); if (timed_thread_test(me, 0)) { timed_thread_exit(me); } continue; } timed_thread_unlock(me); if (conn->error) { // fprintf(stderr, "%s\n", conn->errorStr); mpd_closeConnection(conn); conn = 0; if (timed_thread_test(me, 0)) { timed_thread_exit(me); } continue; } mpd_freeStatus(status); /* if (conn) { mpd_closeConnection(conn); conn = 0; } */ if (timed_thread_test(me, 0)) { timed_thread_exit(me); } continue; } /* never reached */ }
static void _mpdule_update_song (Instance * inst) { mpd_Connection *mpd; Evas_Object *mpdule; Evas_Object *o_popup; if (!inst->mpd) return; mpd = inst->mpd; mpdule = inst->mpdule; o_popup = inst->o_popup; mpd_sendStatusCommand (mpd); if (mpd->error == 0) { mpd_Status *status = mpd_getStatus (mpd); if (status) { if (status->state == MPD_STATUS_STATE_UNKNOWN) { edje_object_part_text_set (mpdule, "mpdule.status", D_ ("Unknown")); edje_object_part_text_set (o_popup, "mpdule.status", D_ ("Unknown")); } else if (status->state == MPD_STATUS_STATE_STOP) { edje_object_part_text_set (mpdule, "mpdule.status", D_ ("Stopped")); edje_object_part_text_set (o_popup, "mpdule.status", D_ ("Stopped")); } else if (status->state == MPD_STATUS_STATE_PLAY) { edje_object_part_text_set (mpdule, "mpdule.status", D_ ("Playing")); edje_object_part_text_set (o_popup, "mpdule.status", D_ ("Playing")); } else if (status->state == MPD_STATUS_STATE_PAUSE) { edje_object_part_text_set (mpdule, "mpdule.status", D_ ("Paused")); edje_object_part_text_set (o_popup, "mpdule.status", D_ ("Paused")); } if (status->state > MPD_STATUS_STATE_STOP) { mpd_sendCurrentSongCommand (mpd); mpd_InfoEntity *entity = NULL; while ((entity = mpd_getNextInfoEntity (mpd))) { if (entity->type == MPD_INFO_ENTITY_TYPE_SONG && entity->info.song->id == status->songid) { mpd_Song *song = entity->info.song; if (song->artist) { edje_object_part_text_set (mpdule, "mpdule.artist", song->artist); edje_object_part_text_set (o_popup, "mpdule.artist", song->artist); } else { edje_object_part_text_set (mpdule, "mpdule.artist", ""); edje_object_part_text_set (o_popup, "mpdule.artist", ""); } if (song->title) { edje_object_part_text_set (mpdule, "mpdule.title", song->title); edje_object_part_text_set (o_popup, "mpdule.title", song->title); } else { edje_object_part_text_set (mpdule, "mpdule.title", ""); edje_object_part_text_set (o_popup, "mpdule.title", ""); } if (song->album) { edje_object_part_text_set (mpdule, "mpdule.album", song->album); edje_object_part_text_set (o_popup, "mpdule.album", song->album); } else { edje_object_part_text_set (mpdule, "mpdule.album", ""); edje_object_part_text_set (o_popup, "mpdule.album", ""); } if (song->track) { edje_object_part_text_set (mpdule, "mpdule.track", song->track); edje_object_part_text_set (o_popup, "mpdule.track", song->track); } else { edje_object_part_text_set (mpdule, "mpdule.track", ""); edje_object_part_text_set (o_popup, "mpdule.track", ""); } if (song->date) { edje_object_part_text_set (mpdule, "mpdule.date", song->date); edje_object_part_text_set (o_popup, "mpdule.date", song->date); } else { edje_object_part_text_set (mpdule, "mpdule.date", ""); edje_object_part_text_set (o_popup, "mpdule.date", ""); } if (song->genre) { edje_object_part_text_set (mpdule, "mpdule.genre", song->genre); edje_object_part_text_set (o_popup, "mpdule.genre", song->genre); } else { edje_object_part_text_set (mpdule, "mpdule.genre", ""); edje_object_part_text_set (o_popup, "mpdule.genre", ""); } if (song->composer) { edje_object_part_text_set (mpdule, "mpdule.composer", song->composer); edje_object_part_text_set (o_popup, "mpdule.composer", song->composer); } else { edje_object_part_text_set (mpdule, "mpdule.composer", ""); edje_object_part_text_set (o_popup, "mpdule.composer", ""); } if (song->time) { //char * songtime; //sprintf(songtime, "%i", song->time); //edje_object_part_text_set (mpdule, "mpdule.time", songtime); //edje_object_part_text_set (o_popup, "mpdule.time", songtime); } else { edje_object_part_text_set (mpdule, "mpdule.time", ""); edje_object_part_text_set (o_popup, "mpdule.time", ""); } if (song->file) { edje_object_part_text_set (mpdule, "mpdule.file", song->file); edje_object_part_text_set (o_popup, "mpdule.file", song->file); } else { edje_object_part_text_set (mpdule, "mpdule.file", ""); edje_object_part_text_set (o_popup, "mpdule.file", ""); } } mpd_freeInfoEntity (entity); } } mpd_freeStatus (status); } } else { _mpdule_disconnect (inst); _mpdule_connect (inst); } }