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(); } }
/** * Returns the id of the current song, but only if it is really * playing (playing or paused). */ static int get_active_song(const struct mpd_status *status) { return mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE ? mpd_status_get_song_id(status) : -1; }
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; }
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); }
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; }
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; }
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; }
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); }
static bool should_enable_update_timer(void) { return (mpdclient_is_connected(mpd) && (mpd->source == NULL || /* "idle" not supported */ (mpd->status != NULL && mpd_status_get_state(mpd->status) == MPD_STATE_PLAY))) #ifndef NCMPC_MINI || options.display_time #endif ; }
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 cmd_toggle(gcc_unused int argc, gcc_unused char **argv, struct mpd_connection *conn) { struct mpd_status *status = getStatus(conn); if (mpd_status_get_state(status) == MPD_STATE_PLAY) { cmd_pause(0, NULL, conn); } else { cmd_play(0, NULL, conn); } return 1; }
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 event_player(G_GNUC_UNUSED const struct mpd_connection *conn, const struct mpd_song *song, const struct mpd_status *status) { enum mpd_state state; g_assert(status != NULL); state = mpd_status_get_state(status); if (state == MPD_STATE_PAUSE) { song_paused(); return MPDCRON_EVENT_SUCCESS; } else if (state != MPD_STATE_PLAY) song_stopped(); if (was_paused) { if (song != NULL && mpd_song_get_id(song) == last_id) song_continued(); was_paused = false; } /* Submit the previous song */ if (prev != NULL && (song == NULL || mpd_song_get_id(prev) != mpd_song_get_id(song))) song_ended(prev); if (song != NULL) { if (mpd_song_get_id(song) != last_id) { /* New song. */ song_started(song); last_id = mpd_song_get_id(song); } else { /* still playing the previous song */ song_playing(song, mpd_status_get_elapsed_time(status)); } } if (prev != NULL) { mpd_song_free(prev); prev = NULL; } if (song != NULL) { if ((prev = mpd_song_dup(song)) == NULL) { g_critical("mpd_song_dup failed: out of memory"); return MPDCRON_EVENT_UNLOAD; } } return MPDCRON_EVENT_SUCCESS; }
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 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 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); } }
static void do_mpd_update(void) { if (mpdclient_is_connected(mpd) && (mpd->source == NULL || mpd->events != 0 || (mpd->status != NULL && mpd_status_get_state(mpd->status) == MPD_STATE_PLAY))) mpdclient_update(mpd); #ifndef NCMPC_MINI if (options.enable_xterm_title) update_xterm_title(); #endif screen_update(mpd); mpd->events = 0; mpdclient_put_connection(mpd); check_reconnect(); }
void Mpd_status::assign_status(struct mpd_status * status) { const struct mpd_audio_format *format; uint32_t ms; volume = mpd_status_get_volume(status); repeat = mpd_status_get_repeat(status); single = mpd_status_get_single(status); random = mpd_status_get_random(status); consume = mpd_status_get_consume(status); playlist_length = mpd_status_get_queue_length(status); playlist = mpd_status_get_queue_version(status); state = mpd_status_get_state(status); crossfade = mpd_status_get_crossfade(status); song = mpd_status_get_song_pos(status); songid = mpd_status_get_song_id(status); time_total = mpd_status_get_total_time(status); db_updating = mpd_status_get_update_id(status); /* Time elapsed */ #if LIBMPDCLIENT_CHECK_VERSION(2, 1, 0) ms = mpd_status_get_elapsed_ms(status); #else ms = mpd_status_get_elapsed(status) * 1000; #endif set_time_elapsed_ms(ms); /* Audio format */ bitrate = mpd_status_get_kbit_rate(status); format = mpd_status_get_audio_format(status); if (!format) { return; } samplerate = format->sample_rate; bits = format->bits; channels = format->channels; }
/*my_mpd_run_pause*/ void my_mpd_run_pause(void) { struct mpd_status *status = NULL; pthread_mutex_lock(&mutex); status = mpd_run_status(conn); if (!status) { printf("nd my_mpd_run_pause mpd_run_status1 %s \n", mpd_connection_get_error_message(conn)); } else { if (mpd_status_get_state(status) == MPD_STATE_PLAY) { mpd_run_pause(conn, true); } mpd_status_free(status); } pthread_mutex_unlock(&mutex); }
void do_status(struct mpd_connection *con, int *current, unsigned int *current_elapsed, unsigned int *current_total, unsigned int *length, unsigned int *version) { struct mpd_status *st = mpd_run_status(con); if (!st) return; if (check_error(con, NULL, false)) goto out; switch (mpd_status_get_state(st)) { case MPD_STATE_UNKNOWN: case MPD_STATE_STOP: if (current) *current = -1; if (current_elapsed) *current_elapsed = 0; if (current_total) *current_total = 0; break; case MPD_STATE_PLAY: case MPD_STATE_PAUSE: if (current) *current = mpd_status_get_song_pos(st); if (current_elapsed) *current_elapsed = mpd_status_get_elapsed_time(st); if (current_total) *current_total = mpd_status_get_total_time(st); break; } if (length) *length = mpd_status_get_queue_length(st); if (version) *version = mpd_status_get_queue_version(st); out: mpd_status_free(st); }
static void update_xterm_title(void) { static char title[BUFSIZE]; char tmp[BUFSIZE]; struct mpd_status *status = NULL; const struct mpd_song *song = NULL; if (mpd) { status = mpd->status; song = mpd->song; } if (options.xterm_title_format && status && song && mpd_status_get_state(status) == MPD_STATE_PLAY) strfsong(tmp, BUFSIZE, options.xterm_title_format, song); else g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE); if (strncmp(title, tmp, BUFSIZE)) { g_strlcpy(title, tmp, BUFSIZE); set_xterm_title("%s", title); } }
/* * 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); }
int cmd_seek(gcc_unused int argc, gcc_unused char **argv, struct mpd_connection *conn) { struct mpd_status *status; char * arg = argv[0]; int seekchange; int total_secs; int rel = 0; status = getStatus(conn); if (mpd_status_get_state(status) == MPD_STATE_STOP) DIE("not currently playing\n"); /* Detect +/- if exists point to the next char */ if (*arg == '+') rel = 1; else if (*arg == '-') rel = -1; if (rel != 0) ++arg; /* If seeking by percent */ if (arg[strlen(arg) - 1] == '%') { /* Remove the % */ arg[strlen(arg) - 1] = '\0'; /* percent seek, strtod is needed for percent with decimals */ char *test; double perc = strtod(arg,&test); if (*test != '\0' || (rel == 0 && (perc < 0 || perc > 100)) || (rel && perc > abs(100))) DIE("\"%s\" is not an number between 0 and 100\n", arg); seekchange = perc * mpd_status_get_total_time(status) / 100 + 0.5; } else { /* If seeking by absolute seek time */ if (strchr(arg, ':') != NULL) { int hr = 0; int min = 0; int sec = 0; /* Take the seconds off the end of arg */ char *sec_ptr = strrchr(arg, ':'); /* Remove ':' and move the pointer one byte up */ *sec_ptr = '\0'; ++sec_ptr; /* If hour is in the argument, else just point to the arg */ char *min_ptr = strrchr(arg, ':'); if (min_ptr != NULL) { /* Remove ':' and move the pointer one byte up */ *min_ptr = '\0'; ++min_ptr; /* If the argument still exists, it's the hour */ if (arg != NULL) { char *hr_ptr = arg; char *test; hr = strtol(hr_ptr, &test, 10); if (*test != '\0' || (rel == 0 && hr < 0)) DIE("\"%s\" is not a positive number\n", sec_ptr); } } else { min_ptr = arg; } /* Change the pointers to a integer */ char *test; sec = strtol(sec_ptr, &test, 10); if (*test != '\0' || (rel == 0 && sec < 0)) DIE("\"%s\" is not a positive number\n", sec_ptr); min = strtol( min_ptr, &test, 10 ); if( *test != '\0' || (rel == 0 && min < 0 )) DIE("\"%s\" is not a positive number\n", min_ptr); /* If mins exist, check secs. If hrs exist, check mins */ if (min && strlen(sec_ptr) != 2) DIE("\"%s\" is not two digits\n", sec_ptr); else if (hr && strlen(min_ptr) != 2) DIE("\"%s\" is not two digits\n", min_ptr); /* Finally, make sure they're not above 60 if higher unit exists */ if (min && sec > 60) DIE("\"%s\" is greater than 60\n", sec_ptr); else if (hr && min > 60 ) DIE("\"%s\" is greater than 60\n", min_ptr); total_secs = (hr * 3600) + (min * 60) + sec; } else { /* absolute seek (in seconds) */ char *test; total_secs = strtol(arg, &test, 10); /* get the # of seconds */ if (*test != '\0' || (rel == 0 && total_secs < 0)) DIE("\"%s\" is not a positive number\n", arg); } seekchange = total_secs; } /* This detects +/- and is necessary due to the parsing of HH:MM:SS numbers*/ int seekto; if (rel == 1) seekto = mpd_status_get_elapsed_time(status) + seekchange; else if (rel == -1) seekto = mpd_status_get_elapsed_time(status) - seekchange; else seekto = seekchange; if (seekto > (int)mpd_status_get_total_time(status)) DIE("Seek amount would seek past the end of the song\n"); if (!mpd_run_seek_id(conn, mpd_status_get_song_id(status), seekto)) printErrorAndExit(conn); mpd_status_free(status); return 1; }
void updatempdstatus(void) { char statustext[STEXTSIZE]; struct mpd_status *s = NULL; struct mpd_song *so = NULL; int s_volume, s_flagmask, s_queuelength, s_songpos, s_state, s_songlen, s_songtime; int s_time_r, s_time_min, s_time_sec; const char *artist, *title; if(mpdcmd_connect() != 0) return; s = mpd_run_status(mpdc); if(s == NULL) return; // retrieve status information if((s_state = mpd_status_get_state(s)) == MPD_STATE_STOP || (so = mpd_run_current_song(mpdc)) == NULL) goto EXIT; s_volume = mpd_status_get_volume(s); s_queuelength = mpd_status_get_queue_length(s); s_songpos = mpd_status_get_song_pos(s); s_songlen = mpd_status_get_total_time(s); s_songtime = mpd_status_get_elapsed_time(s); s_time_r = s_songlen - s_songtime; s_time_sec = s_time_r % 60; s_time_min = (s_time_r - s_time_sec) / 60; if(mpd_status_get_consume(s) == 1) s_flagmask |= MpdFlag_Consume; if(mpd_status_get_single(s) == 1) s_flagmask |= MpdFlag_Single; if(mpd_status_get_random(s) == 1) s_flagmask |= MpdFlag_Random; if(mpd_status_get_repeat(s) == 1) s_flagmask |= MpdFlag_Repeat; artist = mpd_song_get_tag(so, MPD_TAG_ARTIST, 0); title = mpd_song_get_tag(so, MPD_TAG_TITLE, 0); //%artist - %title (-%total-%elapsed) [$flags] [#%pos/%queuelength] snprintf(statustext, STEXTSIZE, "%s - %s (-%d:%02d)", artist != NULL ? artist : "名無し", title != NULL ? title : "曲名無し", s_time_min, s_time_sec); strncpy(stext, statustext, STEXTSIZE); EXIT:; mpd_song_free(so); mpd_status_free(s); return; }
bool handle_player_command(struct mpdclient *c, command_t cmd) { struct mpd_connection *connection; if (!mpdclient_is_connected(c) || c->status == NULL) return false; cancel_seek_timer(); switch(cmd) { /* case CMD_PLAY: mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING); break; */ case CMD_PAUSE: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_pause(connection, mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) mpdclient_handle_error(c); break; case CMD_STOP: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_stop(connection)) mpdclient_handle_error(c); break; case CMD_CROP: mpdclient_cmd_crop(c); break; case CMD_SEEK_FORWARD: if (!setup_seek(c)) break; seek_target_time += options.seek_time; if (seek_target_time > (int)mpd_status_get_total_time(c->status)) seek_target_time = mpd_status_get_total_time(c->status); break; case CMD_TRACK_NEXT: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_next(connection)) mpdclient_handle_error(c); break; case CMD_SEEK_BACKWARD: if (!setup_seek(c)) break; seek_target_time -= options.seek_time; if (seek_target_time < 0) seek_target_time = 0; break; case CMD_TRACK_PREVIOUS: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_previous(connection)) mpdclient_handle_error(c); break; case CMD_SHUFFLE: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (mpd_run_shuffle(connection)) screen_status_message(_("Shuffled playlist")); else mpdclient_handle_error(c); break; case CMD_CLEAR: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (mpdclient_cmd_clear(c)) screen_status_message(_("Cleared playlist")); break; case CMD_REPEAT: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_repeat(connection, !mpd_status_get_repeat(c->status))) mpdclient_handle_error(c); break; case CMD_RANDOM: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_random(connection, !mpd_status_get_random(c->status))) mpdclient_handle_error(c); break; case CMD_SINGLE: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_single(connection, !mpd_status_get_single(c->status))) mpdclient_handle_error(c); break; case CMD_CONSUME: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_consume(connection, !mpd_status_get_consume(c->status))) mpdclient_handle_error(c); break; case CMD_CROSSFADE: connection = mpdclient_get_connection(c); if (connection == NULL) break; if (!mpd_run_crossfade(connection, mpd_status_get_crossfade(c->status) > 0 ? 0 : options.crossfade_time)) mpdclient_handle_error(c); break; case CMD_DB_UPDATE: screen_database_update(c, NULL); break; case CMD_VOLUME_UP: mpdclient_cmd_volume_up(c); break; case CMD_VOLUME_DOWN: mpdclient_cmd_volume_down(c); break; default: return false; } return true; }
/*btn3_short_press*/ void btn3_short_press(void) { int res = json_type_null; json_object * pjson_obj_read = NULL; struct mpd_status *status = NULL; ncurrt_time = 0; system("killall aplay"); system("killall xfchat"); pthread_rwlock_wrlock(&json_rwlock_voice); pjson_obj_read = json_object_from_file(PLAY_VOICE_JSON_PATH); pthread_rwlock_unlock(&json_rwlock_voice); res = json_object_get_type(pjson_obj_read); printf("json_type: %u \n", json_object_get_type(pjson_obj_read)); // printf("json_length: %u \n", json_object_array_length(pjson_obj_read)); json_object_put(pjson_obj_read); if ((json_type_array == res) && (0 < json_object_array_length(pjson_obj_read)) && (!bplay_audio)) { printf("nd btn3_short_press ret: %d, bplay_audio: %d\n", res, bplay_audio); my_mpd_run_pause(); bplay_audio = true; } else { int quere_len = 0; int online_size = 0; bplay_audio = false; online_size = get_file_size(ONLINE_LIST_PATH); if (online_size > 0) { quere_len = get_mpc_quere_len(conn); pthread_mutex_lock(&mutex); if (quere_len > 0) { mpd_run_stop(conn); mpd_run_clear(conn); } bool mpc_load = mpd_run_load(conn, "online.lst"); bool mpc_list_clear = mpd_run_playlist_clear(conn, "online.lst"); printf("nd btn3_short_press mpc_load: %d, mpc_list_clear: %d\n", mpc_load, mpc_list_clear); pthread_mutex_unlock(&mutex); res = i2c_smbus_write_byte_data(i2c_file, 0x00, BLUE_OFF); if (res < 0) { printf("nd btn3_short_press i2c_smbus_write_byte_data BLUE_OFF failed, ret: %d\n", res); } } quere_len = get_mpc_quere_len(conn); if (quere_len > 0) { printf("nd btn3_short_press get_mpc_quere_len quere_len: %d\n", quere_len); pthread_mutex_lock(&mutex); status = mpd_run_status(conn); if (!status) { printf("nd btn3_short_press mpd_run_status2 %s \n", mpd_connection_get_error_message(conn)); } else { if (mpd_status_get_state(status) == MPD_STATE_PLAY) { printf("nd btn3_short_press mpd_status_get_state MPD_STATE_PLAY\n"); mpd_run_pause(conn, true); } else { printf("nd btn3_short_press mpd_status_get_state other state\n"); mpd_run_play(conn); } mpd_status_free(status); } pthread_mutex_unlock(&mutex); } } }
/* This function update the internal status tracking * the MPD status. At then end, if the status has changed, a signal is emmited. */ void Player::updateStatus() { struct mpd_status *statusMpd; EMSPlayerStatus statusEMS; /* Get the status structure from MPD */ mpd_send_status(conn); statusMpd = mpd_recv_status(conn); if (statusMpd == NULL) { qCritical() << "Error while trying to get MPD status"; qCritical() << "Reconnecting..."; connectToMpd(); /* Reconnect */ return; } if (mpd_status_get_error(statusMpd) != NULL) { qCritical() << "MPD error: " << QString::fromUtf8(mpd_status_get_error(statusMpd)); } if (!mpd_response_finish(conn)) { qCritical() << "Error while trying to get MPD status" ; qCritical() << "Reconnecting..."; connectToMpd(); return; } /* Fill the internal state from the answer */ switch (mpd_status_get_state(statusMpd)) { case MPD_STATE_PAUSE: statusEMS.state = STATUS_PAUSE; break; case MPD_STATE_PLAY: statusEMS.state = STATUS_PLAY; break; case MPD_STATE_STOP: statusEMS.state = STATUS_STOP; break; default: statusEMS.state = STATUS_UNKNOWN; break; } statusEMS.repeat = mpd_status_get_repeat(statusMpd); statusEMS.random = mpd_status_get_random(statusMpd); if (statusEMS.state == STATUS_PLAY || statusEMS.state == STATUS_PAUSE) { if (mpd_status_get_queue_length(statusMpd) != (unsigned int)playlist.tracks.size()) { qCritical() << "Error: the playlist does not have the same size as the one used by MPD!"; /* We should not do this... */ removeAllTracks(); } statusEMS.posInPlaylist = mpd_status_get_song_pos(statusMpd); statusEMS.progress = mpd_status_get_elapsed_time(statusMpd); } mpd_status_free(statusMpd); if (statusEMS.posInPlaylist != status.posInPlaylist || statusEMS.progress != status.progress || statusEMS.random != status.random || statusEMS.repeat != status.repeat || statusEMS.state != status.state ) { mutex.lock(); status = statusEMS; mutex.unlock(); emit statusChanged(statusEMS); } }
static void env_export_status(struct mpd_status *status) { char *envstr; const struct mpd_audio_format *fmt; envstr = g_strdup_printf("%d", mpd_status_get_volume(status)); g_setenv("MPD_STATUS_VOLUME", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_repeat(status)); g_setenv("MPD_STATUS_REPEAT", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_random(status)); g_setenv("MPD_STATUS_RANDOM", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_single(status)); g_setenv("MPD_STATUS_SINGLE", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_consume(status)); g_setenv("MPD_STATUS_CONSUME", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_queue_length(status)); g_setenv("MPD_STATUS_QUEUE_LENGTH", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_queue_version(status)); g_setenv("MPD_STATUS_QUEUE_VERSION", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_crossfade(status)); g_setenv("MPD_STATUS_CROSSFADE", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_song_pos(status)); g_setenv("MPD_STATUS_SONG_POS", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%d", mpd_status_get_song_id(status)); g_setenv("MPD_STATUS_SONG_ID", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", mpd_status_get_elapsed_time(status)); g_setenv("MPD_STATUS_ELAPSED_TIME", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", mpd_status_get_elapsed_ms(status)); g_setenv("MPD_STATUS_ELAPSED_MS", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", mpd_status_get_total_time(status)); g_setenv("MPD_STATUS_TOTAL_TIME", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", mpd_status_get_kbit_rate(status)); g_setenv("MPD_STATUS_KBIT_RATE", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", mpd_status_get_update_id(status)); g_setenv("MPD_STATUS_UPDATE_ID", envstr, 1); g_free(envstr); g_setenv("MPD_STATUS_STATE", env_strstate(mpd_status_get_state(status)), 1); if ((fmt = mpd_status_get_audio_format(status)) == NULL) g_setenv("MPD_STATUS_AUDIO_FORMAT", "0", 1); else { g_setenv("MPD_STATUS_AUDIO_FORMAT", "1", 1); envstr = g_strdup_printf("%u", fmt->sample_rate); g_setenv("MPD_STATUS_AUDIO_FORMAT_SAMPLE_RATE", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", fmt->bits); g_setenv("MPD_STATUS_AUDIO_FORMAT_BITS", envstr, 1); g_free(envstr); envstr = g_strdup_printf("%u", fmt->channels); g_setenv("MPD_STATUS_AUDIO_FORMAT_CHANNELS", envstr, 1); g_free(envstr); } }
/* * Returns: 0 - error; 1 - ok */ int mgr_mpd_fetch_status( struct mpd_connection *conn, int dump_status ) { struct mpd_status *status; struct mpd_stats *stats; if (DEBUG > 1) fprintf(stderr, "DEBUG: [mgr-thread] mpd: -status fetching...\n"); if (conn == NULL) { fprintf(stderr, "ERROR: MPD: Unable to retrieve the MPD status: not connected\n"); return 0; } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { fprintf(stderr, "ERROR: MPD: Unable to retrieve MPD status: connection error\n"); return 0; } if (DEBUG > 1) fprintf(stderr, "DEBUG: [mgr-thread] mpd: run status\n"); status = mpd_run_status(conn); if (!status) { fprintf(stderr, "ERROR: MPD: Unable to retrieve MPD status\n"); return 0; } cur_mpd_player_status = mpd_status_get_state(status); if (DEBUG > 1) { fprintf(stderr, "DEBUG: [mgr-thread] mpd: +status received:\n"); } if (DEBUG && dump_status) { fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => mode : %s\n", (cur_mpd_player_status == MPD_STATE_PLAY ? "Play" : "Other")); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => random : %d\n", mpd_status_get_random(status)); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => repeat : %d\n", mpd_status_get_random(status)); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => volume : %d\n", mpd_status_get_volume(status)); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => queue ver : %d\n", mpd_status_get_queue_version(status)); fprintf(stderr, "DEBUG: [mgr-thread] mpd status: => queue len : %d\n", mpd_status_get_queue_length(status)); } cur_mpd_random = mpd_status_get_random(status) ? 1 : 0; cur_mpd_volume = mpd_status_get_volume(status); cur_mpd_status_queue_len = mpd_status_get_queue_length(status); if (DEBUG > 1) fprintf(stderr, "DEBUG: [mgr-thread] mpd: release status\n"); if (mpd_status_get_error(status) != NULL) { fprintf(stderr, "WARNING: MPD: Error Received from MPD: %s\n", mpd_status_get_error(status)); // TODO - clear error } mpd_status_free(status); mpd_response_finish(conn); stats = mpd_run_stats(conn); if (stats == NULL) { fprintf(stderr, "ERROR: MPD: Unable to retrieve MPD statistics\n"); return 0; } cur_mpd_stats_number_of_songs = mpd_stats_get_number_of_songs( stats ); if (DEBUG && dump_status) { fprintf(stderr, "DEBUG: [mgr-thread] mpd stats : => # of songs: %d\n", cur_mpd_stats_number_of_songs); } mpd_stats_free( stats ); return 1; }
/*** 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; }