5 #include "libmpdclient.h"
10 struct information *current_info = &info;
12 mpd_newConnection(current_info->mpd.host,
13 current_info->mpd.port, 10);
14 if (current_info->conn->error) {
15 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
16 mpd_closeConnection(current_info->conn);
17 if (current_info->mpd.artist == NULL)
18 current_info->mpd.artist =
19 malloc(TEXT_BUFFER_SIZE);
20 if (current_info->mpd.album == NULL)
21 current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
22 if (current_info->mpd.title == NULL)
23 current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
24 strcpy(current_info->mpd.artist, "Unknown");
25 strcpy(current_info->mpd.album, "Unknown");
26 strcpy(current_info->mpd.title, "Unknown");
27 current_info->mpd.status = "MPD not responding";
28 current_info->mpd.bitrate = 0;
29 current_info->mpd.progress = 0;
30 current_info->mpd.elapsed = 0;
31 current_info->mpd.length = 0;
36 mpd_InfoEntity *entity;
37 mpd_sendCommandListOkBegin(current_info->conn);
38 mpd_sendStatusCommand(current_info->conn);
39 mpd_sendCurrentSongCommand(current_info->conn);
40 mpd_sendCommandListEnd(current_info->conn);
41 if ((status = mpd_getStatus(current_info->conn)) == NULL) {
42 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
43 mpd_closeConnection(current_info->conn);
44 if (current_info->mpd.artist == NULL)
45 current_info->mpd.artist =
46 malloc(TEXT_BUFFER_SIZE);
47 if (current_info->mpd.album == NULL)
48 current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
49 if (current_info->mpd.title == NULL)
50 current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
51 strcpy(current_info->mpd.artist, "Unknown");
52 strcpy(current_info->mpd.album, "Unknown");
53 strcpy(current_info->mpd.title, "Unknown");
54 current_info->mpd.status = "MPD not responding";
55 current_info->mpd.bitrate = 0;
56 current_info->mpd.progress = 0;
57 current_info->mpd.elapsed = 0;
58 current_info->mpd.length = 0;
61 current_info->mpd.volume = status->volume;
63 //printf("error: %s\n", status->error);
65 if (status->state == MPD_STATUS_STATE_PLAY) {
66 current_info->mpd.status = "Playing";
68 if (status->state == MPD_STATUS_STATE_STOP) {
69 current_info->mpd.status = "Stopped";
70 current_info->mpd.bitrate = 0;
71 current_info->mpd.progress = 0;
72 current_info->mpd.elapsed = 0;
73 current_info->mpd.length = 0;
75 if (status->state == MPD_STATUS_STATE_PAUSE) {
76 current_info->mpd.status = "Paused";
78 if (status->state == MPD_STATUS_STATE_UNKNOWN) {
79 current_info->mpd.status = "Unknown";
80 current_info->mpd.bitrate = 0;
81 current_info->mpd.progress = 0;
82 current_info->mpd.elapsed = 0;
83 current_info->mpd.length = 0;
85 if (status->state == MPD_STATUS_STATE_PLAY ||
86 status->state == MPD_STATUS_STATE_PAUSE) {
87 current_info->mpd.bitrate = status->bitRate;
88 current_info->mpd.progress =
89 (float) status->elapsedTime / status->totalTime;
90 current_info->mpd.elapsed = status->elapsedTime;
91 current_info->mpd.length = status->totalTime;
95 if (current_info->conn->error) {
96 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
97 mpd_closeConnection(current_info->conn);
101 mpd_nextListOkCommand(current_info->conn);
103 while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
104 mpd_Song *song = entity->info.song;
105 if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
106 mpd_freeInfoEntity(entity);
110 if (current_info->mpd.artist == NULL)
111 current_info->mpd.artist =
112 malloc(TEXT_BUFFER_SIZE);
113 if (current_info->mpd.album == NULL)
114 current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
115 if (current_info->mpd.title == NULL)
116 current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
118 strcpy(current_info->mpd.artist, song->artist);
120 strcpy(current_info->mpd.artist, "Unknown");
123 strcpy(current_info->mpd.album, song->album);
125 strcpy(current_info->mpd.album, "Unknown");
128 strcpy(current_info->mpd.title, song->title);
130 strcpy(current_info->mpd.title, "Unknown");
132 if (entity != NULL) {
133 mpd_freeInfoEntity(entity);
136 if (entity != NULL) {
137 mpd_freeInfoEntity(entity);
140 if (current_info->conn->error) {
141 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
142 mpd_closeConnection(current_info->conn);
146 mpd_finishCommand(current_info->conn);
147 if (current_info->conn->error) {
148 //fprintf(stderr, "%s\n", current_info->conn->errorStr);
149 mpd_closeConnection(current_info->conn);
152 mpd_freeStatus(status);
153 mpd_closeConnection(current_info->conn);