Signed-off-by: Ali Polatel <[email protected]>
---
Makefile.am | 11 +---
src/lmc.c | 208 ++++++++++++++++++++++++-----------------------------
src/lmc.h | 2 +-
src/mpdscribble.c | 31 +++++---
4 files changed, 118 insertions(+), 134 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 3bb8922..22ad1c3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -9,12 +9,6 @@ EXTRA_DIST = autogen.sh
bin_PROGRAMS = src/mpdscribble
-libmpdclient_SOURCES = \
- src/libmpdclient/str_pool.c src/libmpdclient/str_pool.h \
- src/libmpdclient/resolver.c src/libmpdclient/resolver.h \
- src/libmpdclient/song.c src/libmpdclient/song.h \
- src/libmpdclient/libmpdclient.c src/libmpdclient/libmpdclient.h
-
src_mpdscribble_SOURCES = \
src/mpdscribble.c \
src/compat.h \
@@ -26,8 +20,7 @@ src_mpdscribble_SOURCES = \
src/file.c src/file.h \
src/journal.c src/journal.h \
src/lmc.c src/lmc.h \
- src/log.c src/log.h \
- $(libmpdclient_SOURCES)
+ src/log.c src/log.h
if HAVE_CURL
src_mpdscribble_SOURCES += src/http_client_curl.c
@@ -42,7 +35,7 @@ AM_CPPFLAGS = $(libmpdclient_CFLAGS) \
$(libgthread_CFLAGS) \
$(libgcrypt_CFLAGS) \
$(libglib_CFLAGS) \
- -I$(srcdir)/src -I$(srcdir)/src/libmpdclient
+ -I$(srcdir)/src
LDADD = $(libmpdclient_LIBS) \
$(libcurl_LIBS) $(libsoup_LIBS) \
$(libgthread_LIBS) \
diff --git a/src/lmc.c b/src/lmc.c
index d973d19..9f60b82 100644
--- a/src/lmc.c
+++ b/src/lmc.c
@@ -23,6 +23,7 @@
#include "compat.h"
#include <glib.h>
+#include <mpd/client.h>
#include <assert.h>
#include <stdbool.h>
@@ -31,7 +32,8 @@
#include <string.h>
#include <unistd.h>
-static mpd_Connection *g_mpd = NULL;
+static struct mpd_connection *g_mpd = NULL;
+static const unsigned *g_version = NULL;
static bool idle_supported, idle_notified;
static int last_id = -1;
static struct mpd_song *current_song;
@@ -53,11 +55,14 @@ lmc_schedule_idle(void);
static void lmc_failure(void)
{
- char *msg = g_strescape(g_mpd->errorStr, NULL);
+ char *msg;
- g_warning("mpd error (%i): %s\n", g_mpd->error, msg);
+ assert(g_mpd != NULL);
+ msg = g_strescape(mpd_connection_get_error_message(g_mpd), NULL);
+
+ g_warning("mpd error (%i): %s\n", mpd_connection_get_error(g_mpd), msg);
g_free(msg);
- mpd_closeConnection(g_mpd);
+ mpd_connection_free(g_mpd);
g_mpd = NULL;
}
@@ -73,8 +78,9 @@ lmc_reconnect(G_GNUC_UNUSED gpointer data)
password = g_strndup(g_host, at - g_host);
}
- g_mpd = mpd_newConnection(host, g_port, 10);
- if (g_mpd->error) {
+ if ((g_mpd = mpd_connection_new(host, g_port, 10)) == NULL)
+ g_error("Error creating mpd connection: out of memory\n");
+ if (mpd_connection_get_error(g_mpd) != MPD_ERROR_SUCCESS) {
lmc_failure();
g_free(password);
return true;
@@ -85,19 +91,20 @@ lmc_reconnect(G_GNUC_UNUSED gpointer data)
if (password) {
g_debug("sending MPD password\n");
- mpd_sendPasswordCommand(g_mpd, password);
- mpd_finishCommand(g_mpd);
+ if (!mpd_run_password(g_mpd, password)) {
+ lmc_failure();
+ g_free(password);
+ return true;
+ }
g_free(password);
}
- if (g_mpd->error) {
- lmc_failure();
- return true;
- }
-
- g_message("connected to mpd %i.%i.%i at %s:%i\n",
- g_mpd->version[0], g_mpd->version[1], g_mpd->version[2],
- host, g_port);
+ if ((g_version = mpd_connection_get_server_version(g_mpd)) != NULL)
+ g_message("connected to mpd %i.%i.%i at %s:%i\n",
+ g_version[0], g_version[1], g_version[2],
+ host, g_port);
+ else
+ g_message("connected to mpd unknown at %s:%i\n", host, g_port);
lmc_schedule_update();
@@ -136,12 +143,12 @@ void lmc_disconnect(void)
g_source_remove(idle_source_id);
if (g_mpd) {
- mpd_closeConnection(g_mpd);
+ mpd_connection_free(g_mpd);
g_mpd = NULL;
}
if (current_song != NULL) {
- mpd_freeSong(current_song);
+ mpd_song_free(current_song);
current_song = NULL;
}
}
@@ -149,75 +156,60 @@ void lmc_disconnect(void)
static int
lmc_current(struct mpd_song **song_r, int *elapsed_r)
{
- mpd_Status *status;
int state;
- struct mpd_InfoEntity *entity;
+ struct mpd_status *status;
+ struct mpd_song *song;
assert(g_mpd != NULL);
- mpd_sendCommandListOkBegin(g_mpd);
- mpd_sendStatusCommand(g_mpd);
- mpd_sendCurrentSongCommand(g_mpd);
- mpd_sendCommandListEnd(g_mpd);
-
- status = mpd_getStatus(g_mpd);
- if (!status) {
+ if (!mpd_command_list_begin(g_mpd, true) ||
+ !mpd_send_status(g_mpd) ||
+ !mpd_send_current_song(g_mpd) ||
+ !mpd_command_list_end(g_mpd)) {
lmc_failure();
- return MPD_STATUS_STATE_UNKNOWN;
+ return MPD_STATE_UNKNOWN;
}
- if (status->error) {
- /* FIXME: clearing stuff doesn't seem to help, it keeps printing the
- same error over and over, so just let's just ignore these errors
- for now. */
- // warning ("mpd status error: %s\n", status->error);
- // mpd_executeCommand(g_mpd, "clearerror");
- mpd_clearError(g_mpd);
+ status = mpd_recv_status(g_mpd);
+ if (!status) {
+ lmc_failure();
+ return MPD_STATE_UNKNOWN;
}
- state = status->state;
- *elapsed_r = status->elapsedTime;
+ state = mpd_status_get_state(status);
+ *elapsed_r = mpd_status_get_elapsed_time(status);
- mpd_freeStatus(status);
+ mpd_status_free(status);
- if (state != MPD_STATUS_STATE_PLAY) {
- mpd_finishCommand(g_mpd);
+ if (state != MPD_STATE_PLAY) {
+ if (!mpd_response_finish(g_mpd)) {
+ lmc_failure();
+ return MPD_STATE_UNKNOWN;
+ }
return state;
}
- if (g_mpd->error) {
+ if (!mpd_response_next(g_mpd)) {
lmc_failure();
- return MPD_STATUS_STATE_UNKNOWN;
- }
-
- mpd_nextListOkCommand(g_mpd);
-
- while ((entity = mpd_getNextInfoEntity(g_mpd)) != NULL
- && entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
- mpd_freeInfoEntity(entity);
+ return MPD_STATE_UNKNOWN;
}
- if (entity == NULL) {
- mpd_finishCommand(g_mpd);
- return MPD_STATUS_STATE_UNKNOWN;
+ song = mpd_recv_song(g_mpd);
+ if (song == NULL) {
+ if (!mpd_response_finish(g_mpd))
+ lmc_failure();
+ return MPD_STATE_UNKNOWN;
}
- if (g_mpd->error) {
- mpd_freeInfoEntity(entity);
+ if (!mpd_response_finish(g_mpd)) {
+ mpd_song_free(song);
lmc_failure();
- return MPD_STATUS_STATE_UNKNOWN;
+ return MPD_STATE_UNKNOWN;
}
- mpd_finishCommand(g_mpd);
- if (g_mpd->error) {
- mpd_freeInfoEntity(entity);
- lmc_failure();
- return MPD_STATUS_STATE_UNKNOWN;
- }
-
- *song_r = mpd_songDup(entity->info.song);
- mpd_freeInfoEntity(entity);
- return MPD_STATUS_STATE_PLAY;
+ *song_r = mpd_song_dup(song);
+ mpd_song_free(song);
+ return MPD_STATE_PLAY;
}
/**
@@ -228,11 +220,12 @@ lmc_update(G_GNUC_UNUSED gpointer data)
{
struct mpd_song *prev;
int state, elapsed = -1;
+ const char *artist, *title;
prev = current_song;
state = lmc_current(¤t_song, &elapsed);
- if (state == MPD_STATUS_STATE_PAUSE) {
+ if (state == MPD_STATE_PAUSE) {
if (!was_paused)
song_paused();
was_paused = true;
@@ -244,39 +237,39 @@ lmc_update(G_GNUC_UNUSED gpointer data)
}
return true;
- } else if (state != MPD_STATUS_STATE_PLAY) {
+ } else if (state != MPD_STATE_PLAY) {
current_song = NULL;
last_id = -1;
was_paused = false;
- } else if (current_song->artist == NULL ||
- current_song->title == NULL) {
- if (current_song->id != last_id) {
+ } else if (((artist = mpd_song_get_tag(current_song, MPD_TAG_ARTIST, 0)) == NULL) ||
+ ((title = mpd_song_get_tag(current_song, MPD_TAG_TITLE, 0)) == NULL)) {
+ if ((int)mpd_song_get_id(current_song) != last_id) {
g_message("new song detected with tags missing (%s)\n",
- current_song->file);
- last_id = current_song->id;
+ mpd_song_get_uri(current_song));
+ last_id = mpd_song_get_id(current_song);
}
- mpd_freeSong(current_song);
+ mpd_song_free(current_song);
current_song = NULL;
}
if (was_paused) {
- if (current_song != NULL && current_song->id == last_id)
+ if (current_song != NULL && (int)mpd_song_get_id(current_song) == last_id)
song_continued();
was_paused = false;
}
/* submit the previous song */
if (prev != NULL &&
- (current_song == NULL || prev->id != current_song->id))
+ (current_song == NULL || mpd_song_get_id(prev) != mpd_song_get_id(current_song)))
song_ended(prev);
if (current_song != NULL) {
- if (current_song->id != last_id) {
+ if ((int)mpd_song_get_id(current_song) != last_id) {
/* new song. */
song_started(current_song);
- last_id = current_song->id;
+ last_id = mpd_song_get_id(current_song);
} else {
/* still playing the previous song */
@@ -285,7 +278,7 @@ lmc_update(G_GNUC_UNUSED gpointer data)
}
if (prev != NULL)
- mpd_freeSong(prev);
+ mpd_song_free(prev);
if (g_mpd == NULL) {
lmc_schedule_reconnect();
@@ -311,18 +304,6 @@ lmc_schedule_update(void)
lmc_update, NULL);
}
-static void
-lmc_idle_callback(G_GNUC_UNUSED struct _mpd_Connection *connection,
- unsigned flags, G_GNUC_UNUSED void *userdata)
-{
- assert(g_mpd == connection);
-
- /* we only care about the "player" event */
-
- if (flags & IDLE_PLAYER)
- idle_notified = true;
-}
-
static gboolean
lmc_idle(G_GNUC_UNUSED GIOChannel *source,
G_GNUC_UNUSED GIOCondition condition,
@@ -330,31 +311,30 @@ lmc_idle(G_GNUC_UNUSED GIOChannel *source,
{
assert(idle_source_id != 0);
assert(g_mpd != NULL);
- assert(g_mpd->error == MPD_ERROR_SUCCESS);
+ assert(mpd_connection_get_error(g_mpd) == MPD_ERROR_SUCCESS);
idle_source_id = 0;
/* an even on the MPD connection socket: end idle mode and
query result */
- mpd_stopIdle(g_mpd);
+ if ((idle_notified = mpd_recv_idle(g_mpd, false)) == 0) {
+ if ((mpd_connection_get_error(g_mpd) == MPD_ERROR_SERVER) &&
+ (mpd_connection_get_server_error(g_mpd) == MPD_SERVER_ERROR_UNKNOWN_CMD)) {
+ /* MPD does not recognize the "idle" command - disable
+ it for this connection */
- if (g_mpd->error == MPD_ERROR_ACK &&
- g_mpd->errorCode == MPD_ACK_ERROR_UNKNOWN_CMD) {
- /* MPD does not recognize the "idle" command - disable
- it for this connection */
+ g_message("MPD does not support the 'idle' command - "
+ "falling back to polling\n");
- g_message("MPD does not support the 'idle' command - "
- "falling back to polling\n");
-
- idle_supported = false;
- lmc_schedule_update();
- return false;
- }
-
- if (g_mpd->error != MPD_ERROR_SUCCESS) {
- lmc_failure();
- lmc_schedule_reconnect();
- return false;
+ idle_supported = false;
+ lmc_schedule_update();
+ return false;
+ }
+ if (mpd_connection_get_error(g_mpd) != MPD_ERROR_SUCCESS) {
+ lmc_failure();
+ lmc_schedule_reconnect();
+ return false;
+ }
}
if (idle_notified)
@@ -370,6 +350,7 @@ lmc_idle(G_GNUC_UNUSED GIOChannel *source,
static void
lmc_schedule_idle(void)
{
+ int sock;
GIOChannel *channel;
assert(idle_source_id == 0);
@@ -377,9 +358,9 @@ lmc_schedule_idle(void)
idle_notified = false;
- mpd_startIdle(g_mpd, lmc_idle_callback, NULL);
- if (g_mpd->error == MPD_ERROR_ACK &&
- g_mpd->errorCode == MPD_ACK_ERROR_UNKNOWN_CMD) {
+ if ((!mpd_send_idle_mask(g_mpd, MPD_IDLE_PLAYER)) &&
+ (mpd_connection_get_error(g_mpd) == MPD_ERROR_SERVER) &&
+ (mpd_connection_get_server_error(g_mpd) == MPD_SERVER_ERROR_UNKNOWN_CMD)) {
/* MPD does not recognize the "idle" command - disable
it for this connection */
@@ -391,7 +372,7 @@ lmc_schedule_idle(void)
return;
}
- if (g_mpd->error != MPD_ERROR_SUCCESS) {
+ if (mpd_connection_get_error(g_mpd) != MPD_ERROR_SUCCESS) {
lmc_failure();
lmc_schedule_reconnect();
return;
@@ -399,7 +380,8 @@ lmc_schedule_idle(void)
/* add a GLib watch on the libmpdclient socket */
- channel = g_io_channel_unix_new(g_mpd->sock);
+ sock = mpd_connection_get_fd(g_mpd);
+ channel = g_io_channel_unix_new(sock);
idle_source_id = g_io_add_watch(channel, G_IO_IN, lmc_idle, NULL);
g_io_channel_unref(channel);
}
diff --git a/src/lmc.h b/src/lmc.h
index 1d32a55..4b92fd4 100644
--- a/src/lmc.h
+++ b/src/lmc.h
@@ -21,7 +21,7 @@
#ifndef LMC_H
#define LMC_H
-#include <libmpdclient/libmpdclient.h>
+#include <mpd/client.h>
#include <stdio.h>
void lmc_connect(char *host, int port);
diff --git a/src/mpdscribble.c b/src/mpdscribble.c
index 347285b..b7a81d3 100644
--- a/src/mpdscribble.c
+++ b/src/mpdscribble.c
@@ -95,19 +95,26 @@ static bool
song_repeated(const struct mpd_song *song, int elapsed, int prev_elapsed)
{
return elapsed < 60 && prev_elapsed > elapsed &&
- played_long_enough(prev_elapsed - elapsed, song->time);
+ played_long_enough(prev_elapsed - elapsed, mpd_song_get_duration(song));
}
static void song_changed(const struct mpd_song *song)
{
+ const char *artist, *title, *album, *musicbrainz_trackid;
+
+ artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
+ title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
+ album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0);
+ musicbrainz_trackid = mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0);
+
g_message("new song detected (%s - %s), id: %i, pos: %i\n",
- song->artist, song->title, song->id, song->pos);
+ artist, title, mpd_song_get_id(song), mpd_song_get_pos(song));
g_timer_start(timer);
- as_now_playing(song->artist, song->title, song->album,
- song->musicbrainz_trackid,
- song->time);
+ as_now_playing(artist, title, album,
+ musicbrainz_trackid,
+ mpd_song_get_duration(song));
}
/**
@@ -173,16 +180,18 @@ song_ended(const struct mpd_song *song)
{
int elapsed = g_timer_elapsed(timer, NULL);
- if (!played_long_enough(elapsed, song->time))
+ if (!played_long_enough(elapsed, mpd_song_get_duration(song)))
return;
/* FIXME:
libmpdclient doesn't have any way to fetch the musicbrainz id. */
- as_songchange(song->file, song->artist,
- song->title,
- song->album, song->musicbrainz_trackid,
- song->time >
- 0 ? song->time : (int)
+ as_songchange(mpd_song_get_uri(song),
+ mpd_song_get_tag(song, MPD_TAG_ARTIST, 0),
+ mpd_song_get_tag(song, MPD_TAG_TITLE, 0),
+ mpd_song_get_tag(song, MPD_TAG_ALBUM, 0),
+ mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0),
+ mpd_song_get_duration(song) >
+ 0 ? (int)mpd_song_get_duration(song) : (int)
g_timer_elapsed(timer,
NULL),
NULL);
------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________
Musicpd-dev-team mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/musicpd-dev-team