forked from DeaDBeeF-Player/deadbeef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.c
3908 lines (3560 loc) · 107 KB
/
playlist.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
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
/*
This file is part of Deadbeef Player source code
http://deadbeef.sourceforge.net
playlist management
Copyright (C) 2009-2013 Alexey Yakovenko
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Alexey Yakovenko [email protected]
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fnmatch.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>
#ifndef __linux__
#define _POSIX_C_SOURCE 1
#endif
#include <limits.h>
#include <errno.h>
#include <math.h>
#include "gettext.h"
#include "playlist.h"
#include "streamer.h"
#include "messagepump.h"
#include "plugins.h"
#include "junklib.h"
#include "vfs.h"
#include "conf.h"
#include "utf8.h"
#include "common.h"
#include "threading.h"
#include "metacache.h"
#include "volume.h"
#include "pltmeta.h"
#include "escape.h"
#include "strdupa.h"
#include "tf.h"
#include "playqueue.h"
#include "cueutil.h"
// disable custom title function, until we have new title formatting (0.7)
#define DISABLE_CUSTOM_TITLE
#define DISABLE_LOCKING 0
#define DEBUG_LOCKING 0
#define DETECT_PL_LOCK_RC 0
#if DETECT_PL_LOCK_RC
#include <pthread.h>
#endif
// file format revision history
// 1.1->1.2 changelog:
// added flags field
// 1.0->1.1 changelog:
// added sample-accurate seek positions for sub-tracks
// 1.1->1.2 changelog:
// added flags field
// 1.2->1.3 changelog:
// removed legacy data used for compat with 0.4.4
// note: ddb-0.5.0 should keep using 1.2 playlist format
// 1.3 support is designed for transition to ddb-0.6.0
#define PLAYLIST_MAJOR_VER 1
#define PLAYLIST_MINOR_VER 2
#if (PLAYLIST_MINOR_VER<2)
#error writing playlists in format <1.2 is not supported
#endif
#define min(x,y) ((x)<(y)?(x):(y))
static int playlists_count = 0;
static playlist_t *playlists_head = NULL;
static playlist_t *playlist = NULL; // current playlist
static int plt_loading = 0; // disable sending event about playlist switch, config regen, etc
#if !DISABLE_LOCKING
static uintptr_t mutex;
#endif
#define LOCK {pl_lock();}
#define UNLOCK {pl_unlock();}
// used at startup to prevent crashes
static playlist_t dummy_playlist = {
.refc = 1
};
static int pl_order = -1; // mirrors "playback.order" config variable
static int no_remove_notify;
static playlist_t *addfiles_playlist; // current playlist for adding files/folders; set in pl_add_files_begin
int conf_cue_prefer_embedded = 0;
typedef struct ddb_fileadd_listener_s {
int id;
int (*callback)(ddb_fileadd_data_t *data, void *user_data);
void *user_data;
struct ddb_fileadd_listener_s *next;
} ddb_fileadd_listener_t;
typedef struct ddb_fileadd_filter_s {
int id;
int (*callback)(ddb_file_found_data_t *data, void *user_data);
void *user_data;
struct ddb_fileadd_filter_s *next;
} ddb_fileadd_filter_t;
static ddb_fileadd_listener_t *file_add_listeners;
static ddb_fileadd_filter_t *file_add_filters;
typedef struct ddb_fileadd_beginend_listener_s {
int id;
void (*callback_begin)(ddb_fileadd_data_t *data, void *user_data);
void (*callback_end)(ddb_fileadd_data_t *data, void *user_data);
void *user_data;
struct ddb_fileadd_beginend_listener_s *next;
} ddb_fileadd_beginend_listener_t;
static ddb_fileadd_beginend_listener_t *file_add_beginend_listeners;
void
pl_set_order (int order) {
int prev_order = pl_order;
if (pl_order != order) {
pl_order = order;
for (playlist_t *plt = playlists_head; plt; plt = plt->next) {
plt_reshuffle (plt, NULL, NULL);
}
}
streamer_notify_order_changed (prev_order, pl_order);
}
int
pl_get_order (void) {
return pl_order;
}
int
pl_init (void) {
if (playlist) {
return 0; // avoid double init
}
playlist = &dummy_playlist;
#if !DISABLE_LOCKING
mutex = mutex_create ();
#endif
return 0;
}
void
pl_free (void) {
LOCK;
playqueue_clear ();
plt_loading = 1;
while (playlists_head) {
for (playItem_t *it = playlists_head->head[PL_MAIN]; it; it = it->next[PL_MAIN]) {
if (it->_refc > 1) {
fprintf (stderr, "\033[0;31mWARNING: playitem %p %s(%s) has refc=%d at delete time\033[37;0m\n", it, pl_find_meta_raw (it, ":URI"), pl_find_meta_raw (it, "track"), it->_refc);
}
}
//fprintf (stderr, "\033[0;31mplt refc %d\033[37;0m\n", playlists_head->refc);
plt_remove (0);
}
plt_loading = 0;
UNLOCK;
#if !DISABLE_LOCKING
if (mutex) {
mutex_free (mutex);
mutex = 0;
}
#endif
playlist = NULL;
}
#if DEBUG_LOCKING
volatile int pl_lock_cnt = 0;
#endif
#if DETECT_PL_LOCK_RC
static pthread_t tids[1000];
static int ntids = 0;
pthread_t pl_lock_tid = 0;
#endif
void
pl_lock (void) {
#if !DISABLE_LOCKING
mutex_lock (mutex);
#if DETECT_PL_LOCK_RC
pl_lock_tid = pthread_self ();
tids[ntids++] = pl_lock_tid;
#endif
#if DEBUG_LOCKING
pl_lock_cnt++;
printf ("pcnt: %d\n", pl_lock_cnt);
#endif
#endif
}
void
pl_unlock (void) {
#if !DISABLE_LOCKING
#if DETECT_PL_LOCK_RC
if (ntids > 0) {
ntids--;
}
if (ntids > 0) {
pl_lock_tid = tids[ntids-1];
}
else {
pl_lock_tid = 0;
}
#endif
mutex_unlock (mutex);
#if DEBUG_LOCKING
pl_lock_cnt--;
printf ("pcnt: %d\n", pl_lock_cnt);
#endif
#endif
}
static void
pl_item_free (playItem_t *it);
static void
plt_gen_conf (void) {
if (plt_loading) {
return;
}
LOCK;
int cnt = plt_get_count ();
int i;
conf_remove_items ("playlist.tab.");
playlist_t *p = playlists_head;
for (i = 0; i < cnt; i++, p = p->next) {
char s[100];
snprintf (s, sizeof (s), "playlist.tab.%05d", i);
conf_set_str (s, p->title);
snprintf (s, sizeof (s), "playlist.cursor.%d", i);
conf_set_int (s, p->current_row[PL_MAIN]);
snprintf (s, sizeof (s), "playlist.scroll.%d", i);
conf_set_int (s, p->scroll);
}
UNLOCK;
}
playlist_t *
plt_get_list (void) {
return playlists_head;
}
playlist_t *
plt_get_curr (void) {
LOCK;
if (!playlist) {
playlist = playlists_head;
}
playlist_t *plt = playlist;
if (plt) {
plt_ref (plt);
assert (plt->refc > 1);
}
UNLOCK;
return plt;
}
playlist_t *
plt_get_for_idx (int idx) {
LOCK;
playlist_t *p = playlists_head;
for (int i = 0; p && i <= idx; i++, p = p->next) {
if (i == idx) {
plt_ref (p);
UNLOCK;
return p;
}
}
UNLOCK;
return NULL;
}
void
plt_ref (playlist_t *plt) {
LOCK;
plt->refc++;
UNLOCK;
}
void
plt_unref (playlist_t *plt) {
LOCK;
assert (plt->refc > 0);
plt->refc--;
if (plt->refc < 0) {
trace ("\033[0;31mplaylist: bad refcount on playlist %p (%s)\033[37;0m\n", plt, plt->title);
}
if (plt->refc <= 0) {
plt_free (plt);
}
UNLOCK;
}
int
plt_get_count (void) {
return playlists_count;
}
playItem_t *
plt_get_head (int plt) {
playlist_t *p = playlists_head;
for (int i = 0; p && i <= plt; i++, p = p->next) {
if (i == plt) {
if (p->head[PL_MAIN]) {
pl_item_ref (p->head[PL_MAIN]);
}
return p->head[PL_MAIN];
}
}
return NULL;
}
int
plt_get_sel_count (int plt) {
playlist_t *p = playlists_head;
for (int i = 0; p && i <= plt; i++, p = p->next) {
if (i == plt) {
return plt_getselcount (p);
}
}
return 0;
}
playlist_t *
plt_alloc (const char *title) {
playlist_t *plt = malloc (sizeof (playlist_t));
memset (plt, 0, sizeof (playlist_t));
plt->refc = 1;
plt->title = strdup (title);
return plt;
}
int
plt_add (int before, const char *title) {
assert (before >= 0);
playlist_t *plt = plt_alloc (title);
plt_modified (plt);
LOCK;
playlist_t *p_before = NULL;
playlist_t *p_after = playlists_head;
int i;
for (i = 0; i < before; i++) {
if (i >= before+1) {
break;
}
p_before = p_after;
if (p_after) {
p_after = p_after->next;
}
else {
p_after = playlists_head;
}
}
if (p_before) {
p_before->next = plt;
}
else {
playlists_head = plt;
}
plt->next = p_after;
playlists_count++;
if (!playlist) {
playlist = plt;
if (!plt_loading) {
// shift files
for (int i = playlists_count-1; i >= before+1; i--) {
char path1[PATH_MAX];
char path2[PATH_MAX];
if (snprintf (path1, sizeof (path1), "%s/playlists/%d.dbpl", dbconfdir, i) > sizeof (path1)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
continue;
}
if (snprintf (path2, sizeof (path2), "%s/playlists/%d.dbpl", dbconfdir, i+1) > sizeof (path2)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
continue;
}
int err = rename (path1, path2);
if (err != 0) {
fprintf (stderr, "playlist rename failed: %s\n", strerror (errno));
}
}
}
}
UNLOCK;
plt_gen_conf ();
if (!plt_loading) {
plt_save_n (before);
conf_save ();
messagepump_push (DB_EV_PLAYLISTSWITCHED, 0, 0, 0);
messagepump_push (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CREATED, 0);
}
return before;
}
// NOTE: caller must ensure that configuration is saved after that call
void
plt_remove (int plt) {
int i;
assert (plt >= 0 && plt < playlists_count);
LOCK;
// find playlist and notify streamer
playlist_t *p = playlists_head;
playlist_t *prev = NULL;
for (i = 0; p && i < plt; i++) {
prev = p;
p = p->next;
}
if (!p) {
UNLOCK;
return;
}
streamer_notify_playlist_deleted (p);
if (!plt_loading) {
// move files (will decrease number of files by 1)
for (int i = plt+1; i < playlists_count; i++) {
char path1[PATH_MAX];
char path2[PATH_MAX];
if (snprintf (path1, sizeof (path1), "%s/playlists/%d.dbpl", dbconfdir, i-1) > sizeof (path1)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
continue;
}
if (snprintf (path2, sizeof (path2), "%s/playlists/%d.dbpl", dbconfdir, i) > sizeof (path2)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
continue;
}
int err = rename (path2, path1);
if (err != 0) {
fprintf (stderr, "playlist rename failed: %s\n", strerror (errno));
}
}
}
if (!plt_loading && (playlists_head && !playlists_head->next)) {
pl_clear ();
free (playlist->title);
playlist->title = strdup (_("Default"));
UNLOCK;
plt_gen_conf ();
conf_save ();
plt_save_n (0);
messagepump_push (DB_EV_PLAYLISTSWITCHED, 0, 0, 0);
messagepump_push (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_DELETED, 0);
return;
}
if (i != plt) {
trace ("plt_remove %d failed\n", i);
}
if (p) {
if (!prev) {
playlists_head = p->next;
}
else {
prev->next = p->next;
}
}
playlist_t *next = p->next;
playlist_t *old = playlist;
playlist = p;
playlist = old;
if (p == playlist) {
if (next) {
playlist = next;
}
else {
playlist = prev ? prev : playlists_head;
}
}
plt_unref (p);
playlists_count--;
UNLOCK;
plt_gen_conf ();
conf_save ();
if (!plt_loading) {
messagepump_push (DB_EV_PLAYLISTSWITCHED, 0, 0, 0);
messagepump_push (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_DELETED, 0);
}
}
int
plt_find (const char *name) {
playlist_t *p = playlists_head;
int i = -1;
for (i = 0; p; i++, p = p->next) {
if (!strcmp (p->title, name)) {
return i;
}
}
return -1;
}
void
plt_set_curr (playlist_t *plt) {
LOCK;
if (plt != playlist) {
playlist = plt ? plt : &dummy_playlist;
if (!plt_loading) {
messagepump_push (DB_EV_PLAYLISTSWITCHED, 0, 0, 0);
conf_set_int ("playlist.current", plt_get_curr_idx ());
conf_save ();
}
}
UNLOCK;
}
void
plt_set_curr_idx (int plt) {
int i;
LOCK;
playlist_t *p = playlists_head;
for (i = 0; p && p->next && i < plt; i++) {
p = p->next;
}
if (p != playlist) {
playlist = p;
if (!plt_loading) {
messagepump_push (DB_EV_PLAYLISTSWITCHED, 0, 0, 0);
conf_set_int ("playlist.current", plt);
conf_save ();
}
}
UNLOCK;
}
int
plt_get_curr_idx(void) {
int i;
LOCK;
playlist_t *p = playlists_head;
for (i = 0; p && i < playlists_count; i++) {
if (p == playlist) {
UNLOCK;
return i;
}
p = p->next;
}
UNLOCK;
return -1;
}
int
plt_get_idx_of (playlist_t *plt) {
int i;
LOCK;
playlist_t *p = playlists_head;
for (i = 0; p && i < playlists_count; i++) {
if (p == plt) {
UNLOCK;
return i;
}
p = p->next;
}
UNLOCK;
return -1;
}
int
plt_get_title (playlist_t *p, char *buffer, int bufsize) {
LOCK;
if (!buffer) {
int l = (int)strlen (p->title);
UNLOCK;
return l;
}
strncpy (buffer, p->title, bufsize);
buffer[bufsize-1] = 0;
UNLOCK;
return 0;
}
int
plt_set_title (playlist_t *p, const char *title) {
LOCK;
free (p->title);
p->title = strdup (title);
plt_gen_conf ();
UNLOCK;
conf_save ();
if (!plt_loading) {
messagepump_push (DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_TITLE, 0);
}
return 0;
}
void
plt_modified (playlist_t *plt) {
pl_lock ();
plt->modification_idx++;
pl_unlock ();
}
int
plt_get_modification_idx (playlist_t *plt) {
pl_lock ();
int idx = plt->modification_idx;
pl_unlock ();
return idx;
}
void
plt_free (playlist_t *plt) {
LOCK;
plt_clear (plt);
if (plt->title) {
free (plt->title);
}
while (plt->meta) {
DB_metaInfo_t *m = plt->meta;
plt->meta = m->next;
metacache_remove_string (m->key);
metacache_remove_string (m->value);
free (m);
}
free (plt);
UNLOCK;
}
void
plt_move (int from, int to) {
if (from == to) {
return;
}
int i;
LOCK;
playlist_t *p = playlists_head;
playlist_t *pfrom = NULL;
playlist_t *prev = NULL;
// move 'from' to temp file
char path1[PATH_MAX];
char temp[PATH_MAX];
if (snprintf (path1, sizeof (path1), "%s/playlists/%d.dbpl", dbconfdir, from) > sizeof (path1)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
UNLOCK;
return;
}
if (snprintf (temp, sizeof (temp), "%s/playlists/temp.dbpl", dbconfdir) > sizeof (temp)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
UNLOCK;
return;
}
struct stat st;
int err = stat (path1, &st);
if (!err) {
int err = rename (path1, temp);
if (err != 0) {
fprintf (stderr, "playlist rename %s->%s failed: %s\n", path1, temp, strerror (errno));
UNLOCK;
return;
}
}
// remove 'from' from list
for (i = 0; p && i <= from; i++) {
if (i == from) {
pfrom = p;
if (prev) {
prev->next = p->next;
}
else {
playlists_head = p->next;
}
break;
}
prev = p;
p = p->next;
}
if (!pfrom) {
UNLOCK;
return;
}
// shift files to fill the gap
for (int i = from; i < playlists_count-1; i++) {
char path2[PATH_MAX];
if (snprintf (path1, sizeof (path1), "%s/playlists/%d.dbpl", dbconfdir, i) > sizeof (path1)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
continue;
}
if (snprintf (path2, sizeof (path2), "%s/playlists/%d.dbpl", dbconfdir, i+1) > sizeof (path2)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
continue;
}
int err = stat (path2, &st);
if (!err) {
int err = rename (path2, path1);
if (err != 0) {
fprintf (stderr, "playlist rename %s->%s failed: %s\n", path2, path1, strerror (errno));
}
}
}
// open new gap
for (int i = playlists_count-2; i >= to; i--) {
char path2[PATH_MAX];
if (snprintf (path1, sizeof (path1), "%s/playlists/%d.dbpl", dbconfdir, i) > sizeof (path1)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
continue;
}
if (snprintf (path2, sizeof (path2), "%s/playlists/%d.dbpl", dbconfdir, i+1) > sizeof (path2)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
continue;
}
int err = stat (path1, &st);
if (!err) {
int err = rename (path1, path2);
if (err != 0) {
fprintf (stderr, "playlist rename %s->%s failed: %s\n", path1, path2, strerror (errno));
}
}
}
// move temp file
if (snprintf (path1, sizeof (path1), "%s/playlists/%d.dbpl", dbconfdir, to) > sizeof (path1)) {
fprintf (stderr, "error: failed to make path string for playlist file\n");
}
else {
int err = stat (temp, &st);
if (!err) {
int err = rename (temp, path1);
if (err != 0) {
fprintf (stderr, "playlist rename %s->%s failed: %s\n", temp, path1, strerror (errno));
}
}
}
// move pointers
if (to == 0) {
// insert 'from' as head
pfrom->next = playlists_head;
playlists_head = pfrom;
}
else {
// insert 'from' in the middle
p = playlists_head;
for (i = 0; p && i < to; i++) {
if (i == to-1) {
playlist_t *next = p->next;
p->next = pfrom;
pfrom->next = next;
break;
}
p = p->next;
}
}
UNLOCK;
plt_gen_conf ();
conf_save ();
}
void
plt_clear (playlist_t *plt) {
pl_lock ();
while (plt->head[PL_MAIN]) {
plt_remove_item (plt, plt->head[PL_MAIN]);
}
plt->current_row[PL_MAIN] = -1;
plt->current_row[PL_SEARCH] = -1;
plt_modified (plt);
pl_unlock ();
}
void
pl_clear (void) {
LOCK;
plt_clear (playlist);
UNLOCK;
}
playItem_t * /* insert internal cuesheet - called by plugins */
plt_insert_cue_from_buffer (playlist_t *playlist, playItem_t *after, playItem_t *origin, const uint8_t *buffer, int buffersize, int numsamples, int samplerate) {
if (playlist->loading_cue) {
// means it was called from _load_cue
playlist->cue_numsamples = numsamples;
playlist->cue_samplerate = samplerate;
}
return NULL;
}
playItem_t * /* insert external cuesheet - called by plugins */
plt_insert_cue (playlist_t *plt, playItem_t *after, playItem_t *origin, int numsamples, int samplerate) {
if (plt->loading_cue) {
// means it was called from _load_cue
plt->cue_numsamples = numsamples;
plt->cue_samplerate = samplerate;
}
return NULL;
}
static playItem_t *
plt_insert_dir_int (int visibility, playlist_t *playlist, DB_vfs_t *vfs, playItem_t *after, const char *dirname, int *pabort, int (*cb)(playItem_t *it, void *data), void *user_data);
static playItem_t *
plt_load_int (int visibility, playlist_t *plt, playItem_t *after, const char *fname, int *pabort, int (*cb)(playItem_t *it, void *data), void *user_data);
static int
fileadd_filter_test (ddb_file_found_data_t *data) {
for (ddb_fileadd_filter_t *f = file_add_filters; f; f = f->next) {
int res = f->callback (data, f->user_data);
if (res < 0) {
return res;
}
}
return 0;
}
static playItem_t *
plt_insert_file_int (int visibility, playlist_t *playlist, playItem_t *after, const char *fname, int *pabort, int (*cb)(playItem_t *it, void *data), void *user_data) {
if (!fname || !(*fname)) {
return NULL;
}
// check if that is supported container format
if (!playlist->ignore_archives) {
DB_vfs_t **vfsplugs = plug_get_vfs_list ();
for (int i = 0; vfsplugs[i]; i++) {
if (vfsplugs[i]->is_container) {
if (vfsplugs[i]->is_container (fname)) {
playItem_t *it = plt_insert_dir_int (visibility, playlist, vfsplugs[i], after, fname, pabort, cb, user_data);
if (it) {
return it;
}
}
}
}
}
const char *fn = strrchr (fname, '/');
if (!fn) {
fn = fname;
}
else {
fn++;
}
// add all possible streams as special-case:
// set decoder to NULL, and filetype to "content"
// streamer is responsible to determine content type on 1st access and
// update decoder and filetype fields
if (strncasecmp (fname, "file://", 7)) {
const char *p = fname;
int detect_on_access = 1;
// check if it's URI
for (; *p; p++) {
if (!strncmp (p, "://", 3)) {
break;
}
if (!isalpha (*p)) {
detect_on_access = 0;
break;
}
}
if (detect_on_access) {
// find vfs plugin
DB_vfs_t **vfsplugs = plug_get_vfs_list ();
for (int i = 0; vfsplugs[i]; i++) {
if (vfsplugs[i]->get_schemes) {
const char **sch = vfsplugs[i]->get_schemes ();
int s = 0;
for (s = 0; sch[s]; s++) {
if (!strncasecmp (sch[s], fname, strlen (sch[s]))) {
break;
}
}
if (sch[s]) {
if (!vfsplugs[i]->is_streaming || !vfsplugs[i]->is_streaming()) {
detect_on_access = 0;
}
break;
}
}
}
}
if (detect_on_access && *p == ':') {
// check for wrong chars like CR/LF, TAB, etc
// they are not allowed and might lead to corrupt display in GUI
int32_t i = 0;
while (fname[i]) {
uint32_t c = u8_nextchar (fname, &i);
if (c < 0x20) {
return NULL;
}
}
playItem_t *it = pl_item_alloc_init (fname, NULL);
pl_replace_meta (it, ":FILETYPE", "content");
after = plt_insert_item (addfiles_playlist ? addfiles_playlist : playlist, after, it);
pl_item_unref (it);
return after;
}
}
else {
char *escaped = uri_unescape (fname, (int)strlen (fname));
if (escaped) {
fname = strdupa (escaped);
free (escaped);
}
fname += 7;
}
// detect decoder
const char *eol = strrchr (fname, '.');
if (!eol) {
return NULL;
}
eol++;
#ifdef __MINGW32__
// replace backslashes with normal slashes
char fname_conv[strlen(fname)+1];
if (strchr(fname, '\\')) {
trace ("plt_insert_file_int: backslash(es) detected: %s\n", fname);
strcpy (fname_conv, fname);
char *slash_p = fname_conv;
while (slash_p = strchr(slash_p, '\\')) {
*slash_p = '/';
slash_p++;
}
fname = fname_conv;
}
// path should start with "X:/", not "/X:/", fixing to avoid file opening problems
if (fname[0] == '/' && isalpha(fname[1]) && fname[2] == ':') {
fname++;
}
#endif
// handle cue files
if (!strcasecmp (eol, "cue")) {
playItem_t *inserted = plt_load_cue_file(playlist, after, fname, NULL, NULL, 0);
return inserted;
}