Example #1
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;
}
Example #2
0
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;
}
Example #3
0
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;
}
Example #4
0
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;
}
Example #5
0
File: mpd.c Project: cmende/mpd2irc
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;
	}
}
Example #6
0
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;
}
Example #7
0
/*
 * 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);
}
Example #8
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();
}
/* 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);
    }
}
/*** 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;
}
Example #11
0
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
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);
  }
}
Example #13
0
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;
    }
}
Example #14
0
File: status.c Project: GGbaba/mpc
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);
}