Exemplo n.º 1
0
Arquivo: mpd.c Projeto: cmende/mpd2irc
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();
	}
}
Exemplo n.º 2
0
int mpd_put_current_song(char *buffer)
{
    char *cur = buffer;
    const char *end = buffer + MAX_SIZE;
    struct mpd_song *song;

    song = mpd_run_current_song(mpd.conn);
    if(song == NULL)
        return 0;

    cur += json_emit_raw_str(cur, end - cur, "{\"type\": \"song_change\", \"data\":{\"pos\":");
    cur += json_emit_int(cur, end - cur, mpd_song_get_pos(song));
    cur += json_emit_raw_str(cur, end - cur, ",\"title\":");
    cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song));

    if(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0) != NULL)
    {
        cur += json_emit_raw_str(cur, end - cur, ",\"artist\":");
        cur += json_emit_quoted_str(cur, end - cur, mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
    }

    if(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0) != NULL)
    {
        cur += json_emit_raw_str(cur, end - cur, ",\"album\":");
        cur += json_emit_quoted_str(cur, end - cur, mpd_song_get_tag(song, MPD_TAG_ALBUM, 0));
    }

    cur += json_emit_raw_str(cur, end - cur, "}}");
    mpd_song_free(song);
    mpd_response_finish(mpd.conn);

    return cur - buffer;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
/*
 * Retrieves the currently playing song from MPD, and caches it locally.
 *
 * Returns true on success, false on failure.
 */
bool
Control::get_current_song()
{
	bool status_ok;
	struct mpd_song * song;

	EXIT_IDLE;

	song = mpd_run_current_song(conn->h());
	status_ok = get_error_bool();

	if (!status_ok) {
		return status_ok;
	}

	if (_song != NULL) {
		delete _song;
		_song = NULL;
	}

	if (song) {
		_song = new Song(song);
		mpd_song_free(song);
	}

	return get_error_bool();
}
Exemplo n.º 5
0
std::string Playback::getCurrentSongInfo(Playback::song_property property)
{
	printf("in Playback::getCurrentSongInfo\n");
	int error = mpd_connection_get_error(Connection::getConnection());
	printf("Error: %d\n", error);
	this->currentSong = mpd_run_current_song(Connection::getConnection());
	error = mpd_connection_get_error(Connection::getConnection());
	printf("Error: %d\n", error);

	std::string retVal;

	if (this->currentSong == nullptr)
		retVal = "ERROR";

	else
	{
		switch (property)
		{
		case Playback::ALBUM: retVal = mpd_song_get_tag(this->currentSong, MPD_TAG_ALBUM, 0); printf("%s\n", retVal.c_str()); break;
		case Playback::ARTIST: retVal = mpd_song_get_tag(this->currentSong, MPD_TAG_ARTIST, 0); printf("%s\n", retVal.c_str()); break;
		case Playback::TITLE: retVal = mpd_song_get_tag(this->currentSong, MPD_TAG_TITLE, 0); printf("%s\n", retVal.c_str()); break;
		case Playback::YEAR: retVal = mpd_song_get_tag(this->currentSong, MPD_TAG_DATE, 0); printf("%s\n", retVal.c_str()); break;
		case Playback::TRACKNR: retVal = mpd_song_get_tag(this->currentSong, MPD_TAG_TRACK, 0); printf("%s\n", retVal.c_str()); break;
		default: retVal = ""; printf("%s\n", retVal.c_str()); break;
		}
	}
	return retVal;
}
Exemplo n.º 6
0
int get_current_song_rating()
{
    int rating;
    struct mpd_song *song;

    song = mpd_run_current_song(mpd.conn);
    if(song == NULL)
        return 0;

    rating = db_get_song_rating(mpd_get_title(song), mpd_get_artist(song));

    mpd_song_free(song);
    return rating;
}
Exemplo n.º 7
0
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;
}
Exemplo n.º 8
0
CLIENT_OBJECT * new_client() {

    CLIENT_OBJECT *cli = (CLIENT_OBJECT *)malloc(sizeof(*cli));
    cli->conn = mpd_connection_new(NULL,0,0);
    cli->status = mpd_run_status(cli->conn);
    cli->song = mpd_run_current_song(cli->conn);
    cli->stats = mpd_run_stats(cli->conn);

    if (!(cli->conn && cli->status && cli->stats))
        return NULL;

    cli->idle = false;
    return cli;
}
Exemplo n.º 9
0
char* mpd_get_current_artist()
{   
    struct mpd_song *song;

    if (mpd.conn_state == MPD_CONNECTED) {
        song = mpd_run_current_song(mpd.conn);
        if(song == NULL) {
            printf("song == NULL\n");
            return NULL;
        }
        sprintf(artistbuf, "%s", mpd_get_artist(song));
        mpd_song_free(song);
    }

    return artistbuf;
}
Exemplo n.º 10
0
char* mpd_get_current_album()
{
    struct mpd_song *song;

    if (mpd.conn_state == MPD_CONNECTED) {
        song = mpd_run_current_song(mpd.conn);
        if(song == NULL) {
            printf("song == NULL\n");
            return NULL;
        }
        sprintf(albumbuf, "%s", mpd_get_album(song));
        mpd_song_free(song);
    }

    mpd_response_finish(mpd.conn);

    return albumbuf;
}
Exemplo n.º 11
0
int mpd_delete_current_song(struct mpd_connection *conn)
{
    struct mpd_song *song;
    char *currentsonguri = NULL;

    song = mpd_run_current_song(conn);
    if(song == NULL)
        return 0;

    db_update_song_rating(mpd_get_title(song),
            mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), -5);

    currentsonguri = mpd_song_get_uri(song);
    printf("%s: let's delete song: %s\n", __func__, currentsonguri);

    delete_file_forever(currentsonguri);

    mpd_song_free(song);

    return 1;
}
Exemplo n.º 12
0
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;
	unsigned int current = 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);

		while (true) {
			unsigned int got = 0;

			mpd_run_idle_mask(mpd, MPD_IDLE_PLAYER);
			CHECK_MPD_CONN(mpd);

			song = mpd_run_current_song(mpd);
			CHECK_MPD_CONN(mpd);

			got = mpd_song_get_id(song);
			mpd_song_free(song);

			if (current != got) {
				mpd_run_play_id(mpd, id);
				CHECK_MPD_CONN(mpd);

				mpd_connection_free(mpd);
				return 0;
			}
		}
	}

	mpd_connection_free(mpd);

	return 0;
}
Exemplo n.º 13
0
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;
}
Exemplo n.º 14
0
void print_mpd(yajl_gen json_gen, char *buffer, const char *host, int port, const char *password, const char *format) {
        const char *walk;
        char *outwalk = buffer;
        static char titlebuf[40];
        static char artistbuf[40];
        static char albumbuf[40];
        static char trackbuf[10];
        static char datebuf[10];

        static struct mpd_connection *conn;
        struct mpd_status *status = NULL;
        enum mpd_state state;
        struct mpd_song *song;

        /* First run */
        if (conn == NULL) {
                conn = mpd_connection_new(host, port, 1500);
                if (conn == NULL) {
                        START_COLOR("color_bad");
                        outwalk += sprintf(outwalk, "%s", "ERROR");
                        goto print_end;
                }

                if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
                        START_COLOR("color_bad");
                        outwalk += sprintf(outwalk, "%s", "CONNECT ERROR");
                        mpd_connection_free(conn);
                        conn = NULL;
                        goto print_end;
                }

                if (password != NULL && strcmp(password, "") != 0 &&
                                !mpd_run_password(conn, password)) {
                        START_COLOR("color_bad");
                        outwalk += sprintf(outwalk, "%s", "PASS ERROR");
                        mpd_connection_free(conn);
                        conn = NULL;
                        goto print_end;
                }
        }

        if ((status = mpd_run_status(conn))) {
                state = mpd_status_get_state(status);
        }

        if (!status || (state != MPD_STATE_PLAY && state != MPD_STATE_PAUSE)) {
                START_COLOR("color_bad");
                outwalk += sprintf(outwalk, "%s", "Stopped");
                mpd_connection_free(conn);
                conn = NULL;
                goto print_end;
        }

        mpd_status_free(status);

        if (state == MPD_STATE_PLAY)
                START_COLOR("color_good");
        else if (state == MPD_STATE_PAUSE)
                START_COLOR("color_degraded");

        song = mpd_run_current_song(conn);

        COPY_CROP(titlebuf, mpd_song_get_tag(song, MPD_TAG_TITLE, 0));
        COPY_CROP(artistbuf, mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
        COPY_CROP(albumbuf, mpd_song_get_tag(song, MPD_TAG_ALBUM, 0));
        COPY_CROP(trackbuf, mpd_song_get_tag(song, MPD_TAG_TRACK, 0));
        COPY_CROP(datebuf, mpd_song_get_tag(song, MPD_TAG_DATE, 0));

        mpd_song_free(song);

        for (walk = format; *walk != '\0'; walk++) {
                if (*walk != '%') {
                        *(outwalk++) = *walk;
                        continue;
                }

                if (BEGINS_WITH(walk+1, "title")) {
                        if (*titlebuf)
                                outwalk += sprintf(outwalk, "%s", titlebuf);
                        walk += strlen("title");
                }
                else if (BEGINS_WITH(walk+1, "artist")) {
                        if (*artistbuf)
                                outwalk += sprintf(outwalk, "%s", artistbuf);
                        walk += strlen("artist");
                }
                else if (BEGINS_WITH(walk+1, "album")) {
                        if (*albumbuf)
                                outwalk += sprintf(outwalk, "%s", albumbuf);
                        walk += strlen("album");
                }
                else if (BEGINS_WITH(walk+1, "track")) {
                        if (*trackbuf)
                                outwalk += sprintf(outwalk, "%s", trackbuf);
                        walk += strlen("track");
                }
                else if (BEGINS_WITH(walk+1, "date")) {
                        if (*datebuf)
                                outwalk += sprintf(outwalk, "%s", datebuf);
                        walk += strlen("date");
                }
        }

print_end:

        END_COLOR;
        OUTPUT_FULL_TEXT(buffer);

        return;
}
Exemplo n.º 15
0
void
CMPD::Update()
{
	if(!_connected) {
        iprintf("Reconnecting in 10 seconds.");
        sleep(10);
		if(Connect())
            iprintf("%s", "Reconnected!");
        else {
            eprintf("%s", "Could not reconnect.");
            return;
        }
	}

    mpd_status *status = mpd_run_status(_conn);
    mpd_stats *stats = mpd_run_stats(_conn);

    if(status && stats) {
        int newsongid = mpd_status_get_song_id(status);
        int newsongpos = mpd_status_get_elapsed_time(status);
        int curplaytime = mpd_stats_get_play_time(stats);

        // new song
        if(newsongid != _songid) {
            _songid = newsongid;
            _songpos = newsongpos;
            _start = curplaytime;

            mpd_song *song = mpd_run_current_song(_conn);
            if(song) {
                GotNewSong(song);
                mpd_song_free(song);
            }
        }

        // song playing
        if(newsongpos != _songpos) {
            _songpos = newsongpos;
            CheckSubmit(curplaytime);
        }

        // check for client-to-client messages
        if(mpd_send_read_messages(_conn)) {
            mpd_message *msg;
            while((msg = mpd_recv_message(_conn)) != NULL) {
                const char *text = mpd_message_get_text(msg);
                if(_gotsong && text && !strncmp(text, "love", 4))
                    AudioScrobbler->LoveTrack(_song);
                mpd_message_free(msg);
            }
            mpd_response_finish(_conn);
        }

        mpd_status_free(status);
        mpd_stats_free(stats);
    }
    else { // we have most likely lost our connection
        eprintf("Could not query MPD server: %s", mpd_connection_get_error_message(_conn));
        _connected = false;
    }
}