From: Olaf Wintermann Date: Sun, 21 Sep 2025 14:22:09 +0000 (+0200) Subject: implement play previous X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=8d65f703049e3e567e277811f4f3ddc7da62493a;p=uwplayer.git implement play previous --- diff --git a/application/Sidebar.c b/application/Sidebar.c index 85bd0eb..fbccd9f 100644 --- a/application/Sidebar.c +++ b/application/Sidebar.c @@ -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); diff --git a/application/playlist.c b/application/playlist.c index b5e2de4..5393bb1 100644 --- a/application/playlist.c +++ b/application/playlist.c @@ -33,7 +33,12 @@ 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; +} diff --git a/application/playlist.h b/application/playlist.h index b9d6f21..df91c53 100644 --- a/application/playlist.h +++ b/application/playlist.h @@ -29,18 +29,24 @@ 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 diff --git a/application/settings.c b/application/settings.c index a2dc541..0caece1 100644 --- a/application/settings.c +++ b/application/settings.c @@ -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; diff --git a/application/window.c b/application/window.c index b350920..4adac0e 100644 --- a/application/window.c +++ b/application/window.c @@ -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) { diff --git a/application/window.h b/application/window.h index 7519308..dd7d3fe 100644 --- a/application/window.h +++ b/application/window.h @@ -59,7 +59,9 @@ typedef struct Player { typedef struct { CxList *tracks; + CxList *history; int current_track; + int current_history_pos; PlaybackMode playback; } PlayList;