To do this, we add store, restore, and flushstored to sxmo_state.sh.
store output an id that the caller have to give back to restore. This way,
multiple caller can store, and only the first one that restore actually
does so. This ensure some consistency to the end state workflow.
Any user click in between automatically flushstored, so that the end of
proximitylock will not restore to initial state.
Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
---
scripts/core/sxmo_proximitylock.sh | 16 +++------------
scripts/core/sxmo_state.sh | 33 ++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 13 deletions(-)
diff --git a/scripts/core/sxmo_proximitylock.sh b/scripts/core/sxmo_proximitylock.sh
index cef75fdf..863f7bb2 100755
--- a/scripts/core/sxmo_proximitylock.sh
+++ b/scripts/core/sxmo_proximitylock.sh
@@ -10,28 +10,16 @@
finish() {
sxmo_wakelock.sh unlock sxmo_proximity_lock_running
-
- if [ -n "$INITIALSTATE" ]; then
- sxmo_state.sh set "$INITIALSTATE"
- fi
-
+ sxmo_state.sh restore "$storeid"
exit
}
near() {
- if [ -z "$INITIALSTATE" ]; then
- INITIALSTATE="$(sxmo_state.sh get)"
- fi
-
sxmo_debug "near"
sxmo_state.sh set screenoff
}
far() {
- if [ -z "$INITIALSTATE" ]; then
- INITIALSTATE="$(sxmo_state.sh get)"
- fi
-
sxmo_debug "far"
sxmo_state.sh set unlock
}
@@ -47,6 +35,8 @@ else
prox_raw_bus="$SXMO_PROX_RAW_BUS"
fi
+storeid="$(sxmo_state.sh store)"
+
while : ; do
value="$(cat "$prox_raw_bus")"
if [ "$value" -gt 100 ] && [ "$last" != "near" ]; then
diff --git a/scripts/core/sxmo_state.sh b/scripts/core/sxmo_state.sh
index 592597e9..6b51e48d 100644
--- a/scripts/core/sxmo_state.sh
+++ b/scripts/core/sxmo_state.sh
@@ -92,6 +92,7 @@ click() {
else
transition "$state"
fi
+ flushstored
}
idle() {
@@ -113,6 +114,25 @@ idle() {
sxmo_log "idle: not transitioning from $state"
}
+store() {
+ storeid="$(tr -dc 'a-zA-Z0-9' < /dev/urandom 2>/dev/null | head -c 10)"
+ printf %s "$state" > "$SXMO_STATE.stored.$storeid"
+ printf %s "$storeid"
+}
+
+flushstored() {
+ find "$(dirname "$SXMO_STATE")" -name 'sxmo.state.stored.*' -delete
+}
+
+restore() {
+ storeid="$1"
+ if [ -f "$SXMO_STATE.stored.$storeid" ]; then
+ state="$(cat "$SXMO_STATE.stored.$storeid")"
+ transition "$state"
+ flushstored
+ fi
+}
+
exec 3<> "$SXMO_STATE.lock"
lock_exclusive() {
@@ -146,4 +166,17 @@ case "$action" in
transition "$1"
fi
;;
+ store)
+ lock_exclusive
+ read_state
+ store
+ ;;
+ restore)
+ lock_exclusive
+ restore "$1"
+ ;;
+ flushstored)
+ lock_exclusive
+ flushstored
+ ;;
esac
--
2.44.0