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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
|
/* the Music Player Daemon (MPD)
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
* This project's homepage is: http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "playlist.h"
#include "player_control.h"
#include "command.h"
#include "ls.h"
#include "tag.h"
#include "song_print.h"
#include "conf.h"
#include "directory.h"
#include "log.h"
#include "path.h"
#include "utils.h"
#include "state_file.h"
#include "storedPlaylist.h"
#include "ack.h"
#include "myfprintf.h"
#include "os_compat.h"
#define PLAYLIST_STATE_STOP 0
#define PLAYLIST_STATE_PLAY 1
#define PLAYLIST_PREV_UNLESS_ELAPSED 10
#define PLAYLIST_STATE_FILE_STATE "state: "
#define PLAYLIST_STATE_FILE_RANDOM "random: "
#define PLAYLIST_STATE_FILE_REPEAT "repeat: "
#define PLAYLIST_STATE_FILE_CURRENT "current: "
#define PLAYLIST_STATE_FILE_TIME "time: "
#define PLAYLIST_STATE_FILE_CROSSFADE "crossfade: "
#define PLAYLIST_STATE_FILE_PLAYLIST_BEGIN "playlist_begin"
#define PLAYLIST_STATE_FILE_PLAYLIST_END "playlist_end"
#define PLAYLIST_STATE_FILE_STATE_PLAY "play"
#define PLAYLIST_STATE_FILE_STATE_PAUSE "pause"
#define PLAYLIST_STATE_FILE_STATE_STOP "stop"
#define PLAYLIST_BUFFER_SIZE 2*MPD_PATH_MAX
#define PLAYLIST_HASH_MULT 4
#define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16)
#define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS 0
static Playlist playlist;
static int playlist_state = PLAYLIST_STATE_STOP;
int playlist_max_length = DEFAULT_PLAYLIST_MAX_LENGTH;
static int playlist_stopOnError;
static int playlist_errorCount;
static int playlist_noGoToNext;
int playlist_saveAbsolutePaths = DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS;
static void swapOrder(int a, int b);
static void playPlaylistOrderNumber(int orderNum);
static void randomizeOrder(int start, int end);
static void incrPlaylistVersion(void)
{
static unsigned long max = ((mpd_uint32) 1 << 31) - 1;
playlist.version++;
if (playlist.version >= max) {
int i;
for (i = 0; i < playlist.length; i++) {
playlist.songMod[i] = 0;
}
playlist.version = 1;
}
}
void playlistVersionChange(void)
{
int i;
for (i = 0; i < playlist.length; i++) {
playlist.songMod[i] = playlist.version;
}
incrPlaylistVersion();
}
static void incrPlaylistCurrent(void)
{
if (playlist.current < 0)
return;
if (playlist.current >= playlist.length - 1) {
if (playlist.repeat)
playlist.current = 0;
else
playlist.current = -1;
} else
playlist.current++;
}
void initPlaylist(void)
{
char *test;
int i;
ConfigParam *param;
playlist.length = 0;
playlist.repeat = 0;
playlist.version = 1;
playlist.random = 0;
playlist.queued = -1;
playlist.current = -1;
param = getConfigParam(CONF_MAX_PLAYLIST_LENGTH);
if (param) {
playlist_max_length = strtol(param->value, &test, 10);
if (*test != '\0') {
FATAL("max playlist length \"%s\" is not an integer, "
"line %i\n", param->value, param->line);
}
}
playlist_saveAbsolutePaths = getBoolConfigParam(
CONF_SAVE_ABSOLUTE_PATHS, 1);
if (playlist_saveAbsolutePaths == CONF_BOOL_UNSET)
playlist_saveAbsolutePaths =
DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS;
playlist.songs = xmalloc(sizeof(Song *) * playlist_max_length);
playlist.songMod = xmalloc(sizeof(mpd_uint32) * playlist_max_length);
playlist.order = xmalloc(sizeof(int) * playlist_max_length);
playlist.idToPosition = xmalloc(sizeof(int) * playlist_max_length *
PLAYLIST_HASH_MULT);
playlist.positionToId = xmalloc(sizeof(int) * playlist_max_length);
memset(playlist.songs, 0, sizeof(char *) * playlist_max_length);
srandom(time(NULL));
for (i = 0; i < playlist_max_length * PLAYLIST_HASH_MULT; i++) {
playlist.idToPosition[i] = -1;
}
}
static int getNextId(void)
{
static int cur = -1;
do {
cur++;
if (cur >= playlist_max_length * PLAYLIST_HASH_MULT) {
cur = 0;
}
} while (playlist.idToPosition[cur] != -1);
return cur;
}
void finishPlaylist(void)
{
int i;
for (i = 0; i < playlist.length; i++) {
if (playlist.songs[i]->type == SONG_TYPE_URL) {
freeJustSong(playlist.songs[i]);
}
}
playlist.length = 0;
free(playlist.songs);
playlist.songs = NULL;
free(playlist.songMod);
playlist.songMod = NULL;
free(playlist.order);
playlist.order = NULL;
free(playlist.idToPosition);
playlist.idToPosition = NULL;
free(playlist.positionToId);
playlist.positionToId = NULL;
}
void clearPlaylist(void)
{
int i;
stopPlaylist();
for (i = 0; i < playlist.length; i++) {
if (playlist.songs[i]->type == SONG_TYPE_URL) {
freeJustSong(playlist.songs[i]);
}
playlist.idToPosition[playlist.positionToId[i]] = -1;
playlist.songs[i] = NULL;
}
playlist.length = 0;
playlist.current = -1;
incrPlaylistVersion();
}
int clearStoredPlaylist(int fd, const char *utf8file)
{
return removeAllFromStoredPlaylistByPath(fd, utf8file);
}
int showPlaylist(int fd)
{
int i;
char path_max_tmp[MPD_PATH_MAX];
for (i = 0; i < playlist.length; i++) {
fdprintf(fd, "%i:%s\n", i,
get_song_url(path_max_tmp, playlist.songs[i]));
}
return 0;
}
void savePlaylistState(FILE *fp)
{
fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE);
switch (playlist_state) {
case PLAYLIST_STATE_PLAY:
switch (getPlayerState()) {
case PLAYER_STATE_PAUSE:
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PAUSE);
break;
default:
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_PLAY);
}
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CURRENT,
playlist.order[playlist.current]);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_TIME,
getPlayerElapsedTime());
break;
default:
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_STATE_STOP);
break;
}
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_RANDOM, playlist.random);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_REPEAT, playlist.repeat);
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE,
(int)(getPlayerCrossFade()));
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN);
fflush(fp);
showPlaylist(fileno(fp));
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END);
}
static void loadPlaylistFromStateFile(FILE *fp, char *buffer,
int state, int current, int seek_time)
{
char *temp;
int song;
if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp))
state_file_fatal();
while (strcmp(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
temp = strtok(buffer, ":");
if (temp == NULL)
state_file_fatal();
song = atoi(temp);
if (!(temp = strtok(NULL, "")))
state_file_fatal();
if (!addToPlaylist(STDERR_FILENO, temp, NULL)
&& current == song) {
if (state != PLAYER_STATE_STOP) {
playPlaylist(STDERR_FILENO,
playlist.length - 1, 0);
}
if (state == PLAYER_STATE_PAUSE) {
playerPause();
}
if (state != PLAYER_STATE_STOP) {
seekSongInPlaylist(STDERR_FILENO,
playlist.length - 1,
seek_time);
}
}
if (!myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp))
state_file_fatal();
}
}
void readPlaylistState(FILE *fp)
{
int current = -1;
int seek_time = 0;
int state = PLAYER_STATE_STOP;
char buffer[PLAYLIST_BUFFER_SIZE];
while (myFgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
if (strncmp(buffer, PLAYLIST_STATE_FILE_STATE,
strlen(PLAYLIST_STATE_FILE_STATE)) == 0) {
if (strcmp(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]),
PLAYLIST_STATE_FILE_STATE_PLAY) == 0) {
state = PLAYER_STATE_PLAY;
} else
if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_STATE)]),
PLAYLIST_STATE_FILE_STATE_PAUSE)
== 0) {
state = PLAYER_STATE_PAUSE;
}
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_TIME,
strlen(PLAYLIST_STATE_FILE_TIME)) == 0) {
seek_time =
atoi(&(buffer[strlen(PLAYLIST_STATE_FILE_TIME)]));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_REPEAT,
strlen(PLAYLIST_STATE_FILE_REPEAT)) == 0) {
if (strcmp
(&(buffer[strlen(PLAYLIST_STATE_FILE_REPEAT)]),
"1") == 0) {
setPlaylistRepeatStatus(1);
} else
setPlaylistRepeatStatus(0);
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_CROSSFADE,
strlen(PLAYLIST_STATE_FILE_CROSSFADE)) == 0) {
setPlayerCrossFade(atoi
(&
(buffer
[strlen
(PLAYLIST_STATE_FILE_CROSSFADE)])));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_RANDOM,
strlen(PLAYLIST_STATE_FILE_RANDOM)) == 0) {
if (strcmp
(&
(buffer
[strlen(PLAYLIST_STATE_FILE_RANDOM)]),
"1") == 0) {
setPlaylistRandomStatus(1);
} else
setPlaylistRandomStatus(0);
} else if (strncmp(buffer, PLAYLIST_STATE_FILE_CURRENT,
strlen(PLAYLIST_STATE_FILE_CURRENT))
== 0) {
if (strlen(buffer) ==
strlen(PLAYLIST_STATE_FILE_CURRENT))
state_file_fatal();
current = atoi(&(buffer
[strlen
(PLAYLIST_STATE_FILE_CURRENT)]));
} else
if (strncmp
(buffer, PLAYLIST_STATE_FILE_PLAYLIST_BEGIN,
strlen(PLAYLIST_STATE_FILE_PLAYLIST_BEGIN)
) == 0) {
if (state == PLAYER_STATE_STOP)
current = -1;
loadPlaylistFromStateFile(fp, buffer, state,
current, seek_time);
}
}
}
static void printPlaylistSongInfo(int fd, int song)
{
printSongInfo(fd, playlist.songs[song]);
fdprintf(fd, "Pos: %i\nId: %i\n", song, playlist.positionToId[song]);
}
int playlistChanges(int fd, mpd_uint32 version)
{
int i;
for (i = 0; i < playlist.length; i++) {
if (version > playlist.version ||
playlist.songMod[i] >= version ||
playlist.songMod[i] == 0) {
printPlaylistSongInfo(fd, i);
}
}
return 0;
}
int playlistChangesPosId(int fd, mpd_uint32 version)
{
int i;
for (i = 0; i < playlist.length; i++) {
if (version > playlist.version ||
playlist.songMod[i] >= version ||
playlist.songMod[i] == 0) {
fdprintf(fd, "cpos: %i\nId: %i\n",
i, playlist.positionToId[i]);
}
}
return 0;
}
int playlistInfo(int fd, int song)
{
int i;
int begin = 0;
int end = playlist.length;
if (song >= 0) {
begin = song;
end = song + 1;
}
if (song >= playlist.length) {
commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song);
return -1;
}
for (i = begin; i < end; i++)
printPlaylistSongInfo(fd, i);
return 0;
}
# define checkSongId(id) { \
if(id < 0 || id >= PLAYLIST_HASH_MULT*playlist_max_length || \
playlist.idToPosition[id] == -1 ) \
{ \
commandError(fd, ACK_ERROR_NO_EXIST, \
"song id doesn't exist: \"%i\"", id); \
return -1; \
} \
}
int playlistId(int fd, int id)
{
int i;
int begin = 0;
int end = playlist.length;
if (id >= 0) {
checkSongId(id);
begin = playlist.idToPosition[id];
end = begin + 1;
}
for (i = begin; i < end; i++)
printPlaylistSongInfo(fd, i);
return 0;
}
static void swapSongs(int song1, int song2)
{
Song *sTemp;
int iTemp;
sTemp = playlist.songs[song1];
playlist.songs[song1] = playlist.songs[song2];
playlist.songs[song2] = sTemp;
playlist.songMod[song1] = playlist.version;
playlist.songMod[song2] = playlist.version;
playlist.idToPosition[playlist.positionToId[song1]] = song2;
playlist.idToPosition[playlist.positionToId[song2]] = song1;
iTemp = playlist.positionToId[song1];
playlist.positionToId[song1] = playlist.positionToId[song2];
playlist.positionToId[song2] = iTemp;
}
static void queueNextSongInPlaylist(void)
{
char path_max_tmp[MPD_PATH_MAX];
if (playlist.current < playlist.length - 1) {
playlist.queued = playlist.current + 1;
DEBUG("playlist: queue song %i:\"%s\"\n",
playlist.queued,
get_song_url(path_max_tmp,
playlist.
songs[playlist.order[playlist.queued]]));
queueSong(playlist.songs[playlist.order[playlist.queued]]);
} else if (playlist.length && playlist.repeat) {
if (playlist.length > 1 && playlist.random) {
randomizeOrder(0, playlist.length - 1);
}
playlist.queued = 0;
DEBUG("playlist: queue song %i:\"%s\"\n",
playlist.queued,
get_song_url(path_max_tmp,
playlist.
songs[playlist.order[playlist.queued]]));
queueSong(playlist.songs[playlist.order[playlist.queued]]);
}
}
static void syncPlaylistWithQueue(void)
{
switch (getPlayerQueueState()) {
case PLAYER_QUEUE_EMPTY:
setQueueState(PLAYER_QUEUE_BLANK);
if (playlist.queued >= 0) {
DEBUG("playlist: now playing queued song\n");
playlist.current = playlist.queued;
}
playlist.queued = -1;
break;
case PLAYER_QUEUE_DECODE:
if (playlist.queued != -1)
setQueueState(PLAYER_QUEUE_PLAY);
else
setQueueState(PLAYER_QUEUE_STOP);
break;
case PLAYER_QUEUE_FULL:
case PLAYER_QUEUE_PLAY:
case PLAYER_QUEUE_STOP:
case PLAYER_QUEUE_BLANK:
break;
}
}
static void clearPlayerQueue(void)
{
if (getPlayerQueueState() == PLAYER_QUEUE_PLAY ||
getPlayerQueueState() == PLAYER_QUEUE_FULL) {
playerQueueLock();
syncPlaylistWithQueue();
}
playlist.queued = -1;
switch (getPlayerQueueState()) {
case PLAYER_QUEUE_BLANK:
case PLAYER_QUEUE_DECODE:
case PLAYER_QUEUE_STOP:
case PLAYER_QUEUE_EMPTY:
break;
case PLAYER_QUEUE_FULL:
DEBUG("playlist: dequeue song\n");
setQueueState(PLAYER_QUEUE_BLANK);
break;
case PLAYER_QUEUE_PLAY:
DEBUG("playlist: stop decoding queued song\n");
setQueueState(PLAYER_QUEUE_STOP);
break;
}
playerQueueUnlock();
}
int addToPlaylist(int fd, const char *url, int *added_id)
{
Song *song;
DEBUG("add to playlist: %s\n", url);
if ((song = getSongFromDB(url))) {
} else if (!(isValidRemoteUtf8Url(url) &&
(song = newSong(url, SONG_TYPE_URL, NULL)))) {
commandError(fd, ACK_ERROR_NO_EXIST,
"\"%s\" is not in the music db or is "
"not a valid url", url);
return -1;
}
return addSongToPlaylist(fd, song, added_id);
}
int addToStoredPlaylist(int fd, const char *url, const char *utf8file)
{
Song *song;
DEBUG("add to stored playlist: %s\n", url);
song = getSongFromDB(url);
if (song) {
appendSongToStoredPlaylistByPath(fd, utf8file, song);
return 0;
}
if (!isValidRemoteUtf8Url(url))
goto fail;
song = newSong(url, SONG_TYPE_URL, NULL);
if (song) {
appendSongToStoredPlaylistByPath(fd, utf8file, song);
freeJustSong(song);
return 0;
}
fail:
commandError(fd, ACK_ERROR_NO_EXIST, "\"%s\" is not in the music db"
"or is not a valid url", url);
return -1;
}
int addSongToPlaylist(int fd, Song * song, int *added_id)
{
int id;
if (playlist.length == playlist_max_length) {
commandError(fd, ACK_ERROR_PLAYLIST_MAX,
"playlist is at the max size");
return -1;
}
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0
&& playlist.current == playlist.length - 1)
clearPlayerQueue();
}
id = getNextId();
playlist.songs[playlist.length] = song;
playlist.songMod[playlist.length] = playlist.version;
playlist.order[playlist.length] = playlist.length;
playlist.positionToId[playlist.length] = id;
playlist.idToPosition[playlist.positionToId[playlist.length]] =
playlist.length;
playlist.length++;
if (playlist.random) {
int swap;
int start;
/*if(playlist_state==PLAYLIST_STATE_STOP) start = 0;
else */ if (playlist.queued >= 0)
start = playlist.queued + 1;
else
start = playlist.current + 1;
if (start < playlist.length) {
swap = random() % (playlist.length - start);
swap += start;
swapOrder(playlist.length - 1, swap);
}
}
incrPlaylistVersion();
if (added_id)
*added_id = id;
return 0;
}
int swapSongsInPlaylist(int fd, int song1, int song2)
{
int queuedSong = -1;
int currentSong;
if (song1 < 0 || song1 >= playlist.length) {
commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song1);
return -1;
}
if (song2 < 0 || song2 >= playlist.length) {
commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song2);
return -1;
}
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0) {
queuedSong = playlist.order[playlist.queued];
}
currentSong = playlist.order[playlist.current];
if (queuedSong == song1 || queuedSong == song2
|| currentSong == song1 || currentSong == song2)
clearPlayerQueue();
}
swapSongs(song1, song2);
if (playlist.random) {
int i;
int k;
int j = -1;
for (i = 0; playlist.order[i] != song1; i++) {
if (playlist.order[i] == song2)
j = i;
}
k = i;
for (; j == -1; i++)
if (playlist.order[i] == song2)
j = i;
swapOrder(k, j);
} else {
if (playlist.current == song1)
playlist.current = song2;
else if (playlist.current == song2)
playlist.current = song1;
}
incrPlaylistVersion();
return 0;
}
int swapSongsInPlaylistById(int fd, int id1, int id2)
{
checkSongId(id1);
checkSongId(id2);
return swapSongsInPlaylist(fd, playlist.idToPosition[id1],
playlist.idToPosition[id2]);
}
#define moveSongFromTo(from, to) { \
playlist.idToPosition[playlist.positionToId[from]] = to; \
playlist.positionToId[to] = playlist.positionToId[from]; \
playlist.songs[to] = playlist.songs[from]; \
playlist.songMod[to] = playlist.version; \
}
int deleteFromPlaylist(int fd, int song)
{
int i;
int songOrder;
if (song < 0 || song >= playlist.length) {
commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song);
return -1;
}
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0
&& (playlist.order[playlist.queued] == song
|| playlist.order[playlist.current] == song))
clearPlayerQueue();
}
if (playlist.songs[song]->type == SONG_TYPE_URL) {
freeJustSong(playlist.songs[song]);
}
playlist.idToPosition[playlist.positionToId[song]] = -1;
/* delete song from songs array */
for (i = song; i < playlist.length - 1; i++) {
moveSongFromTo(i + 1, i);
}
/* now find it in the order array */
for (i = 0; i < playlist.length - 1; i++) {
if (playlist.order[i] == song)
break;
}
songOrder = i;
/* delete the entry from the order array */
for (; i < playlist.length - 1; i++)
playlist.order[i] = playlist.order[i + 1];
/* readjust values in the order array */
for (i = 0; i < playlist.length - 1; i++) {
if (playlist.order[i] > song)
playlist.order[i]--;
}
/* now take care of other misc stuff */
playlist.songs[playlist.length - 1] = NULL;
playlist.length--;
incrPlaylistVersion();
if (playlist_state != PLAYLIST_STATE_STOP
&& playlist.current == songOrder) {
/*if(playlist.current>=playlist.length) return playerStop(fd);
else return playPlaylistOrderNumber(fd,playlist.current); */
playerWait();
playlist_noGoToNext = 1;
}
if (playlist.current > songOrder) {
playlist.current--;
} else if (playlist.current >= playlist.length) {
incrPlaylistCurrent();
}
if (playlist.queued > songOrder) {
playlist.queued--;
}
return 0;
}
int deleteFromPlaylistById(int fd, int id)
{
checkSongId(id);
return deleteFromPlaylist(fd, playlist.idToPosition[id]);
}
void deleteASongFromPlaylist(Song * song)
{
int i;
if (NULL == playlist.songs)
return;
for (i = 0; i < playlist.length; i++) {
if (song == playlist.songs[i]) {
deleteFromPlaylist(STDERR_FILENO, i);
}
}
}
void stopPlaylist(void)
{
DEBUG("playlist: stop\n");
playerWait();
playlist.queued = -1;
playlist_state = PLAYLIST_STATE_STOP;
playlist_noGoToNext = 0;
if (playlist.random)
randomizeOrder(0, playlist.length - 1);
}
static void playPlaylistOrderNumber(int orderNum)
{
char path_max_tmp[MPD_PATH_MAX];
playlist_state = PLAYLIST_STATE_PLAY;
playlist_noGoToNext = 0;
playlist.queued = -1;
DEBUG("playlist: play %i:\"%s\"\n", orderNum,
get_song_url(path_max_tmp,
playlist.songs[playlist.order[orderNum]]));
playerPlay(playlist.songs[playlist.order[orderNum]]);
playlist.current = orderNum;
}
int playPlaylist(int fd, int song, int stopOnError)
{
int i = song;
clearPlayerError();
if (song == -1) {
if (playlist.length == 0)
return 0;
if (playlist_state == PLAYLIST_STATE_PLAY) {
playerSetPause(0);
return 0;
}
if (playlist.current >= 0 && playlist.current < playlist.length) {
i = playlist.current;
} else {
i = 0;
}
} else if (song < 0 || song >= playlist.length) {
commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song);
return -1;
}
if (playlist.random) {
if (song == -1 && playlist_state == PLAYLIST_STATE_PLAY) {
randomizeOrder(0, playlist.length - 1);
} else {
if (song >= 0)
for (i = 0; song != playlist.order[i]; i++) ;
if (playlist_state == PLAYLIST_STATE_STOP) {
playlist.current = 0;
}
swapOrder(i, playlist.current);
i = playlist.current;
}
}
playlist_stopOnError = stopOnError;
playlist_errorCount = 0;
playPlaylistOrderNumber(i);
return 0;
}
int playPlaylistById(int fd, int id, int stopOnError)
{
if (id == -1) {
return playPlaylist(fd, id, stopOnError);
}
checkSongId(id);
return playPlaylist(fd, playlist.idToPosition[id], stopOnError);
}
static void syncCurrentPlayerDecodeMetadata(void)
{
Song *songPlayer = playerCurrentDecodeSong();
Song *song;
int songNum;
char path_max_tmp[MPD_PATH_MAX];
if (!songPlayer)
return;
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
songNum = playlist.order[playlist.current];
song = playlist.songs[songNum];
if (song->type == SONG_TYPE_URL &&
0 == strcmp(get_song_url(path_max_tmp, song), songPlayer->url) &&
!tag_equal(song->tag, songPlayer->tag)) {
if (song->tag)
tag_free(song->tag);
song->tag = tag_dup(songPlayer->tag);
playlist.songMod[songNum] = playlist.version;
incrPlaylistVersion();
}
}
void syncPlayerAndPlaylist(void)
{
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
if (getPlayerState() == PLAYER_STATE_STOP)
playPlaylistIfPlayerStopped();
else {
syncPlaylistWithQueue();
if (getPlayerQueueState() == PLAYER_QUEUE_BLANK)
queueNextSongInPlaylist();
}
syncCurrentPlayerDecodeMetadata();
}
static void currentSongInPlaylist(void)
{
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
playlist_stopOnError = 0;
syncPlaylistWithQueue();
if (playlist.current >= 0 && playlist.current < playlist.length)
playPlaylistOrderNumber(playlist.current);
else
stopPlaylist();
}
void nextSongInPlaylist(void)
{
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
syncPlaylistWithQueue();
playlist_stopOnError = 0;
if (playlist.current < playlist.length - 1) {
playPlaylistOrderNumber(playlist.current + 1);
} else if (playlist.length && playlist.repeat) {
if (playlist.random)
randomizeOrder(0, playlist.length - 1);
playPlaylistOrderNumber(0);
} else {
incrPlaylistCurrent();
stopPlaylist();
}
}
void playPlaylistIfPlayerStopped(void)
{
if (getPlayerState() == PLAYER_STATE_STOP) {
int error = getPlayerError();
if (error == PLAYER_ERROR_NOERROR)
playlist_errorCount = 0;
else
playlist_errorCount++;
if (playlist_state == PLAYLIST_STATE_PLAY
&& ((playlist_stopOnError && error != PLAYER_ERROR_NOERROR)
|| error == PLAYER_ERROR_AUDIO
|| error == PLAYER_ERROR_SYSTEM
|| playlist_errorCount >= playlist.length)) {
stopPlaylist();
} else if (playlist_noGoToNext)
currentSongInPlaylist();
else
nextSongInPlaylist();
}
}
int getPlaylistRepeatStatus(void)
{
return playlist.repeat;
}
int getPlaylistRandomStatus(void)
{
return playlist.random;
}
void setPlaylistRepeatStatus(int status)
{
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.repeat && !status && playlist.queued == 0)
clearPlayerQueue();
}
playlist.repeat = status;
}
int moveSongInPlaylist(int fd, int from, int to)
{
int i;
Song *tmpSong;
int tmpId;
int currentSong;
if (from < 0 || from >= playlist.length) {
commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", from);
return -1;
}
if ((to >= 0 && to >= playlist.length) ||
(to < 0 && abs(to) > playlist.length)) {
commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", to);
return -1;
}
if (from == to) /* no-op */
return 0;
/*
* (to < 0) => move to offset from current song
* (-playlist.length == to) => move to position BEFORE current song
*/
currentSong = playlist.order[playlist.current];
if (to < 0 && playlist.current >= 0) {
if (currentSong == from)
/* no-op, can't be moved to offset of itself */
return 0;
to = (currentSong + abs(to)) % playlist.length;
}
if (playlist_state == PLAYLIST_STATE_PLAY) {
int queuedSong = -1;
if (playlist.queued >= 0)
queuedSong = playlist.order[playlist.queued];
if (queuedSong == from || queuedSong == to
|| currentSong == from || currentSong == to)
clearPlayerQueue();
}
tmpSong = playlist.songs[from];
tmpId = playlist.positionToId[from];
/* move songs to one less in from->to */
for (i = from; i < to; i++) {
moveSongFromTo(i + 1, i);
}
/* move songs to one more in to->from */
for (i = from; i > to; i--) {
moveSongFromTo(i - 1, i);
}
/* put song at _to_ */
playlist.idToPosition[tmpId] = to;
playlist.positionToId[to] = tmpId;
playlist.songs[to] = tmpSong;
playlist.songMod[to] = playlist.version;
/* now deal with order */
if (playlist.random) {
for (i = 0; i < playlist.length; i++) {
if (playlist.order[i] > from && playlist.order[i] <= to) {
playlist.order[i]--;
} else if (playlist.order[i] < from &&
playlist.order[i] >= to) {
playlist.order[i]++;
} else if (from == playlist.order[i]) {
playlist.order[i] = to;
}
}
}
else
{
if (playlist.current == from)
playlist.current = to;
else if (playlist.current > from && playlist.current <= to) {
playlist.current--;
} else if (playlist.current >= to && playlist.current < from) {
playlist.current++;
}
/* this first if statement isn't necessary since the queue
* would have been cleared out if queued == from */
if (playlist.queued == from)
playlist.queued = to;
else if (playlist.queued > from && playlist.queued <= to) {
playlist.queued--;
} else if (playlist.queued>= to && playlist.queued < from) {
playlist.queued++;
}
}
incrPlaylistVersion();
return 0;
}
int moveSongInPlaylistById(int fd, int id1, int to)
{
checkSongId(id1);
return moveSongInPlaylist(fd, playlist.idToPosition[id1], to);
}
static void orderPlaylist(void)
{
int i;
if (playlist.current >= 0 && playlist.current < playlist.length) {
playlist.current = playlist.order[playlist.current];
}
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0)
clearPlayerQueue();
}
for (i = 0; i < playlist.length; i++) {
playlist.order[i] = i;
}
}
static void swapOrder(int a, int b)
{
int bak = playlist.order[a];
playlist.order[a] = playlist.order[b];
playlist.order[b] = bak;
}
static void randomizeOrder(int start, int end)
{
int i;
int ri;
DEBUG("playlist: randomize from %i to %i\n", start, end);
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= start && playlist.queued <= end)
clearPlayerQueue();
}
for (i = start; i <= end; i++) {
ri = random() % (end - start + 1) + start;
if (ri == playlist.current)
playlist.current = i;
else if (i == playlist.current)
playlist.current = ri;
swapOrder(i, ri);
}
}
void setPlaylistRandomStatus(int status)
{
int statusWas = playlist.random;
playlist.random = status;
if (status != statusWas) {
if (playlist.random) {
/*if(playlist_state==PLAYLIST_STATE_PLAY) {
randomizeOrder(playlist.current+1,
playlist.length-1);
}
else */ randomizeOrder(0, playlist.length - 1);
if (playlist.current >= 0 &&
playlist.current < playlist.length) {
swapOrder(playlist.current, 0);
playlist.current = 0;
}
} else
orderPlaylist();
}
}
void previousSongInPlaylist(void)
{
static time_t lastTime;
time_t diff = time(NULL) - lastTime;
lastTime += diff;
if (playlist_state != PLAYLIST_STATE_PLAY)
return;
syncPlaylistWithQueue();
if (diff && getPlayerElapsedTime() > PLAYLIST_PREV_UNLESS_ELAPSED) {
playPlaylistOrderNumber(playlist.current);
} else {
if (playlist.current > 0) {
playPlaylistOrderNumber(playlist.current - 1);
} else if (playlist.repeat) {
playPlaylistOrderNumber(playlist.length - 1);
} else {
playPlaylistOrderNumber(playlist.current);
}
}
}
int shufflePlaylist(mpd_unused int fd)
{
int i;
int ri;
if (playlist.length > 1) {
if (playlist_state == PLAYLIST_STATE_PLAY) {
clearPlayerQueue();
/* put current playing song first */
swapSongs(0, playlist.order[playlist.current]);
if (playlist.random) {
int j;
for (j = 0; 0 != playlist.order[j]; j++) ;
playlist.current = j;
} else
playlist.current = 0;
i = 1;
} else {
i = 0;
playlist.current = -1;
}
/* shuffle the rest of the list */
for (; i < playlist.length; i++) {
ri = random() % (playlist.length - 1) + 1;
swapSongs(i, ri);
}
incrPlaylistVersion();
}
return 0;
}
int deletePlaylist(int fd, const char *utf8file)
{
char path_max_tmp[MPD_PATH_MAX];
utf8_to_fs_playlist_path(path_max_tmp, utf8file);
if (!isPlaylist(path_max_tmp)) {
commandError(fd, ACK_ERROR_NO_EXIST,
"playlist \"%s\" not found", utf8file);
return -1;
}
if (unlink(path_max_tmp) < 0) {
commandError(fd, ACK_ERROR_SYSTEM,
"problems deleting file");
return -1;
}
return 0;
}
int savePlaylist(int fd, const char *utf8file)
{
FILE *fp;
int i;
struct stat sb;
char path_max_tmp[MPD_PATH_MAX];
if (!valid_playlist_name(fd, utf8file))
return -1;
utf8_to_fs_playlist_path(path_max_tmp, utf8file);
if (!stat(path_max_tmp, &sb)) {
commandError(fd, ACK_ERROR_EXIST, "a file or directory already "
"exists with the name \"%s\"", utf8file);
return -1;
}
while (!(fp = fopen(path_max_tmp, "w")) && errno == EINTR);
if (fp == NULL) {
commandError(fd, ACK_ERROR_SYSTEM, "failed to create file");
return -1;
}
for (i = 0; i < playlist.length; i++) {
char tmp[MPD_PATH_MAX];
get_song_url(path_max_tmp, playlist.songs[i]);
utf8_to_fs_charset(tmp, path_max_tmp);
if (playlist_saveAbsolutePaths &&
playlist.songs[i]->type == SONG_TYPE_FILE)
fprintf(fp, "%s\n", rmp2amp_r(tmp, tmp));
else
fprintf(fp, "%s\n", tmp);
}
while (fclose(fp) && errno == EINTR) ;
return 0;
}
int getPlaylistCurrentSong(void)
{
if (playlist.current >= 0 && playlist.current < playlist.length) {
return playlist.order[playlist.current];
}
return -1;
}
unsigned long getPlaylistVersion(void)
{
return playlist.version;
}
int getPlaylistLength(void)
{
return playlist.length;
}
int seekSongInPlaylist(int fd, int song, float seek_time)
{
int i, ret;
if (song < 0 || song >= playlist.length) {
commandError(fd, ACK_ERROR_NO_EXIST,
"song doesn't exist: \"%i\"", song);
return -1;
}
if (playlist.random)
for (i = 0; song != playlist.order[i]; i++) ;
else
i = song;
clearPlayerError();
playlist_stopOnError = 1;
playlist_errorCount = 0;
if (playlist_state == PLAYLIST_STATE_PLAY) {
if (playlist.queued >= 0)
clearPlayerQueue();
} else
playPlaylistOrderNumber(i);
if (playlist.current != i) {
playPlaylistOrderNumber(i);
}
ret = playerSeek(playlist.songs[playlist.order[i]], seek_time);
if (ret < 0)
commandError(fd, ACK_ERROR_PLAYER_SYNC,
"player not currently playing");
return ret;
}
int seekSongInPlaylistById(int fd, int id, float seek_time)
{
checkSongId(id);
return seekSongInPlaylist(fd, playlist.idToPosition[id], seek_time);
}
int getPlaylistSongId(int song)
{
return playlist.positionToId[song];
}
int PlaylistInfo(int fd, const char *utf8file, int detail)
{
ListNode *node;
List *list;
if (!(list = loadStoredPlaylist(fd, utf8file)))
return -1;
node = list->firstNode;
while (node != NULL) {
char *temp = node->data;
int wrote = 0;
if (detail) {
Song *song = getSongFromDB(temp);
if (song) {
printSongInfo(fd, song);
wrote = 1;
}
}
if (!wrote) {
fdprintf(fd, SONG_FILE "%s\n", temp);
}
node = node->nextNode;
}
freeList(list);
return 0;
}
int loadPlaylist(int fd, const char *utf8file)
{
ListNode *node;
List *list;
if (!(list = loadStoredPlaylist(fd, utf8file)))
return -1;
node = list->firstNode;
while (node != NULL) {
char *temp = node->data;
if ((addToPlaylist(STDERR_FILENO, temp, NULL)) < 0) {
/* for windows compatibility, convert slashes */
char *temp2 = xstrdup(temp);
char *p = temp2;
while (*p) {
if (*p == '\\')
*p = '/';
p++;
}
if ((addToPlaylist(STDERR_FILENO, temp2, NULL)) < 0) {
commandError(fd, ACK_ERROR_PLAYLIST_LOAD,
"can't add file \"%s\"", temp2);
}
free(temp2);
}
node = node->nextNode;
}
freeList(list);
return 0;
}
void searchForSongsInPlaylist(int fd, int numItems, LocateTagItem * items)
{
int i;
char **originalNeedles = xmalloc(numItems * sizeof(char *));
for (i = 0; i < numItems; i++) {
originalNeedles[i] = items[i].needle;
items[i].needle = strDupToUpper(originalNeedles[i]);
}
for (i = 0; i < playlist.length; i++) {
if (strstrSearchTags(playlist.songs[i], numItems, items))
printPlaylistSongInfo(fd, i);
}
for (i = 0; i < numItems; i++) {
free(items[i].needle);
items[i].needle = originalNeedles[i];
}
free(originalNeedles);
}
void findSongsInPlaylist(int fd, int numItems, LocateTagItem * items)
{
int i;
for (i = 0; i < playlist.length; i++) {
if (tagItemsFoundAndMatches(playlist.songs[i], numItems, items))
printPlaylistSongInfo(fd, i);
}
}
/*
* Not supporting '/' was done out of laziness, and we should really
* strive to support it in the future.
*
* Not supporting '\r' and '\n' is done out of protocol limitations (and
* arguably laziness), but bending over head over heels to modify the
* protocol (and compatibility with all clients) to support idiots who
* put '\r' and '\n' in filenames isn't going to happen, either.
*/
int is_valid_playlist_name(const char *utf8path)
{
return strchr(utf8path, '/') == NULL &&
strchr(utf8path, '\n') == NULL &&
strchr(utf8path, '\r') == NULL;
}
int valid_playlist_name(int err_fd, const char *utf8path)
{
if (!is_valid_playlist_name(utf8path)) {
commandError(err_fd, ACK_ERROR_ARG, "playlist name \"%s\" is "
"invalid: playlist names may not contain slashes,"
" newlines or carriage returns",
utf8path);
return 0;
}
return 1;
}
|