std::string Mpd::getTag(const char *_album, mpd_tag_type type) { std::string tagRet = ""; mpd_song *song; if (!mpd_search_db_songs(conn, false)) { err(); return NULL; } if (!mpd_search_add_tag_constraint(conn, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM, _album)) { err(); return NULL; } if (!mpd_search_commit(conn)) { err(); return NULL; } if (type == MPD_TAG_UNKNOWN) { if ((song = mpd_recv_song(conn)) != NULL) { if (mpd_song_get_uri(song) != NULL) tagRet = mpd_song_get_uri(song); else tagRet = ""; } } else if ((song = mpd_recv_song(conn)) != NULL) { if (mpd_song_get_tag(song, type, 0) != NULL) tagRet = mpd_song_get_tag(song, type, 0); else tagRet = ""; } mpd_song_free(song); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { err(); return NULL; } if (!mpd_response_finish(conn)) { err(); return NULL; } return tagRet; }
void songlist_update(void) { struct mpd_song *song; if (!mpd_send_list_queue_meta(conn)) printErrorAndExit(conn); int i = 0; while ((song = mpd_recv_song(conn)) != NULL && i < MAX_SONGLIST_STORE_LENGTH) { pretty_copy(songlist->meta[i].title, get_song_tag(song, MPD_TAG_TITLE), 512, -1); pretty_copy(songlist->meta[i].pretty_title, get_song_tag(song, MPD_TAG_TITLE), 128, 26); pretty_copy(songlist->meta[i].artist, get_song_tag(song, MPD_TAG_ARTIST), 128, 14); pretty_copy(songlist->meta[i].album, get_song_tag(song, MPD_TAG_ALBUM), 128, -1); songlist->meta[i].id = i + 1; ++i; mpd_song_free(song); } songlist->length = i; my_finishCommand(conn); }
int cmd_tab ( int argc, char ** argv, struct mpd_connection *conn ) { struct mpd_song *song; char empty[] = ""; char *dir = empty; char *tmp = NULL; if (argc == 1) { if (strrchr(argv[0], '/')) { dir = strdup(argv[0]); if (!dir) return 0; tmp = strrchr(dir, '/'); if (tmp) *tmp = '\0'; // XXX: It's unpossible for tmp to be NULL. } } if (!mpd_send_list_all(conn, dir)) printErrorAndExit(conn); if (*dir) free(dir); while ((song = mpd_recv_song(conn)) != NULL) { if (argc != 1 || strncmp(mpd_song_get_uri(song), argv[0], strlen(argv[0])) == 0) printf("%s\n", charset_from_utf8(mpd_song_get_uri(song))); mpd_song_free(song); } my_finishCommand(conn); return 0; }
static enum mpd_state lmc_current(struct mpd_song **song_r, unsigned *elapsed_r) { struct mpd_status *status; enum mpd_state state; struct mpd_song *song; assert(g_mpd != NULL); mpd_command_list_begin(g_mpd, true); mpd_send_status(g_mpd); mpd_send_current_song(g_mpd); mpd_command_list_end(g_mpd); status = mpd_recv_status(g_mpd); if (!status) { lmc_failure(); return MPD_STATE_UNKNOWN; } state = mpd_status_get_state(status); *elapsed_r = mpd_status_get_elapsed_time(status); mpd_status_free(status); if (state != MPD_STATE_PLAY) { if (!mpd_response_finish(g_mpd)) { lmc_failure(); return MPD_STATE_UNKNOWN; } return state; } if (!mpd_response_next(g_mpd)) { lmc_failure(); return MPD_STATE_UNKNOWN; } song = mpd_recv_song(g_mpd); if (song == NULL) { if (!mpd_response_finish(g_mpd)) { lmc_failure(); return MPD_STATE_UNKNOWN; } return MPD_STATE_UNKNOWN; } if (!mpd_response_finish(g_mpd)) { mpd_song_free(song); lmc_failure(); return MPD_STATE_UNKNOWN; } *song_r = song; return MPD_STATE_PLAY; }
Song Connection::GetSong(const std::string &path) { prechecksNoCommandsList(); mpd_send_list_all_meta(m_connection.get(), path.c_str()); mpd_song *s = mpd_recv_song(m_connection.get()); mpd_response_finish(m_connection.get()); checkErrors(); return Song(s); }
Song Connection::GetCurrentSong() { prechecksNoCommandsList(); mpd_send_current_song(m_connection.get()); mpd_song *s = mpd_recv_song(m_connection.get()); mpd_response_finish(m_connection.get()); checkErrors(); return Song(s); }
char * getmpdstat() { struct mpd_song * song = NULL; const char * title = NULL; const char * artist = NULL; const char * uri = NULL; const char * titletag = NULL; const char * artisttag = NULL; char * retstr = NULL; int elapsed = 0, total = 0; struct mpd_connection * conn ; if (!(conn = mpd_connection_new("localhost", 0, 30000)) || mpd_connection_get_error(conn)){ return smprintf(""); } mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); struct mpd_status* theStatus = mpd_recv_status(conn); if ((theStatus) && (mpd_status_get_state(theStatus) == MPD_STATE_PLAY)) { mpd_response_next(conn); song = mpd_recv_song(conn); titletag = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); artisttag = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); title = smprintf("%s",titletag); artist = smprintf("%s",artisttag); uri = smprintf("%s",mpd_song_get_uri(song)); elapsed = mpd_status_get_elapsed_time(theStatus); total = mpd_status_get_total_time(theStatus); mpd_song_free(song); /* If the song isn't tagged, then just use the filename */ if(artisttag == NULL && titletag == NULL) { retstr = smprintf("%s[%s%s%s | %s%.2d:%.2d%s/%.2d:%.2d%s]", colcyan, colyellow, uri, colcyan, colbyellow, elapsed/60, elapsed%60, colyellow, total/60, total%60, colcyan); } else { retstr = smprintf("%s[%s%s%s | %s%s%s | %s%.2d:%.2d%s/%.2d:%.2d%s]", colcyan, colyellow, artist, colcyan, colred, title, colcyan, colbyellow, elapsed/60, elapsed%60, colyellow, total/60, total%60, colcyan); } free((char*)title); free((char*)artist); free((char*)uri); } else retstr = smprintf(""); mpd_response_finish(conn); mpd_connection_free(conn); return retstr; }
char * get_mpd(void) { struct mpd_connection *con; struct mpd_status *status; struct mpd_song *song; int status_type; char *res; const char *artist = NULL, *title = NULL; con = mpd_connection_new(NULL, 0, 30000); if(mpd_connection_get_error(con)) { mpd_connection_free(con); return NO_MPD; } mpd_command_list_begin(con, true); mpd_send_status(con); mpd_send_current_song(con); mpd_command_list_end(con); status = mpd_recv_status(con); if(!status) { mpd_connection_free(con); return NO_MPD; } mpd_response_next(con); song = mpd_recv_song(con); title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); if(!title) title = mpd_song_get_uri(song); artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); status_type = mpd_status_get_state(status); switch(status_type) { case(MPD_STATE_PLAY): res = smprintf(MPD, "Playing", artist, title); break; case(MPD_STATE_PAUSE): res = smprintf(MPD, "Paused", artist, title); break; case(MPD_STATE_STOP): res = smprintf(MPD, "Stopped", artist, title); break; default: res = NO_MPD; break; } mpd_song_free(song); mpd_response_finish(con); mpd_status_free(status); mpd_connection_free(con); return res; }
int main() { char fullpath[100], text[100]; char *buf = NULL, *sub = NULL, *state = NULL; conn = mpd_connection_new(NULL, 0, 10000); notify = notify_notification_new(NULL, NULL, NULL); notify_init("mpdnotify"); while(mpd_run_idle_mask(conn, MPD_IDLE_PLAYER)) { mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); status = mpd_recv_status(conn); if(mpd_status_get_state(status) == MPD_STATE_PLAY) state = "Playing:"; else if(mpd_status_get_state(status) == MPD_STATE_PAUSE) state = "Paused:"; else state = "Stopped:"; mpd_response_next(conn); song = mpd_recv_song(conn); if(song) { artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); path = mpd_song_get_uri(song); sprintf(text, "%s - %s", artist, title); buf = fullpath; sub = strrchr(path, '/'); buf = strcpy(buf, mpdroot); buf = strncat(buf, path, strlen(path) - strlen(sub) + 1); buf = strcat(buf, albumart); icon = gdk_pixbuf_new_from_file(fullpath, NULL); if(icon) notify_notification_update(notify, state, text, NULL); else notify_notification_update(notify, state, text, "sound"); notify_notification_set_icon_from_pixbuf(notify, icon); notify_notification_show(notify, &gerror); mpd_song_free(song); } mpd_response_finish(conn); } mpd_connection_free(conn); notify_uninit(); return 0; }
static int fetch_items(MENU_SEARCH *menu,char *str,CLIENT_OBJECT *cli,DB_OBJECT *db) { if (!(str && strlen(str))) return 1; /* send query */ if (!mpd_search_db_songs(cli->conn,false)) clear_or_exit_on_error(cli->conn); if (!mpd_search_add_tag_constraint(cli->conn,MPD_OPERATOR_DEFAULT,MPD_TAG_TITLE,str)) clear_or_exit_on_error(cli->conn); if (!mpd_search_commit(cli->conn)) clear_or_exit_on_error(cli->conn); /* process response */ int block = 1024; menu->items = (MENU_SEARCH_ITEM*)calloc(block,sizeof(MENU_SEARCH_ITEM)); struct mpd_song *song; int i = 0; while ((song = mpd_recv_song(cli->conn))) { /* realloc if memory not enough */ if (i >= block) { block *= 2; menu->items = (MENU_SEARCH_ITEM*)realloc(menu->items,block*sizeof(MENU_SEARCH_ITEM)); } unsigned id = mpd_song_get_id(song); const char *uri = mpd_song_get_uri(song); const char *title = mpd_song_get_tag(song,MPD_TAG_TITLE,0); menu->items[i].id = id; menu->items[i].title = (char *)malloc((strlen(title)+1)*sizeof(char)); strcpy(menu->items[i].title,title); menu->items[i++].node = get_node_by_uri(db,uri); mpd_song_free(song); } if (mpd_connection_get_error(cli->conn) == MPD_ERROR_SUCCESS) mpd_response_finish(cli->conn); else clear_or_exit_on_error(cli->conn); menu->num = i; return 0; }
gboolean mpd_connect(void) { mpd.conn = mpd_connection_new(prefs.mpd_server, prefs.mpd_port, 10000); mpd.idle_source = 0; if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) { g_warning("Failed to connect to MPD: %s", mpd_connection_get_error_message(mpd.conn)); return FALSE; } else if (mpd_connection_cmp_server_version(mpd.conn, 0, 14, 0) < 0) { g_critical("MPD too old, please upgrade to 0.14 or newer"); return FALSE; } else { GIOChannel *channel; mpd_command_list_begin(mpd.conn, TRUE); if (prefs.mpd_password) mpd_send_password(mpd.conn, prefs.mpd_password); mpd_send_status(mpd.conn); mpd_send_current_song(mpd.conn); mpd_command_list_end(mpd.conn); mpd.status = mpd_recv_status(mpd.conn); if (!mpd_response_next(mpd.conn)) { mpd_report_error(); return FALSE; } mpd.song = mpd_recv_song(mpd.conn); if (!mpd_response_finish(mpd.conn)) { mpd_report_error(); return FALSE; } g_message("Connected to MPD"); irc_say("Connected to MPD"); mpd_send_idle_mask(mpd.conn, MPD_IDLE_PLAYER); channel = g_io_channel_unix_new( mpd_connection_get_fd(mpd.conn)); mpd.idle_source = g_io_add_watch(channel, G_IO_IN, (GIOFunc) mpd_parse, NULL); g_io_channel_unref(channel); return TRUE; } }
int mpd_search(char *buffer, char *searchstr) { int i = 0; char *cur = buffer; const char *end = buffer + MAX_SIZE; struct mpd_song *song; if(mpd_search_db_songs(mpd.conn, false) == false) RETURN_ERROR_AND_RECOVER("mpd_search_db_songs"); else if(mpd_search_add_any_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, searchstr) == false) RETURN_ERROR_AND_RECOVER("mpd_search_add_any_tag_constraint"); else if(mpd_search_commit(mpd.conn) == false) RETURN_ERROR_AND_RECOVER("mpd_search_commit"); else { cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"search\",\"data\":[ "); while((song = mpd_recv_song(mpd.conn)) != NULL) { cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"song\",\"uri\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_song_get_uri(song)); cur += json_emit_raw_str(cur, end - cur, ",\"album\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_get_album(song)); cur += json_emit_raw_str(cur, end - cur, ",\"artist\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_get_artist(song)); cur += json_emit_raw_str(cur, end - cur, ",\"duration\":"); cur += json_emit_int(cur, end - cur, mpd_song_get_duration(song)); cur += json_emit_raw_str(cur, end - cur, ",\"title\":"); cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song)); cur += json_emit_raw_str(cur, end - cur, "},"); mpd_song_free(song); /* Maximum results */ if(i++ >= 300) { cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"wrap\"},"); break; } } /* remove last ',' */ cur--; cur += json_emit_raw_str(cur, end - cur, "]}"); } return cur - buffer; }
int cmd_playlist(mpd_unused int argc, mpd_unused char **argv, struct mpd_connection *conn) { struct mpd_song *song; if (!mpd_send_list_queue_meta(conn)) printErrorAndExit(conn); while ((song = mpd_recv_song(conn)) != NULL) { pretty_print_song(song); mpd_song_free(song); printf("\n"); } my_finishCommand(conn); return 0; }
struct mpd_song * mpd_run_get_queue_song_id(struct mpd_connection *connection, unsigned id) { struct mpd_song *song; if (!mpd_run_check(connection) || !mpd_send_get_queue_song_id(connection, id)) return NULL; song = mpd_recv_song(connection); if (!mpd_response_finish(connection) && song != NULL) { mpd_song_free(song); return NULL; } return song; }
struct mpd_song * mpd_run_current_song(struct mpd_connection *connection) { struct mpd_song *song; if (!mpd_run_check(connection) || !mpd_send_current_song(connection)) return NULL; song = mpd_recv_song(connection); if (song == NULL) return NULL; if (!mpd_response_finish(connection)) { mpd_song_free(song); return NULL; } return song; }
int cmd_current(mpd_unused int argc, mpd_unused char ** argv, struct mpd_connection *conn) { if (options.wait) wait_current(conn); struct mpd_status *status; if (!mpd_command_list_begin(conn, true) || !mpd_send_status(conn) || !mpd_send_current_song(conn) || !mpd_command_list_end(conn)) printErrorAndExit(conn); status = mpd_recv_status(conn); if (status == NULL) printErrorAndExit(conn); if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { struct mpd_song *song; if (!mpd_response_next(conn)) printErrorAndExit(conn); song = mpd_recv_song(conn); if (song != NULL) { pretty_print_song(song); printf("\n"); mpd_song_free(song); } if (!mpd_response_finish(conn)) printErrorAndExit(conn); } mpd_status_free(status); return 0; }
static int find_songname_id(struct mpd_connection *conn, const char *s) { int res = -1; mpd_search_queue_songs(conn, false); const char *pattern = s; //charset_to_utf8(s); mpd_search_add_any_tag_constraint(conn, MPD_OPERATOR_DEFAULT, pattern); mpd_search_commit(conn); struct mpd_song *song = mpd_recv_song(conn); if (song != NULL) { res = mpd_song_get_id(song); mpd_song_free(song); } my_finishCommand(conn); return res; }
void cmd_np(irc_session_t* session, const char* cmd, const char* origin, char* args) { struct mpd_connection* conn; struct mpd_song* song; char master_nick[20]; const char* title; const char* album; const char* artist; int out_len = 20 + 18 /* master_nick + " is now playing: "*/; char* out_str; irc_target_get_nick(master_origin, master_nick, 20); conn = mpd_connection_new(NULL, 0, 30000); if(mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { log_and_print("Impossible de se connecter à MPD"); mpd_connection_free(conn); return; } mpd_send_current_song(conn); song = mpd_recv_song(conn); if(song == NULL) { out_len += strlen("THE GAME"); out_str = malloc(out_len * sizeof(char)); strcpy(out_str, master_nick); strcat(out_str, " is now playing: THE GAME"); irc_cmd_msg(session, channel, out_str); free(out_str); return; } title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0); artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); if(title) out_len += strlen(title) + 2; if(album) out_len += strlen(album) + 2; if(artist) out_len += strlen(artist) + 2; out_str = malloc(out_len * sizeof(char)); strcpy(out_str, master_nick); strcat(out_str, " is now playing: "); if(title) strcat(out_str, title); if(artist) { strcat(out_str, " - "); strcat(out_str, artist); } if(album) { strcat(out_str, " - "); strcat(out_str, album); } irc_cmd_msg(session, channel, out_str); free(out_str); mpd_song_free(song); mpd_connection_free(conn); }
void basic_state_checking(void) { int rep, ran, sgl, len, crt, vol, ply, btr; struct mpd_status *status; status = getStatus(conn); rep = mpd_status_get_repeat(status); ran = mpd_status_get_random(status); sgl = mpd_status_get_single(status); len = mpd_status_get_queue_length(status); crt = mpd_status_get_song_id(status); vol = mpd_status_get_volume(status); ply = mpd_status_get_state(status); btr = mpd_status_get_kbit_rate(status); basic_info->crt_time = mpd_status_get_elapsed_time(status); basic_info->total_time = mpd_status_get_total_time(status); mpd_status_free(status); if(rep != basic_info->repeat || ran != basic_info->random || sgl != basic_info->single || len != basic_info->total || crt != basic_info->current || vol != basic_info->volume || ply != basic_info->state) { basic_info->repeat = rep; basic_info->random = ran; basic_info->single = sgl; basic_info->total = len; basic_info->current = crt; basic_info->volume = vol; basic_info->state = ply; signal_all_wins(); } /* as many songs's bit rate varies while playing we refresh the bit rate display only when the relevant change greater than 0.2 */ if(abs(basic_info->bit_rate - btr) / (float)(btr + 1) > 0.2) { basic_info->bit_rate = btr; signal_win(BASIC_INFO); } /* get current song's name */ status = init_mpd_status(conn); strncpy(basic_info->format, "null", 16); *basic_info->crt_name = '\0'; if (basic_info->state == MPD_STATE_PLAY || basic_info->state == MPD_STATE_PAUSE) { struct mpd_song *song; if (!mpd_response_next(conn)) printErrorAndExit(conn); song = mpd_recv_song(conn); if (song != NULL) { strncpy(basic_info->crt_name, get_song_tag(song, MPD_TAG_TITLE), 512); strncpy(basic_info->format, get_song_format(song), 16); } mpd_song_free(song); } mpd_status_free(status); my_finishCommand(conn); }
int main(int argc, char *argv[]) { int opts; const char *short_opts = "t:r:b:a:p:s:h"; struct option long_opts[] = { { "title", required_argument, 0, 't' }, { "artist", required_argument, 0, 'r' }, { "album", required_argument, 0, 'b' }, { "addr", required_argument, 0, 'a' }, { "port", required_argument, 0, 'p' }, { "secret", required_argument, 0, 's' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 } }; unsigned int id = 0; struct mpd_song *song = NULL; struct mpd_connection *mpd = NULL; char *mpd_addr = getenv("MPD_HOST"); int mpd_port = getenv("MPD_PORT") ? atoi(getenv("MPD_PORT")) : 0; char *mpd_pass = NULL; char *title = NULL; char *artist = NULL; char *album = NULL; if (argc == 1) { help(); return -1; } while ((opts = getopt_long(argc, argv, short_opts, long_opts, 0)) != -1) { switch (opts) { case 't': { title = optarg; break; } case 'r': { artist = optarg; break; } case 'b': { album = optarg; break; } case 'a': { mpd_addr = optarg; break; } case 'p': { mpd_port = atoi(optarg); break; } case 's': { mpd_pass = optarg; break; } default : case 'h': { help(); return -1; } } } mpd = mpd_connection_new(mpd_addr, mpd_port, 30000); CHECK_MPD_CONN(mpd); if (mpd_pass != NULL) { mpd_run_password(mpd, mpd_pass); CHECK_MPD_CONN(mpd); } mpd_search_queue_songs(mpd, false); if (title) { mpd_search_add_tag_constraint( mpd, MPD_OPERATOR_DEFAULT, MPD_TAG_TITLE, title ); } if (artist) { mpd_search_add_tag_constraint( mpd, MPD_OPERATOR_DEFAULT, MPD_TAG_ARTIST, artist ); } if (album) { mpd_search_add_tag_constraint( mpd, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM, album ); } mpd_search_commit(mpd); song = mpd_recv_song(mpd); mpd_response_finish(mpd); if (song) { id = mpd_song_get_id(song); mpd_song_free(song); mpd_run_play_id(mpd, id); CHECK_MPD_CONN(mpd); } mpd_connection_free(mpd); return 0; }
void infinite_loop(struct mpd_connection *conn) { char *notification; struct mpd_status *status; NotifyNotification *netlink = NULL; time_t start_time = time(NULL); while (mpd_run_idle_mask(conn, 0xff)) { mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); if ((status = mpd_recv_status(conn)) == NULL) { fprintf(stderr, "Cannot connect to MPD. Retrying..."); continue; } if (mpd_status_get_state(status) == MPD_STATE_STOP) notification = strdup("Stopped playback"); else if (mpd_status_get_state(status) == MPD_STATE_PLAY) { struct mpd_song *song; char *title, *album, *artist; mpd_response_next(conn); song = mpd_recv_song(conn); title = (char *)mpd_song_get_tag(song, MPD_TAG_TITLE, 0); album = (char *)mpd_song_get_tag(song, MPD_TAG_ALBUM, 0); artist = (char *)mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); notification = format_notification(title, album, artist); mpd_song_free(song); } else if (mpd_status_get_state(status) == MPD_STATE_PAUSE) notification = strdup("Paused playback"); else notification = strdup("tagadatugrut"); if (netlink != NULL && time(NULL) - start_time >= SLEEP_TIME) { notify_notification_close(netlink, NULL); netlink = NULL; } while (netlink == NULL) { netlink = notify_notification_new("MPD Notification", NULL, "sound"); start_time = time(NULL); } notify_notification_update(netlink, "MPD", notification, "sound"); notify_notification_show(netlink, NULL); mpd_response_finish(conn); free(notification); } }
int main(int argc, char ** argv) { struct mpd_connection *conn; conn = mpd_connection_new(NULL, 0, 30000); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); { int i; for(i=0;i<3;i++) { printf("version[%i]: %i\n",i, mpd_connection_get_server_version(conn)[i]); } } if(argc==1) { struct mpd_status * status; struct mpd_song *song; const struct mpd_audio_format *audio_format; mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); status = mpd_recv_status(conn); if (status == NULL) return handle_error(conn); printf("volume: %i\n", mpd_status_get_volume(status)); printf("repeat: %i\n", mpd_status_get_repeat(status)); printf("queue version: %u\n", mpd_status_get_queue_version(status)); printf("queue length: %i\n", mpd_status_get_queue_length(status)); if (mpd_status_get_error(status) != NULL) printf("error: %s\n", mpd_status_get_error(status)); if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { printf("song: %i\n", mpd_status_get_song_pos(status)); printf("elaspedTime: %i\n",mpd_status_get_elapsed_time(status)); printf("elasped_ms: %u\n", mpd_status_get_elapsed_ms(status)); printf("totalTime: %i\n", mpd_status_get_total_time(status)); printf("bitRate: %i\n", mpd_status_get_kbit_rate(status)); } audio_format = mpd_status_get_audio_format(status); if (audio_format != NULL) { printf("sampleRate: %i\n", audio_format->sample_rate); printf("bits: %i\n", audio_format->bits); printf("channels: %i\n", audio_format->channels); } mpd_status_free(status); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); mpd_response_next(conn); while ((song = mpd_recv_song(conn)) != NULL) { printf("uri: %s\n", mpd_song_get_uri(song)); print_tag(song, MPD_TAG_ARTIST, "artist"); print_tag(song, MPD_TAG_ALBUM, "album"); print_tag(song, MPD_TAG_TITLE, "title"); print_tag(song, MPD_TAG_TRACK, "track"); print_tag(song, MPD_TAG_NAME, "name"); print_tag(song, MPD_TAG_DATE, "date"); if (mpd_song_get_duration(song) > 0) { printf("time: %u\n", mpd_song_get_duration(song)); } printf("pos: %u\n", mpd_song_get_pos(song)); mpd_song_free(song); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if(argc==3 && strcmp(argv[1],"lsinfo")==0) { struct mpd_entity * entity; if (!mpd_send_list_meta(conn, argv[2])) return handle_error(conn); while ((entity = mpd_recv_entity(conn)) != NULL) { const struct mpd_song *song; const struct mpd_directory *dir; const struct mpd_playlist *pl; switch (mpd_entity_get_type(entity)) { case MPD_ENTITY_TYPE_UNKNOWN: break; case MPD_ENTITY_TYPE_SONG: song = mpd_entity_get_song(entity); printf("uri: %s\n", mpd_song_get_uri(song)); print_tag(song, MPD_TAG_ARTIST, "artist"); print_tag(song, MPD_TAG_ALBUM, "album"); print_tag(song, MPD_TAG_TITLE, "title"); print_tag(song, MPD_TAG_TRACK, "track"); break; case MPD_ENTITY_TYPE_DIRECTORY: dir = mpd_entity_get_directory(entity); printf("directory: %s\n", mpd_directory_get_path(dir)); break; case MPD_ENTITY_TYPE_PLAYLIST: pl = mpd_entity_get_playlist(entity); printf("playlist: %s\n", mpd_playlist_get_path(pl)); break; } mpd_entity_free(entity); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if(argc==2 && strcmp(argv[1],"artists")==0) { struct mpd_pair *pair; if (!mpd_search_db_tags(conn, MPD_TAG_ARTIST) || !mpd_search_commit(conn)) return handle_error(conn); while ((pair = mpd_recv_pair_tag(conn, MPD_TAG_ARTIST)) != NULL) { printf("%s\n", pair->value); mpd_return_pair(conn, pair); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "playlists") == 0) { if (!mpd_send_list_playlists(conn)) return handle_error(conn); struct mpd_playlist *playlist; while ((playlist = mpd_recv_playlist(conn)) != NULL) { printf("%s\n", mpd_playlist_get_path(playlist)); mpd_playlist_free(playlist); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "idle") == 0) { enum mpd_idle idle = mpd_run_idle(conn); if (idle == 0 && mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); for (unsigned j = 0;; ++j) { enum mpd_idle i = 1 << j; const char *name = mpd_idle_name(i); if (name == NULL) break; if (idle & i) printf("%s\n", name); } } else if (argc == 3 && strcmp(argv[1], "subscribe") == 0) { /* subscribe to a channel and print all messages */ if (!mpd_run_subscribe(conn, argv[2])) return handle_error(conn); while (mpd_run_idle_mask(conn, MPD_IDLE_MESSAGE) != 0) { if (!mpd_send_read_messages(conn)) return handle_error(conn); struct mpd_message *msg; while ((msg = mpd_recv_message(conn)) != NULL) { printf("%s\n", mpd_message_get_text(msg)); mpd_message_free(msg); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "channels") == 0) { /* print a list of channels */ if (!mpd_send_channels(conn)) return handle_error(conn); struct mpd_pair *pair; while ((pair = mpd_recv_channel_pair(conn)) != NULL) { printf("%s\n", pair->value); mpd_return_pair(conn, pair); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 4 && strcmp(argv[1], "message") == 0) { /* send a message to a channel */ if (!mpd_run_send_message(conn, argv[2], argv[3])) return handle_error(conn); } mpd_connection_free(conn); return 0; }
void MpdWidget::psMpdHB() { timerMpd->stop(); QString dataSender, dataVol, dataTitle; struct mpd_connection *conn; struct mpd_status * status; struct mpd_song *song; // const struct mpd_audio_format *audio_format; conn = mpd_connection_new(NULL, 0, 30000); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { timerMpd->start(); return; } if (aktPlay) { aktPlay = false; mpd_send_command(conn, "play", NULL); timerMpd->start(); return; } if (aktStop) { aktStop = false; mpd_send_command(conn, "stop", NULL); timerMpd->start(); return; } mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); // Status status = mpd_recv_status(conn); if (status == NULL) { timerMpd->start(); return ; } // Volume dataVol.number(mpd_status_get_volume(status)); if (labelVol->text().compare(dataVol) != 0) labelVol->setText(dataVol); if (mpd_status_get_error(status) != NULL) new QListWidgetItem(QString("status error: %1"). arg(mpd_status_get_error(status))); mpd_status_free(status); // Status mpd_response_next(conn); // Song song = mpd_recv_song(conn); if (song == NULL) { handle_error(conn); timerMpd->start(); return ; } // Title dataTitle = mpd_song_get_uri(song); if (labelTitle->text().compare(dataTitle) != 0) labelTitle->setText(dataTitle); unsigned int i = 0; QString value; while ((value = mpd_song_get_tag(song, MPD_TAG_TITLE, i++)) != NULL) { if (i > 0) dataSender.append(" "); dataSender.append(value); } if (labelSender->text().compare(dataSender) != 0) { labelSender->setText(dataSender); qDebug() << "refresh"; } mpd_song_free(song); // Song mpd_connection_free(conn); timerMpd->start(); }
/*** main ***/ int main(int argc, char ** argv) { char * album = NULL, * artist = NULL, * icon = NULL, * notifystr = NULL, * title = NULL; GError * error = NULL; unsigned short int errcount = 0, state = MPD_STATE_UNKNOWN; const char * mpd_host = MPD_HOST, * music_dir = NULL, * uri = NULL;; unsigned mpd_port = MPD_PORT, mpd_timeout = MPD_TIMEOUT; struct mpd_song * song = NULL; unsigned int i; program = argv[0]; music_dir = getenv("XDG_MUSIC_DIR"); /* get the verbose status */ while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) { switch (i) { case 'v': verbose++; break; } } /* reinitialize getopt() by resetting optind to 0 */ optind = 0; /* say hello */ if (verbose > 0) printf("%s: %s v%s (compiled: " __DATE__ ", " __TIME__ ")\n", program, PROGNAME, VERSION); /* get command line options */ while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) { switch (i) { case 'h': fprintf(stderr, "usage: %s [-h] [-H HOST] [-p PORT] [-m MUSIC-DIR] [-v]\n", program); return EXIT_SUCCESS; case 'p': mpd_port = atoi(optarg); if (verbose > 0) printf("%s: using port %d\n", program, mpd_port); break; case 'm': music_dir = optarg; if (verbose > 0) printf("%s: using music-dir %s\n", program, music_dir); break; case 'H': mpd_host = optarg; if (verbose > 0) printf("%s: using host %s\n", program, mpd_host); break; } } /* disable artwork stuff if we are connected to a foreign host */ if (mpd_host != NULL) music_dir = NULL; /* change directory to music base directory */ if (music_dir != NULL) { if (chdir(music_dir) == -1) { fprintf(stderr, "%s: Can not change directory to '%s'.\n", program, music_dir); music_dir = NULL; } } /* libav */ av_register_all(); conn = mpd_connection_new(mpd_host, mpd_port, mpd_timeout); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { fprintf(stderr,"%s: %s\n", program, mpd_connection_get_error_message(conn)); mpd_connection_free(conn); exit(EXIT_FAILURE); } if(notify_init(PROGNAME) == FALSE) { fprintf(stderr, "%s: Can't create notify.\n", program); exit(EXIT_FAILURE); } notification = # if NOTIFY_CHECK_VERSION(0, 7, 0) notify_notification_new(TEXT_TOPIC, TEXT_NONE, ICON_SOUND); # else notify_notification_new(TEXT_TOPIC, TEXT_NONE, ICON_SOUND, NULL); # endif notify_notification_set_category(notification, PROGNAME); notify_notification_set_urgency (notification, NOTIFY_URGENCY_NORMAL); signal(SIGHUP, received_signal); signal(SIGINT, received_signal); signal(SIGTERM, received_signal); signal(SIGUSR1, received_signal); while(doexit == 0 && mpd_run_idle_mask(conn, MPD_IDLE_PLAYER)) { mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); state = mpd_status_get_state(mpd_recv_status(conn)); if (state == MPD_STATE_PLAY) { mpd_response_next(conn); song = mpd_recv_song(conn); uri = mpd_song_get_uri(song); if (music_dir != NULL && uri != NULL) icon = get_icon(music_dir, uri); if (verbose > 0 && icon != NULL) printf("%s: found icon: %s\n", program, icon); if ((title = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_TITLE, 0), -1)) == NULL) title = strdup(TEXT_UNKNOWN); if ((artist = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), -1)) == NULL) artist = strdup(TEXT_UNKNOWN); if ((album = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), -1)) == NULL) album = strdup(TEXT_UNKNOWN); notifystr = malloc(sizeof(TEXT_PLAY) + strlen(title) + strlen(artist) + strlen(album)); sprintf(notifystr, TEXT_PLAY, title, artist, album); free(title); free(artist); free(album); mpd_song_free(song); } else if (state == MPD_STATE_PAUSE) notifystr = TEXT_PAUSE; else if (state == MPD_STATE_STOP) notifystr = TEXT_STOP; else notifystr = TEXT_UNKNOWN; if (verbose > 0) printf("%s: %s\n", program, notifystr); notify_notification_update(notification, TEXT_TOPIC, notifystr, icon ? icon : ICON_SOUND); notify_notification_set_timeout(notification, NOTIFICATION_TIMEOUT); while(notify_notification_show(notification, &error) == FALSE) { if (errcount > 1) { fprintf(stderr, "%s: Looks like we can not reconnect to notification daemon... Exiting.\n", program); exit(EXIT_FAILURE); } else { g_printerr("%s: Error \"%s\" while trying to show notification. Trying to reconnect.\n", program, error->message); errcount++; g_error_free(error); error = NULL; notify_uninit(); usleep(500 * 1000); if(notify_init(PROGNAME) == FALSE) { fprintf(stderr, "%s: Can't create notify.\n", program); exit(EXIT_FAILURE); } } } errcount = 0; if (state == MPD_STATE_PLAY) free(notifystr); if (icon != NULL) { free(icon); icon = NULL; } mpd_response_finish(conn); } if (verbose > 0) printf("Exiting...\n"); mpd_connection_free(conn); g_object_unref(G_OBJECT(notification)); notify_uninit(); return EXIT_SUCCESS; }
/* * pgmpc_status * * Show current song and status. */ Datum pgmpc_status(PG_FUNCTION_ARGS) { #define PGMPC_STATUS_COLUMNS 7 Datum values[PGMPC_STATUS_COLUMNS]; bool nulls[PGMPC_STATUS_COLUMNS]; TupleDesc tupdesc; HeapTuple tuple; Datum result; if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) elog(ERROR, "return type must be a row type"); pgmpc_init(); /* Initialize the values of return tuple */ MemSet(values, 0, sizeof(values)); MemSet(nulls, true, sizeof(nulls)); /* * Send all necessary commands at once to avoid unnecessary round * trips. The following information is obtained in an aync way: * - current status of server * - current song run on server */ if (!mpd_command_list_begin(mpd_conn, true) || !mpd_send_status(mpd_conn) || !mpd_send_current_song(mpd_conn) || !mpd_command_list_end(mpd_conn)) pgmpc_print_error(); /* Obtain status from server and check it */ mpd_status = mpd_recv_status(mpd_conn); if (mpd_status == NULL) pgmpc_print_error(); /* Show current song if any */ if (mpd_status_get_state(mpd_status) == MPD_STATE_PLAY || mpd_status_get_state(mpd_status) == MPD_STATE_PAUSE) { struct mpd_song *song; /* There should be a next response, in this case a song */ if (!mpd_response_next(mpd_conn)) pgmpc_print_error(); /* And now get it */ song = mpd_recv_song(mpd_conn); if (song != NULL) { /* Get information about the current song */ const char *title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); const char *artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); const char *album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0); unsigned int elapsed_time = mpd_status_get_elapsed_time(mpd_status); unsigned int total_time = mpd_status_get_total_time(mpd_status); int song_pos = mpd_status_get_song_pos(mpd_status) + 1; int volume = mpd_status_get_volume(mpd_status); /* Build tuple using this information */ if (title) { nulls[0] = false; values[0] = CStringGetTextDatum(title); } else nulls[0] = true; if (artist) { nulls[1] = false; values[1] = CStringGetTextDatum(artist); } else nulls[1] = true; if (album) { nulls[2] = false; values[2] = CStringGetTextDatum(album); } else nulls[2] = true; nulls[3] = false; values[3] = UInt32GetDatum(elapsed_time); nulls[4] = false; values[4] = UInt32GetDatum(total_time); nulls[5] = false; values[5] = Int32GetDatum(song_pos); nulls[6] = false; values[6] = Int32GetDatum(volume); /* Song data is no more needed */ mpd_song_free(song); } if (!mpd_response_finish(mpd_conn)) pgmpc_print_error(); } /* Cleanup MPD status */ pgmpc_reset(); /* Form result and return it */ tuple = heap_form_tuple(tupdesc, values, nulls); result = HeapTupleGetDatum(tuple); PG_RETURN_DATUM(result); }
void mpd_query_status(struct mpd_connection *conn) { struct mpd_status *status; struct mpd_song *song; const struct mpd_audio_format *audio; if (!conn) return; if (!mpd_command_list_begin(conn, true) || !mpd_send_status(conn) || !mpd_send_current_song(conn) || !mpd_command_list_end(conn)) { mpd_printerror("queue_commands"); return; } status = mpd_recv_status(conn); if (status == NULL) { mpd_printerror("recv_status"); return; } if (currentSong != NULL) { mpd_song_free(currentSong); currentSong = NULL; } if (!mpd_response_next(conn)) { mpd_printerror("response_next"); return; } song = mpd_recv_song(conn); if (song != NULL) { currentSong = mpd_song_dup(song); mpd_song_free(song); l_elapsedTimeSec = mpd_status_get_elapsed_time(status); l_totalTimeSec = mpd_status_get_total_time(status); l_bitRate = mpd_status_get_kbit_rate(status); } else { l_elapsedTimeSec = 0; l_totalTimeSec = 0; l_bitRate = 0; } l_state = mpd_status_get_state(status); l_repeatEnabled = mpd_status_get_repeat(status); l_randomEnabled = mpd_status_get_random(status); l_singleEnabled = mpd_status_get_single(status); l_consumeEnabled = mpd_status_get_consume(status); l_volume = mpd_status_get_volume(status); l_currentSongPos = mpd_status_get_song_pos(status) + 1; l_playlistLength = mpd_status_get_queue_length(status); audio = mpd_status_get_audio_format(status); if (audio) { l_sampleRate = audio->sample_rate; l_channels = audio->channels; } else { l_sampleRate = 0; l_channels = 0; } if (mpd_status_get_error(status) != NULL) error("[MPD] query status : %s", charset_from_utf8(mpd_status_get_error(status))); mpd_status_free(status); if (!mpd_response_finish(conn)) { mpd_printerror("response_finish"); return; } }
/* * pgmpc_playlist * List all songs in given playlist. If not playlist is specified list * songs of current playlist. */ Datum pgmpc_playlist(PG_FUNCTION_ARGS) { TupleDesc tupdesc; Tuplestorestate *tupstore; char *playlist = NULL; bool ret; if (PG_NARGS() == 1 && !PG_ARGISNULL(0)) playlist = text_to_cstring(PG_GETARG_TEXT_PP(0)); /* Initialize function context */ pgmpc_init_setof_single(fcinfo, TEXTOID, "playlist", &tupdesc, &tupstore); /* * Run the command to get all the songs. */ pgmpc_init(); ret = playlist ? mpd_send_list_playlist_meta(mpd_conn, playlist) : mpd_send_list_queue_meta(mpd_conn); if (!ret) pgmpc_print_error(); /* Now get all the songs and send them back to caller */ while (true) { Datum values[1]; bool nulls[1]; struct mpd_song *song = mpd_recv_song(mpd_conn); /* Leave if done */ if (song == NULL) break; /* Assign song name */ nulls[0] = false; values[0] = CStringGetTextDatum(mpd_song_get_uri(song)); /* Save values */ tuplestore_putvalues(tupstore, tupdesc, values, nulls); /* Clean up for the next one */ mpd_song_free(song); } /* We may be in error state, so check for it */ if (mpd_connection_get_error(mpd_conn) != MPD_ERROR_SUCCESS) { const char *message = mpd_connection_get_error_message(mpd_conn); pgmpc_reset(); ereport(ERROR, (errcode(ERRCODE_SYSTEM_ERROR), errmsg("mpd command failed: %s", message))); } /* Clean up */ pgmpc_reset(); /* clean up and return the tuplestore */ tuplestore_donestoring(tupstore); return (Datum) 0; }
void print_status(struct mpd_connection *conn) { if (!mpd_command_list_begin(conn, true) || !mpd_send_status(conn) || !mpd_send_current_song(conn) || !mpd_command_list_end(conn)) printErrorAndExit(conn); struct mpd_status *status = mpd_recv_status(conn); if (status == NULL) printErrorAndExit(conn); if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { if (!mpd_response_next(conn)) printErrorAndExit(conn); struct mpd_song *song = mpd_recv_song(conn); if (song != NULL) { pretty_print_song(song); printf("\n"); mpd_song_free(song); } if (mpd_status_get_state(status) == MPD_STATE_PLAY) printf("[playing]"); else printf("[paused] "); printf(" #%i/%u %3i:%02i/%i:%02i (%u%%)\n", mpd_status_get_song_pos(status) + 1, mpd_status_get_queue_length(status), mpd_status_get_elapsed_time(status) / 60, mpd_status_get_elapsed_time(status) % 60, mpd_status_get_total_time(status) / 60, mpd_status_get_total_time(status) % 60, elapsed_percent(status)); } if (mpd_status_get_update_id(status) > 0) printf("Updating DB (#%u) ...\n", mpd_status_get_update_id(status)); if (mpd_status_get_volume(status) >= 0) printf("volume:%3i%c ", mpd_status_get_volume(status), '%'); else { printf("volume: n/a "); } printf("repeat: "); if (mpd_status_get_repeat(status)) printf("on "); else printf("off "); printf("random: "); if (mpd_status_get_random(status)) printf("on "); else printf("off "); printf("single: "); if (mpd_status_get_single(status)) printf("on "); else printf("off "); printf("consume: "); if (mpd_status_get_consume(status)) printf("on \n"); else printf("off\n"); if (mpd_status_get_error(status) != NULL) printf("ERROR: %s\n", charset_from_utf8(mpd_status_get_error(status))); mpd_status_free(status); if (!mpd_response_finish(conn)) printErrorAndExit(conn); }