Rework the sxmo screenlock

This commit is contained in:
Anjandev Momi 2021-04-30 20:34:19 -04:00 committed by Stacy Harper
parent 7fbc1d347a
commit 725d89d6d6
No known key found for this signature in database
GPG key ID: 5BAC92328B7C5D65
12 changed files with 352 additions and 604 deletions

View file

@ -166,9 +166,9 @@ programchoicesinit() {
power )
# Power menu
CHOICES="
$icon_lck Lock ^ 0 ^ sxmo_lock.sh
$icon_lck Lock (Screen off) ^ 0 ^ sxmo_lock.sh --screen-off
$icon_zzz Suspend ^ 0 ^ sxmo_lock.sh --suspend
$icon_lck Lock ^ 0 ^ sxmo_screenlock.sh lock && sxmo_unlocklistener.sh
$icon_lck Lock (Screen off) ^ 0 ^ sxmo_screenlock.sh off && sxmo_unlocklistener.sh
$icon_zzz Suspend ^ 0 ^ sxmo_screenlock.sh crust
$icon_out Logout ^ 0 ^ pkill -9 dwm
$icon_rld Reboot ^ 0 ^ sxmo_terminal.sh sudo reboot
$icon_pwr Poweroff ^ 0 ^ sxmo_terminal.sh sudo poweroff

View file

@ -19,6 +19,10 @@ export LOGDIR="$XDG_DATA_HOME"/sxmo/modem
export CONTACTFILE="$XDG_CONFIG_HOME/sxmo/contacts.tsv"
# shellcheck disable=SC2034
export MODEMSTATEFILE="$XDG_RUNTIME_DIR/sxmo.modem.state"
# shellcheck disable=SC2034
export UNSUSPENDREASONFILE="$XDG_RUNTIME_DIR/sxmo.suspend.reason"
# shellcheck disable=SC2034
export LASTSTATE="$XDG_RUNTIME_DIR/sxmo.suspend.laststate"
command -v "$KEYBOARD" > /dev/null || export KEYBOARD=svkbd-mobile-intl
command -v "$EDITOR" > /dev/null || export EDITOR=vis

View file

@ -9,6 +9,44 @@ ACTION="$1"
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"
crust() {
if [ -n "$SXMO_RTCWAKEINTERVAL" ]; then
sxmo_screenlock.sh rtc "$SXMO_RTCWAKEINTERVAL"
else
sxmo_screenlock.sh crust
fi
}
lock_screen() {
if [ "$SXMO_LOCK_SCREEN_OFF" = "1" ]; then
sxmo_screenlock.sh off
else
sxmo_screenlock.sh lock
fi
if [ "$SXMO_LOCK_SUSPEND" = "1" ]; then
crust
fi
}
if [ "$(sxmo_screenlock.sh getCurState)" != "unlock" ]; then
case "$ACTION" in
"volup_three")
crust
;;
"voldown_three")
if [ "$(sxmo_screenlock.sh getCurState)" = "lock" ]; then
sxmo_screenlock.sh off
else
lock_screen
fi
;;
"powerbutton_three")
sxmo_screenlock.sh unlock
;;
esac
exit
fi
XPROPOUT="$(xprop -id "$(xdotool getactivewindow)")"
WMCLASS="$(echo "$XPROPOUT" | grep WM_CLASS | cut -d ' ' -f3-)"
WMNAME=$(echo "$XPROPOUT" | grep -E "^WM_NAME" | cut -d ' ' -f3-)
@ -153,7 +191,7 @@ if [ "$HANDLE" -ne 0 ]; then
sxmo_appmenu.sh sys
;;
"volup_three")
sxmo_lock.sh
lock_screen
;;
"voldown_one")
xdotool key --clearmodifiers Super+space
@ -174,7 +212,7 @@ if [ "$HANDLE" -ne 0 ]; then
sxmo_appmenu.sh scripts &
;;
"bottomleftcorner")
sxmo_lock.sh &
lock_screen
;;
"bottomrightcorner")
sxmo_rotate.sh &

View file

@ -1,17 +1,12 @@
#!/usr/bin/env sh
islocked() {
pgrep -f sxmo_lock.sh > /dev/null
}
syncstate() {
islocked && STATE=locked || STATE=free
isLocked() {
curState="$(sxmo_screenlock.sh getCurState)"
[ "$curState" = "lock" ] || [ "$curState" = "off" ]
}
finish() {
kill "$(jobs -p)"
syncstate
[ free = "$STATE" ] && [ true = "$WASLOCKED" ] && sxmo_lock.sh &
sxmo_screenlock.sh "$INITIALSTATE"
exit 0
}
@ -27,26 +22,15 @@ TARGET=30
mainloop() {
while true; do
distance="$(distance)"
# here we do not syncstate to allow user manual lock
if [ locked = "$STATE" ] && [ "$distance" -lt "$TARGET" ]; then
pkill -f sxmo_lock.sh
STATE=free
elif [ free = "$STATE" ] && [ "$distance" -gt "$TARGET" ]; then
islocked && pkill -f sxmo_lock.sh # we want screen-off on proximity
sxmo_lock.sh --screen-off &
STATE=locked
if isLocked && [ "$distance" -lt "$TARGET" ]; then
sxmo_screenlock.sh unlock
elif ! isLocked && [ "$distance" -gt "$TARGET" ]; then
sxmo_screenlock.sh off
fi
sleep 0.5
done
}
syncstate
if [ locked = "$STATE" ]; then
WASLOCKED=true
# we dont want to loose the initial lock if the phone is forgotten somewhere
# without proximity as this will prevent going back to crust
sxmo_movement.sh waitmovement
fi
INITIALSTATE="$(sxmo_screenlock.sh getCurState)"
mainloop

View file

@ -1,11 +0,0 @@
#!/bin/sh
# this script resets the wireless scan interval (value is in ms)
# it is invoked with a delay after waking from sleep
# to prevent the scan interval from being too quick, and thus
# too battery consuming, whilst no networks are found
# the kernel parameter must be writable for the user
# or this script must have the setsuid bit set!
echo 16000 > /sys/module/8723cs/parameters/rtw_scan_interval_thr

196
scripts/core/sxmo_screenlock.sh Executable file
View file

@ -0,0 +1,196 @@
#!/usr/bin/env sh
# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"
# Run xinput and get touchscreen id
TOUCH_POINTER_ID="${TOUCH_POINTER_ID:-"8"}"
REDLED_PATH="/sys/class/leds/red:indicator/brightness"
BLUELED_PATH="/sys/class/leds/blue:indicator/brightness"
WAKEUPRTC="/sys/class/wakeup/wakeup1/active_count"
MODEMUPRTC="/sys/class/wakeup/wakeup10/active_count"
NETWORKRTCSCAN="/sys/module/8723cs/parameters/rtw_scan_interval_thr"
OLD_RTC_WAKECOUNT="$XDG_RUNTIME_DIR/wakeup.rtc.count"
OLD_MODEM_WAKECOUNT="$XDG_RUNTIME_DIR/wakeup.modem.count"
saveAllEventCounts() {
cat "$WAKEUPRTC" > "$OLD_RTC_WAKECOUNT"
cat "$MODEMUPRTC" > "$OLD_MODEM_WAKECOUNT"
# TODO: add logic for modem wakeup
}
whichWake() {
if [ "$(cat $WAKEUPRTC)" -gt "$(cat $OLD_RTC_WAKECOUNT)" ] ; then
echo "rtc"
elif [ "$(cat $MODEMUPRTC)" -gt "$(cat $OLD_MODEM_WAKECOUNT)" ] ; then
echo "modem"
else
# button does not have a active count so if it's none of the above, it has to be the button
echo "button"
fi
}
getCurState() {
if xinput list-props "$TOUCH_POINTER_ID" | grep "Device Enabled" | grep -q "0$"; then
if xset q | grep -q "Off: 3"; then
echo "off"
else
echo "lock"
fi
else
echo "unlock"
fi
}
updateLed() {
case "$(getCurState)" in
"off")
echo 1 > "$REDLED_PATH"
echo 1 > "$BLUELED_PATH"
;;
"lock")
echo 0 > "$REDLED_PATH"
echo 1 > "$BLUELED_PATH"
;;
"unlock")
echo 0 > "$REDLED_PATH"
echo 0 > "$BLUELED_PATH"
;;
esac
}
if [ "$1" = "lock" ] ; then
# always echo last state first so that user can use it in their hooks
# TODO: Document LASTSTATE
getCurState > "$LASTSTATE"
# Do we want this hook after disabling all the input devices so users can enable certain devices?
if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/lock" ]; then
"$XDG_CONFIG_HOME/sxmo/hooks/lock"
fi
xset dpms 0 0 0
xset dpms force on
# TODO: Could be improved by running xinput and disabling ALL input devices automatically but would need
# to decide on the hook issues. Do we want a prelock and postlock? Or should users
# be expected to edit the source code for disabling certain input devices?
# this code allows us to not use the slock locking mechanism in the original sxmo_lock.sh
# when combined with a working slock (see ~iv's) implementation, this should be secure.
xinput disable "$TOUCH_POINTER_ID"
killall lisgd
updateLed
exit 0
elif [ "$1" = "unlock" ] ; then
getCurState > "$LASTSTATE"
if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/unlock" ]; then
"$XDG_CONFIG_HOME/sxmo/hooks/unlock"
fi
xset dpms 0 0 0
xset dpms force on
xinput enable "$TOUCH_POINTER_ID"
sxmo_lisgdstart.sh
updateLed
exit 0
elif [ "$1" = "off" ] ; then
getCurState > "$LASTSTATE"
# TODO: document this hook
if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/screenoff" ]; then
"$XDG_CONFIG_HOME/sxmo/hooks/screenoff"
fi
xset dpms 0 0 3
xset dpms force off
# stop responding to input
xinput disable "$TOUCH_POINTER_ID"
killall lisgd
updateLed
exit 0
elif [ "$1" = "crust" ] ; then
getCurState > "$LASTSTATE"
if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/presuspend" ]; then
"$XDG_CONFIG_HOME/sxmo/hooks/presuspend"
fi
echo 1 > "$REDLED_PATH"
echo 0 > "$BLUELED_PATH"
xset dpms force off
# configure crust
# TODO: disable all wakeup sources other than button, rtc, and modem.
# TODO: make sure there is logic in whichWake and saveAllEventCounts functions
# Do I need to unbind? https://git.sr.ht/~mil/sxmo-utils/commit/bcf4f5c24968df0055d15a9fca649f67de9ced6a
echo "deep" > /sys/power/mem_sleep # deep sleep
echo "mem" > /sys/power/state
echo "crust" > "$LASTSTATE"
updateLed
xset dpms force on
# all we know is it's not the rtc. Maybe modem?
# TODO: Check mmcli or something or sxmo's notifs when
# https://gitlab.freedesktop.org/mobile-broadband/ModemManager/-/issues/356
# https://gitlab.com/postmarketOS/pmaports/-/merge_requests/2066
# fixed
# TODO: Document UNSUSPENDREASONFILE
echo "nonrtc" > "$UNSUSPENDREASONFILE"
if [ "$(whichWake)" = "button" ] && [ -x "$XDG_CONFIG_HOME/sxmo/hooks/postwake" ]; then
"$XDG_CONFIG_HOME/sxmo/hooks/postwake"
fi
exit 0
elif [ "$1" = "rtc" ] ; then
getCurState > "$LASTSTATE"
# USER MUST USE sxmo_screenlock.sh rtc rather than using rtcwake directly.
# With this new version of lock, we dont check the exit code of the user hook. User must execute "sxmo_screenlock.sh rtc $TIME" at the end of their hook (depending on whether they want to re-rtc)
echo 1 > "$REDLED_PATH"
echo 0 > "$BLUELED_PATH"
saveAllEventCounts
if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/presuspend" ]; then
"$XDG_CONFIG_HOME/sxmo/hooks/presuspend"
fi
xset dpms force off
rtcwake -m mem -s "$2"
whichWake > "$UNSUSPENDREASONFILE"
echo "crust" > "$LASTSTATE"
updateLed
if [ "$(whichWake)" = "rtc" ]; then
WAKEHOOK="$XDG_CONFIG_HOME/sxmo/hooks/rtcwake";
elif [ "$(whichWake)" = "button" ]; then
WAKEHOOK="$XDG_CONFIG_HOME/sxmo/hooks/postwake";
fi
if [ "$(whichWake)" != "rtc" ]; then
xset dpms force on
fi
if [ -x "$WAKEHOOK" ]; then
echo 1200 > "$NETWORKRTCSCAN"
"$WAKEHOOK"
echo 16000 > "$NETWORKRTCSCAN"
fi
exit 0
elif [ "$1" = "getCurState" ] ; then
getCurState
exit 0
fi
echo "usage: sxmo_screenlock.sh [lock|unlock|off|crust|rtc|getCurState]"