phone: add basic power management

This commit is contained in:
chayleaf 2023-12-24 16:28:34 +07:00
parent 95c0cc7134
commit 42f89647cb
Signed by: chayleaf
GPG key ID: 78171AD46227E68E
2 changed files with 62 additions and 16 deletions

View file

@ -34,6 +34,21 @@ audioPrev = pkgs.writeShellScript "playerctl-prev" ''
${pkgs.playerctl}/bin/playerctl play ${pkgs.playerctl}/bin/playerctl play
fi fi
''; '';
swaylock =
if config.phone.enable
then "${pkgs.schlock}/bin/schlock -fp /secrets/schlock.pin"
else "${pkgs.swaylock}/bin/swaylock -f";
swaylock-start = pkgs.writeShellScript "swaylock-start" ''
${pkgs.procps}/bin/pgrep -fx "${swaylock}" || ${swaylock}
'';
dpms-off = pkgs.writeShellScript "sway-dpms-off" ''
${config.wayland.windowManager.sway.package}/bin/swaymsg output "*" power off
${config.wayland.windowManager.sway.package}/bin/swaymsg input type:touch events disabled
'';
dpms-on = pkgs.writeShellScript "sway-dpms-on" ''
${config.wayland.windowManager.sway.package}/bin/swaymsg output "*" power on
${config.wayland.windowManager.sway.package}/bin/swaymsg input type:touch events enabled
'';
barConfig = { barConfig = {
mode = "overlay"; mode = "overlay";
hiddenState = "hide"; hiddenState = "hide";
@ -86,7 +101,7 @@ commonConfig = {
${pkgs.systemd}/bin/busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b true ${pkgs.systemd}/bin/busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b true
''} ''}
${pkgs.home-daemon}/bin/home-daemon system76-scheduler& ${pkgs.home-daemon}/bin/home-daemon system76-scheduler&
${pkgs.gnome.zenity}/bin/zenity --password | (${pkgs.keepassxc}/bin/keepassxc --pw-stdin ~/Nextcloud/keepass.kdbx&) ${pkgs.gnome.zenity}/bin/zenity --password | tee /dev/stdout | (${pkgs.keepassxc}/bin/keepassxc --pw-stdin ~/Nextcloud/keepass.kdbx ~/var/local.kdbx&)
# nextcloud and nheko need secret service access # nextcloud and nheko need secret service access
${pkgs.nextcloud-client}/bin/nextcloud --background& ${pkgs.nextcloud-client}/bin/nextcloud --background&
${pkgs.nheko}/bin/nheko& ${pkgs.nheko}/bin/nheko&
@ -247,8 +262,17 @@ in
} }
]; ];
terminal = config.terminalBin; terminal = config.terminalBin;
window = commonConfig.window // { commands = [ window = commonConfig.window // { commands = lib.optionals config.phone.enable [
{ command = "floating enable; move workspace current"; { command = "floating off; fullscreen off";
criteria = {
floating = true;
}; }
{ command = "fullscreen off";
criteria = {
tiling = true;
}; }
] ++ [
{ command = "floating on; move workspace current";
criteria = { criteria = {
app_id = "^org.keepassxc.KeePassXC$"; app_id = "^org.keepassxc.KeePassXC$";
title = "^KeePassXC - (?:Browser |)?(?:Access Request|)$"; title = "^KeePassXC - (?:Browser |)?(?:Access Request|)$";
@ -320,6 +344,13 @@ in
"--locked --inhibited XF86AudioPlay" = "exec ${pkgs.playerctl}/bin/playerctl play-pause"; "--locked --inhibited XF86AudioPlay" = "exec ${pkgs.playerctl}/bin/playerctl play-pause";
"--locked --inhibited XF86AudioNext" = "exec ${audioNext}"; "--locked --inhibited XF86AudioNext" = "exec ${audioNext}";
"--locked --inhibited XF86AudioPrev" = "exec ${audioPrev}"; "--locked --inhibited XF86AudioPrev" = "exec ${audioPrev}";
"--locked --inhibited --release XF86PowerOff" = lib.mkIf config.phone.enable "exec ${pkgs.writeShellScript "power-key" ''
if ${config.wayland.windowManager.sway.package}/bin/swaymsg -rt get_outputs | ${pkgs.jq}/bin/jq ".[].power" | ${pkgs.gnugrep}/bin/grep true; then
${dpms-off}
else
${dpms-on}
fi
''}";
}); });
startup = commonConfig.startup ++ [ startup = commonConfig.startup ++ [
{ {
@ -365,25 +396,26 @@ in
''; '';
}; };
services.swayidle = let services.swayidle = let
swaylock = lock = pkgs.writeShellScript "lock-start" ''
if config.phone.enable ${swaylock-start}
then "${pkgs.schlock}/bin/schlock -f /secrets/schlock.pin" ${lib.optionalString config.phone.enable
else "${pkgs.swaylock}/bin/swaylock -f"; # suspend if nothing is playing
swaylock-start = toString (pkgs.writeShellScript "swaylock-start" '' ''
${pkgs.procps}/bin/pgrep -fx "${swaylock}" || ${swaylock} ${pkgs.playerctl}/bin/playerctl -a status | ${pkgs.gnugrep}/bin/grep Playing >/dev/null || /run/current-system/sw/bin/systemctl suspend
''); ''}
'';
in { in {
enable = config.wayland.windowManager.sway.enable; enable = config.wayland.windowManager.sway.enable;
events = [ events = [
{ event = "before-sleep"; command = swaylock-start; } { event = "before-sleep"; command = toString swaylock-start; }
# after-resume, lock, unlock # after-resume, lock, unlock
]; ];
timeouts = [ timeouts = [
{ timeout = 300; { timeout = if config.phone.enable then 30 else 300;
command = "${config.wayland.windowManager.sway.package}/bin/swaymsg \"output * power off\""; command = toString dpms-off;
resumeCommand = "${config.wayland.windowManager.sway.package}/bin/swaymsg \"output * power on\""; } resumeCommand = toString dpms-on; }
{ timeout = 600; { timeout = if config.phone.enable then 60 else 600;
command = swaylock-start; } command = toString lock; }
]; ];
}; };
programs.swaylock.settings = rec { programs.swaylock.settings = rec {

View file

@ -27,6 +27,20 @@
"video" "video"
] ++ lib.optional (config.networking.modemmanager.enable || config.networking.networkmanager.enable) "networkmanager"; ] ++ lib.optional (config.networking.modemmanager.enable || config.networking.networkmanager.enable) "networkmanager";
security.polkit.extraConfig = ''
polkit.addRule(function(action, subject) {
if ((action.id.indexOf("org.freedesktop.login1.suspend" == 0)
|| action.id.indexOf("org.freedesktop.login1.reboot" == 0)
|| action.id.indexOf("org.freedesktop.login1.power-off" == 0)
|| action.id.indexOf("org.freedesktop.inhibit") == 0)
&& subject.user == "${config.common.mainUsername}")
{
return polkit.Result.YES;
}
});
'';
common.minimal = false; common.minimal = false;
services.sshd.enable = true; services.sshd.enable = true;
services.tlp.enable = true; services.tlp.enable = true;