add playlist data structure
[uwplayer.git] / application / playlist.c
1 /*
2  * Copyright 2022 Olaf Wintermann
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a 
5  * copy of this software and associated documentation files (the "Software"), 
6  * to deal in the Software without restriction, including without limitation 
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8  * and/or sell copies of the Software, and to permit persons to whom the 
9  * Software is furnished to do so, subject to the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be included in 
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 #include "playlist.h"
24
25 #include "player.h"
26 #include "utils.h"
27
28 void PlayListInit(MainWindow *win) {
29     win->playlist.current_track = -1;
30 }
31
32 void PlayListAddFile(MainWindow *win, const char *file) {
33     char *f = strdup(file);
34     win->playlist.tracks = ucx_list_append(win->playlist.tracks, f);
35 }
36
37 void PlayListPlayNext(MainWindow *win, bool force) {
38     UcxList *tracks = win->playlist.tracks;
39     if(!tracks) return;
40     size_t len = ucx_list_size(tracks);
41     
42     int current = win->playlist.current_track;
43     if(win->playlist.repeatTrack) {
44         if(force) {
45             current++;
46         }
47     } else if(current < len) {
48         current++;
49     } else if(win->playlist.autoplayFolder) {
50         char *next_file = util_find_next_file(win->file);
51         win->playlist.tracks = ucx_list_append(win->playlist.tracks, next_file);
52         current = len;
53     } else {
54         current = 0;
55     }
56     
57     win->playlist.current_track = current;
58     
59     UcxList *fileElm = ucx_list_get(tracks, current);
60     if(!fileElm) {
61         return;
62     }
63     win->file = fileElm->data;
64     
65     PlayerOpenFile(win);
66 }