Group :: Sound
RPM: mpdscribble
Main Changelog Spec Patches Sources Download Gear Bugs and FR Repocop
Patch: mpdscribble-alt-src-no_id3_tags.patch
Download
Download
diff --git a/mpdscribble/src/lmc.c b/mpdscribble/src/lmc.c
index f86f11a..b5d25e1 100644
--- a/mpdscribble/src/lmc.c
+++ b/mpdscribble/src/lmc.c
@@ -18,6 +18,11 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+/* for strndup() */
+#define _GNU_SOURCE
+
+#include "libmpdclient/str_pool.h"
+
#include "lmc.h"
#include "file.h"
@@ -28,6 +33,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
#include <unistd.h>
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 14
@@ -58,6 +64,9 @@ lmc_schedule_update(void);
static void
lmc_schedule_idle(void);
+static void
+extract_track_from_filename(const char *filename, char **artist, char **title);
+
static void lmc_failure(void)
{
char *msg = g_strescape(g_mpd->errorStr, NULL);
@@ -226,6 +235,15 @@ lmc_update(G_GNUC_UNUSED gpointer data)
prev = current_song;
state = lmc_current(¤t_song);
+ if (current_song != NULL &&
+ current_song->artist == NULL &&
+ current_song->title == NULL) {
+ extract_track_from_filename(
+ current_song->file,
+ ¤t_song->artist,
+ ¤t_song->title);
+ }
+
if (state == MPD_STATUS_STATE_PAUSE) {
if (!was_paused)
song_paused();
@@ -390,3 +408,113 @@ lmc_schedule_idle(void)
idle_source_id = g_io_add_watch(channel, G_IO_IN, lmc_idle, NULL);
g_io_channel_unref(channel);
}
+
+#define SEPARATOR " - "
+
+static char *
+extract_artist(const char *filename)
+{
+ char *end = NULL;
+ size_t size = 0;
+
+ assert(filename != NULL);
+
+ end = strstr(filename, SEPARATOR);
+ if (end == NULL)
+ return NULL;
+
+ if (end == filename)
+ return NULL;
+
+ /* end points to begin of separator, so move it to next symbol */
+ end--;
+
+ /* skip trailing spaces */
+ while (*end == ' ')
+ --end;
+
+ /* skip leading spaces */
+ filename += strspn(filename, " ");
+
+ size = end - filename + 1;
+ if (size == 0)
+ return NULL;
+
+ return strndup(filename, size);
+}
+
+static char *
+extract_title(const char *filename)
+{
+ char *p = NULL;
+ char *point = NULL;
+
+ assert(filename != NULL);
+
+ p = strstr(filename, SEPARATOR);
+ if (p == NULL)
+ return NULL;
+
+ /* skip separator */
+ p += (sizeof(SEPARATOR) - 1);
+
+ /* skip leading spaces */
+ p += strspn(p, " ");
+
+ if (*p == '\0')
+ return NULL;
+
+ point = strrchr(p, '.');
+ if (point != NULL &&
+ (strcasecmp(point, ".mp3") == 0 ||
+ strcasecmp(point, ".ogg") == 0 ||
+ strcasecmp(point, ".wma") == 0))
+ {
+ size_t size = point - p;
+ if (size == 0)
+ return NULL;
+
+ return strndup(p, size);
+ }
+
+ return strdup(p);
+}
+
+static void
+extract_track_from_filename(const char *filename, char **artist, char **title)
+{
+ char *tmp_artist = NULL;
+ char *tmp_title = NULL;
+ const char *p = NULL;
+
+ assert(filename != NULL);
+
+ /* skip path to file */
+ p = strrchr(filename, '/');
+ if (p == NULL)
+ p = filename;
+ else
+ p++; /* p points to slash, so move it to next symbol */
+
+ tmp_artist = extract_artist(p);
+ if (tmp_artist == NULL)
+ return;
+
+ tmp_title = extract_title(p);
+ if (tmp_title == NULL)
+ {
+ free(tmp_artist);
+ return;
+ }
+
+ *artist = str_pool_get(tmp_artist);
+ *title = str_pool_get(tmp_title);
+
+ free(tmp_artist);
+ free(tmp_title);
+
+ if (file_config.verbose > 1)
+ g_message("extracted info from file: '%s' - '%s'\n", *artist, *title);
+}
+
+