예제 #1
0
파일: mpd_client.c 프로젝트: HyShai/ympd
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;
}
예제 #2
0
파일: command.cpp 프로젝트: reynhout/pms
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;
}
예제 #3
0
파일: lib.c 프로젝트: qsn/mpdfs
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);
}
예제 #4
0
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);
	}
}
예제 #5
0
파일: manager.c 프로젝트: zaruba/zmp3
/*
 * 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;
}
예제 #6
0
파일: status.c 프로젝트: alip/luampdclient
static int lmpdstatus_index(lua_State *L)
{
	const char *key;
	const struct mpd_audio_format *audio_format;
	struct mpd_status **status;

	status = luaL_checkudata(L, 1, MPD_STATUS_T);
	key = luaL_checkstring(L, 2);

	assert(*status != NULL);

	if (strncmp(key, "volume", 7) == 0) {
		lua_pushinteger(L, mpd_status_get_volume(*status));
		return 1;
	}
	else if (strncmp(key, "repeat", 7) == 0) {
		lua_pushinteger(L, mpd_status_get_repeat(*status));
		return 1;
	}
	else if (strncmp(key, "random", 7) == 0) {
		lua_pushinteger(L, mpd_status_get_random(*status));
		return 1;
	}
	else if (strncmp(key, "single", 7) == 0) {
		lua_pushinteger(L, mpd_status_get_single(*status));
		return 1;
	}
	else if (strncmp(key, "consume", 8) == 0) {
		lua_pushinteger(L, mpd_status_get_consume(*status));
		return 1;
	}
	else if (strncmp(key, "queue_length", 16) == 0) {
		lua_pushnumber(L, mpd_status_get_queue_length(*status));
		return 1;
	}
	else if (strncmp(key, "queue_version", 17) == 0) {
		lua_pushnumber(L, mpd_status_get_queue_version(*status));
		return 1;
	}
	else if (strncmp(key, "state", 6) == 0) {
		lua_pushinteger(L, mpd_status_get_state(*status));
		return 1;
	}
	else if (strncmp(key, "crossfade", 10) == 0) {
		lua_pushinteger(L, mpd_status_get_crossfade(*status));
		return 1;
	}
	else if (strncmp(key, "song_pos", 5) == 0) {
		lua_pushinteger(L, mpd_status_get_song_pos(*status));
		return 1;
	}
	else if (strncmp(key, "song_id", 7) == 0) {
		lua_pushinteger(L, mpd_status_get_song_id(*status));
		return 1;
	}
	else if (strncmp(key, "elapsed_time", 13) == 0) {
		lua_pushinteger(L, mpd_status_get_elapsed_time(*status));
		return 1;
	}
	else if (strncmp(key, "total_time", 11) == 0) {
		lua_pushinteger(L, mpd_status_get_total_time(*status));
		return 1;
	}
	else if (strncmp(key, "kbit_rate", 10) == 0) {
		lua_pushinteger(L, mpd_status_get_kbit_rate(*status));
		return 1;
	}
	else if (strncmp(key, "audio_format", 13) == 0) {
		audio_format = mpd_status_get_audio_format(*status);
		lua_newtable(L);

		lua_pushinteger(L, audio_format->sample_rate);
		lua_setfield(L, -2, "sample_rate");

		lua_pushinteger(L, audio_format->bits);
		lua_setfield(L, -2, "bits");

		lua_pushinteger(L, audio_format->channels);
		lua_setfield(L, -2, "channels");

		return 1;
	}
	else if (strncmp(key, "update_id", 11) == 0) {
		lua_pushinteger(L, mpd_status_get_update_id(*status));
		return 1;
	}
	else if (strncmp(key, "error", 6) == 0) {
		lua_pushstring(L, mpd_status_get_error(*status));
		return 1;
	}
	else
		return luaL_error(L, "Invalid key `%s'", key);
}
예제 #7
0
파일: example.c 프로젝트: Fyzick/capstone
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;
}