/
status.c
290 lines (222 loc) · 6.12 KB
/
status.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <mpd/status.h>
#include <mpd/song.h>
#include <mpd/tag.h>
#include "mpdinfo.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int getStatusByFunc(Config* config, void* func) {
if (!config->mpd_status) {
return -1;
}
int (*f)(struct mpd_status* status) = func;
int status = f(config->mpd_status);
return status;
}
int getDBUpdateStatus(Config* config, int status) {
int update = getStatusByFunc(config, &mpd_status_get_update_id);
if (update != 0) {
return 1;
}
return 0;
}
int getRandomStatus(Config* config, int status) {
return getStatusByFunc(config, &mpd_status_get_random);
}
int getRepeatStatus(Config* config, int status) {
return getStatusByFunc(config, &mpd_status_get_repeat);
}
int getStatus(Config* config, int status) {
return getStatusByFunc(config, &mpd_status_get_state);
}
char* getTokenStatusString(int status, int playStatus, TokenConfigItem* item) {
char* output = NULL;
if (status) {
switch (playStatus) {
case MPD_STATE_STOP:
output = item->stop;
break;
case MPD_STATE_PAUSE:
output = item->pause;
break;
case MPD_STATE_PLAY:
output = item->play;
break;
default:
output = item->none;
break;
}
} else {
output = item->off;
}
// check for NULL pointer
char* str_status;
if (!output) {
str_status = strdup("");
} else {
str_status = strdup(output);
}
return str_status;
}
char* getRepeatString(Config* config, int status) {
return getTokenStatusString(getRepeatStatus(config, status), status, config->tokens->repeat);
}
char* getRandomString(Config* config, int status) {
return getTokenStatusString(getRandomStatus(config, status), status, config->tokens->random);
}
char* getDBUpdateString(Config* config, int status) {
return getTokenStatusString(getDBUpdateStatus(config, status), status, config->tokens->dbupdate);
}
char* getStatusString(Config* config, int status) {
switch (status) {
case MPD_STATE_PLAY:
return strdup("playing");
case MPD_STATE_PAUSE:
return strdup("pause");
case MPD_STATE_STOP:
return strdup("stopped");
default:
return strdup("unkown");
}
}
char* getId3Tag(Config* config, int status, enum mpd_tag_type tag_type) {
if (!config->curr_song) {
return strdup("");
}
const char* tag = mpd_song_get_tag(config->curr_song, tag_type, 0);
if (tag == NULL || !strcmp(tag, "")) {
return strdup("");
} else {
return strdup(tag);
}
}
char* getTitle(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_TITLE);
}
char* getArtist(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_ARTIST);
}
char* getAlbum(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_ALBUM);
}
char* getAlbumArtist(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_ALBUM_ARTIST);
}
char* getGenre(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_GENRE);
}
char* getTrack(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_TRACK);
}
char* getDisc(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_DISC);
}
char* getComment(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_COMMENT);
}
char* getDate(Config* config, int status) {
return getId3Tag(config, status, MPD_TAG_DATE);
}
char* getFilename(Config* config, int status) {
if (!config->curr_song) {
return strdup("");
}
const char* uri = mpd_song_get_uri(config->curr_song);
if (!uri || uri[0] == 0) {
return strdup("");
}
const char* str = strrchr(uri, '/');
// check if more then the / is in the string
if (!str || strlen(str) < 2) {
str = uri;
}
str++;
return strdup(str);
}
char* timeToString(Config* config, unsigned time) {
unsigned sec;
unsigned min;
sec = time % 60;
min = time / 60;
int length = snprintf(NULL, 0, "%d:%02d", min, sec) + 1;
char* out = calloc(length, sizeof(char));
snprintf(out, length, "%d:%02d", min, sec);
return out;
}
char* getElapsedTime(Config* config, int status) {
if (!config->mpd_status) {
return strdup("");
}
unsigned time = mpd_status_get_elapsed_time(config->mpd_status);
return timeToString(config, time);
}
char* getDuration(Config* config, int status) {
if (!config->curr_song) {
return timeToString(config, 0);
}
unsigned time = mpd_song_get_duration(config->curr_song);
return timeToString(config, time);
}
char* getQueueLength(Config* config, int status) {
logprintf(config->log, LOG_DEBUG, "queueLength\n");
if (!config->mpd_status) {
return strdup("0");
}
unsigned len = mpd_status_get_queue_length(config->mpd_status);
unsigned sc = snprintf(NULL, 0, "%d", len) + 1;
char* s = calloc(sc, sizeof(char));
snprintf(s, sc, "%d", len);
return s;
}
char* getTimeBar(Config* config, int status) {
if (config->timebar < 3) {
return strdup("");
}
char* timeBar = calloc(config->timebar + 1, sizeof(char));
unsigned duration;
if (config->curr_song) {
duration = mpd_song_get_duration(config->curr_song);
} else {
duration = 0;
}
unsigned elapsed = mpd_status_get_elapsed_time(config->mpd_status);
unsigned blockSize = duration / (config->timebar - 2);
unsigned block = 0;
if (blockSize) {
block = elapsed / blockSize;
}
if (block > (config->timebar - 2)) {
block = config->timebar - 2;
}
timeBar[0] = '[';
unsigned i;
for (i = 1; i < block; i++) {
timeBar[i] = '=';
}
if (i > config->timebar - 2) {
i = config->timebar - 2;
}
timeBar[i] = '>';
i++;
for (; i < config->timebar; i++) {
timeBar[i] = '-';
}
timeBar[config->timebar - 1] = ']';
timeBar[config->timebar] = 0;
return timeBar;
}
int getVolume(Config* config, int status) {
if(!config->mpd_status) {
return -1;
}
int volume = mpd_status_get_volume(config->mpd_status);
return volume;
}
char* getVolumeString(Config* config, int status) {
int vol = getVolume(config, status);
unsigned length = snprintf(NULL, 0, "%d", vol) + 1;
char* volString = calloc(length, sizeof(char));
snprintf(volString, length, "%d", vol);
return volString;
}