static gboolean lmc_reconnect(G_GNUC_UNUSED gpointer data) { const unsigned *version; g_mpd = mpd_connection_new(g_host, g_port, 0); if (mpd_connection_get_error(g_mpd) != MPD_ERROR_SUCCESS) { lmc_failure(); return true; } idle_supported = mpd_connection_cmp_server_version(g_mpd, 0, 14, 0) >= 0; version = mpd_connection_get_server_version(g_mpd); char *name = connection_settings_name(g_mpd); g_message("connected to mpd %i.%i.%i at %s\n", version[0], version[1], version[2], name); g_free(name); #if LIBMPDCLIENT_CHECK_VERSION(2,5,0) subscribed = mpd_run_subscribe(g_mpd, "mpdscribble"); if (!subscribed && !mpd_connection_clear_error(g_mpd)) { lmc_failure(); return true; } #endif lmc_schedule_update(); reconnect_source_id = 0; return false; }
int cmd_version(mpd_unused int argc, mpd_unused char **argv, struct mpd_connection *conn) { const unsigned *version = mpd_connection_get_server_version(conn); if (version != NULL) printf("mpd version: %i.%i.%i\n", version[0], version[1], version[2]); else printf("mpd version: unknown\n"); return 0; }
unsigned Connection::Version() const { return m_connection ? mpd_connection_get_server_version(m_connection.get())[1] : 0; }
/** * This timer is installed when the connection to the MPD server is * broken. It tries to recover by reconnecting periodically. */ static gboolean timer_reconnect(G_GNUC_UNUSED gpointer data) { bool success; struct mpd_connection *connection; assert(!mpdclient_is_connected(mpd)); reconnect_source_id = 0; char *name = default_settings_name(); screen_status_printf(_("Connecting to %s... [Press %s to abort]"), name, get_key_names(CMD_QUIT, false)); g_free(name); doupdate(); mpdclient_disconnect(mpd); success = mpdclient_connect(mpd, options.host, options.port, options.timeout_ms, options.password); if (!success) { /* try again in 5 seconds */ reconnect_source_id = g_timeout_add(5000, timer_reconnect, NULL); return FALSE; } connection = mpdclient_get_connection(mpd); #ifndef NCMPC_MINI /* quit if mpd is pre 0.11.0 - song id not supported by mpd */ if (mpd_connection_cmp_server_version(connection, 0, 12, 0) < 0) { const unsigned *version = mpd_connection_get_server_version(connection); screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"), version[0], version[1], version[2], "0.12.0"); mpdclient_disconnect(mpd); doupdate(); /* try again after 30 seconds */ reconnect_source_id = g_timeout_add(30000, timer_reconnect, NULL); return FALSE; } #endif if (mpd_connection_cmp_server_version(connection, 0, 14, 0) >= 0) mpd->source = mpd_glib_new(connection, idle_callback, mpd); name = connection_settings_name(connection); screen_status_printf(_("Connected to %s"), name); g_free(name); doupdate(); /* update immediately */ mpd->events = MPD_IDLE_ALL; do_mpd_update(); auto_update_timer(); return FALSE; }
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; }