void MPDConnection::connectToMPD(const ServerInfo &server) {
	if (d->connected) {
		if (d->server == server) // Trying to reconncet to same server, ignore
			return;
		// Trying to connect to another server. disconnect, then connect to it.
		disconnectFromMPD();
	}

	setCaller("MPDConnection::connectToMPD", "mpd_newConnection");
	d->connection = mpd_newConnection(server.address().toUtf8(), server.port(), Config::instance()->timeoutTime());
	if (!finishCommand()) {
		disconnectFromMPD(tr("Could not connect to server %1:%2").arg(server.address()).arg(server.port()));
		return;
	}

	if (!server.password().isEmpty()) {
		mpd_call(MPDConnection::connectToMPD, Password, server.password().toUtf8());
		if (!finishCommand()) {
			disconnectFromMPD(tr("Authentication failed"));
			return;
		}
	}

	d->connected = true;
	d->server = server;
	emit connected(server);
}
Exemple #2
0
bool MpdClient::start()
{
	char *host = getenv("MPD_HOST");
	char *port = getenv("MPD_PORT");

	if (host == NULL)
		host = "localhost";
	if (port == NULL)
		port = "6600";

	conn = mpd_newConnection(host, atoi(port), 10);

	if (conn->error) {
		error(conn->errorStr);
		return false;
	}

	update();

	QTimer *timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(update()));
	timer->start(1000);

	return true;
}
static void
_mpdule_connect (Instance * inst)
{
  mpd_Connection *mpd;
  Config_Item *ci;

  ci = inst->ci;
  mpd = mpd_newConnection (ci->hostname, ci->port, 3.0);
  inst->mpd = mpd;
}
Exemple #4
0
void mpdConnect()
{
	char *hostname = strdup(config->mpd_hostname);
	int port = config->mpd_hostport;
	int timeout = config->mpd_timeout;

	conn = mpd_newConnection(hostname,port,timeout);

	if(conn->error) {
		strcpy(songname, conn->errorStr);
		scrollpos = 0;
		updateSongTitle();
		mpd_closeConnection(conn);
	}
}
Exemple #5
0
/******************** Connection handling ***********************************/
static int  connectToMPD(unsigned timeout) {
	disconnectFromMPD();

	D.conn = mpd_newConnection(D.host, D.port, timeout);
	pdie_on(!D.conn, "connect");

	if (D.password && !error()) {
		mpd_sendPasswordCommand(D.conn, D.password);
		if (!error()) {
			mpd_finishCommand(D.conn);
		}
	}

	return !error();
}
Exemple #6
0
int main(int argc, char ** argv) {
	char *string;
	mpd_Connection * conn;
	conn = mpd_newConnection("192.150.0.108",6600,10);

	if(conn->error) {
		fprintf(stderr,"%s\n",conn->errorStr);
		mpd_closeConnection(conn);
		return -1;
	}
	int i=0;
//	for(i=0;i<1000;i++)
//	{
/*/	
	mpd_sendCommandsCommand(conn);
	while (( string = mpd_getNextCommand(conn)) != NULL)
	{
		//printf("%s\n", string);
		free(string);
	}
	mpd_finishCommand(conn);
    */
//	}
//
//
//
//



    
    for(i=0;i<100;i++)
    {
        mpd_sendPlChangesCommand(conn,0); 
        mpd_InfoEntity *ent = NULL;
        while((ent = mpd_getNextInfoEntity(conn)) != NULL){
//            		printf("%s\n", ent->info.song->artist);
            mpd_freeInfoEntity(ent);
        }
    }
	mpd_closeConnection(conn);

	return 0;
}
Exemple #7
0
static int mpdclient_connect() {
	char *hostname = getenv("MPD_HOST");
	char *port = getenv("MPD_PORT");

	if(hostname == NULL)
		hostname = "localhost";
	if(port == NULL)
		port = "6600";

	conn = mpd_newConnection(hostname,atoi(port),10);

	if(conn->error) {
		fprintf(stderr,"%s\n",conn->errorStr);
		mpd_closeConnection(conn);
		return -1;
	}

	return 0;
}
Exemple #8
0
/*
=================
MPD_Connect - connect to MPD server
=================
*/
void MPD_Connect(void)
{
	MPD_Disconnect();

	/* Connecting to MPD server */
	conn = mpd_newConnection(mpd_host->string, mpd_port->integer, 10);

	/* Checking if we failed to connect */
	if(conn->error)
	{
		Com_Printf("MPD: %s\n", conn->errorStr);
		mpd_closeConnection(conn);
		return;
	} 
	
	mpdConnection = true;

	// send da password if we're using one
	if(mpd_pass->string[0]) {
		mpd_sendPasswordCommand(conn, mpd_pass->string);
		mpd_finishCommand(conn);
	}
}
Exemple #9
0
static void *update_mpd_thread(void *arg)
{
	static mpd_Connection *conn = NULL;
	mpd_Status *status;
	mpd_InfoEntity *entity;
	timed_thread *me = *(timed_thread **)arg;
	const char *emptystr = "";

	while (1) {
		if (!conn)
			conn = mpd_newConnection(mpd_host, mpd_port, 10);

		if (*mpd_password) {
			mpd_sendPasswordCommand(conn, mpd_password);
			mpd_finishCommand(conn);
		}

		timed_thread_lock(me);

		if (conn->error || conn == NULL) {
			NORM_ERR("MPD error: %s\n", conn->errorStr);
			mpd_closeConnection(conn);
			conn = 0;
			clear_mpd();

			mpd_info.status = "MPD not responding";
			timed_thread_unlock(me);
			if (timed_thread_test(me, 0)) {
				timed_thread_exit(me);
			}
			continue;
		}

		mpd_sendStatusCommand(conn);
		if ((status = mpd_getStatus(conn)) == NULL) {
			NORM_ERR("MPD error: %s\n", conn->errorStr);
			mpd_closeConnection(conn);
			conn = 0;
			clear_mpd();

			mpd_info.status = "MPD not responding";
			timed_thread_unlock(me);
			if (timed_thread_test(me, 0)) {
				timed_thread_exit(me);
			}
			continue;
		}
		mpd_finishCommand(conn);
		if (conn->error) {
			// fprintf(stderr, "%s\n", conn->errorStr);
			mpd_closeConnection(conn);
			conn = 0;
			timed_thread_unlock(me);
			if (timed_thread_test(me, 0)) {
				timed_thread_exit(me);
			}
			continue;
		}

		mpd_info.vol = status->volume;
		if (status->random == 0) {
			mpd_info.random = "Off";
		} else if (status->random == 1) {
			mpd_info.random = "On";
		} else {
			mpd_info.random = "";
		}
		if (status->repeat == 0) {
			mpd_info.repeat = "Off";
		} else if (status->repeat == 1) {
			mpd_info.repeat = "On";
		} else {
			mpd_info.repeat = "";
		}
		/* if (status->error) {
			printf("error: %s\n", status->error);
		} */

		switch (status->state) {
			case MPD_STATUS_STATE_PLAY:
				mpd_info.status = "Playing";
				break;
			case MPD_STATUS_STATE_STOP:
				mpd_info.status = "Stopped";
				break;
			case MPD_STATUS_STATE_PAUSE:
				mpd_info.status = "Paused";
				break;
			default:
				mpd_info.status = "";
				clear_mpd();
				break;
		}

		if (status->state == MPD_STATUS_STATE_PLAY ||
		    status->state == MPD_STATUS_STATE_PAUSE) {
			mpd_info.is_playing = 1;
			mpd_info.bitrate = status->bitRate;
			mpd_info.progress = (float) status->elapsedTime /
				status->totalTime;
			mpd_info.elapsed = status->elapsedTime;
			mpd_info.length = status->totalTime;
		} else {
			mpd_info.progress = 0;
			mpd_info.is_playing = 0;
			mpd_info.elapsed = 0;
		}

		if (conn->error) {
			// fprintf(stderr, "%s\n", conn->errorStr);
			mpd_closeConnection(conn);
			conn = 0;
			timed_thread_unlock(me);
			if (timed_thread_test(me, 0)) {
				timed_thread_exit(me);
			}
			continue;
		}

		mpd_sendCurrentSongCommand(conn);
		while ((entity = mpd_getNextInfoEntity(conn))) {
			mpd_Song *song = entity->info.song;

			if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
				mpd_freeInfoEntity(entity);
				continue;
			}
#define SONGSET(x) {                            \
	free(mpd_info.x);                       \
	if(song->x)                             \
		mpd_info.x = strmdup(song->x);  \
	else                                    \
		mpd_info.x = strmdup(emptystr); \
}
			SONGSET(artist);
			SONGSET(albumartist);
			SONGSET(album);
			SONGSET(title);
			SONGSET(date);
			SONGSET(track);
			SONGSET(name);
			SONGSET(file);
#undef SONGSET
			if (entity != NULL) {
				mpd_freeInfoEntity(entity);
				entity = NULL;
			}
		}
		mpd_finishCommand(conn);
		if (conn->error) {
			// fprintf(stderr, "%s\n", conn->errorStr);
			mpd_closeConnection(conn);
			conn = 0;
			timed_thread_unlock(me);
			if (timed_thread_test(me, 0)) {
				timed_thread_exit(me);
			}
			continue;
		}

		timed_thread_unlock(me);
		if (conn->error) {
			// fprintf(stderr, "%s\n", conn->errorStr);
			mpd_closeConnection(conn);
			conn = 0;
			if (timed_thread_test(me, 0)) {
				timed_thread_exit(me);
			}
			continue;
		}

		mpd_freeStatus(status);
		/* if (conn) {
			mpd_closeConnection(conn);
			conn = 0;
		} */
		if (timed_thread_test(me, 0)) {
			timed_thread_exit(me);
		}
		continue;
	}
	/* never reached */
}
Exemple #10
0
int mpd_connect_real(MpdObj *mi,mpd_Connection *connection)
{
    int retv;
    if(mi == NULL)
	{
		/* should return some spiffy error here */
		debug_printf(DEBUG_ERROR, "mi != NULL failed");
		return MPD_ARGS_ERROR;
	}
	/* reset errors */
	mi->error = 0;
	mi->error_mpd_code = 0;
	if(mi->error_msg != NULL)
	{
		free(mi->error_msg);
	}
	mi->error_msg = NULL;

	debug_printf(DEBUG_INFO, "connecting\n");
	mpd_init_MpdServerState(&(mi->CurrentState));

	memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState));

	if(mi->connected)
	{
		/* disconnect */
		mpd_disconnect(mi);
	}

	if(mi->hostname == NULL)
	{
		mpd_set_hostname(mi, "localhost");
	}
	/* make sure this is locked */
	if(!mi->connection_lock)
	{
		mpd_lock_conn(mi);
	}
	if(connection) {
		mi->connection = connection;
	} else {
		/* make timeout configurable */
		mi->connection = mpd_newConnection(mi->hostname,mi->port,mi->connection_timeout);
	}
	if(mi->connection == NULL)
	{
		/* TODO: make seperate error message? */
		return MPD_NOT_CONNECTED;
	}
	if(mpd_check_error(mi) != MPD_OK)
	{
		/* TODO: make seperate error message? */
		return MPD_NOT_CONNECTED;
	}

	/* set connected state */
	mi->connected = TRUE;
	if(mpd_unlock_conn(mi))
	{
		return MPD_LOCK_FAILED;
	}

	/* get the commands we are allowed to use */
	retv = mpd_server_get_allowed_commands(mi);
    if(retv!= MPD_OK)
    {
        return retv;
    }
    if(mi->the_connection_changed_callback != NULL)
	{
		mi->the_connection_changed_callback( mi, TRUE, mi->the_connection_changed_signal_userdata );
	}

	debug_printf(DEBUG_INFO, "Connected to mpd");
	return MPD_OK;
}