From a4898309493a3d678a608afa4b6afec731ca42ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Mon, 17 Jun 2024 20:36:50 +0200 Subject: [PATCH] xautolock: Key is now a toggle --- hm/desktop/default.nix | 2 +- hm/desktop/frobar/default.nix | 59 ++++++++------------------- hm/desktop/frobar/frobar/__init__.py | 1 + hm/desktop/frobar/frobar/providers.py | 22 ++++++++++ hm/desktop/frobar/module.nix | 25 ++++++++++++ hm/desktop/lock/default.nix | 22 ++++++++-- 6 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 hm/desktop/frobar/module.nix diff --git a/hm/desktop/default.nix b/hm/desktop/default.nix index 2497c80..35d26b2 100644 --- a/hm/desktop/default.nix +++ b/hm/desktop/default.nix @@ -5,7 +5,7 @@ ./autorandr ./background ./browser - ./frobar + ./frobar/module.nix ./i3.nix ./lock ./mpd diff --git a/hm/desktop/frobar/default.nix b/hm/desktop/frobar/default.nix index 60326b5..a5df2bb 100644 --- a/hm/desktop/frobar/default.nix +++ b/hm/desktop/frobar/default.nix @@ -1,48 +1,21 @@ -{ pkgs ? import { config = { }; overlays = [ ]; }, lib, config, ... }: +{ pkgs ? import { config = { }; overlays = [ ]; }, ... }: # Tried using pyproject.nix but mpd2 dependency wouldn't resolve, # is called pyton-mpd2 on PyPi but mpd2 in nixpkgs. -let - frobar = pkgs.python3Packages.buildPythonApplication { - pname = "frobar"; - version = "2.0"; +pkgs.python3Packages.buildPythonApplication { + pname = "frobar"; + version = "2.0"; - runtimeInputs = with pkgs; [ lemonbar-xft wirelesstools ]; - propagatedBuildInputs = with pkgs.python3Packages; [ - coloredlogs - notmuch - i3ipc - mpd2 - psutil - pulsectl - pyinotify - ]; - makeWrapperArgs = [ "--prefix PATH : ${pkgs.lib.makeBinPath (with pkgs; [ lemonbar-xft wirelesstools ])}" ]; + runtimeInputs = with pkgs; [ lemonbar-xft wirelesstools ]; + propagatedBuildInputs = with pkgs.python3Packages; [ + coloredlogs + notmuch + i3ipc + mpd2 + psutil + pulsectl + pyinotify + ]; + makeWrapperArgs = [ "--prefix PATH : ${pkgs.lib.makeBinPath (with pkgs; [ lemonbar-xft wirelesstools ])}" ]; - src = ./.; - }; -in -{ - config = lib.mkIf config.frogeye.desktop.xorg { - xsession.windowManager.i3.config.bars = [ ]; - programs.autorandr.hooks.postswitch = { - frobar = "${pkgs.systemd}/bin/systemctl --user restart frobar"; - }; - systemd.user.services.frobar = { - Unit = { - Description = "frobar"; - After = [ "graphical-session-pre.target" ]; - PartOf = [ "graphical-session.target" ]; - }; - - Service = { - # Wait for i3 to start. Can't use ExecStartPre because otherwise it blocks graphical-session.target, and there's nothing i3/systemd - # TODO Do that better - ExecStart = ''${pkgs.bash}/bin/bash -c "while ! ${pkgs.i3}/bin/i3-msg; do ${pkgs.coreutils}/bin/sleep 1; done; ${frobar}/bin/frobar"''; - }; - - Install = { WantedBy = [ "graphical-session.target" ]; }; - }; - }; + src = ./.; } -# TODO Connection with i3 is lost on start sometimes, more often than with Arch? -# TODO Restore ability to build frobar with nix-build diff --git a/hm/desktop/frobar/frobar/__init__.py b/hm/desktop/frobar/frobar/__init__.py index 4f22cf1..e2848ba 100644 --- a/hm/desktop/frobar/frobar/__init__.py +++ b/hm/desktop/frobar/frobar/__init__.py @@ -49,6 +49,7 @@ def run(): NETWORK_THEME = 5 # TODO Disk space provider # TODO Screen (connected, autorandr configuration, bbswitch) provider + Bar.addSectionAll(XautolockProvider(theme=PERIPHERAL_THEME), BarGroupType.RIGHT) Bar.addSectionAll(PulseaudioProvider(theme=PERIPHERAL_THEME), BarGroupType.RIGHT) Bar.addSectionAll(RfkillProvider(theme=PERIPHERAL_THEME), BarGroupType.RIGHT) Bar.addSectionAll(NetworkProvider(theme=NETWORK_THEME), BarGroupType.RIGHT) diff --git a/hm/desktop/frobar/frobar/providers.py b/hm/desktop/frobar/frobar/providers.py index 2431c7d..691ecb6 100644 --- a/hm/desktop/frobar/frobar/providers.py +++ b/hm/desktop/frobar/frobar/providers.py @@ -4,6 +4,7 @@ import datetime import ipaddress import json import logging +import os import random import socket import subprocess @@ -224,6 +225,27 @@ class BatteryProvider(AlertingSection, PeriodicUpdater): self.changeInterval(5) +class XautolockProvider(Section, InotifyUpdater): + ICON = "" + + def fetcher(self) -> str | None: + with open(self.path) as fd: + state = fd.read().strip() + if state == "enabled": + return None + elif state == "disabled": + return "" + else: + return "?" + + def __init__(self, theme=None): + Section.__init__(self, theme=theme) + InotifyUpdater.__init__(self) + # TODO XDG + self.path = os.path.realpath(os.path.expanduser("~/.cache/xautolock")) + self.addPath(self.path) + + class PulseaudioProvider(StatefulSection, ThreadedUpdater): NUMBER_STATES = 3 DEFAULT_STATE = 1 diff --git a/hm/desktop/frobar/module.nix b/hm/desktop/frobar/module.nix new file mode 100644 index 0000000..a084a10 --- /dev/null +++ b/hm/desktop/frobar/module.nix @@ -0,0 +1,25 @@ +{ pkgs, lib, config, ... }: +{ + config = lib.mkIf config.frogeye.desktop.xorg { + xsession.windowManager.i3.config.bars = [ ]; + programs.autorandr.hooks.postswitch = { + frobar = "${pkgs.systemd}/bin/systemctl --user restart frobar"; + }; + systemd.user.services.frobar = { + Unit = { + Description = "frobar"; + After = [ "graphical-session-pre.target" ]; + PartOf = [ "graphical-session.target" ]; + }; + + Service = { + # Wait for i3 to start. Can't use ExecStartPre because otherwise it blocks graphical-session.target, and there's nothing i3/systemd + # TODO Do that better + ExecStart = ''${pkgs.bash}/bin/bash -c "while ! ${pkgs.i3}/bin/i3-msg; do ${pkgs.coreutils}/bin/sleep 1; done; ${pkgs.callPackage ./. {}}/bin/frobar"''; + }; + + Install = { WantedBy = [ "graphical-session.target" ]; }; + }; + }; +} +# TODO Connection with i3 is lost on start sometimes, more often than with Arch? diff --git a/hm/desktop/lock/default.nix b/hm/desktop/lock/default.nix index f6d7029..c006122 100644 --- a/hm/desktop/lock/default.nix +++ b/hm/desktop/lock/default.nix @@ -11,6 +11,7 @@ let ''; lockPng = pkgs.runCommand "lock.png" { } "${pkgs.imagemagick}/bin/convert ${lockSvg} $out"; mod = config.xsession.windowManager.i3.config.modifier; + xautolockState = "${config.xdg.cacheHome}/xautolock"; in { config = lib.mkIf config.frogeye.desktop.xorg { @@ -43,12 +44,27 @@ in keybindings = { # Screen off commands "${mod}+F1" = "--release exec --no-startup-id ${pkgs.xorg.xset}/bin/xset dpms force off"; - "${mod}+F4" = "exec --no-startup-id ${pkgs.xautolock}/bin/xautolock -disable"; - "${mod}+F5" = "exec --no-startup-id ${pkgs.xautolock}/bin/xautolock -enable"; + # Toggle to save on buttons + # xautolock -toggle doesn't allow to read state. + # Writing into a file also allows frobar to display a lock icon + "${mod}+F5" = "exec --no-startup-id ${pkgs.writeShellScript "xautolock-toggle" '' + state="$(cat "${xautolockState}")" + if [ "$state" = "disabled" ] + then + ${pkgs.xautolock}/bin/xautolock -enable + echo enabled > ${xautolockState} + else + ${pkgs.xautolock}/bin/xautolock -disable + echo disabled > ${xautolockState} + fi + ''}"; }; startup = [ # Stop screen after 10 minutes, 1 minutes after lock it - { notification = false; command = "${pkgs.xautolock}/bin/xautolock -time 10 -locker '${pkgs.xorg.xset}/bin/xset dpms force standby' -killtime 1 -killer xlock"; } + { notification = false; command = "${pkgs.writeShellScript "xautolock-start" '' + echo enabled > ${xautolockState} + ${pkgs.xautolock}/bin/xautolock -time 10 -locker '${pkgs.xorg.xset}/bin/xset dpms force standby' -killtime 1 -killer xlock + ''}"; } # services.screen-locker.xautolock is hardcoded to use systemd for -locker (doesn't even work...) ]; };