add playlist data structure
[uwplayer.git] / application / playlist.c
diff --git a/application/playlist.c b/application/playlist.c
new file mode 100644 (file)
index 0000000..06172c3
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2022 Olaf Wintermann
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"), 
+ * to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+ * and/or sell copies of the Software, and to permit persons to whom the 
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in 
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "playlist.h"
+
+#include "player.h"
+#include "utils.h"
+
+void PlayListInit(MainWindow *win) {
+    win->playlist.current_track = -1;
+}
+
+void PlayListAddFile(MainWindow *win, const char *file) {
+    char *f = strdup(file);
+    win->playlist.tracks = ucx_list_append(win->playlist.tracks, f);
+}
+
+void PlayListPlayNext(MainWindow *win, bool force) {
+    UcxList *tracks = win->playlist.tracks;
+    if(!tracks) return;
+    size_t len = ucx_list_size(tracks);
+    
+    int current = win->playlist.current_track;
+    if(win->playlist.repeatTrack) {
+        if(force) {
+            current++;
+        }
+    } else if(current < len) {
+        current++;
+    } else if(win->playlist.autoplayFolder) {
+        char *next_file = util_find_next_file(win->file);
+        win->playlist.tracks = ucx_list_append(win->playlist.tracks, next_file);
+        current = len;
+    } else {
+        current = 0;
+    }
+    
+    win->playlist.current_track = current;
+    
+    UcxList *fileElm = ucx_list_get(tracks, current);
+    if(!fileElm) {
+        return;
+    }
+    win->file = fileElm->data;
+    
+    PlayerOpenFile(win);
+}