From d3a3ce47b3745ba3652147ffb2e1687616e0a338 Mon Sep 17 00:00:00 2001 From: Franck ALARY Date: Mon, 11 Mar 2024 14:16:22 +0100 Subject: [PATCH] save/load user playing state at any shutdown --- Makefile | 2 +- src/storyTeller/music_player.h | 37 ++++++++++++++++++++++++++++++-- src/storyTeller/stories_reader.h | 1 - src/storyTeller/storyTeller.c | 2 +- src/storyTeller/time_helper.h | 1 + 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index dee319c05..941335d3a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ ########################################################### TARGET=TelmiOS -VERSION=1.0.4 +VERSION=1.1.0 ########################################################### diff --git a/src/storyTeller/music_player.h b/src/storyTeller/music_player.h index df39c7a12..367c80c3d 100644 --- a/src/storyTeller/music_player.h +++ b/src/storyTeller/music_player.h @@ -4,13 +4,15 @@ #include #include +#include #include #include -#include #include #include "system/display.h" #include "utils/str.h" +#include "utils/json.h" +#include "./app_file.h" #include "./app_autosleep.h" #include "./sdl_helper.h" #include "./app_parameters.h" @@ -36,6 +38,29 @@ static long int musicPlayerScreenUpdate = 0; static long int musicPlayerLastActivity = 0; static void (*callback_musicplayer_autoplay)(void); + +void musicplayer_saveSession(void) +{ + file_save( + APP_SAVEFILE, + "{\"app\":%d, \"musicIndex\":%d, \"musicPosition\":%d}", + APP_MUSIC, + musicPlayerTrackIndex, + musicPlayerTrackPosition + ); +} + +void musicplayer_loadSession(void) +{ + cJSON *savedState = json_load(APP_SAVEFILE); + int a; + if(savedState != NULL && json_getInt(savedState, "app", &a) && a == APP_MUSIC) { + json_getInt(savedState, "musicIndex", &musicPlayerTrackIndex); + json_getInt(savedState, "musicPosition", &musicPlayerTrackPosition); + remove(APP_SAVEFILE); + } +} + void musicplayer_autosleep_unlock(void) { autosleep_unlock(parameters_getScreenOnInactivityTime(), parameters_getScreenOffInactivityTime()); } @@ -225,6 +250,8 @@ void musicplayer_load(void) video_displayImage(SYSTEM_RESOURCES, "noMusic.png"); return; } + + musicplayer_loadSession(); if(musicPlayerTrackIndex < 0) { musicPlayerTrackIndex = musicPlayerTracksCount - 1; @@ -404,7 +431,13 @@ void musicplayer_menu(void) void musicplayer_save(void) { - + if(Mix_PlayingMusic() == 1) { + if (Mix_PausedMusic() != 1) { + musicplayer_pause(); + } + } + + musicplayer_saveSession(); } bool musicplayer_isMp3File(const char *fileName) diff --git a/src/storyTeller/stories_reader.h b/src/storyTeller/stories_reader.h index d4ddaefdf..fd1b24e8d 100644 --- a/src/storyTeller/stories_reader.h +++ b/src/storyTeller/stories_reader.h @@ -4,7 +4,6 @@ #include #include #include -#include "time.h" #include "system/display.h" #include "utils/str.h" #include "utils/json.h" diff --git a/src/storyTeller/storyTeller.c b/src/storyTeller/storyTeller.c index be2d104e8..a95d9399f 100644 --- a/src/storyTeller/storyTeller.c +++ b/src/storyTeller/storyTeller.c @@ -63,7 +63,6 @@ int main(int argc, char *argv[]) while (1) { if(autosleep_isSleepingTime()) { - app_save(); goto exit_loop; } @@ -173,6 +172,7 @@ int main(int argc, char *argv[]) } exit_loop: + app_save(); display_setScreen(true); video_audio_quit(); system_shutdown(); diff --git a/src/storyTeller/time_helper.h b/src/storyTeller/time_helper.h index cd640ef02..586ba6546 100644 --- a/src/storyTeller/time_helper.h +++ b/src/storyTeller/time_helper.h @@ -1,6 +1,7 @@ #ifndef STORYTELLER_TIME_HELPER__ #define STORYTELLER_TIME_HELPER__ +#include static long int get_time(void)