source: titan/titan/playlist.h @ 42368

Last change on this file since 42368 was 40382, checked in by obi, 7 years ago

fix

File size: 10.5 KB
Line 
1#ifndef PLAYLIST_H
2#define PLAYLIST_H
3
4void debugplaylist()
5{
6        struct mainplaylist* mainplaylistnode = mainplaylist;
7        struct playlist* playlistnode = NULL;
8
9        while(mainplaylistnode != NULL)
10        {
11                printf("%s\n", mainplaylistnode->name);
12                playlistnode = mainplaylistnode->playlist;
13                while(playlistnode != NULL)
14                {
15                        printf("%p <%p >%p\n", playlistnode, playlistnode->prev, playlistnode->next);
16                        playlistnode = playlistnode->next;
17                }
18                mainplaylistnode = mainplaylistnode->next;
19        }
20}
21
22struct playlist* getlastplaylist(struct playlist* node)
23{
24        struct playlist *prev = NULL;
25
26        while(node != NULL)
27        {
28                prev = node;
29                node = node->next;
30        }
31
32        return prev;
33}
34
35int moveplaylistdown(struct playlist* node)
36{
37        struct playlist* prev = NULL, *next = NULL;
38        struct mainplaylist* mainplaylistnode = NULL;
39
40        if(node == NULL)
41        {
42                err("NULL detect");
43                return 1;
44        }
45
46        mainplaylistnode = getmainplaylistbyplaylistpointer(node);
47        if(mainplaylistnode == NULL)
48        {
49                err("NULL detect");
50                return 1;
51        }
52       
53        //only one node
54        if(node->prev == NULL && node->next == NULL)
55                return 0;
56
57        //last node
58        if(node->next == NULL)
59        {
60                if(node->prev != NULL)
61                        node->prev->next = NULL;
62                node->prev = NULL;
63                node->next = mainplaylistnode->playlist;
64                mainplaylistnode->playlist->prev = node;
65                mainplaylistnode->playlist = node;
66                return 0;
67        }
68
69        //haenge node aus
70        if(node->prev != NULL)
71                node->prev->next = node->next;
72        else
73                mainplaylistnode->playlist = node->next;
74        node->next->prev = node->prev;
75
76        //save nodes next and prev
77        next = node->next;
78        prev = node->prev;
79
80        //haenge es eine pos nacher ein
81        node->next = next->next;
82        node->prev = next;
83       
84        if(next->next != NULL)
85                next->next->prev = node;
86        next->next = node;
87
88        status.writeplaylist = 1;
89        return 0;
90}
91
92int moveplaylistup(struct playlist* node)
93{
94        struct playlist* prev = NULL, *next = NULL, *last = NULL;
95        struct mainplaylist* mainplaylistnode = NULL;
96
97        if(node == NULL)
98        {
99                err("NULL detect");
100                return 1;
101        }
102
103        mainplaylistnode = getmainplaylistbyplaylistpointer(node);
104        if(mainplaylistnode == NULL)
105        {
106                err("NULL detect");
107                return 1;
108        }
109       
110        //only one node
111        if(node->prev == NULL && node->next == NULL)
112                return 0;
113
114        //first node
115        if(node->prev == NULL)
116        {
117                last = getlastplaylist(mainplaylistnode->playlist);
118
119                if(node->next != NULL)
120                        node->next->prev = NULL;
121                mainplaylistnode->playlist = node->next;
122                node->next = NULL;
123                last->next = node;
124                node->prev = last;
125                return 0;
126        }
127
128        //haenge node aus
129        node->prev->next = node->next;
130        if(node->next != NULL)
131                node->next->prev = node->prev;
132
133        //save nodes next and prev
134        next = node->next;
135        prev = node->prev;
136
137        //haenge es eine pos voher ein
138        node->next = prev;
139        node->prev = prev->prev;
140       
141        if(prev->prev != NULL)
142                prev->prev->next = node;
143        else
144                mainplaylistnode->playlist = node;
145        prev->prev = node;
146
147        status.writeplaylist = 1;
148        return 0;
149}
150
151struct playlist* getplaylist(char* file)
152{
153        struct mainplaylist* mainplaylistnode = mainplaylist;
154        struct playlist* playlistnode = NULL;
155
156        while(mainplaylistnode != NULL)
157        {
158                playlistnode = mainplaylistnode->playlist;
159                while(playlistnode != NULL)
160                {
161                        if(ostrcmp(playlistnode->file, file) == 0)
162                                return playlistnode;
163
164                        playlistnode = playlistnode->next;
165                }
166                mainplaylistnode = mainplaylistnode->next;
167        }
168
169        return NULL;
170}
171
172struct playlist* addplaylist(struct playlist **firstnode, char *line, int count, struct playlist* last)
173{
174        struct playlist *newnode = NULL, *prev = NULL, *node = *firstnode;
175        int ret = 0;
176        char* file = NULL;
177
178        newnode = (struct playlist*)calloc(1, sizeof(struct playlist));
179        if(newnode == NULL)
180        {
181                err("no memory");
182                return NULL;
183        }
184
185        file = malloc(MINMALLOC);
186        if(file == NULL)
187        {
188                err("no memory");
189                free(newnode);
190                return NULL;
191        }
192
193        status.writeplaylist = 1;
194
195        ret = sscanf(line, "%s", file);
196        if(ret != 1)
197        {
198                if(count > 0)
199                {
200                        err("playlist line %d not ok", count);
201                }
202                else
203                {
204                        err("add playlist");
205                }
206                free(newnode);
207                free(file);
208                return NULL;
209        }
210
211        free(newnode->file);
212        newnode->file = ostrshrink(file);
213
214        if(last == NULL)
215        {
216                while(node != NULL)
217                {
218                        prev = node;
219                        node = node->next;
220                }
221        }
222        else
223        {
224                prev = last;
225                node = last->next;
226        }
227
228        if(prev == NULL)
229                *firstnode = newnode;
230        else
231        {
232                prev->next = newnode;
233                newnode->prev = prev;
234        }
235        newnode->next = node;
236        if(node != NULL) node->prev = newnode;
237
238        return newnode;
239}
240
241int readplaylist(char* filename, struct playlist** firstnode)
242{
243        FILE *fd = NULL;
244        char *fileline = NULL;
245        int linecount = 0, len = 0;
246        struct playlist* last = NULL, *tmplast = NULL;
247
248        fileline = malloc(MINMALLOC);
249        if(fileline == NULL)
250        {
251                err("no memory");
252                return 1;
253        }
254
255        fd = fopen(filename, "r");
256        if(fd == NULL)
257        {
258                perr("can't open %s", filename);
259                free(fileline);
260                return 1;
261        }
262
263        while(fgets(fileline, MINMALLOC, fd) != NULL)
264        {
265                if(fileline[0] == '#' || fileline[0] == '\n')
266                        continue;
267                len = strlen(fileline) - 1;
268                if(len >= 0 && fileline[len] == '\n')
269                        fileline[len] = '\0';
270                len--;
271                if(len >= 0 && fileline[len] == '\r')
272                        fileline[len] = '\0';
273
274                linecount++;
275
276                if(last == NULL) last = tmplast;
277                last = addplaylist(firstnode, fileline, linecount, last);
278                if(last != NULL) tmplast = last;
279        }
280
281        status.writeplaylist = 0;
282        free(fileline);
283        fclose(fd);
284        return 0;
285}
286
287void delplaylist(char* file, struct playlist** firstnode)
288{
289        struct playlist *node = *firstnode, *prev = *firstnode;
290
291        while(node != NULL)
292        {
293                if(ostrcmp(node->file, file) == 0)
294                {
295                        status.writeplaylist = 1;
296                        if(node == *firstnode)
297                        {
298                                *firstnode = node->next;
299                                if(*firstnode != NULL)
300                                        node->next->prev = NULL;
301                        }
302                        else
303                        {
304                                prev->next = node->next;
305                                if(node->next != NULL)
306                                        node->next->prev = prev;
307                        }
308
309                        free(node->file);
310                        node->file = NULL;
311
312                        free(node);
313                        node = NULL;
314                        break;
315                }
316
317                prev = node;
318                node = node->next;
319        }
320}
321
322void freeplaylist(struct playlist** firstnode)
323{
324        struct playlist *node = *firstnode, *prev = *firstnode;
325
326        while(node != NULL)
327        {
328                prev = node;
329                node = node->next;
330                if(prev != NULL)
331                        delplaylist(prev->file, firstnode);
332        }
333}
334
335int writeplaylist(const char *filename, struct playlist *node)
336{
337        FILE *fd = NULL;
338        int ret = 0;
339
340        fd = fopen(filename, "w");
341        if(fd == NULL)
342        {
343                perr("can't open %s", filename);
344                return 1;
345        }
346
347        while(node != NULL)
348        {
349                ret = fprintf(fd, "%s\n", node->file);
350                if(ret < 0)
351                {
352                        perr("writting file %s", filename);
353                }
354                node = node->next;
355        }
356
357        fclose(fd);
358        return 0;
359}
360
361void screenplaylist(struct mainplaylist* mplaylist)
362{
363        int rcret = 0;
364        struct skin* playlist = getscreen("playlist_main");
365        struct skin* listbox = getscreennode(playlist, "listbox");
366        struct skin* tmp = NULL;
367        struct playlist* node = NULL;
368        struct playlist* movesel = NULL;
369        char* ret = NULL;
370        char* tmpstr = NULL;
371        //int count = 0;
372
373        if(mplaylist == NULL) return;
374start:
375        node = mplaylist->playlist;
376        tmp = NULL;
377        delmarkedscreennodes(playlist, 1);
378
379        while(node != NULL)
380        {
381                tmp = addlistbox(playlist, listbox, tmp, 1);
382                if(tmp != NULL)
383                {
384/*
385                        count ++;
386                        char* tmpstr1 = NULL;
387                        char* title = NULL;
388                        struct splitstr* ret1 = NULL;
389                        int count1 = 0;
390                        tmpstr1 = ostrcat(NULL, node->file, 0, 0);
391                        int i = 0;
392                        ret1 = strsplit(tmpstr1, "/", &count1);
393                        if(count1 >= 1)
394                                i = count1 - 1;
395                        title = ostrcat(NULL, (&ret1[i])->part, 0, 0);
396                        changetext(tmp, title);
397*/                     
398
399                        debug(50, "node->file: %s", node->file);
400                        debug(50, "basename(node->file): %s", basename(node->file));
401                                               
402                        changetext(tmp, basename(node->file));                 
403//                      changetext(tmp, node->file);
404                        tmp->handle = (char*)node;
405                        tmp->del = 1;
406/*
407                        free(title), title = NULL;
408                        free(ret1), ret1 = NULL;
409                        free(tmpstr1), tmpstr1 = NULL;
410*/
411                }
412                node = node->next;
413        }
414
415        drawscreen(playlist, 0, 0);
416        if(movesel == NULL) addscreenrc(playlist, listbox);
417
418        while(1)
419        {
420                rcret = waitrc(playlist, 0, 0);
421
422                if(rcret == getrcconfigint("rcexit", NULL)) break;
423                if(movesel != NULL && rcret == getrcconfigint("rcup", NULL))
424                {
425                        moveplaylistup(movesel);
426                        goto start;
427                }
428                if(movesel != NULL && rcret == getrcconfigint("rcdown", NULL))
429                {
430                        moveplaylistdown(movesel);
431                        goto start;
432                }
433                if(rcret == getrcconfigint("rcok", NULL))
434                {
435                        if(listbox->select != NULL)
436                        {
437                                if(movesel == NULL)
438                                {
439                                        status.markmodus = 1;
440                                        delrc(getrcconfigint("rcright", NULL), playlist, listbox);
441                                        delrc(getrcconfigint("rcleft", NULL), playlist, listbox);
442                                        movesel = (struct playlist*)listbox->select->handle;
443                                }
444                                else
445                                {
446                                        status.markmodus = 0;
447                                        addscreenrc(playlist, listbox);
448                                        movesel = NULL;
449                                }
450                                drawscreen(playlist, 0, 0);
451                        }
452                }
453                if(movesel == NULL && rcret == getrcconfigint("rcgreen", NULL))
454                {
455                        clearscreen(playlist);
456                        ret = screendir(getconfig("addplaylistpath", NULL), "*.mp3 *.flac *.ogg *.wma *.ra *.avi *.dat *.divx *.flv *.mkv *.m4v *.mp4 *.mov *.mpg *.mpeg *.mts *.m2ts *.pls *.trp *.ts *.vdr *.vob *.wmv *.rm", NULL, NULL, NULL, NULL, 0, "SELECT", 0, NULL, 0, NULL, 0, 1200, 0, 600, 0, 0);
457
458                        tmpstr = ostrcat(ret, NULL, 0, 0);
459                        if(tmpstr != NULL) addconfig("addplaylistpath", dirname(tmpstr));
460                                free(tmpstr); tmpstr = NULL;
461
462                        if(ret != NULL)
463                                addplaylist(&mplaylist->playlist, ret, 1, NULL);
464                        free(ret);
465                        goto start;
466                }
467                if(movesel == NULL && rcret == getrcconfigint("rcred", NULL))
468                {
469                        if(listbox->select != NULL && listbox->select->handle != NULL)
470                                delplaylist(((struct playlist*)listbox->select->handle)->file, &mplaylist->playlist);
471
472                        goto start;
473                }
474        }
475
476        status.markmodus = 0;
477        delmarkedscreennodes(playlist, 1);
478        delownerrc(playlist);
479        clearscreen(playlist);
480}
481
482//used in mc, but don't use this function more
483void getplaylistmaxold(struct skin* playlist, int* maxdirs, int* maxfiles)
484{
485        struct skin* node = playlist;
486
487        while(node != NULL)
488        {
489                if(node->del == 2)
490                {
491                        if(node->input != NULL)
492                                (*maxdirs)++;
493                        else
494                                (*maxfiles)++;
495                }
496                node = node->next;
497        }
498}
499
500//used in mc, but don't use this function more
501struct skin* getplaylistrandomold(struct skin* playlist, int maxdirs, int maxfiles)
502{
503        int count = 0;
504        struct skin* node = playlist;
505
506        if(maxfiles < 1) return NULL;
507
508        int r = getrandomnum(maxfiles);
509        r++;
510
511        while(node != NULL)
512        {
513                if(node->del == 2 && node->input == NULL)
514                {
515                        count++;
516                        if(count == r) break;
517                }
518
519                node = node->next;
520        }
521
522        return node;
523}
524
525int getplaylistmax(struct playlist* plist)
526{
527        int count = 0;
528        struct playlist* node = plist;
529
530        while(node != NULL)
531        {
532                count++;
533                node = node->next;
534        }
535
536        return count;
537}
538
539struct playlist* getplaylistrandom(struct playlist* plist, int max)
540{
541        int count = 0;
542        struct playlist* node = plist;
543       
544        if(max < 1) return NULL;
545
546        int r = getrandomnum(max);
547        r++;
548
549        while(node != NULL)
550        {
551                count++;
552                if(count == r) break;
553
554                node = node->next;
555        }
556
557        return node;
558}
559
560#endif
Note: See TracBrowser for help on using the repository browser.