source: titan/titan/playlist.h @ 22550

Last change on this file since 22550 was 19844, checked in by nit, 11 years ago

[titan] fix segfault in playlist

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