Skip to content

Commit

Permalink
save/load user playing state at any shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
DantSu committed Mar 11, 2024
1 parent 9046546 commit d3a3ce4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###########################################################

TARGET=TelmiOS
VERSION=1.0.4
VERSION=1.1.0

###########################################################

Expand Down
37 changes: 35 additions & 2 deletions src/storyTeller/music_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#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"
Expand All @@ -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());
}
Expand Down Expand Up @@ -225,6 +250,8 @@ void musicplayer_load(void)
video_displayImage(SYSTEM_RESOURCES, "noMusic.png");
return;
}

musicplayer_loadSession();

if(musicPlayerTrackIndex < 0) {
musicPlayerTrackIndex = musicPlayerTracksCount - 1;
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/storyTeller/stories_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include "time.h"
#include "system/display.h"
#include "utils/str.h"
#include "utils/json.h"
Expand Down
2 changes: 1 addition & 1 deletion src/storyTeller/storyTeller.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ int main(int argc, char *argv[])

while (1) {
if(autosleep_isSleepingTime()) {
app_save();
goto exit_loop;
}

Expand Down Expand Up @@ -173,6 +172,7 @@ int main(int argc, char *argv[])
}

exit_loop:
app_save();
display_setScreen(true);
video_audio_quit();
system_shutdown();
Expand Down
1 change: 1 addition & 0 deletions src/storyTeller/time_helper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef STORYTELLER_TIME_HELPER__
#define STORYTELLER_TIME_HELPER__

#include <time.h>


static long int get_time(void)
Expand Down

0 comments on commit d3a3ce4

Please sign in to comment.