summaryrefslogtreecommitdiff
path: root/components/mpd.c
blob: b32706e2859c18d5aa5009ed6c4cff90f30966b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* See LICENSE file for copyright and license details. */
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "../slstatus.h"
#include "../util.h"

const char *
mpd_track(const char *path)
{
	FILE *fp;
	const unsigned short N = 128;
	char track[N];
	char state[16];

	if (!(fp = fopen(path, "r")))
		return NULL;

	if (fgets(track, N, fp) == NULL || fscanf(fp, "%s", state) != 1)
	{
		fclose(fp);
		return NULL;
	}

	fclose(fp);

	// otherwise statusbar will show a weird symbol
	track[strlen(track)-1] = '\0';

	if (strcmp(state, "[playing]") == 0)
		return bprintf("%s ", track);

	if (strcmp(state, "[paused]") == 0)
		return bprintf("%s ", track);

	return NULL;
}