Unify sxmo state switch and ensure consistency

If two power handler trigger at the same time, the current behavior is
that we switch two time to the same target state.

This move the flock to a central script that make sure only one switch
is ocurring at the same time. Concurrent switch will wait for the
previous one to finish before guessing the new target state.

This also refactorise the sxmo_proximitylock.sh to make it simple,
stripping the two related proximitylock and proximityunlock states.

Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
Signed-off-by: Anjandev Momi <anjan@momi.ca>
This commit is contained in:
Willow Barraco
2023-09-09 10:44:42 +02:00
committed by Anjandev Momi
parent ee180f6e51
commit ef8254681c
14 changed files with 114 additions and 86 deletions

View File

@@ -15,4 +15,4 @@ case "$SXMO_WM" in
;; ;;
esac esac
sxmo_hook_unlock.sh sxmo_state_switch.sh set unlock

View File

@@ -8,9 +8,6 @@
# shellcheck source=scripts/core/sxmo_common.sh # shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh . sxmo_common.sh
sxmo_log "transitioning to stage unlock"
printf unlock > "$SXMO_STATE"
sxmo_wakelock.sh lock sxmo_stay_awake "${SXMO_UNLOCK_IDLE_TIME:-120}s" sxmo_wakelock.sh lock sxmo_stay_awake "${SXMO_UNLOCK_IDLE_TIME:-120}s"
sxmo_hook_statusbar.sh state_change & sxmo_hook_statusbar.sh state_change &

View File

@@ -88,7 +88,7 @@ case "$WMCLASS" in
) ^ 1 ^ (rm $XDG_CACHE_HOME/sxmo/sxmo.nosuspend 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.nosuspend) ) ^ 1 ^ (rm $XDG_CACHE_HOME/sxmo/sxmo.nosuspend 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.nosuspend)
$icon_zzz Auto-screen-off $( $icon_zzz Auto-screen-off $(
[ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ] && printf "%s" "$icon_tof" || printf "%s" "$icon_ton" [ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ] && printf "%s" "$icon_tof" || printf "%s" "$icon_ton"
) ^ 1 ^ (rm $XDG_CACHE_HOME/sxmo/sxmo.noidle 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.noidle) && sxmo_hook_unlock.sh ) ^ 1 ^ (rm $XDG_CACHE_HOME/sxmo/sxmo.noidle 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.noidle) && sxmo_state_switch.sh set unlock
$icon_ror Autorotate $( $icon_ror Autorotate $(
sxmo_daemons.sh running autorotate -q && sxmo_daemons.sh running autorotate -q &&
printf "%s" "$icon_ton" || printf "%s" "$icon_tof" printf "%s" "$icon_ton" || printf "%s" "$icon_tof"
@@ -107,8 +107,8 @@ case "$WMCLASS" in
power) power)
# Power menu # Power menu
CHOICES=" CHOICES="
$icon_lck Lock ^ 0 ^ sxmo_hook_lock.sh $icon_lck Lock ^ 0 ^ sxmo_state_switch.sh set lock
$icon_lck Lock (Screen off) ^ 0 ^ sxmo_hook_screenoff.sh $icon_lck Lock (Screen off) ^ 0 ^ sxmo_state_switch.sh set screenoff
$icon_out Logout ^ 0 ^ confirm Logout && sxmo_power.sh logout $icon_out Logout ^ 0 ^ confirm Logout && sxmo_power.sh logout
$([ -f "$(xdg_data_path xsessions/sxmo.desktop)" ] && $([ -f "$(xdg_data_path xsessions/sxmo.desktop)" ] &&
[ -f "$(xdg_data_path wayland-sessions/swmo.desktop)" ] && [ -f "$(xdg_data_path wayland-sessions/swmo.desktop)" ] &&

View File

@@ -62,7 +62,7 @@ case "$SXMO_WM" in
esac esac
# To setup initial lock state # To setup initial lock state
sxmo_hook_unlock.sh sxmo_state_switch.sh set unlock
# Turn on auto-suspend # Turn on auto-suspend
if [ -w "/sys/power/wakeup_count" ] && [ -f "/sys/power/wake_lock" ]; then if [ -w "/sys/power/wakeup_count" ] && [ -f "/sys/power/wake_lock" ]; then

View File

@@ -27,11 +27,13 @@ set_state() {
return return
fi fi
if sxmo_daemons.sh running proximity_lock; then
sxmobar -a -e bold -f orange state 90 "$icon_state_proximity" # circle with dot
return
fi
STATE_LABEL="$(cat "$SXMO_STATE")" STATE_LABEL="$(cat "$SXMO_STATE")"
case "$STATE_LABEL" in case "$STATE_LABEL" in
proximity*)
sxmobar -a -e bold -f orange state 90 "$icon_state_proximity" # circle with dot
;;
screenoff) screenoff)
sxmobar -a -e bold -f red state 90 "$icon_state_screenoff" # filled circle sxmobar -a -e bold -f red state 90 "$icon_state_screenoff" # filled circle
;; ;;

View File

@@ -15,29 +15,6 @@ stop_proximity_lock() {
sxmo_daemons.sh stop proximity_lock sxmo_daemons.sh stop proximity_lock
} }
# this action will move the lock state $1 levels higher
lock_screen_action() {
count="${1:-1}"
state="$(cat "$SXMO_STATE")"
while [ "$count" -gt 0 ]; do
case "$state" in
unlock)
state=screenoff
;;
screenoff)
state=lock
;;
lock)
state=unlock
;;
esac
count=$((count-1))
done
sxmo_hook_"$state".sh
}
XPROPOUT="$(sxmo_wm.sh focusedwindow)" XPROPOUT="$(sxmo_wm.sh focusedwindow)"
WMCLASS="$(printf %s "$XPROPOUT" | grep app: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')" WMCLASS="$(printf %s "$XPROPOUT" | grep app: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')"
WMNAME="$(printf %s "$XPROPOUT" | grep title: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')" WMNAME="$(printf %s "$XPROPOUT" | grep title: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')"
@@ -61,16 +38,16 @@ if ! grep -q unlock "$SXMO_STATE"; then
esac esac
case "$ACTION" in case "$ACTION" in
"powerbutton_one") "powerbutton_one")
lock_screen_action sxmo_state_switch.sh up
;; ;;
"powerbutton_two") "powerbutton_two")
lock_screen_action 2 sxmo_state_switch.sh up 2
;; ;;
"powerbutton_three") "powerbutton_three")
if grep -q proximity "$SXMO_STATE"; then if grep -q proximity "$SXMO_STATE"; then
stop_proximity_lock stop_proximity_lock
else else
lock_screen_action 2 sxmo_state_switch.sh up 2
fi fi
;; ;;
"voldown_one") "voldown_one")
@@ -332,12 +309,12 @@ case "$ACTION" in
if echo "$WMCLASS" | grep -i "megapixels"; then if echo "$WMCLASS" | grep -i "megapixels"; then
sxmo_type.sh -k space sxmo_type.sh -k space
else else
lock_screen_action sxmo_state_switch.sh up
fi fi
exit 0 exit 0
;; ;;
"powerbutton_two") "powerbutton_two")
lock_screen_action 2 sxmo_state_switch.sh up 2
exit 0 exit 0
;; ;;
"powerbutton_three") "powerbutton_three")
@@ -472,9 +449,9 @@ case "$ACTION" in
sxmo_dmenu.sh close sxmo_dmenu.sh close
sxmo_keyboard.sh close sxmo_keyboard.sh close
if [ -n "$WMCLASS" ]; then if [ -n "$WMCLASS" ]; then
sxmo_hook_lock.sh sxmo_state_switch.sh set lock
else else
sxmo_hook_screenoff.sh sxmo_state_switch.sh set screenoff
fi fi
exit 0 exit 0
;; ;;

View File

@@ -14,18 +14,11 @@
# shellcheck source=scripts/core/sxmo_common.sh # shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh . sxmo_common.sh
exec 3<> "$SXMO_STATE.lock"
flock -x 3
sxmo_log "transitioning to stage lock"
printf lock > "$SXMO_STATE"
sxmo_wakelock.sh lock sxmo_not_screenoff infinite sxmo_wakelock.sh lock sxmo_not_screenoff infinite
# This hook is called when the system reaches a locked state # This hook is called when the system reaches a locked state
sxmo_led.sh blink blue & sxmo_led.sh blink blue &
sxmo_hook_statusbar.sh state_change &
[ "$SXMO_WM" = "sway" ] && swaymsg mode default [ "$SXMO_WM" = "sway" ] && swaymsg mode default
sxmo_wm.sh dpms off sxmo_wm.sh dpms off
@@ -37,7 +30,7 @@ sxmo_daemons.sh stop periodic_wakelock_check
# Go to screenoff after 8 seconds of inactivity # Go to screenoff after 8 seconds of inactivity
if ! [ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ]; then if ! [ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ]; then
sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \ sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
timeout "${SXMO_LOCK_IDLE_TIME:-8}" "sxmo_hook_screenoff.sh" timeout "${SXMO_LOCK_IDLE_TIME:-8}" "sxmo_state_switch.sh set screenoff"
fi fi
wait wait

View File

@@ -16,13 +16,6 @@
# This hook is called when the system reaches a off state (screen off) # This hook is called when the system reaches a off state (screen off)
exec 3<> "$SXMO_STATE.lock"
flock -x 3
sxmo_log "transitioning to stage off"
printf screenoff > "$SXMO_STATE"
sxmo_hook_statusbar.sh state_change &
[ "$SXMO_WM" = "sway" ] && swaymsg mode default [ "$SXMO_WM" = "sway" ] && swaymsg mode default
sxmo_wm.sh dpms on sxmo_wm.sh dpms on
sxmo_wm.sh inputevent touchscreen off sxmo_wm.sh inputevent touchscreen off

View File

@@ -8,16 +8,9 @@
# This hook is called when the system becomes unlocked again # This hook is called when the system becomes unlocked again
exec 3<> "$SXMO_STATE.lock"
flock -x 3
sxmo_log "transitioning to stage unlock"
printf unlock > "$SXMO_STATE"
sxmo_wakelock.sh lock sxmo_not_screenoff infinite sxmo_wakelock.sh lock sxmo_not_screenoff infinite
sxmo_led.sh blink red green & sxmo_led.sh blink red green &
sxmo_hook_statusbar.sh state_change &
sxmo_wm.sh dpms off sxmo_wm.sh dpms off
sxmo_wm.sh inputevent touchscreen on sxmo_wm.sh inputevent touchscreen on
@@ -34,12 +27,12 @@ else
sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \ sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
timeout "${SXMO_UNLOCK_IDLE_TIME:-120}" 'sh -c " timeout "${SXMO_UNLOCK_IDLE_TIME:-120}" 'sh -c "
swaymsg mode default; swaymsg mode default;
exec sxmo_hook_lock.sh exec sxmo_state_switch.sh set lock
"' "'
;; ;;
dwm) dwm)
sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \ sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
timeout "${SXMO_UNLOCK_IDLE_TIME:-120}" "sxmo_hook_lock.sh" timeout "${SXMO_UNLOCK_IDLE_TIME:-120}" "sxmo_state_switch.sh set lock"
;; ;;
esac esac
fi fi

View File

@@ -20,38 +20,35 @@ finish() {
printf 6553 > "$prox_path/events/in_proximity_thresh_rising_value" printf 6553 > "$prox_path/events/in_proximity_thresh_rising_value"
sxmo_wakelock.sh unlock sxmo_proximity_lock_running sxmo_wakelock.sh unlock sxmo_proximity_lock_running
sxmo_daemons.sh start state_change_bar sxmo_hook_statusbar.sh state_change
exec sxmo_hook_"$INITIALSTATE".sh if [ -n "$INITIALSTATE" ]; then
sxmo_state_switch.sh set "$INITIALSTATE"
fi
exit
} }
near() { near() {
if [ -z "$INITIALSTATE" ]; then
INITIALSTATE="$(cat "$SXMO_STATE")"
fi
sxmo_debug "near" sxmo_debug "near"
sxmo_wm.sh dpms on sxmo_state_switch.sh set screenoff
sxmo_wm.sh inputevent touchscreen off
printf proximitylock > "$SXMO_STATE"
} }
far() { far() {
if [ -z "$INITIALSTATE" ]; then
INITIALSTATE="$(cat "$SXMO_STATE")"
fi
sxmo_debug "far" sxmo_debug "far"
sxmo_wm.sh dpms off sxmo_state_switch.sh set unlock
sxmo_wm.sh inputevent touchscreen on
printf proximityunlock > "$SXMO_STATE"
} }
exec 3<> "$SXMO_STATE.lock"
flock -x 3
sxmo_log "transitioning to stage proximitylock"
INITIALSTATE="$(cat "$SXMO_STATE")"
printf proximitylock > "$SXMO_STATE"
trap 'finish' TERM INT trap 'finish' TERM INT
sxmo_daemons.sh stop idle_locker
sxmo_wakelock.sh lock sxmo_proximity_lock_running infinite sxmo_wakelock.sh lock sxmo_proximity_lock_running infinite
sxmo_daemons.sh start state_change_bar sxmo_hook_statusbar.sh state_change
# find the device # find the device
if [ -z "$SXMO_PROX_RAW_BUS" ]; then if [ -z "$SXMO_PROX_RAW_BUS" ]; then

View File

@@ -0,0 +1,76 @@
#!/bin/sh
# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
transition() {
sxmo_log "transitioning to stage $state"
printf %s "$state" > "$SXMO_STATE"
sxmo_hook_"$state".sh &
sxmo_hook_statusbar.sh state_change &
wait
}
up() {
count="${1:-1}"
while [ "$count" -gt 0 ]; do
case "$state" in
unlock)
state=screenoff
;;
screenoff)
state=lock
;;
lock)
state=unlock
;;
esac
count=$((count-1))
done
transition
}
down() {
count="${1:-1}"
while [ "$count" -gt 0 ]; do
case "$state" in
unlock)
state=lock
;;
screenoff)
state=unlock
;;
lock)
state=screenoff
;;
esac
count=$((count-1))
done
transition
}
exec 3<> "$SXMO_STATE.lock"
flock -x 3
state="$(cat "$SXMO_STATE")"
action="$1"
shift
case "$action" in
up)
up "$@"
;;
down)
down "$@"
;;
set)
case "$1" in
lock|unlock|screenoff)
state="$1"
transition
;;
esac
;;
esac

View File

@@ -199,7 +199,7 @@ processmms() {
fi fi
if grep -q screenoff "$SXMO_STATE"; then if grep -q screenoff "$SXMO_STATE"; then
sxmo_hook_lock.sh sxmo_state_switch.sh set lock
fi fi
if [ "$NUM_RECIPIENTS" -gt 1 ]; then if [ "$NUM_RECIPIENTS" -gt 1 ]; then

View File

@@ -266,7 +266,7 @@ checkfornewtexts() {
fi fi
if grep -q screenoff "$SXMO_STATE"; then if grep -q screenoff "$SXMO_STATE"; then
sxmo_hook_lock.sh sxmo_state_switch.sh set lock
fi fi
sxmo_hook_sms.sh "$CONTACTNAME" "$TEXT" sxmo_hook_sms.sh "$CONTACTNAME" "$TEXT"

View File

@@ -44,7 +44,7 @@ processvvm() {
fi fi
if grep -q screenoff "$SXMO_STATE"; then if grep -q screenoff "$SXMO_STATE"; then
sxmo_hook_lock.sh sxmo_state_switch.sh set lock
fi fi
sxmo_hook_sms.sh "$VVM_SENDER" "VVM" "$VVM_ID" sxmo_hook_sms.sh "$VVM_SENDER" "VVM" "$VVM_ID"