int cmd_volume(int argc, char **argv, struct mpd_connection *conn) { struct int_value_change ch; if (argc == 1) { if (!parse_int_value_change(argv[0], &ch)) DIE("\"%s\" is not an integer\n", argv[0]); } else { struct mpd_status *status = getStatus(conn); if (mpd_status_get_volume(status) >= 0) printf("volume:%3i%c\n", mpd_status_get_volume(status), '%'); else printf("volume: n/a\n"); mpd_status_free(status); return 0; } if (ch.is_relative) { #if LIBMPDCLIENT_CHECK_VERSION(2,9,0) if (mpd_connection_cmp_server_version(conn, 0, 18, 0) >= 0) { /* MPD 0.18 knows the "volume" command for relative changes */ if (!mpd_run_change_volume(conn, ch.value)) printErrorAndExit(conn); return 1; } #endif struct mpd_status *status = getStatus(conn); int old_volume = mpd_status_get_volume(status); mpd_status_free(status); ch.value += old_volume; if (ch.value < 0) ch.value = 0; else if (ch.value > 100) ch.value = 100; if (ch.value == old_volume) return 1; } if (!mpd_run_set_volume(conn, ch.value)) printErrorAndExit(conn); return 1; }
int mpd_put_state(char *buffer, int *current_song_id, unsigned *queue_version) { struct mpd_status *status; int len; status = mpd_run_status(mpd.conn); if (!status) { fprintf(stderr, "MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn)); mpd.conn_state = MPD_FAILURE; return 0; } len = snprintf(buffer, MAX_SIZE, "{\"type\":\"state\", \"data\":{" " \"state\":%d, \"volume\":%d, \"repeat\":%d," " \"single\":%d, \"consume\":%d, \"random\":%d, " " \"songpos\": %d, \"elapsedTime\": %d, \"totalTime\":%d, " " \"currentsongid\": %d" "}}", mpd_status_get_state(status), mpd_status_get_volume(status), mpd_status_get_repeat(status), mpd_status_get_single(status), mpd_status_get_consume(status), mpd_status_get_random(status), mpd_status_get_song_pos(status), mpd_status_get_elapsed_time(status), mpd_status_get_total_time(status), mpd_status_get_song_id(status)); *current_song_id = mpd_status_get_song_id(status); *queue_version = mpd_status_get_queue_version(status); mpd_status_free(status); return len; }
int main(int argc, const char* const argv[]) { if (argc > 2 || (argc == 2 && strncmp(argv[1], "-h", 2) == 0)) { usage(); return 0; } conn = mpd_connection_new(NULL, 0, 0); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { mpd_connection_free(conn); fputs("Could not connect to MPD.\n", stderr); return 0; } stat = mpd_run_status(conn); if (!(song = mpd_run_current_song(conn))) { fputs("No song playing.\n", stderr); return 0; } if (argc == 1) print_info(DEFAULT_FMT); else print_info(argv[1]); if (song) mpd_song_free(song); mpd_status_free(stat); mpd_connection_free(conn); return 0; }
/* * Retrieves status about the state of MPD. */ bool Control::get_status() { mpd_status * status; mpd_stats * stats; EXIT_IDLE; pms->log(MSG_DEBUG, 0, "Retrieving MPD status from server.\n"); if ((status = mpd_run_status(conn->h())) == NULL) { /* FIXME: error handling? */ pms->log(MSG_DEBUG, 0, "mpd_run_status returned NULL pointer.\n"); delete _song; _song = NULL; st->song = MPD_SONG_NO_NUM; st->songid = MPD_SONG_NO_ID; return false; } st->assign_status(status); mpd_status_free(status); if ((stats = mpd_run_stats(conn->h())) == NULL) { /* FIXME ? */ pms->log(MSG_DEBUG, 0, "mpd_run_stats returned NULL pointer.\n"); return false; } st->assign_stats(stats); mpd_stats_free(stats); return true; }
int cmd_play ( int argc, char ** argv, struct mpd_connection *conn ) { int song; int i; if(0==argc) song = -1; else { struct mpd_status *status; for(i=0;i<argc-1;i++) printf("skipping: %s\n",argv[i]); if(!parse_songnum(argv[i], &song)) DIE("error parsing song numbers from: %s\n",argv[i]); song--; /* This is necessary, otherwise mpc will output the wrong playlist number */ status = getStatus(conn); i = mpd_status_get_queue_length(status); mpd_status_free(status); if(song >= i) DIE("song number greater than playlist length.\n"); } if (song >= 0) mpd_run_play_pos(conn, song); else mpd_run_play(conn); return 1; }
void songlist_update_checking(void) { struct mpd_status *status; int queue_len, song_id; status = getStatus(conn); queue_len = mpd_status_get_queue_length(status); song_id = mpd_status_get_song_pos(status) + 1; mpd_status_free(status); if(songlist->current != song_id) { songlist->current = song_id; signal_all_wins(); } if(songlist->update_signal == 1 || songlist->total != queue_len) { songlist->update_signal = 0; songlist->total = queue_len; songlist_update(); signal_all_wins(); } /** for some unexcepted cases songlist->begin may be greater than songlist->length; if this happens, we reset the begin's value */ if(songlist->begin > songlist->length) songlist->begin = 1; }
void songlist_simple_bar(void) { int crt_time, crt_time_perc, total_time, fill_len, rest_len, i; struct mpd_status *status; WINDOW *win = specific_win(SIMPLE_PROC_BAR); const int bar_length = win->_maxx + 1; status = getStatus(conn); crt_time = mpd_status_get_elapsed_time(status); total_time = mpd_status_get_total_time(status); mpd_status_free(status); crt_time_perc = (total_time == 0 ? 0 : 100 * crt_time / total_time); fill_len = crt_time_perc * bar_length / 100; rest_len = bar_length - fill_len; wattron(win, my_color_pairs[2]); for(i = 0; i < fill_len; wprintw(win, "*"), i++); wattroff(win, my_color_pairs[2]); for(i = 0; i < rest_len; wprintw(win, "*"), i++); }
void mpd_put_state(void) { struct mpd_status *status; int len; unsigned queue_len; int song_pos, next_song_pos; status = mpd_run_status(mpd.conn); if (!status) { syslog(LOG_ERR, "%s mpd_run_status: %s\n", __func__, mpd_connection_get_error_message(mpd.conn)); mpd.conn_state = MPD_FAILURE; return; } mpd.song_pos = mpd_status_get_song_pos(status); mpd.next_song_pos = song_pos+1; //TODO: mpd_status_get_next_song_pos(status); mpd.queue_len = mpd_status_get_queue_length(status); mpd.volume = mpd_status_get_volume(status); mpd.state = mpd_status_get_state(status); mpd.repeat = mpd_status_get_repeat(status); mpd.single = mpd_status_get_single(status); mpd.consume = mpd_status_get_consume(status); mpd.random = mpd_status_get_random(status); mpd.elapsed_time = mpd_status_get_elapsed_time(status); mpd.total_time = mpd_status_get_total_time(status); mpd.song_id = mpd_status_get_song_id(status); // printf("%d\n", mpd.song_id); mpd_status_free(status); }
static void mpd_update(void) { enum mpd_state prev = MPD_STATE_UNKNOWN; if (mpd.status) { prev = mpd_status_get_state(mpd.status); mpd_status_free(mpd.status); } mpd.status = mpd_run_status(mpd.conn); if (!mpd_response_finish(mpd.conn)) { mpd_report_error(); return; } if (mpd_status_get_state(mpd.status) == MPD_STATE_PLAY && prev != MPD_STATE_PAUSE) { if (mpd.song) mpd_song_free(mpd.song); mpd.song = mpd_run_current_song(mpd.conn); if (!mpd_response_finish(mpd.conn)) { mpd_report_error(); return; } if (prefs.announce) mpd_announce_song(); } }
static int bool_cmd(int argc, char **argv, struct mpd_connection *conn, bool (*get_mode)(const struct mpd_status *status), bool (*run_set_mode)(struct mpd_connection *conn, bool mode)) { bool mode; if (argc == 1) { int mode_i = get_boolean(argv[0]); if (mode_i < 0) return -1; mode = (bool)mode_i; } else { struct mpd_status *status; status = getStatus(conn); mode = !get_mode(status); mpd_status_free(status); } if (!run_set_mode(conn, mode)) printErrorAndExit(conn); return 1; }
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; }
unsigned mpd_get_queue_length() { struct mpd_status *status = mpd_run_status(mpd.conn); if (status == NULL) return 0; const unsigned length = mpd_status_get_queue_length(status); mpd_status_free(status); return length; }
/** * Wait until the song changes or until playback is started/stopped. */ static void wait_current(struct mpd_connection *c) { struct mpd_status *status = getStatus(c); const int old_song = get_active_song(status); mpd_status_free(status); int new_song; do { enum mpd_idle idle = mpd_run_idle_mask(c, MPD_IDLE_PLAYER); if (idle == 0) printErrorAndExit(c); status = getStatus(c); new_song = get_active_song(status); mpd_status_free(status); } while (new_song == old_song); }
int cmd_volume ( int argc, char ** argv, struct mpd_connection *conn ) { struct int_value_change ch; struct mpd_status *status; if(argc==1) { if(!parse_int_value_change(argv[0], &ch)) DIE("\"%s\" is not an integer\n", argv[0]); } else { status = getStatus(conn); if (mpd_status_get_volume(status) >= 0) printf("volume:%3i%c\n", mpd_status_get_volume(status), '%'); else printf("volume: n/a\n"); mpd_status_free(status); return 0; } if (ch.is_relative) { int old_volume; status = getStatus(conn); old_volume = mpd_status_get_volume(status); mpd_status_free(status); ch.value += old_volume; if (ch.value < 0) ch.value = 0; else if (ch.value > 100) ch.value = 100; if (ch.value == old_volume) return 1; } if (!mpd_run_set_volume(conn, ch.value)) printErrorAndExit(conn); return 1; }
/* * pgmpc_reset * * Cleanup existing mpd context. */ static void pgmpc_reset(void) { if (mpd_conn) mpd_connection_free(mpd_conn); if (mpd_status) mpd_status_free(mpd_status); mpd_conn = NULL; mpd_status = NULL; }
void mpd_say_status(void) { gchar *state; gchar *artist, *title; if (!mpd.conn) { irc_say("Not connected to MPD"); return; } if (mpd.status) mpd_status_free(mpd.status); mpd_run_noidle(mpd.conn); mpd.status = mpd_run_status(mpd.conn); if (!mpd_response_finish(mpd.conn)) { mpd_report_error(); return; } mpd_send_idle_mask(mpd.conn, MPD_IDLE_PLAYER); switch (mpd_status_get_state(mpd.status)) { case MPD_STATE_STOP: state = g_strdup("stopped"); break; case MPD_STATE_PLAY: state = g_strdup("playing"); break; case MPD_STATE_PAUSE: state = g_strdup("paused"); break; default: state = g_strdup("unknown"); break; } if (mpd.song) { artist = g_strdup(mpd_song_get_tag(mpd.song, MPD_TAG_ARTIST, 0)); title = g_strdup(mpd_song_get_tag(mpd.song, MPD_TAG_TITLE, 0)); } else { artist = g_strdup(""); title = g_strdup(""); } irc_say("[%s] %s - %s (%i:%02i/%i:%02i) | repeat: %sabled | " "random: %sabled | announce: %sabled", state, artist, title, mpd_status_get_elapsed_time(mpd.status) / 60, mpd_status_get_elapsed_time(mpd.status) % 60, mpd_status_get_total_time(mpd.status) / 60, mpd_status_get_total_time(mpd.status) % 60, (mpd_status_get_repeat(mpd.status) ? "en" : "dis"), (mpd_status_get_random(mpd.status) ? "en" : "dis"), (prefs.announce ? "en" : "dis")); g_free(state); g_free(artist); g_free(title); }
unsigned mpd_get_queue_length() { unsigned length = 0; struct mpd_status *status = mpd_run_status(mpd.conn); if (status != NULL) { length = mpd_status_get_queue_length(status); mpd_status_free(status); } mpd_response_finish(mpd.conn); return length; }
int del_client(CLIENT_OBJECT *cli) { mpd_stats_free(cli->stats); if (cli->song) mpd_song_free(cli->song); mpd_status_free(cli->status); mpd_connection_free(cli->conn); free(cli); return 0; }
void songlist_scroll_to_current(void) { struct mpd_status *status; int song_id; status = getStatus(conn); song_id = mpd_status_get_song_pos(status) + 1; mpd_status_free(status); songlist_scroll_to(song_id); }
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; }
static int lmpdstatus_gc(lua_State *L) { struct mpd_status **status; status = luaL_checkudata(L, 1, MPD_STATUS_T); if (*status != NULL) mpd_status_free(*status); *status = NULL; return 0; }
/** * Wait until the song changes or until playback is started/stopped. */ static void wait_current(struct mpd_connection *c) { if (mpd_connection_cmp_server_version(c, 0, 14, 0) < 0) fprintf(stderr, "warning: MPD 0.14 required for this command\n"); struct mpd_status *status = getStatus(c); const int old_song = get_active_song(status); mpd_status_free(status); int new_song; do { enum mpd_idle idle = mpd_run_idle_mask(c, MPD_IDLE_PLAYER); if (idle == 0) printErrorAndExit(c); status = getStatus(c); new_song = get_active_song(status); mpd_status_free(status); } while (new_song == old_song); }
int cmd_crop(mpd_unused int argc, mpd_unused char **argv, struct mpd_connection *conn) { struct mpd_status *status = getStatus( conn ); int length = mpd_status_get_queue_length(status) - 1; if (length < 0) { mpd_status_free(status); DIE( "A playlist longer than 1 song in length is required to crop.\n" ); } else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { if (!mpd_command_list_begin(conn, false)) printErrorAndExit(conn); while( length >= 0 ) { if (length != mpd_status_get_song_pos(status)) { mpd_send_delete(conn, length); } length--; } mpd_status_free(status); if (!mpd_command_list_end(conn) || !mpd_response_finish(conn)) printErrorAndExit(conn); return ( 0 ); } else { mpd_status_free(status); DIE( "You need to be playing to crop the playlist\n" ); } }
int update_client(CLIENT_OBJECT *cli) { mpd_status_free(cli->status); cli->status = mpd_run_status(cli->conn); if (cli->song) mpd_song_free(cli->song); cli->song = mpd_run_current_song(cli->conn); mpd_stats_free(cli->stats); cli->stats = mpd_run_stats(cli->conn); return 0; }
int mpd_crop() { struct mpd_status *status = mpd_run_status(mpd.conn); if (status == 0) return 0; int length = mpd_status_get_queue_length(status) - 1; if (length < 0) { mpd_status_free(status); syslog(LOG_INFO, "%s: A playlist longer than 1 song in length is required to crop.\n", __func__); } else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { if (!mpd_command_list_begin(mpd.conn, false)) { syslog(LOG_ERR, "%s: mpd_command_list_begin failed\n", __func__); return 0; } for (; length >= 0; --length) if (length != mpd_status_get_song_pos(status)) mpd_send_delete(mpd.conn, length); mpd_status_free(status); if (!mpd_command_list_end(mpd.conn) || !mpd_response_finish(mpd.conn)) { syslog(LOG_ERR, "%s: mpd_command_list_end || mpd_response_finish failed\n", __func__); return 0; } return 0; } else { mpd_status_free(status); syslog(LOG_INFO, "%s: You need to be playing to crop the playlist\n", __func__); return 0; } return 1; }
int cmd_insert (int argc, char ** argv, struct mpd_connection *conn ) { int ret; struct mpd_status *status = getStatus(conn); const int from = mpd_status_get_queue_length(status); ret = cmd_add(argc, argv, conn); const int cur_pos = mpd_status_get_song_pos(status); mpd_status_free(status); if (ret != 0) { return ret; } return mpd_run_move_range(conn, from, from+argc, cur_pos+1); }
int main(int argc, char **argv) { int t; bool firsttime = true; pid=getpid(); _debug = (bool) getenv("MADAUDIO_DEBUG"); if(!_debug) daemon(0, 0); int flags = LOG_NDELAY | LOG_PID; if(_debug) flags |= LOG_PERROR; openlog("madaudio-unsuspend", flags, LOG_DAEMON); struct mpd_connection *conn; for(t=1000; t > 0; t++) { conn = mpd_connection_new(MADAUDIO_SOCKET, 0, 0); if(conn && mpd_connection_get_error(conn)==MPD_ERROR_SUCCESS) break; debug("can't connect to mpd, retry", pid); usleep(1000); }; debug("connected..."); while(true) { struct mpd_status * status = mpd_run_status(conn); check(conn); if(!status) err(1, "madaudio-unsuspend[%d]: Can't get status\n"); enum mpd_state state = mpd_status_get_state(status); if(state != oldstate) { if(state == MPD_STATE_PLAY) lock_autosuspend(); else { if(!firsttime) unlock_autosuspend(); } }; oldstate = state; firsttime = false; mpd_status_free(status); mpd_run_idle_mask(conn, MPD_IDLE_PLAYER); check(conn); } }
int cmd_mixrampdb ( int argc, char ** argv, struct mpd_connection *conn ) { if(argc==1) { float db; if(!parse_float(argv[0], &db)) DIE("\"%s\" is not a floating point number\n",argv[0]); mpd_run_mixrampdb(conn, db); my_finishCommand(conn); } else { struct mpd_status *status = getStatus(conn); printf("mixrampdb: %f\n", mpd_status_get_mixrampdb(status)); mpd_status_free(status); } return 0; }
int cmd_crossfade ( int argc, char ** argv, struct mpd_connection *conn ) { if(argc==1) { int seconds; if(!parse_int(argv[0], &seconds) || seconds<0) DIE("\"%s\" is not 0 or positive integer\n",argv[0]); if (!mpd_run_crossfade(conn, seconds)) printErrorAndExit(conn); } else { struct mpd_status *status; status = getStatus(conn); printf("crossfade: %i\n", mpd_status_get_crossfade(status)); mpd_status_free(status); } return 0; }
int mpd_insert (char *song_path ) { struct mpd_status *status = mpd_run_status(mpd.conn); if (status == NULL) return 0; const unsigned from = mpd_status_get_queue_length(status); const int cur_pos = mpd_status_get_song_pos(status); mpd_status_free(status); if (mpd_run_add(mpd.conn, song_path) != true) return 0; /* check the new queue length to find out how many songs were * appended */ const unsigned end = mpd_get_queue_length(); if (end == from) return 0; /* move those songs to right after the current one */ return mpd_run_move_range(mpd.conn, from, end, cur_pos + 1); }