]> uap-core.de Git - uwplayer.git/commitdiff
implement play previous
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 21 Sep 2025 14:22:09 +0000 (16:22 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 21 Sep 2025 14:22:09 +0000 (16:22 +0200)
application/Sidebar.c
application/playlist.c
application/playlist.h
application/settings.c
application/window.c
application/window.h

index 85bd0eb3926270c840b0af734b6ed0d1374280fd..fbccd9fe0062fa2bb0093fcfc8714d23747d711e 100644 (file)
@@ -360,7 +360,7 @@ static void SelectElmAP(Widget w, XEvent *event, String *args, Cardinal *nArgs)
     Sidebar s = (Sidebar)w;
     
     int selected = e->y / s->sidebar.elmHeight;
-    PlayListPlayTrack(s->sidebar.window, selected);
+    PlayListPlayTrack(s->sidebar.window, selected, true);
     s->sidebar.select2 = -1;
     
     SidebarRepaint(w);
index b5e2de4ba4219736cb619057b163b1818742a2ee..5393bb1eb117924351939303c6adfa6c17f58ee2 100644 (file)
 void PlayListInit(MainWindow *win) {
     win->playlist.tracks = cxArrayListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS, 64);
     win->playlist.tracks->collection.simple_destructor = free;
+    win->playlist.tracks->collection.cmpfunc = (cx_compare_func)strcmp;
+    win->playlist.history = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS);
+    win->playlist.history->collection.simple_destructor = free;
+    win->playlist.history->collection.cmpfunc = (cx_compare_func)strcmp;
     win->playlist.current_track = -1;
+    win->playlist.current_history_pos = -1;
 }
 
 void PlayListAddFile(MainWindow *win, const char *file) {
@@ -50,7 +55,7 @@ void PlayListPlayNext(MainWindow *win, bool force) {
     switch(win->playlist.playback) {
         default: {
             if(current+1 < cxListSize(win->playlist.tracks)) {
-                current++;
+                current = PlayListGetNextFromHistory(win);
                 break;
             }
             return;
@@ -65,7 +70,7 @@ void PlayListPlayNext(MainWindow *win, bool force) {
             if(current+1 == cxListSize(win->playlist.tracks)) {
                 current = 0;
             } else {
-                current++;
+                current = PlayListGetNextFromHistory(win);
             }
             break;
         }
@@ -81,10 +86,22 @@ void PlayListPlayNext(MainWindow *win, bool force) {
         }
     }
     
-    PlayListPlayTrack(win, current);
+    PlayListPlayTrack(win, current, true);
+}
+
+void PlayListPlayPrev(MainWindow *win) {
+    CxList *hist = win->playlist.history;
+    if(cxListSize(hist) > 1 && win->playlist.current_history_pos > 0) {
+        win->playlist.current_history_pos--;
+        char *file = cxListAt(hist, win->playlist.current_history_pos);
+        size_t fileIndex = cxListFind(win->playlist.tracks, file);
+        if(cxListIndexValid(win->playlist.tracks, fileIndex)) {
+            PlayListPlayTrack(win, fileIndex, false);
+        }
+    }
 }
 
-void PlayListPlayTrack(MainWindow *win, int i) {
+void PlayListPlayTrack(MainWindow *win, int i, bool add2history) {
     CxList *tracks = win->playlist.tracks;
     if(i < cxListSize(tracks)) {
         char *file = cxListAt(tracks, i);
@@ -93,6 +110,15 @@ void PlayListPlayTrack(MainWindow *win, int i) {
             win->file = file;
             PlayerOpenFile(win);
             win->playlist.current_track = i;
+            if(add2history) {
+                cxListAdd(win->playlist.history, strdup(file));
+                size_t histlen = cxListSize(win->playlist.history);
+                win->playlist.current_history_pos = histlen-1;
+                if(histlen > PLAYLIST_HISTORY_MAX) {
+                    cxListRemove(win->playlist.history, 0);
+                    win->playlist.current_history_pos--;
+                }
+            }
         }
     }
 }
@@ -108,3 +134,23 @@ void PlayListSetMode(MainWindow *win, PlaybackMode mode) {
     XtVaSetValues(win->playAutoPlayButton, XmNset, mode == PLAYBACK_RANDOM, NULL);
     XtVaSetValues(win->playRandom, XmNset, mode == PLAYBACK_AUTOPLAY, NULL);
 }
+
+int PlayListGetNextFromHistory(MainWindow *win) {
+    size_t histlen = cxListSize(win->playlist.history);
+    if(win->playlist.current_history_pos+1 < histlen) {
+        char *hist_file = cxListAt(win->playlist.history, win->playlist.current_history_pos);
+        if(hist_file) {
+            size_t index = cxListFind(win->playlist.tracks, hist_file);
+            if(cxListIndexValid(win->playlist.tracks, index)) {
+                win->playlist.current_history_pos++;
+                return index;
+            }
+        }
+    }
+    
+    if(win->playlist.current_track+1 < cxListSize(win->playlist.tracks)) {
+        return win->playlist.current_track+1;
+    }
+    
+    return 0;
+}
index b9d6f2127c01793bb15be8119648f7d319aa4d74..df91c5356c040c89283336be821e22bda11ca22e 100644 (file)
 extern "C" {
 #endif
     
+#define PLAYLIST_HISTORY_MAX 255
+    
 void PlayListInit(MainWindow *win);
 
 void PlayListAddFile(MainWindow *win, const char *file);
 
 void PlayListPlayNext(MainWindow *win, bool force);
 
-void PlayListPlayTrack(MainWindow *win, int i);
+void PlayListPlayPrev(MainWindow *win);
+
+void PlayListPlayTrack(MainWindow *win, int i, bool add2history);
 
 void PlayListClear(MainWindow *win);
 
 void PlayListSetMode(MainWindow *win, PlaybackMode mode);
 
+int PlayListGetNextFromHistory(MainWindow *win);
+
 #ifdef __cplusplus
 }
 #endif
index a2dc541a0ec1a694cb96f97bf2a21d280197388d..0caece132c308a0ee01fd127a8060561914f5e78 100644 (file)
@@ -369,7 +369,7 @@ static Boolean cmd_open(XtPointer data) {
         clearPlaylist = FALSE;
     }
     PlayListAddFile(win, file);
-    PlayListPlayTrack(win, cxListSize(win->playlist.tracks)-1);
+    PlayListPlayTrack(win, cxListSize(win->playlist.tracks)-1, true);
     
     free(data);
     return 0;
index b350920c71c3161d5d216570f979162568a9106d..4adac0eb71dbf76f677e9ed535765581c463b9c3 100644 (file)
@@ -506,7 +506,7 @@ static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *mbargs, int nm
     win->playRandom = createToggleMenuItem(playMenu, "playRandom", "Random Playback", 'a', False, NULL, NULL, PlayRandomCB, win);
     win->playAutoPlayButton = createToggleMenuItem(playMenu, "playAutoNext", "Autoplay Folder", 'A', False, NULL, NULL, PlayAutoPlayCB, win);
     createMenuSeparator(playMenu);
-    //createMenuItem(playMenu, "playPrev", "Previous", 'P', NULL, NULL, PlayPreviousCB, win);
+    createMenuItem(playMenu, "playPrev", "Previous", 'P', NULL, NULL, PlayPreviousCB, win);
     createMenuItem(playMenu, "playPrev", "Next", 'N', NULL, NULL, PlayNextCB, win);
     XtVaSetValues(win->playRepeatTrackButton, XmNindicatorType, XmONE_OF_MANY, NULL);
     XtVaSetValues(win->playRepeatListButton, XmNindicatorType, XmONE_OF_MANY, NULL);
@@ -674,7 +674,8 @@ static void PlayRandomCB(Widget w, void *udata, void *cdata) {
 }
 
 static void PlayPreviousCB(Widget w, void *udata, void *cdata) {
-    
+    MainWindow *win = udata;
+    PlayListPlayPrev(win);
 }
 
 static void PlayNextCB(Widget w, void *udata, void *cdata) {
index 7519308f8e3748b4d348e831065783196a30becc..dd7d3fe58e4ea61adb41b7cff4c9a769677cdd04 100644 (file)
@@ -59,7 +59,9 @@ typedef struct Player {
 
 typedef struct {
     CxList *tracks;
+    CxList *history;
     int current_track;
+    int current_history_pos;
     PlaybackMode playback;
 } PlayList;