From 8123b9a99f164e450d72b17b7d9100cef1c997c5 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Fri, 13 Feb 2026 21:55:48 +0100 Subject: [PATCH] Remember last cassette on startup, always pause on cassette switch Persist the current cassette filename to last_cassette.txt so the player resumes the same cassette after restart. Switching cassettes now always lands in a paused state to avoid jarring mid-playback jumps. Co-Authored-By: Claude Opus 4.6 --- docs/sdlamp2-fsd.md | 5 +++++ src/sdlamp2.c | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/docs/sdlamp2-fsd.md b/docs/sdlamp2-fsd.md index b814a23..18ddf80 100644 --- a/docs/sdlamp2-fsd.md +++ b/docs/sdlamp2-fsd.md @@ -43,6 +43,11 @@ This document specifies the functional requirements for an SDL2 based media play ## 6. Changelog +### 2026-02-13 — Remember last cassette and pause on switch + +- **Remember last cassette**: The current cassette filename is saved to `last_cassette.txt` in the audio directory on every file switch. On startup, the player resumes the last-loaded cassette instead of always starting with the first file. Falls back to the first file if the saved file is missing or not found. +- **Pause on cassette switch**: Switching cassettes (prev/next) now always lands in a paused state, even if the player was playing. This avoids the jarring effect of immediately resuming from a saved position in a different cassette. + ### 2026-02-13 — Fix power button shutdown regression - **Consolidated input handling**: Power button long-press shutdown is now handled by `rg35xx-screen-monitor.py` instead of a separate `evtest`-based monitor in the wrapper. Both monitors were reading `/dev/input/event0` simultaneously, causing the `evtest` parser to miss power button events on the device's Linux 4.9 kernel. diff --git a/src/sdlamp2.c b/src/sdlamp2.c index a460624..36e1214 100644 --- a/src/sdlamp2.c +++ b/src/sdlamp2.c @@ -197,6 +197,37 @@ static void adjust_volume(float delta) { save_volume(); } +/* --- Last cassette persistence --- */ + +static void save_last_cassette(const char* filename) { + char path[1024]; + snprintf(path, sizeof(path), "%s/last_cassette.txt", audio_dir); + FILE* f = fopen(path, "w"); + if (f) { + fprintf(f, "%s\n", filename); + fclose(f); + } +} + +static int load_last_cassette(void) { + char path[1024]; + snprintf(path, sizeof(path), "%s/last_cassette.txt", audio_dir); + FILE* f = fopen(path, "r"); + if (!f) return 0; + char name[256]; + if (!fgets(name, sizeof(name), f)) { + fclose(f); + return 0; + } + fclose(f); + char* nl = strchr(name, '\n'); + if (nl) *nl = '\0'; + for (int i = 0; i < num_audio_files; i++) { + if (strcmp(audio_files[i], name) == 0) return i; + } + return 0; +} + /* --- File scanning --- */ static int has_audio_extension(const char* name) { @@ -484,11 +515,14 @@ static void switch_file(int index) { decoder_seek(pos); } + save_last_cassette(current_file); + char title[768]; snprintf(title, sizeof(title), "SDLamp2 - %s", current_file); SDL_SetWindowTitle(window, title); - SDL_PauseAudioDevice(audio_device, paused); + paused = SDL_TRUE; + SDL_PauseAudioDevice(audio_device, 1); return; } @@ -674,7 +708,7 @@ int main(int argc, char** argv) { scan_audio_files(audio_dir); volume = load_volume(); if (num_audio_files > 0) { - switch_file(0); + switch_file(load_last_cassette()); } else { SDL_SetWindowTitle(window, "SDLamp2 - No audio files found"); }