Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/usr/lib/rsetup/cli/system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
source "/usr/lib/rsetup/mod/block_helpers.sh"
source "/usr/lib/rsetup/mod/hwid.sh"
source "/usr/lib/rsetup/mod/get_setup_script.sh"
source "/usr/lib/rsetup/mod/suspend.sh"

ALLOWED_RCONFIG_FUNC+=(
"update_generic_hostname"
Expand Down Expand Up @@ -386,3 +387,33 @@ set_autologin_status() {
;;
esac
}

set_suspend_status() {
__parameter_count_check 1 "$@"

local switch="$1" action

case "$switch" in
Comment thread
RadxaYuntian marked this conversation as resolved.
ON)
action="true"
;;
OFF)
action="false"
;;
*)
echo "Invalid options: $switch (use ON/OFF)" >&2
return 1
;;
esac

require_root

__systemd_sleep_control "$action"
__logind_control "$action"
__gnome_schema_patch "$action"
__gnome_gsettings "$action"
__kde_control "$action"

echo "Sleep control applied: $action"
echo "Reboot is recommended to ensure GNOME schema changes fully take effect."
}
157 changes: 157 additions & 0 deletions src/usr/lib/rsetup/mod/suspend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# shellcheck shell=bash

require_root() {
if [[ "$(id -u)" -ne 0 ]]; then
echo "Please run as root"
exit 1
fi
}

cmd_exists() { command -v "$1" >/dev/null 2>&1; }

# -------------------------------------------------------------------
# systemd
# -------------------------------------------------------------------
__systemd_sleep_control() {
local action="$1"
local f="/etc/systemd/sleep.conf.d/99-disable-sleep.conf"

if [[ "$action" == "true" ]]; then
install -d /etc/systemd/sleep.conf.d
cat >"$f" <<EOF
[Sleep]
AllowSuspend=no
AllowHibernation=no
AllowHybridSleep=no
AllowSuspendThenHibernate=no
EOF
systemctl mask sleep.target suspend.target hibernate.target \
hybrid-sleep.target suspend-then-hibernate.target
else
rm -f "$f"
systemctl unmask sleep.target suspend.target hibernate.target \
hybrid-sleep.target suspend-then-hibernate.target
fi
}

# -------------------------------------------------------------------
# logind
# -------------------------------------------------------------------
__logind_control() {
local action="$1"
local f="/etc/systemd/logind.conf.d/99-disable-sleep.conf"

if [[ "$action" == "true" ]]; then
install -d /etc/systemd/logind.conf.d
cat >"$f" <<EOF
[Login]
HandleSuspendKey=ignore
HandleHibernateKey=ignore
HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore
EOF
else
rm -f "$f"
fi
}

# -------------------------------------------------------------------
# GNOME schema
# -------------------------------------------------------------------
__gnome_schema_patch() {
local action="$1"
local schema_dir="/usr/share/glib-2.0/schemas"
local schema_file="$schema_dir/org.gnome.settings-daemon.plugins.power.gschema.xml"
local backup="$schema_file.bak.sleep-toggle"

[[ -f "$schema_file" ]] || return 0

if [[ "$action" == "true" ]]; then
if [[ ! -f "$backup" ]]; then
cp "$schema_file" "$backup"
fi

# set default timeouts to 0 (never)
sed -i \
-e "s|\(<key name=\"sleep-inactive-ac-timeout\" type=\"i\">\s*<default>\)[0-9]\+|\10|g" \
-e "s|\(<key name=\"sleep-inactive-battery-timeout\" type=\"i\">\s*<default>\)[0-9]\+|\10|g" \
"$schema_file"

glib-compile-schemas "$schema_dir"
else
if [[ -f "$backup" ]]; then
cp "$backup" "$schema_file"
glib-compile-schemas "$schema_dir"
fi
fi
}

# -------------------------------------------------------------------
# GNOME user gsettings
# -------------------------------------------------------------------
__gnome_gsettings() {
local action="$1"
cmd_exists gsettings || return 0

for d in /run/user/*; do
[[ -S "$d/bus" ]] || continue
local uid="${d##*/}"
local user
user="$(getent passwd "$uid" | cut -d: -f1)"
[[ -n "$user" ]] || continue

if [[ "$action" == "true" ]]; then
sudo -u "$user" \
DBUS_SESSION_BUS_ADDRESS="unix:path=$d/bus" \
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 0 || true
sudo -u "$user" \
DBUS_SESSION_BUS_ADDRESS="unix:path=$d/bus" \
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout 0 || true
sudo -u "$user" \
DBUS_SESSION_BUS_ADDRESS="unix:path=$d/bus" \
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing' || true
sudo -u "$user" \
DBUS_SESSION_BUS_ADDRESS="unix:path=$d/bus" \
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'nothing' || true
else
sudo -u "$user" \
DBUS_SESSION_BUS_ADDRESS="unix:path=$d/bus" \
gsettings reset org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout || true
sudo -u "$user" \
DBUS_SESSION_BUS_ADDRESS="unix:path=$d/bus" \
gsettings reset org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout || true
sudo -u "$user" \
DBUS_SESSION_BUS_ADDRESS="unix:path=$d/bus" \
gsettings reset org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type || true
sudo -u "$user" \
DBUS_SESSION_BUS_ADDRESS="unix:path=$d/bus" \
gsettings reset org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type || true
fi
done
}

# -------------------------------------------------------------------
# KDE(best-effort)
# -------------------------------------------------------------------
__kde_control() {
local action="$1"
for home in /home/*; do
[[ -d "$home/.config" ]] || continue
local f="$home/.config/powermanagementprofilesrc"

if [[ "$action" == "true" ]]; then
[[ -f "$f" ]] && cp "$f" "$f.bak.sleep-toggle" || true
cat >>"$f" <<EOF

[AC]
idleTime=0

[Battery]
idleTime=0
EOF
fi
done
}

# -------------------------------------------------------------------
26 changes: 26 additions & 0 deletions src/usr/lib/rsetup/tui/system/system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,31 @@ You still need a valid bootloader on another location for the system to work.
Are you sure you want to erase the bootloader?"
}

__system_enable_sleep() {
if set_suspend_status "OFF"
then
msgbox "Sleep and hibernate settings have been restored. Reboot is recommended."
else
msgbox "Failed to restore sleep and hibernate settings." "$RTUI_PALETTE_ERROR"
fi
}

__system_disable_sleep() {
if set_suspend_status "ON"
then
msgbox "Sleep and hibernate have been disabled. Reboot is recommended."
else
msgbox "Failed to disable sleep and hibernate." "$RTUI_PALETTE_ERROR"
fi
}

__system_sleep_menu() {
menu_init
menu_add __system_enable_sleep "Enable Sleep / Hibernate"
Comment thread
RadxaYuntian marked this conversation as resolved.
menu_add __system_disable_sleep "Disable Sleep / Hibernate"
menu_show "Sleep / Hibernate Control"
}

__system_set_target(){
radiolist_init

Expand Down Expand Up @@ -227,5 +252,6 @@ __system() {
menu_add __system_set_target "Change default boot target"
menu_add_separator
menu_add __system_bootloader_menu "Bootloader Management"
menu_add __system_sleep_menu "Sleep / Hibernate Control"
menu_show "System"
}