Compare commits

..

10 Commits

Author SHA1 Message Date
jory 7603975062 refactor: changed effects, add shell completions
Nightly Release / nightly-build (push) Has been cancelled
Code Quality / quality-checks (push) Has been cancelled
Security Scan / security-audit (push) Has been cancelled
- Extract all apply_* effect methods from screenshot.rs into dedicated
  src/effects.rs (blur, vignette, pixelate, swirl) — screenshot.rs
- Rewrite apply_blur: replace broken fastblur::gaussian_blur (horizontal-
  only box blur, backbuf dropped) with correct inline two-pass sliding-
  window box blur operating directly on Cairo surface bytes — ~10×
  faster, no ImageBuffer round-trip, no fastblur dependency
- Remove melting effect entirely (seems to be hard to implement
  properely, tried multiple itterations)
- Drop unused dependencies: fastblur, rand
- Add clap_complete dependency + --completions {bash,fish,zsh} flag,
  with CompletionShell enum in config.rs
- Fix show_* flags defaulting to true: remove default_value_t=true from
  show_media, show_battery, show_network, show_bluetooth, show_album_art,
  show_caps_lock_text — all now opt-in (matching user expectation)
- Convert peek toggle to hold: toggle_peek() → set_peek_held(held: bool),
  Press calls set_peek_held(true), new Release handler iterates all
  surfaces and calls set_peek_held(false), matching Ctrl-hold behavior
- Fix cursor rendering: increase font size 11→14 for peeked chars,
  handle cursor_position == 0 (prevent negative t wrapping to far ring
  end)
- Fix peek hit-test: use shape-aware point_in_shape() instead of simple
  circle pill/square/diamond/hexagon now detect clicks correctly within
  their area
- Remove per-frame DEBUG logs: PEEK/DOT mode in lock.rs,
  set_password_display/peek_password in render/mod.rs
- Update README: embed rustlock-effects.webp demo, remove melting
  references, correct show_* defaults and options table
2026-06-28 18:17:53 +02:00
jory 833706f5a5 feat: peek-to-reveal on click/ctrl, pre-commit hooks, devShell, fmt fixes
Nightly Release / nightly-build (push) Has been cancelled
Code Quality / quality-checks (push) Has been cancelled
Security Scan / security-audit (push) Has been cancelled
- Click indicator ring to toggle password peek (persistent click-to-toggle)
- Ctrl-hold transient peek refactored to share peek path
- Cursor always visible (previously hidden at position 0, now shows at top of ring)
- .pre-commit-config.yaml: standard hooks + cargo fmt/clippy
- flake.nix: devShell with cargo, rustc, rustfmt, clippy, cargo-audit, prek
- cargo fmt pass across all source files (trailing whitespace, line wrapping)
- MPRIS error logging (silent failures now logged)
- Dependabot versioning-strategy: "increase" → "auto"
2026-06-21 20:44:59 +02:00
jory c2c093595a feat: ring shapes, auth reuse, input tests, README docs, CI improvements
Nightly Release / nightly-build (push) Has been cancelled
Code Quality / quality-checks (push) Has been cancelled
Security Scan / security-audit (push) Has been cancelled
- Add --ring-shape (circle/square/diamond/hexagon/pill) with geometry module
- Reuse PAM context across auth attempts (no reload per-keystroke)
- Proper error propagation in screenshot effects (unwrap → Result + context)
- Remove theme presets (modern/pixel/glass) and --theme flag
- Add input cooldown after failed attempt (400ms debounce)
- Add Ctrl+held peek password, Home/End/Delete cursor keys
- Add all input unit tests (23 new tests)
- Add --max-dots, feedback duration, timeout, interval config options
- Add --log-path, --auth-timeout, verifying_color config fields
- Remove unused deps: futures, gio, env_logger, num-traits, thiserror, bytemuck
- Fix media bar: remove stop button, fix art+text overlap, uniform hit areas
- Fix CI: source-only releases, nightly prereleases, dependabot groups, labeler paths
- Update issue templates with structured forms
- Add stale workflow for inactive issues/PRs
- Update README: full options table (28 flags), remove theme docs, add shapes
2026-06-21 17:28:58 +02:00
jory db3e797e36 Merge pull request #7 from josephdunn/perf/lock-idle-redraw-dirty-tracking
perf: only redraw the lock screen when something changes
2026-06-20 14:54:35 +02:00
jory c4ae3e9651 Merge pull request #6 from josephdunn/fix/lock-surface-cleanup-order
fix: clean lock-surface teardown + roundtrip on unlock (Hyprland multi-output)
2026-06-20 14:54:22 +02:00
jory 4372c7f7c0 ci: audit and harden GitHub Actions workflows
Code Quality / quality-checks (push) Has been cancelled
Security Scan / security-audit (push) Has been cancelled
- Bump all actions to latest versions (checkout@v6, upload-artifact@v7,
  download-artifact@v8, labeler@v6, gh-release@v3, gpg-import@v7)
- Replace fragile curl|bash tool install with taiki-e/install-action@v2
- Fix Swatinem/rust-cache ordering: must run AFTER toolchain install
  for correct cache key derivation
- Add --all-targets to clippy (CI now matches local testing)
- Add RUSTDOCFLAGS=-D warnings to doc step (fail on broken links)
- Fix ARM64 build order: install Rust toolchain before adding target
  and configuring linker
- Add rust-toolchain.toml pinning Rust 1.94.0 for deterministic builds
- Update Cargo.lock: rustls-webpki v0.103.11->v0.103.13 (fixes 3 CVEs)
- Fix softprops/action-gh-release@v3: include checksums in files list
  (v3 removed the checksum input parameter)
- Fix clippy manual_checked_ops lint in screenshot.rs
- Clean up trailing whitespace and missing newlines in YAML files
2026-06-20 12:57:48 +02:00
Joseph Dunn e9550f71f5 perf: only redraw the lock screen when something changes
The lock screen re-rendered and re-committed every output's full
screen-sized cairo surface on every 16ms timer tick regardless of
whether anything had changed, burning 60%+ of a core the entire time
the session was locked.

Track a dirty flag per surface and render only when state actually
changes: a keystroke, a system-status change, the clock minute rolling
over, or an in-flight animation. update() now reports whether it
redrew, so the timer commits only the surfaces that changed.

Also fix the fade-in never formally completing. The eased alpha
approaches 1.0 asymptotically while the 0.001 step throttle suppresses
the final sub-threshold increments, leaving fade_alpha stuck just under
1.0. Since "fade_alpha < 1.0" is the "still animating" signal, that kept
it permanently true and forced a full render every frame. Snap alpha to
exactly 1.0 once the fade duration elapses so the animation completes.

Idle CPU while locked drops from 60%+ to ~2%.
2026-06-04 11:02:08 -05:00
Joseph Dunn ff20423a84 fix: clean up lock surfaces and drain compositor events on unlock
Hyprland was treating multi-output rustlock unlocks as client
crashes and showing its "lockscreen died" failsafe instead of
unlocking. Two issues:

1. Order. ext-session-lock-v1 recommends destroying every
   ext_session_lock_surface_v1 before issuing unlock_and_destroy
   on ext_session_lock_v1. rustlock did the opposite. Move
   lock_surfaces.clear() into handle_auth_result so it runs
   before session_lock.unlock(), and drop the now-redundant
   clear from the auth-feedback callback.

2. Drain. After unlock_and_destroy the compositor sends
   keyboard/pointer leave events and delete_id acks; if the
   client disconnects before processing them, Hyprland treats
   the disconnect as unclean. Add a conn.roundtrip() after the
   main loop to drain those events before exit.

Single-output setups tolerate both of these (which is why the
bug does not show up on the upstream author's laptop). The
failsafe only reproduces with multiple outputs.
2026-05-22 16:43:31 -05:00
jory 6c2e3fca5a Renamed pam module to rustlock
Code Quality / quality-checks (push) Has been cancelled
Security Scan / security-audit (push) Has been cancelled
2026-04-12 11:54:41 +02:00
jory c6ec65c851 chore: add GitHub automation and fix rand vulnerability
Code Quality / quality-checks (push) Has been cancelled
Security Scan / security-audit (push) Has been cancelled
GitHub Actions:
- Add Dependabot for weekly dependency updates (cargo, github-actions)
- Add PR labeler workflow with file-based auto-labeling
- Add issue/PR templates (bug report, feature request, PR template)
- Fix release.yml artifact paths and labeler.yml config syntax
Security:
- Upgrade rand 0.8 → 0.10 to fix RUSTSEC-2026-0097 unsoundness
- Update screenshot.rs for rand 0.10 API (thread_rng → rng, gen_range → random_range)
2026-04-11 18:18:41 +02:00
43 changed files with 2804 additions and 1541 deletions
+55
View File
@@ -0,0 +1,55 @@
---
name: Bug Report
about: Report a crash, visual glitch, or unexpected behavior
labels: bug
---
## Description
A clear and concise description of the bug.
## Steps to Reproduce
1.
2.
3.
## Expected Behavior
What did you expect to happen?
## Actual Behavior
What actually happened? Include any error messages, visual glitches, or crashes.
## Screenshots / Screen Recordings
If applicable, add screenshots or recordings to help explain the problem.
## Environment
- **Compositor:** (e.g., Hyprland, Sway, River, Niri)
- **Compositor version:** (e.g., Hyprland 0.47.0)
- **rustlock version:** (`rustlock --version`)
- **Display configuration:** (single monitor, multi-monitor, mixed DPI, etc.)
- **OS/Distro:** (e.g., Arch Linux, Ubuntu 24.04, Fedora 41)
## Config / CLI flags
```sh
# The exact command you used to start rustlock
# e.g., rustlock --debug --effect-blur 5x2 --theme modern
```
## Logs
```
# Paste any relevant logs here
# Run with --debug --log-file to capture verbose logs
```
## Confirmations
- [ ] I searched existing issues and this is not a duplicate
- [ ] I am running the latest version of rustlock
- [ ] I have included relevant logs and environment details
+16
View File
@@ -0,0 +1,16 @@
---
name: Feature Request
about: Suggest an idea for rustlock
labels: enhancement
---
**Is your feature request related to a problem? Please describe.**
**Describe the solution you'd like**
**Describe alternatives you've considered**
**Additional context**
+23
View File
@@ -0,0 +1,23 @@
# Pull Request Template
## Description
Please include a summary of the change and which issue is fixed (if relevant).
## Type of Change
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation update
- [ ] Refactoring
## How Has This Been Tested?
Please describe the tests that you ran to verify your changes.
## Checklist
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
+1 -1
View File
@@ -4,7 +4,7 @@ description: Install system dependencies required for building rustlock
runs:
using: composite
steps:
- name: Install system dependencies (Ubuntu/Debian)
- name: Install system dependencies for Debian based distros
shell: bash
run: |
sudo apt-get update
+44
View File
@@ -0,0 +1,44 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "09:00"
timezone: "UTC"
open-pull-requests-limit: 10
labels:
- "dependencies"
- "rust"
commit-message:
prefix: "deps"
reviewers:
- "JorySeverijnse"
versioning-strategy: "auto"
groups:
rust-dependencies:
patterns:
- "*"
update-types:
- "minor"
- "patch"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "09:00"
timezone: "UTC"
labels:
- "dependencies"
- "github-actions"
groups:
actions-dependencies:
patterns:
- "*"
update-types:
- "minor"
- "patch"
+23
View File
@@ -0,0 +1,23 @@
documentation:
- changed-files:
- any-glob-to-any-file: ['README.md', '*.md', 'docs/**']
features:
- changed-files:
- any-glob-to-any-file: ['src/input.rs', 'src/lock.rs', 'src/config.rs', 'src/auth.rs', 'src/screenshot.rs', 'src/system.rs']
rendering:
- changed-files:
- any-glob-to-any-file: ['src/render/**']
ci:
- changed-files:
- any-glob-to-any-file: ['.github/**', '.github/workflows/**']
dependencies:
- changed-files:
- any-glob-to-any-file: ['Cargo.toml', 'Cargo.lock']
refactoring:
- changed-files:
- any-glob-to-any-file: ['src/util.rs', 'src/main.rs']
@@ -0,0 +1,38 @@
name: Dependabot Automerge
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: write
pull-requests: write
jobs:
automerge:
if: github.actor == 'dependabot[bot]'
runs-on: ubuntu-latest
steps:
- name: Fetch Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v2
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Auto-approve minor/patch updates
if: |
steps.metadata.outputs.update-type == 'version-update:semver-minor' ||
steps.metadata.outputs.update-type == 'version-update:semver-patch'
run: gh pr review --approve "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Enable auto-merge for minor/patch updates
if: |
steps.metadata.outputs.update-type == 'version-update:semver-minor' ||
steps.metadata.outputs.update-type == 'version-update:semver-patch'
run: gh pr merge --auto --squash "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+15
View File
@@ -0,0 +1,15 @@
name: PR Labeler
on:
pull_request:
types: [opened, synchronize]
jobs:
label:
runs-on: ubuntu-latest
steps:
- name: Label PR based on changed files
uses: actions/labeler@v6
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
configuration-path: .github/labeler.yml
+50
View File
@@ -0,0 +1,50 @@
name: Nightly Release
on:
push:
branches: [main, master]
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
jobs:
nightly-build:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: ./.github/actions/deps
- uses: Swatinem/rust-cache@v2
- uses: dtolnay/rust-toolchain@stable
- name: Set version
id: version
run: |
DATE=$(date -u +%Y%m%d%H%M)
SHA=$(git rev-parse --short HEAD)
VERSION=$(grep -m1 '^version =' Cargo.toml | cut -d'"' -f2)
echo "VERSION=${VERSION}-nightly.$DATE.$SHA" >> $GITHUB_OUTPUT
- name: Build release binary
run: cargo build --release
- name: Upload binary
uses: actions/upload-artifact@v4
with:
name: rustlock-${{ steps.version.outputs.VERSION }}
path: target/release/rustlock
- name: Create nightly release
uses: softprops/action-gh-release@v3
with:
tag_name: ${{ steps.version.outputs.VERSION }}
name: Nightly Build ${{ steps.version.outputs.VERSION }}
prerelease: true
generate_release_notes: true
+15 -12
View File
@@ -14,24 +14,27 @@ jobs:
quality-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: actions/checkout@v7
- uses: ./.github/actions/deps
- name: Install Rust with tools
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- name: Check formatting
run: cargo fmt -- --check
- name: Run clippy
run: cargo clippy -- -D warnings
- name: Run unit tests
run: cargo test --quiet
run: cargo clippy --all-targets -- -D warnings
- name: Run unit tests (all features)
run: cargo test --all-features --quiet
- name: Check documentation
run: cargo doc --no-deps --document-private-items
env:
RUSTDOCFLAGS: -D warnings
run: cargo doc --no-deps --document-private-items
+25 -89
View File
@@ -5,109 +5,45 @@ on:
tags:
- 'v*'
env:
CARGO_TERM_COLOR: always
jobs:
release-build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- variant: "default"
args: ""
distro: "ubuntu"
- variant: "no-networking"
args: "--no-default-features"
distro: "ubuntu"
- variant: "default"
args: ""
distro: "debian"
- variant: "default"
args: ""
distro: "fedora"
- variant: "default"
args: ""
distro: "arch"
container: ${{ matrix.distro == 'ubuntu' && 'ubuntu:latest' || matrix.distro == 'debian' && 'debian:stable-slim' || matrix.distro == 'fedora' && 'fedora:latest' || 'archlinux:latest' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install system dependencies (Ubuntu/Debian)
if: matrix.distro == 'ubuntu' || matrix.distro == 'debian'
run: |
apt-get update
apt-get install -y \
curl \
llvm clang libclang-dev \
pkg-config \
libglib2.0-dev libcairo2-dev libpango1.0-dev libatk1.0-dev \
libgdk-pixbuf-2.0-dev libpam0g-dev libdbus-1-dev \
libwayland-dev libxkbcommon-dev
- name: Install system dependencies (Fedora)
if: matrix.distro == 'fedora'
run: |
dnf install -y \
curl \
llvm clang clang-devel \
pkgconfig \
glib2-devel cairo-devel cairo-gobject-devel pango-devel atk-devel \
gdk-pixbuf2-devel pam-devel dbus-devel \
wayland-devel libxkbcommon-devel
- name: Install system dependencies (Arch)
if: matrix.distro == 'arch'
run: |
pacman -Sy --noconfirm \
base-devel \
llvm clang pkgconf \
glib2 cairo pango atk gdk-pixbuf2 \
pam dbus \
wayland libxkbcommon
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Build ${{ matrix.variant }} (${{ matrix.distro }})
run: cargo build --release ${{ matrix.args }}
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: rustlock-${{ matrix.variant }}-${{ matrix.distro }}
path: target/release/rustlock
release:
runs-on: ubuntu-latest
needs: [release-build]
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
- uses: actions/checkout@v7
- name: Determine tag
id: tag
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
- name: Create Release
uses: softprops/action-gh-release@v2
uses: softprops/action-gh-release@v3
with:
tag_name: ${{ github.ref_name }}
name: RustLock v${{ steps.tag.outputs.VERSION }}
draft: true
prerelease: ${{ contains(github.ref_name, 'alpha') || contains(github.ref_name, 'beta') }}
files: |
artifacts/rustlock-default-ubuntu/rustlock/rustlock:rustlock-default-ubuntu
artifacts/rustlock-no-networking-ubuntu/rustlock/rustlock:rustlock-no-networking-ubuntu
artifacts/rustlock-default-debian/rustlock/rustlock:rustlock-default-debian
artifacts/rustlock-default-fedora/rustlock/rustlock:rustlock-default-fedora
artifacts/rustlock-default-arch/rustlock/rustlock:rustlock-default-arch
generate_release_notes: true
publish-crates:
runs-on: ubuntu-latest
needs: [release]
steps:
- uses: actions/checkout@v7
- uses: ./.github/actions/deps
- uses: Swatinem/rust-cache@v2
- uses: dtolnay/rust-toolchain@stable
- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
if [ -z "$CARGO_REGISTRY_TOKEN" ]; then
echo "CARGO_REGISTRY_TOKEN not set — skipping publish"
exit 0
fi
cargo publish
+19 -30
View File
@@ -11,6 +11,14 @@ on:
- 'Cargo.lock'
- 'deny.toml'
- '.github/workflows/security.yml'
pull_request:
branches: [main, master]
paths:
- 'Cargo.toml'
- 'Cargo.lock'
- 'deny.toml'
- '.github/workflows/security.yml'
types: [opened, synchronize, reopened]
env:
CARGO_TERM_COLOR: always
@@ -19,42 +27,23 @@ jobs:
security-audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: actions/checkout@v7
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Install security tools
run: |
curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
cargo binstall --no-confirm cargo-audit cargo-deny cargo-cyclonedx cargo-outdated || \
cargo install cargo-audit cargo-deny cargo-cyclonedx cargo-outdated
uses: taiki-e/install-action@v2
with:
tool: cargo-audit,cargo-deny,cargo-outdated
- name: Run cargo audit
run: cargo audit
- name: Run cargo deny
run: cargo deny check
- name: Generate Software Bill of Materials (SBOM)
run: cargo cyclonedx --format json --override-filename bom
- name: Upload SBOM
uses: actions/upload-artifact@v4
with:
name: sbom
path: bom.json
- name: Check for outdated dependencies
run: cargo outdated --exit-code 1 || echo "Some dependencies are outdated"
- name: Security summary
run: |
echo "=== Security Scan Complete ==="
echo "✅ cargo audit - Vulnerability scanning"
echo "✅ cargo deny - Advisory and license checking"
echo "✅ SBOM generated - Software Bill of Materials"
echo "✅ Outdated dependencies checked"
echo ""
echo "Next scheduled scan: Weekly (Sunday 00:00 UTC)"
+26
View File
@@ -0,0 +1,26 @@
name: Stale Issues and PRs
on:
schedule:
- cron: '0 0 * * *' # Daily
workflow_dispatch:
jobs:
stale:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/stale@v9
with:
stale-issue-message: 'This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within 7 days.'
stale-pr-message: 'This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within 7 days.'
close-issue-message: 'This issue has been automatically closed due to inactivity.'
close-pr-message: 'This PR has been automatically closed due to inactivity.'
days-before-stale: 30
days-before-close: 7
stale-issue-label: 'stale'
stale-pr-label: 'stale'
exempt-issue-labels: 'enhancement,security'
exempt-pr-labels: 'security,work-in-progress'
+28
View File
@@ -0,0 +1,28 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
exclude: \.md$
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files
exclude: ^assets/
- id: check-merge-conflict
- repo: local
hooks:
- id: fmt
name: cargo fmt
entry: cargo fmt
args: ["--", "--check"]
language: system
types: [rust]
pass_filenames: false
- id: clippy
name: cargo clippy
entry: cargo clippy
args: ["--", "-D", "warnings"]
language: system
pass_filenames: false
Generated
+423 -670
View File
File diff suppressed because it is too large Load Diff
+4 -9
View File
@@ -3,6 +3,8 @@ name = "rustlock"
version = "0.1.0"
edition = "2021"
license = "GPL-3.0-or-later"
authors = ["Jory Severijnse"]
description = "A high-performance Wayland screen locker"
[dependencies]
smithay-client-toolkit = { version = "0.19", default-features = false, features = ["calloop", "xkbcommon"] }
@@ -10,30 +12,23 @@ wayland-client = { version = "0.31" }
wayland-protocols = { version = "0.32", features = ["client"] }
wayland-protocols-wlr = { version = "0.3", features = ["client"] }
anyhow = "1.0"
futures = { version = "0.3", default-features = false, features = ["std"] }
cairo-rs = { version = "0.20", default-features = false, features = ["png"] }
gdk-pixbuf = { version = "0.20", default-features = false, features = ["v2_40"] }
gio = { version = "0.20" }
pangocairo = { version = "0.20" }
clap = { version = "4.6", default-features = false, features = ["derive", "std", "help", "usage", "error-context"] }
clap_complete = { version = "4.6", default-features = false }
toml = { version = "1.1", default-features = false, features = ["parse", "display", "serde"] }
serde = { version = "1.0", default-features = false, features = ["derive", "std"] }
zeroize = "1.8"
log = "0.4"
env_logger = { version = "0.11", default-features = false, features = ["color", "humantime"] }
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
whoami = "1.6"
zbus = { version = "5.14", default-features = false, features = ["tokio"] }
mpris = "2.0"
tokio = { version = "1.51", default-features = false, features = ["rt", "rt-multi-thread", "macros", "time", "sync"] }
num-traits = { version = "0.2" }
rand = { version = "0.8" }
reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls"], optional = true }
image = { version = "0.25", default-features = false, features = ["png", "jpeg"] }
fastblur = "0.1"
pam-client = "0.5"
thiserror = "1.0"
bytemuck = "1.25"
pam-client = "0.5"
calloop = "0.13"
xkbcommon = "0.7"
calloop-wayland-source = "0.3"
+21 -21
View File
@@ -1,4 +1,4 @@
GNU GENERAL PUBLIC LICENSE
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
@@ -46,7 +46,7 @@ To “convey” a work means any kind of propagation that enables other parties
An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
1. Source Code.
1. Source Code.
The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.
A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
@@ -59,24 +59,24 @@ The Corresponding Source need not include anything that users can regenerate aut
The Corresponding Source for a work in source code form is that same work.
2. Basic Permissions.
2. Basic Permissions.
All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.
4. Conveying Verbatim Copies.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
@@ -89,7 +89,7 @@ d) If the work has interactive user interfaces, each must display Appropriate Le
A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
6. Conveying Non-Source Forms.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.
@@ -114,7 +114,7 @@ The requirement to provide Installation Information does not include a requireme
Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
7. Additional Terms.
7. Additional Terms.
“Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
@@ -139,7 +139,7 @@ If you add terms to a covered work in accord with this section, you must place,
Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
8. Termination.
8. Termination.
You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
@@ -148,17 +148,17 @@ Moreover, your license from a particular copyright holder is reinstated permanen
Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
9. Acceptance Not Required for Having Copies.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
11. Patents.
11. Patents.
A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”.
A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
@@ -175,13 +175,13 @@ A patent license is “discriminatory” if it does not include within the scope
Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such.
14. Revised Versions of this License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation.
@@ -190,13 +190,13 @@ If the Program specifies that a proxy can decide which future versions of the GN
Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
15. Disclaimer of Warranty.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
@@ -207,7 +207,7 @@ If you develop a new program, and you want it to be of the greatest possible use
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
@@ -220,8 +220,8 @@ Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an “about box”.
+77 -45
View File
@@ -1,10 +1,14 @@
# 🔒 RustLock
[![License](https://img.shields.io/badge/license-AGPL--3.0%2B-blue.svg)](https://github.com/yourusername/rustlock/blob/main/LICENSE)
[![License](https://img.shields.io/badge/license-GPL-3.0-blue.svg)](https://github.com/yourusername/rustlock/blob/main/LICENSE)
[![Version](https://img.shields.io/badge/version-0.1.0-green.svg)](https://github.com/yourusername/rustlock/releases)
A high-performance Wayland screen locker written in Rust, inspired by `swaylock-effects`.
<p align="center">
<img src="assets/rustlock-effects.webp" alt="RustLock effects demo" width="800">
</p>
---
## ✨ Features
@@ -13,7 +17,7 @@ A high-performance Wayland screen locker written in Rust, inspired by `swaylock-
- 🎨 **Visual Effects**:
- Gaussian blur (configurable radius and passes)
- Vignette effect (configurable base and factor)
- Pixelate, Swirl, and Melting effects
- Pixelate and Swirl effects
- Smooth fade-in animation
- 🔐 **Password Indicator**:
- Circular ring with configurable radius and thickness
@@ -42,8 +46,8 @@ A high-performance Wayland screen locker written in Rust, inspired by `swaylock-
- PAM-based authentication
- Configurable grace period (any key press within N seconds unlocks without password)
- 🎯 **Customization**:
- Multiple ring shapes: circle, square, diamond, hexagon, pill
- Custom icons for WiFi, Bluetooth, Battery
- Theme presets (modern, pixel, glass)
- Configuration via config file or CLI
---
@@ -65,6 +69,7 @@ rustlock \
--indicator \
--indicator-radius 100 \
--indicator-thickness 7 \
--ring-shape hexagon \
--effect-blur 7x5 \
--effect-vignette 0.5:0.5 \
--ring-color 785412 \
@@ -72,6 +77,8 @@ rustlock \
--line-color 00000000 \
--inside-color 00000088 \
--separator-color 00000000 \
--show-network \
--show-battery \
--grace 2 \
--fade-in 0.2
```
@@ -96,51 +103,76 @@ Use arrow keys to move the cursor while entering your password:
## ⚙️ Configuration
Options can be provided via command line or a configuration file at `~/.config/rustlock/config.toml`. CLI arguments take precedence over config file, which takes precedence over theme defaults.
Options can be provided via command line or a configuration file at `~/.config/rustlock/config.toml`. CLI arguments take precedence over config file values.
### Options
| Option | Description |
|--------|-------------|
| **General** | |
| `--screenshots` | Capture desktop background before locking |
| `--image <PATH>` | Use custom background image instead of screenshot |
| `--clock` | Display centered clock and date |
| `--indicator` | Show password entry ring (default: true) |
| `--indicator-radius <N>` | Ring radius in pixels (default: 100) |
| `--indicator-thickness <N>` | Ring thickness in pixels (default: 7) |
| **Effects** | |
| `--effect-blur <R>x<P>` | Gaussian blur: radius x passes (e.g., `7x5`) |
| `--effect-pixelate` | Pixelate effect |
| `--effect-swirl` | Swirl distortion effect |
| `--effect-melting` | Melting distortion effect |
| `--effect-vignette <B>:<F>` | Vignette: base:factor (e.g., `0.5:0.5`) |
| **Colors** | |
| `--ring-color <RRGGBB[AA]>` | Outer ring color (hex, optional alpha) |
| `--key-hl-color <RRGGBB[AA]>` | Key highlight segment color |
| `--line-color <RRGGBB[AA]>` | Separator line color |
| `--inside-color <RRGGBB[AA]>` | Inner circle color |
| `--separator-color <RRGGBB[AA]>` | Ring separator color |
| **Display Options** | |
| `--show-media` | Show MPRIS media info (default: true) |
| `--show-battery` | Show battery status (default: true) |
| `--show-network` | Show WiFi status (default: true) |
| `--show-bluetooth` | Show Bluetooth status (default: true) |
| `--show-keyboard-layout` | Show keyboard layout indicator (default: true) |
| `--show-album-art` | Show album art (default: true) |
| `--hide-password` | Hide password dots (default: false, dots are shown) |
| **Custom Icons** | |
| `--wifi-icon <PATH>` | Custom WiFi icon (PNG/SVG) |
| `--bluetooth-icon <PATH>` | Custom Bluetooth icon (PNG/SVG) |
| `--battery-icon <PATH>` | Custom battery icon (PNG/SVG) |
| **Other** | |
| `--grace <SECONDS>` | Grace period in seconds (default: 2) |
| `--fade-in <SECONDS>` | Fade-in animation duration (default: 0.2) |
| `--pam-service <NAME>` | PAM service name (default: "rustlock") |
| `--config <PATH>` | Path to config file |
| `--theme <NAME>` | Theme preset: modern, pixel, glass |
| `--debug` | Enable debug logging |
| `--log-file` | Write logs to `~/.rustlock.log` |
| Option | Default | Description |
|--------|---------|-------------|
| **General** | | |
| `--screenshots` | — | Capture desktop background before locking |
| `--image <PATH>` | — | Use custom background image instead of screenshot |
| `--clock` | — | Display centered clock and date |
| `--indicator` | `true` | Show password entry ring |
| `--hide-password` | `false` | Hide password dots (dots are shown by default) |
| `--config <PATH>` | — | Path to config file |
| `--debug` | — | Enable debug logging |
| **Ring** | | |
| `--indicator-radius <N>` | `100` | Ring radius in pixels |
| `--indicator-thickness <N>` | `7` | Ring thickness in pixels |
| `--ring-shape <SHAPE>` | `circle` | Ring shape: `circle`, `square`, `diamond`, `hexagon`, `pill` |
| `--max-dots <N>` | `24` | Maximum password dots in the ring |
| **Effects** | | |
| `--effect-blur <R>x<P>` | — | Gaussian blur: radius x passes (e.g., `7x5`) |
| `--effect-vignette <B>:<F>` | — | Vignette: base : factor (e.g., `0.5:0.5`) |
| `--effect-pixelate <S>` | — | Pixelate effect with block size in pixels |
| `--effect-swirl <A>` | — | Swirl distortion with angle |
| **Colors** (hex `RRGGBB[AA]`) | | |
| `--ring-color <HEX>` | `#785412` | Outer ring color |
| `--line-color <HEX>` | `#00000000` | Separator line color |
| `--inside-color <HEX>` | `#00000088` | Inner circle fill color |
| `--separator-color <HEX>` | `#00000000` | Ring segment separator color |
| `--key-hl-color <HEX>` | `#4EAC41` | Key highlight segment color |
| `--caps-lock-key-hl-color <HEX>` | `#4EAC41` | Key highlight color when caps lock is on |
| `--caps-lock-bs-hl-color <HEX>` | `#DB3300` | Backspace highlight color in caps lock |
| `--caps-lock-color <HEX>` | `#E5A445` | Caps lock indicator ring color |
| `--caps-lock-text-color <HEX>` | `#E5A445` | Caps lock text color |
| `--verifying-color <HEX>` | `#0072FF` | Verifying feedback ring color |
| `--show-caps-lock-text` | `false` | Show "CAPS" text when caps lock is active |
| **Display** | | |
| `--show-media` | `false` | Show MPRIS media player information |
| `--show-battery` | `false` | Show battery status |
| `--show-network` | `false` | Show WiFi SSID and signal strength |
| `--show-bluetooth` | `false` | Show Bluetooth status |
| `--show-album-art` | `false` | Show album art for media |
| `--show-keyboard-layout` | `false` | Show keyboard layout indicator |
| **Feedback & Timing** | | |
| `--fade-in <SECONDS>` | `0.2` | Fade-in animation duration |
| `--grace <SECONDS>` | `0` | Grace period — any key press unlocks within N seconds |
| `--auth-timeout <MS>` | `10000` | PAM authentication timeout in milliseconds |
| `--wrong-password-duration <MS>` | `500` | Wrong password feedback animation duration |
| `--key-highlight-duration <MS>` | `300` | Key highlight feedback duration |
| `--cleared-feedback-duration <MS>` | `500` | Cleared password feedback duration |
| `--verifying-timeout <MS>` | `5000` | Verifying feedback fallback timeout |
| `--feedback-window-duration <MS>` | `1000` | Wrong password feedback input window |
| `--key-highlight-window-duration <MS>` | `200` | Key highlight input-side window |
| **System** | | |
| `--pam-service <NAME>` | `rustlock` | PAM service name |
| `--system-poll-interval <S>` | `2` | Polling interval for system status updates |
| `--dbus-reconnect-delay <S>` | `5` | Delay before reconnecting DBus on failure |
| `--command-timeout <S>` | `5` | Timeout for system commands |
| **Custom Icons** (PNG/SVG path) | | |
| `--wifi-icon <PATH>` | — | Custom WiFi icon |
| `--bluetooth-icon <PATH>` | — | Custom Bluetooth icon |
| `--battery-icon <PATH>` | — | Custom battery icon |
| `--media-prev-icon <PATH>` | — | Custom previous track icon |
| `--media-stop-icon <PATH>` | — | Custom stop icon |
| `--media-play-icon <PATH>` | — | Custom play icon |
| `--media-pause-icon <PATH>` | — | Custom pause icon |
| `--media-next-icon <PATH>` | — | Custom next track icon |
| **Logging** | | |
| `--log-file` | — | Write verbose logs to `~/.rustlock.log` |
| `--log-path <PATH>` | — | Path for log file (enables file logging, overrides `--log-file` default path) |
---
Binary file not shown.

After

Width:  |  Height:  |  Size: 149 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 760 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 784 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 KiB

+22 -3
View File
@@ -28,9 +28,28 @@
nativeBuildInputs = [
pkgs.pkg-config
pkgs.rustPlatform.bindgenHook
pkgs.rustfmt
pkgs.clippy
];
};
devShells.${system}.default = pkgs.mkShell {
packages = with pkgs; [
cargo
rustc
rustfmt
clippy
cargo-audit
cargo-deny
prek
pkg-config
rustPlatform.bindgenHook
cairo
pam
gdk-pixbuf
librsvg
pango
libxkbcommon
dbus
];
};
};
}
}
+3
View File
@@ -0,0 +1,3 @@
[toolchain]
channel = "1.94.0"
components = ["rustfmt", "clippy"]
+50 -41
View File
@@ -1,5 +1,8 @@
use std::ffi::{CStr, CString};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use log::{debug, error};
use pam_client::{Context, ErrorCode, Flag};
@@ -7,8 +10,10 @@ use smithay_client_toolkit::reexports::{calloop::channel, calloop::EventLoop};
use whoami::username;
use zeroize::Zeroizing;
const SERVICE_NAME: &str = "rustlock";
type AuthChannels = (
channel::Sender<(Zeroizing<String>, u64)>,
channel::Channel<(bool, u64)>,
);
pub struct LockConversation {
pub password: Option<Zeroizing<String>>,
}
@@ -35,60 +40,64 @@ impl pam_client::ConversationHandler for LockConversation {
}
}
pub fn create_and_run_auth_loop(
) -> Option<(channel::Sender<Zeroizing<String>>, channel::Channel<bool>)> {
pub fn create_and_run_auth_loop(service_name: String) -> Option<AuthChannels> {
let username = username();
let conversation = LockConversation { password: None };
match Context::new(SERVICE_NAME, Some(username.as_str()), conversation) {
Ok(_) => {
debug!("Prepared to authenticate user '{}'", username);
}
Err(err) => {
error!("Failed to initialize PAM context: {:?}", err);
error!(
"Ensure that the PAM service '{}' is correctly configured.",
SERVICE_NAME
);
return None;
}
}
let (auth_req_send, auth_req_recv) = channel::channel::<Zeroizing<String>>();
let (auth_res_send, auth_res_recv) = channel::channel::<bool>();
let (auth_req_send, auth_req_recv) = channel::channel::<(Zeroizing<String>, u64)>();
let (auth_res_send, auth_res_recv) = channel::channel::<(bool, u64)>();
thread::spawn(move || {
let mut event_loop: EventLoop<()> = EventLoop::try_new().unwrap();
// Create PAM context once and reuse it for all auth attempts.
// Creating a new context each time is expensive because it
// re-parses configs and re-loads shared libraries for every attempt.
let conversation = LockConversation { password: None };
let mut context =
match Context::new(service_name.as_str(), Some(username.as_str()), conversation) {
Ok(ctx) => {
debug!("Prepared to authenticate user '{}'", username);
ctx
}
Err(err) => {
error!("Failed to initialize PAM context: {:?}", err);
error!(
"Ensure that the PAM service '{}' is correctly configured.",
service_name
);
return;
}
};
let running = Arc::new(AtomicBool::new(true));
let running_clone = running.clone();
event_loop
.handle()
.insert_source(auth_req_recv, |evt, _metadata, _state| match evt {
channel::Event::Msg(password) => {
let conversation = LockConversation {
password: Some(password),
};
match Context::new(SERVICE_NAME, Some(username.as_str()), conversation) {
Ok(mut context) => match context.authenticate(Flag::NONE) {
Ok(()) => {
auth_res_send.send(true).unwrap();
}
Err(err) => {
error!("Pam authenticate failed with {:?}", err);
auth_res_send.send(false).unwrap();
}
},
.insert_source(auth_req_recv, move |evt, _metadata, _state| match evt {
channel::Event::Msg((password, seq)) => {
context.conversation_mut().password = Some(password);
match context.authenticate(Flag::NONE) {
Ok(()) => {
let _ = auth_res_send.send((true, seq));
}
Err(err) => {
error!("Failed to re-initialize PAM context: {:?}", err);
auth_res_send.send(false).unwrap();
error!("Pam authenticate failed with {:?}", err);
let _ = auth_res_send.send((false, seq));
}
}
}
channel::Event::Closed => {}
channel::Event::Closed => {
running_clone.store(false, Ordering::SeqCst);
}
})
.unwrap();
loop {
event_loop.dispatch(None, &mut ()).expect("Failed to run");
while running.load(Ordering::SeqCst) {
let _ = event_loop.dispatch(Some(Duration::from_millis(100)), &mut ());
}
debug!("PAM auth thread exiting cleanly");
});
Some((auth_req_send, auth_res_recv))
+196 -60
View File
@@ -1,7 +1,56 @@
use crate::util;
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub enum RingShape {
#[default]
Circle,
Square,
Diamond,
Hexagon,
Pill,
}
impl FromStr for RingShape {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"circle" => Ok(RingShape::Circle),
"square" => Ok(RingShape::Square),
"diamond" => Ok(RingShape::Diamond),
"hexagon" => Ok(RingShape::Hexagon),
"pill" => Ok(RingShape::Pill),
_ => Err(format!(
"Unknown ring shape '{}'. Options: circle, square, diamond, hexagon, pill",
s
)),
}
}
}
impl fmt::Display for RingShape {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RingShape::Circle => write!(f, "circle"),
RingShape::Square => write!(f, "square"),
RingShape::Diamond => write!(f, "diamond"),
RingShape::Hexagon => write!(f, "hexagon"),
RingShape::Pill => write!(f, "pill"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, clap::ValueEnum)]
pub enum CompletionShell {
Bash,
Fish,
Zsh,
}
#[derive(Parser, Debug, Clone, Serialize, Deserialize)]
#[command(author, version, about, long_about = None)]
@@ -21,6 +70,10 @@ pub struct Config {
#[arg(long, default_value = "7")]
pub indicator_thickness: u32,
#[arg(long, default_value = "circle", value_parser = clap::value_parser!(RingShape))]
#[serde(default)]
pub ring_shape: RingShape,
#[arg(long, value_parser = util::parse_blur_effect)]
#[serde(
deserialize_with = "util::deserialize_blur_effect",
@@ -45,10 +98,6 @@ pub struct Config {
#[serde(default)]
pub effect_swirl: Option<f32>,
#[arg(long)]
#[serde(default)]
pub effect_melting: Option<f32>,
#[arg(long, default_value = "785412", value_parser = util::parse_hex_color)]
#[serde(
deserialize_with = "util::deserialize_hex_color",
@@ -91,7 +140,14 @@ pub struct Config {
)]
pub caps_lock_text_color: (f64, f64, f64, f64),
#[arg(long, action = clap::ArgAction::SetTrue, default_value_t = true)]
#[arg(long, default_value = "0072FF", value_parser = util::parse_hex_color)]
#[serde(
deserialize_with = "util::deserialize_hex_color",
serialize_with = "util::serialize_hex_color"
)]
pub verifying_color: (f64, f64, f64, f64),
#[arg(long, action = clap::ArgAction::SetTrue)]
pub show_caps_lock_text: bool,
#[arg(long, default_value = "00000000", value_parser = util::parse_hex_color)]
@@ -115,7 +171,7 @@ pub struct Config {
)]
pub separator_color: (f64, f64, f64, f64),
#[arg(long, default_value = "2")]
#[arg(long, default_value = "0")]
pub grace: f32,
#[arg(long, default_value = "0.2")]
@@ -134,19 +190,28 @@ pub struct Config {
#[arg(long)]
pub log_file: bool,
#[arg(long, action = clap::ArgAction::SetTrue, default_value_t = true)]
/// Path for log file (enables file logging, overrides --log-file default path)
#[arg(long)]
#[serde(default)]
pub log_path: Option<PathBuf>,
/// Timeout (ms) for PAM authentication before showing failure
#[arg(long, default_value = "10000")]
pub auth_timeout: u64,
#[arg(long, action = clap::ArgAction::SetTrue)]
pub show_media: bool,
#[arg(long, action = clap::ArgAction::SetTrue, default_value_t = true)]
#[arg(long, action = clap::ArgAction::SetTrue)]
pub show_battery: bool,
#[arg(long, action = clap::ArgAction::SetTrue, default_value_t = true)]
#[arg(long, action = clap::ArgAction::SetTrue)]
pub show_network: bool,
#[arg(long, action = clap::ArgAction::SetTrue, default_value_t = true)]
#[arg(long, action = clap::ArgAction::SetTrue)]
pub show_bluetooth: bool,
#[arg(long, action = clap::ArgAction::SetTrue, default_value_t = true)]
#[arg(long, action = clap::ArgAction::SetTrue)]
pub show_album_art: bool,
#[arg(long, action = clap::ArgAction::SetTrue)]
@@ -191,10 +256,50 @@ pub struct Config {
#[serde(default)]
pub media_next_icon: Option<String>,
/// Apply a pre-defined theme preset
#[arg(long)]
#[serde(default)]
pub theme: Option<String>,
/// Maximum number of password dots in the indicator ring
#[arg(long, default_value = "24")]
pub max_dots: u32,
/// Duration (ms) for wrong password feedback animation
#[arg(long, default_value = "500")]
pub wrong_password_duration: u64,
/// Duration (ms) for key highlight feedback animation
#[arg(long, default_value = "300")]
pub key_highlight_duration: u64,
/// Duration (ms) for cleared password feedback animation
#[arg(long, default_value = "500")]
pub cleared_feedback_duration: u64,
/// Duration (ms) for verifying feedback fallback timeout
#[arg(long, default_value = "5000")]
pub verifying_timeout: u64,
/// Duration (ms) that wrong password feedback is shown input-side
#[arg(long, default_value = "1000")]
pub feedback_window_duration: u64,
/// Duration (ms) for key highlight feedback input-side window
#[arg(long, default_value = "200")]
pub key_highlight_window_duration: u64,
/// Polling interval (seconds) for system status updates
#[arg(long, default_value = "2")]
pub system_poll_interval: u64,
/// Delay (seconds) before reconnecting DBus on failure
#[arg(long, default_value = "5")]
pub dbus_reconnect_delay: u64,
/// Timeout (seconds) for system commands (poweroff, reboot, suspend)
#[arg(long, default_value = "5")]
pub command_timeout: u64,
/// Generate shell completions for the given shell
#[arg(long, value_enum)]
#[serde(skip)]
pub completions: Option<CompletionShell>,
}
impl Config {
@@ -202,6 +307,26 @@ impl Config {
use clap::CommandFactory;
let mut config = Config::parse();
// Handle --completions early (before config file merge so file can't inject it)
if let Some(shell) = config.completions {
use clap_complete::Shell;
let mut cmd = Config::command();
let name = "rustlock";
match shell {
CompletionShell::Bash => {
clap_complete::generate(Shell::Bash, &mut cmd, name, &mut std::io::stdout())
}
CompletionShell::Fish => {
clap_complete::generate(Shell::Fish, &mut cmd, name, &mut std::io::stdout())
}
CompletionShell::Zsh => {
clap_complete::generate(Shell::Zsh, &mut cmd, name, &mut std::io::stdout())
}
}
std::process::exit(0);
}
let cmd = Config::command();
let matches = cmd.get_matches();
@@ -209,7 +334,7 @@ impl Config {
let is_cli =
|key: &str| matches.value_source(key) == Some(clap::parser::ValueSource::CommandLine);
// 1. Config file layer (overrides defaults and themes)
// Config file layer (overrides defaults, CLI args take precedence)
let config_path = config.config.clone().unwrap_or_else(|| {
let mut path = std::path::PathBuf::from(std::env::var("HOME").unwrap_or_default());
path.push(".config/rustlock/config.toml");
@@ -242,51 +367,62 @@ impl Config {
}
}
// 2. Theme presets (applied to fields NOT set on CLI or in File)
if let Some(theme) = &config.theme {
match theme.as_str() {
"modern" => {
if config.effect_blur.is_none() && !is_cli("effect_blur") {
config.effect_blur = Some((10, 3));
}
if config.effect_vignette.is_none() && !is_cli("effect_vignette") {
config.effect_vignette = Some((0.5, 0.5));
}
if !is_cli("indicator_radius") {
config.indicator_radius = 120;
}
if !is_cli("ring_color") {
config.ring_color = (0.2, 0.6, 0.8, 1.0);
}
}
"pixel" => {
if config.effect_pixelate.is_none() && !is_cli("effect_pixelate") {
config.effect_pixelate = Some(10);
}
if !is_cli("indicator_radius") {
config.indicator_radius = 80;
}
if !is_cli("ring_color") {
config.ring_color = (0.8, 0.2, 0.2, 1.0);
}
}
"glass" => {
if config.effect_blur.is_none() && !is_cli("effect_blur") {
config.effect_blur = Some((20, 5));
}
if !is_cli("inside_color") {
config.inside_color = (1.0, 1.0, 1.0, 0.1);
}
if !is_cli("ring_color") {
config.ring_color = (1.0, 1.0, 1.0, 0.5);
}
}
_ => {
log::warn!("Unknown theme: {}", theme);
}
}
}
config.auth_timeout = config.auth_timeout.max(100);
config.max_dots = config.max_dots.max(1);
config.fade_in = config.fade_in.max(0.0);
config.grace = config.grace.max(0.0);
config
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_max_dots_default() {
let config = Config::parse_from(["test"]);
assert_eq!(config.max_dots, 24);
}
#[test]
fn test_auth_timeout_default() {
let config = Config::parse_from(["test"]);
assert_eq!(config.auth_timeout, 10000);
}
#[test]
fn test_auth_timeout_min_clamp() {
let mut config = Config::parse_from(["test", "--auth-timeout", "0"]);
config.auth_timeout = config.auth_timeout.max(100);
assert_eq!(config.auth_timeout, 100);
}
#[test]
fn test_max_dots_min_clamp() {
let mut config = Config::parse_from(["test", "--max-dots", "0"]);
config.max_dots = config.max_dots.max(1);
assert_eq!(config.max_dots, 1);
}
#[test]
fn test_fade_in_negative_clamp() {
let mut config = Config::parse_from(["test", "--fade-in=-1"]);
config.fade_in = config.fade_in.max(0.0);
assert_eq!(config.fade_in, 0.0);
}
#[test]
fn test_grace_negative_clamp() {
let mut config = Config::parse_from(["test", "--grace=-1"]);
config.grace = config.grace.max(0.0);
assert_eq!(config.grace, 0.0);
}
#[test]
fn test_log_path_default_none() {
let config = Config::parse_from(["test"]);
assert!(config.log_path.is_none());
}
}
+285
View File
@@ -0,0 +1,285 @@
//! Visual effects for lock screen backgrounds.
//!
//! Each effect takes a `&mut ImageSurface` and processes it in-place.
//! Effects are applied in order: blur → vignette → pixelate → swirl.
use anyhow::{Context, Result};
use cairo::ImageSurface;
/// Apply a swirl effect (radial rotation around the image centre).
pub fn apply_swirl(surface: &mut ImageSurface, angle: f32) -> Result<()> {
let width = surface.width();
let height = surface.height();
let center_x = width as f32 / 2.0;
let center_y = height as f32 / 2.0;
let radius = center_x.min(center_y);
let stride = surface.stride() as usize;
let mut data = vec![0u8; stride * height as usize];
surface
.with_data(|src| data.copy_from_slice(src))
.context("swirl: failed to read surface data")?;
let original = data.clone();
for y in 0..height {
for x in 0..width {
let dx = x as f32 - center_x;
let dy = y as f32 - center_y;
let d = (dx * dx + dy * dy).sqrt();
if d < radius {
let percent = (radius - d) / radius;
let theta = percent * percent * angle;
let s = theta.sin();
let c = theta.cos();
let nx = (c * dx - s * dy + center_x) as i32;
let ny = (s * dx + c * dy + center_y) as i32;
if nx >= 0 && nx < width && ny >= 0 && ny < height {
let src_idx = (ny as usize * stride) + (nx as usize * 4);
let dst_idx = (y as usize * stride) + (x as usize * 4);
data[dst_idx..dst_idx + 4].copy_from_slice(&original[src_idx..src_idx + 4]);
}
}
}
}
let mut surface_data = surface
.data()
.context("swirl: failed to write surface data")?;
surface_data.copy_from_slice(&data);
Ok(())
}
/// Pixelate the surface.
pub fn apply_pixelate(surface: &mut ImageSurface, pixel_size: u32) -> Result<()> {
if pixel_size <= 1 {
return Ok(());
}
let width = surface.width();
let height = surface.height();
let stride = surface.stride() as usize;
let mut data = vec![0u8; stride * height as usize];
surface
.with_data(|src| data.copy_from_slice(src))
.context("pixelate: failed to read surface data")?;
for y in (0..height).step_by(pixel_size as usize) {
for x in (0..width).step_by(pixel_size as usize) {
let mut r = 0u32;
let mut g = 0u32;
let mut b = 0u32;
let mut count = 0u32;
// Average pixels in the block
for py in 0..pixel_size {
for px in 0..pixel_size {
let cur_x = x + px as i32;
let cur_y = y + py as i32;
if cur_x < width && cur_y < height {
let index = (cur_y as usize * stride) + (cur_x as usize * 4);
r += data[index] as u32;
g += data[index + 1] as u32;
b += data[index + 2] as u32;
count += 1;
}
}
}
if count > 0 {
let r = r.checked_div(count).unwrap_or(0) as u8;
let g = g.checked_div(count).unwrap_or(0) as u8;
let b = b.checked_div(count).unwrap_or(0) as u8;
// Fill the block
for py in 0..pixel_size {
for px in 0..pixel_size {
let cur_x = x + px as i32;
let cur_y = y + py as i32;
if cur_x < width && cur_y < height {
let index = (cur_y as usize * stride) + (cur_x as usize * 4);
data[index] = r;
data[index + 1] = g;
data[index + 2] = b;
}
}
}
}
}
}
let mut surface_data = surface
.data()
.context("pixelate: failed to write surface data")?;
surface_data.copy_from_slice(&data);
Ok(())
}
/// Apply a box blur effect (fast two-pass sliding-window implementation).
/// Uses multiply-shift to avoid slow integer division in the hot loop.
pub fn apply_blur(surface: &mut ImageSurface, radius: u32, times: u32) -> Result<()> {
if radius == 0 || times == 0 {
return Ok(());
}
let width = surface.width() as usize;
let height = surface.height() as usize;
let stride = surface.stride() as usize;
let r = radius as usize;
let mut data = vec![0u8; stride * height];
surface
.with_data(|src| data.copy_from_slice(src))
.context("blur: failed to read surface data")?;
let mut scratch = vec![0u8; stride * height];
// Precompute ceil(2^32 / c) for every possible window size c.
let max_count = (2 * r + 1).min(width.max(height));
let factor: Vec<u32> = (0..=max_count)
.map(|c| {
if c == 0 {
0
} else {
(1u64 << 32).div_ceil(c as u64) as u32
}
})
.collect();
#[inline(always)]
fn div_mul(n: u32, factor: u32) -> u8 {
((n as u64 * factor as u64) >> 32) as u8
}
for _ in 0..times {
// Horizontal box blur: data -> scratch
for y in 0..height {
let row = y * stride;
let init_end = r.min(width - 1);
let mut b_acc = 0u32;
let mut g_acc = 0u32;
let mut r_acc = 0u32;
for x in 0..=init_end {
let px = row + x * 4;
b_acc += data[px] as u32;
g_acc += data[px + 1] as u32;
r_acc += data[px + 2] as u32;
}
let mut count = (init_end + 1) as u32;
for x in 0..width {
let dst = row + x * 4;
let f = factor[count as usize];
scratch[dst] = div_mul(b_acc, f);
scratch[dst + 1] = div_mul(g_acc, f);
scratch[dst + 2] = div_mul(r_acc, f);
scratch[dst + 3] = data[dst + 3];
if x >= r {
let old = row + (x - r) * 4;
b_acc -= data[old] as u32;
g_acc -= data[old + 1] as u32;
r_acc -= data[old + 2] as u32;
count -= 1;
}
if x + r + 1 < width {
let new = row + (x + r + 1) * 4;
b_acc += data[new] as u32;
g_acc += data[new + 1] as u32;
r_acc += data[new + 2] as u32;
count += 1;
}
}
}
// Vertical box blur: scratch -> data
let init_end = r.min(height - 1);
let mut b_acc = vec![0u32; width];
let mut g_acc = vec![0u32; width];
let mut r_acc = vec![0u32; width];
for y in 0..=init_end {
let row = y * stride;
for x in 0..width {
let px = row + x * 4;
b_acc[x] += scratch[px] as u32;
g_acc[x] += scratch[px + 1] as u32;
r_acc[x] += scratch[px + 2] as u32;
}
}
let mut count = (init_end + 1) as u32;
for y in 0..height {
let f = factor[count as usize];
let dst_row = y * stride;
for x in 0..width {
let dst = dst_row + x * 4;
data[dst] = div_mul(b_acc[x], f);
data[dst + 1] = div_mul(g_acc[x], f);
data[dst + 2] = div_mul(r_acc[x], f);
}
if y >= r {
let old_row = (y - r) * stride;
for x in 0..width {
let px = old_row + x * 4;
b_acc[x] -= scratch[px] as u32;
g_acc[x] -= scratch[px + 1] as u32;
r_acc[x] -= scratch[px + 2] as u32;
}
count -= 1;
}
if y + r + 1 < height {
let new_row = (y + r + 1) * stride;
for x in 0..width {
let px = new_row + x * 4;
b_acc[x] += scratch[px] as u32;
g_acc[x] += scratch[px + 1] as u32;
r_acc[x] += scratch[px + 2] as u32;
}
count += 1;
}
}
}
let mut surface_data = surface.data()?;
surface_data.copy_from_slice(&data);
Ok(())
}
/// Apply a vignette effect (darken edges).
pub fn apply_vignette(surface: &mut ImageSurface, base: f32, factor: f32) -> Result<()> {
let width = surface.width();
let height = surface.height();
let center_x = width as f32 / 2.0;
let center_y = height as f32 / 2.0;
let max_distance = (center_x * center_x + center_y * center_y).sqrt();
let stride = surface.stride() as usize;
let mut data = vec![0u8; stride * height as usize];
surface
.with_data(|src| data.copy_from_slice(src))
.context("vignette: failed to read surface data")?;
for y in 0..height {
for x in 0..width {
let dx = x as f32 - center_x;
let dy = y as f32 - center_y;
let distance = (dx * dx + dy * dy).sqrt();
let vignette_factor = base + (1.0 - base) * (distance / max_distance).powf(factor);
let index = (y as usize * stride) + (x as usize * 4);
for i in 0..3 {
let value = data[index + i] as f32 * vignette_factor;
data[index + i] = value.clamp(0.0, 255.0) as u8;
}
}
}
let mut surface_data = surface
.data()
.context("vignette: failed to write surface data")?;
surface_data.copy_from_slice(&data);
Ok(())
}
+296 -13
View File
@@ -7,16 +7,20 @@ pub struct InputHandler {
wrong_password_timer: Option<std::time::Instant>,
key_highlight_timer: Option<std::time::Instant>,
caps_lock: bool,
config: crate::config::Config,
last_failed_attempt: Option<std::time::Instant>,
}
impl InputHandler {
pub fn new(_config: crate::config::Config) -> Self {
pub fn new(config: crate::config::Config) -> Self {
Self {
password_buffer: Zeroizing::new(String::new()),
cursor_position: 0,
wrong_password_timer: None,
key_highlight_timer: None,
caps_lock: false,
config,
last_failed_attempt: None,
}
}
@@ -27,7 +31,10 @@ impl InputHandler {
utf8: Option<String>,
modifiers: smithay_client_toolkit::seat::keyboard::Modifiers,
) -> InputAction {
// Update Caps Lock state
if self.is_cooldown() {
return InputAction::None;
}
self.caps_lock = modifiers.caps_lock;
if modifiers.ctrl && keysym == Keysym::u {
@@ -43,12 +50,13 @@ impl InputHandler {
use smithay_client_toolkit::seat::keyboard::Keysym;
match keysym {
Keysym::BackSpace => {
if !self.password_buffer.is_empty() && self.cursor_position > 0 {
self.cursor_position -= 1;
self.password_buffer.remove(self.cursor_position);
if self.password_buffer.is_empty() {
return InputAction::PasswordCleared;
}
if self.password_buffer.is_empty() || self.cursor_position == 0 {
return InputAction::None;
}
self.cursor_position -= 1;
self.password_buffer.remove(self.cursor_position);
if self.password_buffer.is_empty() {
return InputAction::PasswordCleared;
}
return InputAction::PasswordChanged;
}
@@ -116,6 +124,10 @@ impl InputHandler {
InputAction::None
}
pub fn password_buffer(&self) -> &Zeroizing<String> {
&self.password_buffer
}
pub fn password_length(&self) -> usize {
self.password_buffer.len()
}
@@ -127,12 +139,19 @@ impl InputHandler {
/// Set wrong password feedback timer
pub fn set_wrong_password_feedback(&mut self) {
self.wrong_password_timer = Some(std::time::Instant::now());
self.last_failed_attempt = Some(std::time::Instant::now());
}
pub fn is_cooldown(&self) -> bool {
self.last_failed_attempt
.map(|t| t.elapsed() < std::time::Duration::from_millis(400))
.unwrap_or(false)
}
/// Check if wrong password feedback should be shown
pub fn should_show_wrong_password(&self) -> bool {
if let Some(timer) = self.wrong_password_timer {
timer.elapsed() < std::time::Duration::from_millis(1000)
timer.elapsed() < std::time::Duration::from_millis(self.config.feedback_window_duration)
} else {
false
}
@@ -146,15 +165,13 @@ impl InputHandler {
/// Check if key highlight should be shown
pub fn should_show_key_highlight(&self) -> bool {
if let Some(timer) = self.key_highlight_timer {
timer.elapsed() < std::time::Duration::from_millis(200)
timer.elapsed()
< std::time::Duration::from_millis(self.config.key_highlight_window_duration)
} else {
false
}
}
/// Update timers (should be called periodically)
pub fn update(&mut self) {}
/// Get the current Caps Lock state
pub fn caps_lock(&self) -> bool {
self.caps_lock
@@ -171,3 +188,269 @@ pub enum InputAction {
SubmitPassword(Zeroizing<String>),
Cancel,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use clap::Parser;
use smithay_client_toolkit::seat::keyboard::{Keysym, Modifiers};
fn test_config() -> Config {
Config::parse_from(["test"])
}
#[test]
fn test_new_handler_defaults() {
let handler = InputHandler::new(test_config());
assert_eq!(handler.password_length(), 0);
assert_eq!(handler.cursor_position(), 0);
assert!(!handler.caps_lock());
assert!(!handler.should_show_wrong_password());
assert!(!handler.should_show_key_highlight());
}
#[test]
fn test_character_input_appends() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
let action = handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
assert!(matches!(action, InputAction::PasswordChanged));
assert_eq!(handler.password_length(), 1);
assert_eq!(handler.cursor_position(), 1);
let action = handler.handle_key_event(Keysym::b, Some("b".to_string()), mods);
assert!(matches!(action, InputAction::PasswordChanged));
assert_eq!(handler.password_length(), 2);
assert_eq!(handler.cursor_position(), 2);
assert_eq!(&*handler.password_buffer, "ab");
}
#[test]
fn test_backspace_removes_last_char() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
handler.handle_key_event(Keysym::b, Some("b".to_string()), mods);
assert_eq!(handler.password_length(), 2);
let action = handler.handle_key_event(Keysym::BackSpace, None, mods);
assert!(matches!(action, InputAction::PasswordChanged));
assert_eq!(handler.password_length(), 1);
assert_eq!(handler.cursor_position(), 1);
assert_eq!(&*handler.password_buffer, "a");
}
#[test]
fn test_backspace_on_empty_buffer() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
let action = handler.handle_key_event(Keysym::BackSpace, None, mods);
assert!(matches!(action, InputAction::None));
assert_eq!(handler.password_length(), 0);
}
#[test]
fn test_backspace_last_char_clears() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
let action = handler.handle_key_event(Keysym::BackSpace, None, mods);
assert!(matches!(action, InputAction::PasswordCleared));
assert_eq!(handler.password_length(), 0);
}
#[test]
fn test_ctrl_u_clears_buffer() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers {
ctrl: true,
..Modifiers::default()
};
handler.handle_key_event(Keysym::a, Some("a".to_string()), Modifiers::default());
handler.handle_key_event(Keysym::b, Some("b".to_string()), Modifiers::default());
assert_eq!(handler.password_length(), 2);
let action = handler.handle_key_event(Keysym::u, None, mods);
assert!(matches!(action, InputAction::PasswordCleared));
assert_eq!(handler.password_length(), 0);
assert_eq!(handler.cursor_position(), 0);
}
#[test]
fn test_submit_returns_and_clears() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
handler.handle_key_event(Keysym::b, Some("b".to_string()), mods);
let action = handler.handle_key_event(Keysym::Return, None, mods);
match action {
InputAction::SubmitPassword(p) => {
assert_eq!(&*p, "ab");
}
_ => panic!("Expected SubmitPassword, got {:?}", action),
}
// Buffer should be cleared after submission
assert_eq!(handler.password_length(), 0);
assert_eq!(handler.cursor_position(), 0);
}
#[test]
fn test_submit_enter_kp() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
let action = handler.handle_key_event(Keysym::KP_Enter, None, Modifiers::default());
assert!(matches!(action, InputAction::SubmitPassword(_)));
}
#[test]
fn test_escape_cancels() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
let action = handler.handle_key_event(Keysym::Escape, None, mods);
assert!(matches!(action, InputAction::Cancel));
}
#[test]
fn test_cursor_left_right() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
handler.handle_key_event(Keysym::b, Some("b".to_string()), mods);
handler.handle_key_event(Keysym::c, Some("c".to_string()), mods);
assert_eq!(handler.cursor_position(), 3);
// Move left
let action = handler.handle_key_event(Keysym::Left, None, mods);
assert!(matches!(action, InputAction::CursorMoved));
assert_eq!(handler.cursor_position(), 2);
// Left again
handler.handle_key_event(Keysym::Left, None, mods);
assert_eq!(handler.cursor_position(), 1);
// Right
let action = handler.handle_key_event(Keysym::Right, None, mods);
assert!(matches!(action, InputAction::CursorMoved));
assert_eq!(handler.cursor_position(), 2);
}
#[test]
fn test_cursor_left_at_start() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
let action = handler.handle_key_event(Keysym::Left, None, mods);
assert!(matches!(action, InputAction::None));
assert_eq!(handler.cursor_position(), 0);
}
#[test]
fn test_cursor_right_at_end() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
let action = handler.handle_key_event(Keysym::Right, None, mods);
assert!(matches!(action, InputAction::None));
assert_eq!(handler.cursor_position(), 1);
}
#[test]
fn test_home_and_end() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
handler.handle_key_event(Keysym::b, Some("b".to_string()), mods);
handler.handle_key_event(Keysym::c, Some("c".to_string()), mods);
handler.handle_key_event(Keysym::Left, None, mods);
handler.handle_key_event(Keysym::Left, None, mods);
assert_eq!(handler.cursor_position(), 1);
// Home
let action = handler.handle_key_event(Keysym::Home, None, mods);
assert!(matches!(action, InputAction::CursorMoved));
assert_eq!(handler.cursor_position(), 0);
// End
let action = handler.handle_key_event(Keysym::End, None, mods);
assert!(matches!(action, InputAction::CursorMoved));
assert_eq!(handler.cursor_position(), 3);
}
#[test]
fn test_delete_removes_at_cursor() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
handler.handle_key_event(Keysym::b, Some("b".to_string()), mods);
handler.handle_key_event(Keysym::c, Some("c".to_string()), mods);
// cursor at 3, delete should be a no-op
let action = handler.handle_key_event(Keysym::Delete, None, mods);
assert!(matches!(action, InputAction::None));
assert_eq!(handler.password_length(), 3);
// move left, delete at cursor position 2 (removes 'c')
handler.handle_key_event(Keysym::Left, None, mods);
let action = handler.handle_key_event(Keysym::Delete, None, mods);
assert!(matches!(action, InputAction::PasswordChanged));
assert_eq!(handler.password_length(), 2);
assert_eq!(&*handler.password_buffer, "ab");
}
#[test]
fn test_insert_mid_buffer() {
let mut handler = InputHandler::new(test_config());
let mods = Modifiers::default();
handler.handle_key_event(Keysym::a, Some("a".to_string()), mods);
handler.handle_key_event(Keysym::c, Some("c".to_string()), mods);
// Move left, insert 'b' between a and c
handler.handle_key_event(Keysym::Left, None, mods);
handler.handle_key_event(Keysym::b, Some("b".to_string()), mods);
assert_eq!(&*handler.password_buffer, "abc");
assert_eq!(handler.cursor_position(), 2);
}
#[test]
fn test_caps_lock_tracking() {
let mut handler = InputHandler::new(test_config());
assert!(!handler.caps_lock());
let caps_mods = Modifiers {
caps_lock: true,
..Modifiers::default()
};
handler.handle_key_event(Keysym::a, Some("A".to_string()), caps_mods);
assert!(handler.caps_lock());
}
#[test]
fn test_wrong_password_timer() {
let mut handler = InputHandler::new(test_config());
assert!(!handler.should_show_wrong_password());
handler.set_wrong_password_feedback();
assert!(handler.should_show_wrong_password());
}
#[test]
fn test_key_highlight_timer() {
let mut handler = InputHandler::new(test_config());
assert!(!handler.should_show_key_highlight());
handler.set_key_highlight();
assert!(handler.should_show_key_highlight());
}
}
+137 -28
View File
@@ -24,6 +24,16 @@ pub struct LockedSurface {
wayland_surface: Option<wl_surface::WlSurface>,
output: wl_output::WlOutput,
configured: bool,
/// Set whenever rendered state changes (keystroke, status, clock minute,
/// animation step). update() renders only when this is set or an animation
/// is in flight, so an idle lock screen does no per-frame cairo work.
dirty: bool,
/// Last clock minute (unix-minute) we rendered, to detect %H:%M rollover.
last_minute: i64,
ctrl_held: bool,
/// True while the pointer button is held on the indicator ring (hold-to-peek).
/// Cleared on button release, matching Ctrl-hold transient behavior.
peek_toggled: bool,
}
impl LockedSurface {
@@ -54,6 +64,10 @@ impl LockedSurface {
wayland_surface: None,
output,
configured: false,
dirty: true,
last_minute: i64::MIN,
ctrl_held: false,
peek_toggled: false,
})
}
@@ -62,6 +76,7 @@ impl LockedSurface {
log::debug!("LockedSurface: Configured, starting animation");
self.configured = true;
self.start_time = Instant::now();
self.dirty = true;
}
/// Check if this surface matches the given Wayland surface
@@ -72,29 +87,50 @@ impl LockedSurface {
.is_some_and(|ws| ws.id() == surface.id())
}
/// Update the surface state (called on each frame)
pub fn update(&mut self) {
self.input_handler.update();
/// Update the surface state (called on each frame). Returns `true` if the
/// surface was re-rendered and therefore needs to be committed. An idle
/// surface (no input, no animation, same clock minute) returns `false` and
/// does no cairo work, which keeps a locked session near-zero CPU.
pub fn update(&mut self) -> bool {
if !self.configured {
return;
return false;
}
// Update fade animation
if self.fade_alpha < 1.0 {
let elapsed = self.start_time.elapsed();
let fade_duration = std::time::Duration::from_secs_f32(self.config.fade_in);
// Ease-in-out cubic function
let t = (elapsed.as_secs_f64() / fade_duration.as_secs_f64()).clamp(0.0, 1.0);
let eased_t = if t < 0.5 {
4.0 * t * t * t
if fade_duration.is_zero() {
self.fade_alpha = 1.0;
self.renderer.set_fade_alpha(1.0);
self.dirty = true;
} else {
1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
};
let new_alpha = eased_t.min(1.0);
if (new_alpha - self.fade_alpha).abs() > 0.001 {
self.fade_alpha = new_alpha;
self.renderer.set_fade_alpha(self.fade_alpha);
// Ease-in-out cubic function
let t = (elapsed.as_secs_f64() / fade_duration.as_secs_f64()).clamp(0.0, 1.0);
let eased_t = if t < 0.5 {
4.0 * t * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
};
let new_alpha = eased_t.min(1.0);
if t >= 1.0 {
// The eased curve only approaches 1.0 asymptotically, and the
// 0.001 throttle below suppresses the tiny final steps — which
// would leave fade_alpha stuck just under 1.0 forever. Since
// `fade_alpha < 1.0` is our "still animating" signal, that would
// force a full render every frame. Snap to exactly 1.0 once the
// fade duration has elapsed so the animation cleanly completes.
if self.fade_alpha != 1.0 {
self.fade_alpha = 1.0;
self.renderer.set_fade_alpha(1.0);
self.dirty = true;
}
} else if (new_alpha - self.fade_alpha).abs() > 0.001 {
self.fade_alpha = new_alpha;
self.renderer.set_fade_alpha(self.fade_alpha);
self.dirty = true;
}
}
}
@@ -102,6 +138,7 @@ impl LockedSurface {
if self.input_handler.should_show_wrong_password() && !self.wrong_password_shown {
self.renderer.show_wrong_password();
self.wrong_password_shown = true;
self.dirty = true;
} else if !self.input_handler.should_show_wrong_password() && self.wrong_password_shown {
self.wrong_password_shown = false;
}
@@ -110,27 +147,58 @@ impl LockedSurface {
if self.input_handler.should_show_key_highlight() && !self.key_highlight_shown {
self.renderer.show_key_highlight();
self.key_highlight_shown = true;
self.dirty = true;
} else if !self.input_handler.should_show_key_highlight() && self.key_highlight_shown {
self.key_highlight_shown = false;
}
// Update caps lock state in renderer
self.renderer.caps_lock = self.input_handler.caps_lock();
if self.renderer.caps_lock != self.input_handler.caps_lock() {
self.renderer.caps_lock = self.input_handler.caps_lock();
self.dirty = true;
}
// Set background if available and not already applied
if !self.background_applied {
if let Some(ref background) = self.background {
log::info!("Applying background image to renderer");
log::debug!("Applying background image to renderer");
self.renderer.set_background(background.clone());
self.background_applied = true;
self.dirty = true;
}
}
self.renderer
.set_password_display(self.input_handler.password_length());
// The clock displays %H:%M, so it only needs a redraw once per minute.
if self.config.clock {
let minute = chrono::Local::now().timestamp().div_euclid(60);
if self.last_minute != minute {
self.last_minute = minute;
self.dirty = true;
}
}
// Keep emitting frames while an animation is in flight so it can run to
// completion even though no new event arrives.
let animating = self.fade_alpha < 1.0 || self.renderer.is_animating();
if !self.dirty && !animating {
return false;
}
if !self.config.hide_password {
let buf = self.input_handler.password_buffer();
let length = self.input_handler.password_length();
if self.ctrl_held || self.peek_toggled {
self.renderer.peek_password(buf.as_str());
} else {
self.renderer.set_password_display(length);
}
}
self.renderer
.set_cursor_position(self.input_handler.cursor_position());
self.renderer.render();
self.dirty = false;
true
}
/// Commit the rendered frame to the Wayland surface
@@ -163,21 +231,36 @@ impl LockedSurface {
}
self.renderer.resize(width, height);
self.background_applied = false;
self.dirty = true;
}
pub fn show_wrong_password(&mut self) {
self.renderer.clear_verifying();
self.input_handler.set_wrong_password_feedback();
self.wrong_password_shown = false;
self.dirty = true;
}
pub fn show_verifying(&mut self) {
self.renderer.show_verifying();
self.dirty = true;
}
pub fn handle_key_event(
&mut self,
event: smithay_client_toolkit::seat::keyboard::KeyEvent,
event: KeyEvent,
modifiers: smithay_client_toolkit::seat::keyboard::Modifiers,
) -> Option<InputAction> {
self.ctrl_held = modifiers.ctrl;
let action = self
.input_handler
.handle_key_event(event.keysym, event.utf8, modifiers);
// Any key event may change the password display, cursor or caps state,
// so request a redraw on the next update().
self.dirty = true;
match action {
InputAction::PasswordChanged => {
self.input_handler.set_key_highlight();
@@ -207,10 +290,30 @@ impl LockedSurface {
pub fn set_background(&mut self, surface: ImageSurface) {
self.background = Some(surface);
self.background_applied = false;
self.dirty = true;
}
pub fn set_system_status(&mut self, status: SystemStatus) {
self.renderer.system_status = status;
if self.renderer.system_status != status {
self.renderer.system_status = status;
self.dirty = true;
}
}
pub fn set_ctrl_held(&mut self, held: bool) {
if self.ctrl_held != held {
self.ctrl_held = held;
self.dirty = true;
}
}
/// Set peek mode on/off. Called when the user presses (held=true) or
/// releases (held=false) the mouse button on the indicator ring. Works
/// like Ctrl-hold — peek only lasts while the button is held.
pub fn set_peek_held(&mut self, held: bool) {
self.peek_toggled = held;
log::debug!("set_peek_held: peek_toggled = {}", self.peek_toggled);
self.dirty = true;
}
}
@@ -237,12 +340,6 @@ impl LockManager {
}
}
pub fn update(&mut self) {
for surface in &mut self.surfaces {
surface.update();
}
}
pub fn surface_count(&self) -> usize {
self.surfaces.len()
}
@@ -268,12 +365,24 @@ impl LockManager {
let mut action = None;
for surface in &mut self.surfaces {
if let Some(a) = surface.handle_key_event(event.clone(), modifiers) {
action = Some(a);
if let crate::input::InputAction::SubmitPassword(p) = &a {
if !p.is_empty() {
return Some(a);
}
} else {
action = Some(a);
}
}
}
action
}
pub fn set_ctrl_held(&mut self, held: bool) {
for surface in &mut self.surfaces {
surface.set_ctrl_held(held);
}
}
pub fn remove_surface_by_output(&mut self, output: &wl_output::WlOutput) -> Option<usize> {
use wayland_client::Proxy;
let output_id = Proxy::id(output);
+235 -75
View File
@@ -1,5 +1,6 @@
mod auth;
mod config;
mod effects;
mod input;
mod lock;
mod render;
@@ -48,21 +49,39 @@ use smithay_client_toolkit::{
static FILE_LOGGER: std::sync::LazyLock<std::sync::Mutex<Option<std::fs::File>>> =
std::sync::LazyLock::new(|| std::sync::Mutex::new(None));
fn setup_file_logging(_config: &Config) {
let log_path = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string()) + "/.rustlock.log";
match OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&log_path)
{
Ok(file) => {
*FILE_LOGGER.lock().unwrap() = Some(file);
eprintln!("Logging to: {}", log_path);
fn setup_file_logging(config: &Config) {
if let Some(ref path) = config.log_path {
match OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(path)
{
Ok(file) => {
*FILE_LOGGER.lock().unwrap() = Some(file);
eprintln!("Logging to: {}", path.display());
}
Err(e) => {
eprintln!("Failed to open log file {}: {}", path.display(), e);
}
}
Err(e) => {
eprintln!("Failed to open log file {}: {}", log_path, e);
} else if config.log_file {
let default_path =
std::path::PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string()))
.join(".rustlock.log");
match OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&default_path)
{
Ok(file) => {
*FILE_LOGGER.lock().unwrap() = Some(file);
eprintln!("Logging to: {}", default_path.display());
}
Err(e) => {
eprintln!("Failed to open log file {}: {}", default_path.display(), e);
}
}
}
}
@@ -110,7 +129,9 @@ struct WaylandLock {
lock_manager: Arc<Mutex<LockManager>>,
config: Config,
ctrlc_exit: Arc<std::sync::atomic::AtomicBool>,
auth_tx: Option<calloop::channel::Sender<Zeroizing<String>>>,
auth_tx: Option<calloop::channel::Sender<(Zeroizing<String>, u64)>>,
auth_seq: u64,
auth_pending_seq: Option<u64>,
compositor_state: CompositorState,
output_state: OutputState,
registry_state: RegistryState,
@@ -127,6 +148,7 @@ struct WaylandLock {
exit: bool,
screenshot_manager: Option<ScreenshotManager>,
grace_until: Option<Instant>,
auth_pending_at: Option<Instant>,
system_manager: Arc<SystemManager>,
modifiers: Modifiers,
current_layout: u32,
@@ -134,22 +156,29 @@ struct WaylandLock {
impl WaylandLock {
fn handle_auth_result(&mut self, success: bool) {
// Clear grace period on any auth result
// Clear grace period and auth pending on any auth result
self.grace_until = None;
self.auth_pending_at = None;
self.auth_pending_seq = None;
if success {
log::info!("✅ Authentication successful - unlocking session");
// ext-session-lock-v1 recommends destroying lock surfaces before
// issuing unlock_and_destroy. With multiple outputs Hyprland
// otherwise treats the unlock as a client crash and shows its
// failsafe screen.
self.lock_surfaces.clear();
if let Some(session_lock) = &self.session_lock {
session_lock.unlock();
let _ = self.conn.flush();
self.exit = true;
log::debug!("Unlock requested - exiting");
log::info!("Unlock requested - exiting");
} else {
log::error!("No session_lock available to unlock!");
self.exit = true;
}
} else {
log::warn!("❌ Authentication failed - wrong password");
log::error!("❌ Authentication failed - wrong password");
if let Ok(mut lock_manager) = self.lock_manager.lock() {
for surface in &mut lock_manager.surfaces {
surface.show_wrong_password();
@@ -188,17 +217,17 @@ impl WaylandLock {
}
Keysym::F1 => {
self.system_manager
.send_command(system::SystemCommand::Suspend);
.send_command(system::BackendCommand::Suspend);
return;
}
Keysym::F2 => {
self.system_manager
.send_command(system::SystemCommand::Reboot);
.send_command(system::BackendCommand::Reboot);
return;
}
Keysym::F3 => {
self.system_manager
.send_command(system::SystemCommand::PowerOff);
.send_command(system::BackendCommand::PowerOff);
return;
}
_ => {}
@@ -215,30 +244,44 @@ impl WaylandLock {
}
if event.keysym == Keysym::Return {
// Debounce: skip if auth is already pending (user pressed Enter twice)
if self.auth_pending_at.is_some() {
return;
}
log::info!("Enter pressed - submitting password");
if let Ok(mut lock_manager) = self.lock_manager.lock() {
let mut password = Zeroizing::new(String::new());
let modifiers = self.modifiers;
for surface in &mut lock_manager.surfaces {
if let Some(InputAction::SubmitPassword(p)) =
surface.handle_key_event(event.clone(), modifiers)
{
password = p;
let password: Option<Zeroizing<String>> = self
.lock_manager
.lock()
.ok()
.and_then(|mut lm| lm.handle_key_event(event, self.modifiers))
.and_then(|action| {
if let InputAction::SubmitPassword(p) = action {
(!p.is_empty()).then_some(p)
} else {
None
}
});
if let Some(password) = password {
self.auth_seq += 1;
self.auth_pending_at = Some(Instant::now());
self.auth_pending_seq = Some(self.auth_seq);
// Show verifying feedback on ALL surfaces BEFORE sending to PAM.
if let Ok(mut lock_manager) = self.lock_manager.lock() {
for surface in &mut lock_manager.surfaces {
surface.show_verifying();
}
}
if !password.is_empty() {
if let Some(tx) = &self.auth_tx {
let _ = tx.send(password);
}
if let Some(tx) = &self.auth_tx {
let _ = tx.send((password, self.auth_seq));
}
}
} else {
let modifiers = self.modifiers;
let _action = self
.lock_manager
self.lock_manager
.lock()
.map(|mut lm| lm.handle_key_event(event, modifiers))
.unwrap_or(None);
.ok()
.and_then(|mut lm| lm.handle_key_event(event, self.modifiers));
}
}
}
@@ -463,8 +506,19 @@ impl KeyboardHandler for WaylandLock {
modifiers: Modifiers,
layout: u32,
) {
let ctrl_changed = self.modifiers.ctrl != modifiers.ctrl;
self.modifiers = modifiers;
self.current_layout = layout;
if ctrl_changed {
log::debug!(
"update_modifiers: ctrl {} -> {}",
!modifiers.ctrl,
modifiers.ctrl
);
}
if let Ok(mut lock_manager) = self.lock_manager.lock() {
lock_manager.set_ctrl_held(modifiers.ctrl);
}
}
}
@@ -547,7 +601,10 @@ impl Dispatch<ZwlrScreencopyFrameV1, CaptureData> for WaylandLock {
height,
stride,
} => {
let format = format.into_result().unwrap();
let Ok(format) = format.into_result() else {
log::error!("Screencopy: invalid buffer format, skipping capture");
return;
};
let mut info = data.info.lock().unwrap();
*info = Some(screenshot::BufferInfo {
@@ -576,7 +633,11 @@ impl Dispatch<ZwlrScreencopyFrameV1, CaptureData> for WaylandLock {
}
}
Event::Flags { flags } => {
*data.flags.lock().unwrap() = Some(flags.into_result().unwrap());
if let Ok(f) = flags.into_result() {
*data.flags.lock().unwrap() = Some(f);
} else {
log::error!("Screencopy: invalid flags, skipping");
}
}
Event::Ready { .. } => {
log::info!("Screencopy: Ready for output {}", data.output_idx);
@@ -596,7 +657,12 @@ impl Dispatch<ZwlrScreencopyFrameV1, CaptureData> for WaylandLock {
};
if let Ok(surface) = mgr.buffer_to_surface(handle, &mut pool) {
let mut ss = Screenshot::new(surface);
let _ = ss.apply_effects(&state.config);
if let Err(e) = ss.apply_effects(&state.config) {
log::error!(
"Failed to apply effects to screenshot {}: {e}",
data.output_idx
);
}
if data.output_idx < state.captured_backgrounds.len() {
state.captured_backgrounds[data.output_idx] = Some(ss.into_inner());
}
@@ -639,12 +705,14 @@ impl PointerHandler for WaylandLock {
events: &[PointerEvent],
) {
for event in events {
if let PointerEventKind::Press { button, .. } = event.kind {
if button == 0x110 {
match event.kind {
PointerEventKind::Press { button: 0x110, .. } => {
let (x, y) = event.position;
if let Ok(lm) = self.lock_manager.lock() {
for surface in &lm.surfaces {
if surface.matches_surface(&event.surface) {
// Check media controls first
let mut handled = false;
for (action, rx, ry, rw, rh) in &surface.renderer.media_rects {
if x >= *rx && x <= rx + rw && y >= *ry && y <= ry + rh {
match *action {
@@ -654,13 +722,54 @@ impl PointerHandler for WaylandLock {
"prev" => self.system_manager.media_prev(),
_ => {}
}
return;
handled = true;
break;
}
}
if handled {
return;
}
// Check indicator ring hit — hold to peek (like Ctrl)
let cx = surface.renderer.width as f64 / 2.0;
let cy = surface.renderer.height as f64 / 2.0;
let r = surface.renderer.config.indicator_radius as f64;
let shape = surface.renderer.config.ring_shape;
if crate::render::ring_shape::point_in_shape(cx, cy, r, shape, x, y)
{
drop(lm);
if let Ok(mut lm) = self.lock_manager.lock() {
if let Some(s) = lm
.surfaces
.iter_mut()
.find(|s| s.matches_surface(&event.surface))
{
s.set_peek_held(true);
s.update();
let _ = s.commit(&mut self.pool);
}
}
return;
}
}
}
}
}
PointerEventKind::Release { button: 0x110, .. } => {
if let Ok(mut lm) = self.lock_manager.lock() {
// Clear peek on ALL surfaces. Wayland button release events
// can reference a different wl_surface proxy than the press
// event (compositor-dependent pointer grab semantics), so
// matching by surface might miss the LockedSurface that has
// peek_toggled=true. Iterating every surface guarantees peek
// always ends on button release — matching Ctrl-hold behavior.
for surface in &mut lm.surfaces {
surface.set_peek_held(false);
surface.update();
let _ = surface.commit(&mut self.pool);
}
}
}
_ => {}
}
}
}
@@ -678,9 +787,15 @@ wayland_client::delegate_noop!(WaylandLock: ignore wayland_client::protocol::wl_
fn main() -> Result<(), Box<dyn Error>> {
let config = Config::load();
setup_file_logging(&config);
static LOGGER: DualLogger = DualLogger;
log::set_logger(&LOGGER).map(|()| log::set_max_level(log::LevelFilter::Debug))?;
let max_level = if config.debug {
log::LevelFilter::Debug
} else {
log::LevelFilter::Info
};
log::set_logger(&LOGGER).map(|()| log::set_max_level(max_level))?;
log::info!("Starting rustlock v{}", env!("CARGO_PKG_VERSION"));
#[allow(clippy::arc_with_non_send_sync)]
@@ -694,13 +809,18 @@ fn main() -> Result<(), Box<dyn Error>> {
let shm_state = Shm::bind(&globals, &qh).map_err(|_| "wl_shm not supported")?;
let system_manager = Arc::new(SystemManager::new());
let system_manager = Arc::new(SystemManager::new(&config));
let (auth_tx_actual, auth_feedback_rx_actual) = match auth::create_and_run_auth_loop() {
let (auth_tx_actual, auth_feedback_rx_actual) = match auth::create_and_run_auth_loop(
config.pam_service.clone(),
) {
Some(channels) => channels,
None => {
log::error!("Failed to initialize authentication. This usually means PAM is not configured correctly.");
log::error!("Please ensure you have a PAM service file at /etc/pam.d/rustlock");
log::error!(
"Please ensure you have a PAM service file at /etc/pam.d/{}",
config.pam_service
);
std::process::exit(1);
}
};
@@ -735,6 +855,9 @@ fn main() -> Result<(), Box<dyn Error>> {
exit: false,
screenshot_manager: ScreenshotManager::new(&globals, &qh).ok(),
grace_until: None,
auth_pending_at: None,
auth_seq: 0,
auth_pending_seq: None,
system_manager: system_manager.clone(),
modifiers: Modifiers::default(),
current_layout: 0,
@@ -748,31 +871,41 @@ fn main() -> Result<(), Box<dyn Error>> {
if let Ok(img) = image::open(image_path) {
let img = img.to_rgba8();
let (w, h) = img.dimensions();
let mut surface =
cairo::ImageSurface::create(cairo::Format::ARgb32, w as i32, h as i32).unwrap();
if let Ok(mut surface) =
cairo::ImageSurface::create(cairo::Format::ARgb32, w as i32, h as i32)
{
let mut surface_data = surface.data().unwrap();
for y in 0..h {
for x in 0..w {
let pixel = img.get_pixel(x, y);
let idx = ((y * w + x) * 4) as usize;
surface_data[idx] = pixel[2]; // B
surface_data[idx + 1] = pixel[1]; // G
surface_data[idx + 2] = pixel[0]; // R
surface_data[idx + 3] = pixel[3]; // A
let image_ok = {
if let Ok(mut surface_data) = surface.data() {
for y in 0..h {
for x in 0..w {
let pixel = img.get_pixel(x, y);
let idx = ((y * w + x) * 4) as usize;
surface_data[idx] = pixel[2];
surface_data[idx + 1] = pixel[1];
surface_data[idx + 2] = pixel[0];
surface_data[idx + 3] = pixel[3];
}
}
true
} else {
log::error!("Failed to get background image surface data");
false
}
};
if image_ok {
let mut ss = Screenshot::new(surface);
if let Err(e) = ss.apply_effects(&state.config) {
log::error!("Failed to apply effects to custom background image: {e}");
}
let surface = ss.into_inner();
let num_outputs = state.output_state.outputs().count();
state.captured_backgrounds = vec![Some(surface); num_outputs];
state.config.screenshots = false;
}
} else {
log::error!("Failed to create Cairo surface for background image");
}
let mut ss = Screenshot::new(surface);
let _ = ss.apply_effects(&state.config);
let surface = ss.into_inner();
let num_outputs = state.output_state.outputs().count();
state.captured_backgrounds = vec![Some(surface); num_outputs];
// Disable screenshots if image was successfully loaded
state.config.screenshots = false;
} else {
log::error!(
"Failed to load custom background image from {:?}",
@@ -806,10 +939,10 @@ fn main() -> Result<(), Box<dyn Error>> {
event_loop
.handle()
.insert_source(auth_feedback_rx_actual, |event, _, state| {
if let calloop::channel::Event::Msg(success) = event {
state.handle_auth_result(success);
if success {
state.lock_surfaces.clear();
if let calloop::channel::Event::Msg((success, seq)) = event {
// Ignore stale auth results from previous requests (e.g. after timeout or retry).
if state.auth_pending_seq == Some(seq) {
state.handle_auth_result(success);
}
}
})?;
@@ -823,14 +956,35 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}
// Auth timeout: if PAM thread doesn't respond within config.auth_timeout ms,
// treat as auth failure so the user gets feedback instead of hanging forever.
if state.auth_pending_seq.is_some() {
if let Some(at) = state.auth_pending_at {
if Instant::now().duration_since(at)
>= Duration::from_millis(state.config.auth_timeout)
{
log::warn!(
"Authentication timed out after {} ms",
state.config.auth_timeout
);
// Clear pending seq so the eventual PAM result is ignored as stale
state.auth_pending_seq = None;
state.handle_auth_result(false);
}
}
}
let mut status = state.system_manager.get_status();
status.keyboard_layout = Some(state.current_layout.to_string());
if let Ok(mut lm) = state.lock_manager.lock() {
lm.set_system_status(status);
lm.update();
// Only commit surfaces that actually re-rendered this tick. An idle
// lock screen renders nothing and commits nothing.
for surface in &mut lm.surfaces {
let _ = surface.commit(&mut state.pool);
if surface.update() {
let _ = surface.commit(&mut state.pool);
}
}
}
if state.exit {
@@ -906,6 +1060,12 @@ fn main() -> Result<(), Box<dyn Error>> {
event_loop.dispatch(Duration::from_millis(16), &mut state)?;
}
// After unlock_and_destroy the compositor sends keyboard/pointer leave and
// delete_id events that must be processed before we disconnect, otherwise
// Hyprland treats the disconnect as a client crash and shows its failsafe
// screen (only reproducible on multi-output setups).
let _ = state.conn.roundtrip();
log::info!("Exiting rustlock");
Ok(())
}
+106 -26
View File
@@ -1,7 +1,31 @@
use crate::render::ring_shape;
use crate::render::Renderer;
use std::time::Instant;
impl Renderer {
pub(crate) fn draw_verifying_feedback(&self) {
let center_x = self.width as f64 / 2.0;
let center_y = self.height as f64 / 2.0;
let radius = self.config.indicator_radius as f64;
let thickness = self.config.indicator_thickness as f64;
let (r, g, b, a) = self.config.verifying_color;
if a > 0.0 {
self.context.new_path();
self.context.set_source_rgba(r, g, b, a * self.fade_alpha);
self.context.set_line_width(thickness + 2.0);
self.context.set_line_join(cairo::LineJoin::Round);
ring_shape::build_ring_path(
&self.context,
center_x,
center_y,
radius,
self.config.ring_shape,
);
render_try!(self.context.stroke());
}
}
pub(crate) fn draw_wrong_password_feedback(&self) {
let center_x = self.width as f64 / 2.0;
let center_y = self.height as f64 / 2.0;
@@ -9,7 +33,7 @@ impl Renderer {
let thickness = self.config.indicator_thickness as f64;
let intensity = if let Some(start) = self.wrong_password_start {
let elapsed = start.elapsed();
let duration = std::time::Duration::from_millis(500);
let duration = std::time::Duration::from_millis(self.config.wrong_password_duration);
if elapsed < duration {
1.0 - (elapsed.as_secs_f64() / duration.as_secs_f64())
} else {
@@ -24,9 +48,15 @@ impl Renderer {
self.context
.set_source_rgba(1.0, 0.0, 0.0, intensity * self.fade_alpha);
self.context.set_line_width(thickness + 2.0);
self.context
.arc(center_x, center_y, radius, 0.0, 2.0 * std::f64::consts::PI);
self.context.stroke().unwrap();
self.context.set_line_join(cairo::LineJoin::Round);
ring_shape::build_ring_path(
&self.context,
center_x,
center_y,
radius,
self.config.ring_shape,
);
render_try!(self.context.stroke());
}
}
@@ -37,7 +67,7 @@ impl Renderer {
let thickness = self.config.indicator_thickness as f64;
let intensity = if let Some(start) = self.key_highlight_start {
let elapsed = start.elapsed();
let duration = std::time::Duration::from_millis(300);
let duration = std::time::Duration::from_millis(self.config.key_highlight_duration);
if elapsed < duration {
1.0 - (elapsed.as_secs_f64() / duration.as_secs_f64())
} else {
@@ -57,17 +87,26 @@ impl Renderer {
.set_source_rgba(r, g, b, a * intensity * self.fade_alpha);
self.context.set_line_width(thickness + 1.5);
let global_offset = (self.password_display.len() as f64 * 45.0).to_radians();
self.context.new_path();
let actual_start = global_offset + self.key_highlight_angle;
self.context.arc(
self.context.set_line_cap(cairo::LineCap::Round);
// Convert angle range to normalized perimeter t (for circle: t = angle / 2π)
let max_dots = self.config.max_dots as f64;
let t_offset = ring_shape::top_centre_offset(self.config.ring_shape);
let global_t = ((self.password_display.len() as f64) / max_dots) + t_offset;
let random_t = self.key_highlight_angle / (2.0 * std::f64::consts::PI);
let t_start = global_t + random_t;
let sector_t = 40.0 / 360.0;
let t_end = t_start + sector_t;
ring_shape::build_sector_path(
&self.context,
center_x,
center_y,
radius,
actual_start,
actual_start + (40.0_f64).to_radians(),
self.config.ring_shape,
t_start,
t_end,
);
self.context.stroke().unwrap();
render_try!(self.context.stroke());
}
}
@@ -78,7 +117,7 @@ impl Renderer {
let thickness = self.config.indicator_thickness as f64;
let intensity = if let Some(start) = self.cleared_feedback_start {
let elapsed = start.elapsed();
let duration = std::time::Duration::from_millis(500);
let duration = std::time::Duration::from_millis(self.config.cleared_feedback_duration);
if elapsed < duration {
1.0 - (elapsed.as_secs_f64() / duration.as_secs_f64())
} else {
@@ -92,60 +131,91 @@ impl Renderer {
self.context.new_path();
self.context
.set_source_rgba(1.0, 0.0, 0.0, intensity * self.fade_alpha * 0.5);
self.context.arc(
ring_shape::build_fill_path(
&self.context,
center_x,
center_y,
radius - thickness / 2.0,
0.0,
2.0 * std::f64::consts::PI,
thickness,
self.config.ring_shape,
);
self.context.fill().unwrap();
render_try!(self.context.fill());
self.context.new_path();
self.context
.set_source_rgba(1.0, 0.0, 0.0, intensity * self.fade_alpha);
self.context.set_line_width(thickness + 4.0);
self.context
.arc(center_x, center_y, radius, 0.0, 2.0 * std::f64::consts::PI);
self.context.stroke().unwrap();
self.context.set_line_join(cairo::LineJoin::Round);
ring_shape::build_ring_path(
&self.context,
center_x,
center_y,
radius,
self.config.ring_shape,
);
render_try!(self.context.stroke());
self.context.new_path();
self.context.set_font_size(24.0);
self.context
.set_source_rgba(1.0, 1.0, 1.0, intensity * self.fade_alpha);
let text = "CLEARED";
let te = self.context.text_extents(text).unwrap();
let te = render_try!(self.context.text_extents(text));
self.context
.move_to(center_x - te.width() / 2.0, center_y - radius - 20.0);
self.context.show_text(text).unwrap();
render_try!(self.context.show_text(text));
}
}
/// Whether any feedback animation is currently in flight and therefore
/// requires continued per-frame redraws until it finishes.
pub(crate) fn is_animating(&self) -> bool {
self.wrong_password_start.is_some()
|| self.key_highlight_start.is_some()
|| self.cleared_feedback_start.is_some()
|| self.verifying_start.is_some()
}
pub(crate) fn update_feedback_timers(&mut self) {
self.update_uptime();
if let Some(start) = self.wrong_password_start {
if start.elapsed() > std::time::Duration::from_millis(500) {
if start.elapsed()
> std::time::Duration::from_millis(self.config.wrong_password_duration)
{
self.wrong_password_shown = false;
self.wrong_password_start = None;
}
}
if let Some(start) = self.key_highlight_start {
if start.elapsed() > std::time::Duration::from_millis(300) {
if start.elapsed()
> std::time::Duration::from_millis(self.config.key_highlight_duration)
{
self.key_highlight_shown = false;
self.key_highlight_start = None;
}
}
if let Some(start) = self.cleared_feedback_start {
if start.elapsed() > std::time::Duration::from_millis(500) {
if start.elapsed()
> std::time::Duration::from_millis(self.config.cleared_feedback_duration)
{
self.cleared_feedback_shown = false;
self.cleared_feedback_start = None;
}
}
if let Some(start) = self.verifying_start {
if start.elapsed() > std::time::Duration::from_millis(self.config.auth_timeout) {
self.verifying_shown = false;
self.verifying_start = None;
}
}
}
pub fn show_wrong_password(&mut self) {
self.wrong_password_shown = true;
self.wrong_password_start = Some(Instant::now());
// Clear verifying state — wrong password replaces it
self.verifying_shown = false;
self.verifying_start = None;
}
pub fn show_key_highlight(&mut self) {
@@ -154,8 +224,8 @@ impl Renderer {
use std::time::SystemTime;
let seed = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos() as u64;
.map(|d| d.as_nanos() as u64)
.unwrap_or(0);
let random_val = seed.wrapping_mul(6364136223846793005).wrapping_add(1);
self.key_highlight_angle = ((random_val % 360) as f64).to_radians();
}
@@ -164,4 +234,14 @@ impl Renderer {
self.cleared_feedback_shown = true;
self.cleared_feedback_start = Some(Instant::now());
}
pub fn show_verifying(&mut self) {
self.verifying_shown = true;
self.verifying_start = Some(Instant::now());
}
pub fn clear_verifying(&mut self) {
self.verifying_shown = false;
self.verifying_start = None;
}
}
+76 -33
View File
@@ -1,3 +1,4 @@
use crate::render::ring_shape;
use crate::render::Renderer;
impl Renderer {
@@ -6,35 +7,40 @@ impl Renderer {
let center_y = self.height as f64 / 2.0;
let radius = self.config.indicator_radius as f64;
let thickness = self.config.indicator_thickness as f64;
let shape = self.config.ring_shape;
// Filled center
self.context.new_path();
let (r, g, b, a) = self.config.inside_color;
self.context.set_source_rgba(r, g, b, a * self.fade_alpha);
self.context.arc(
ring_shape::build_fill_path(
&self.context,
center_x,
center_y,
radius - thickness / 2.0,
0.0,
2.0 * std::f64::consts::PI,
thickness,
shape,
);
self.context.fill().unwrap();
render_try!(self.context.fill());
// Separator line behind the ring
let (lr, lg, lb, la) = self.config.line_color;
if la > 0.0 {
self.context.new_path();
self.context
.set_source_rgba(lr, lg, lb, la * self.fade_alpha);
self.context.set_line_width(1.0);
self.context.arc(
ring_shape::build_ring_path(
&self.context,
center_x,
center_y,
radius - thickness / 2.0,
0.0,
2.0 * std::f64::consts::PI,
shape,
);
self.context.stroke().unwrap();
render_try!(self.context.stroke());
}
// Outer ring
let (r, g, b, a) = if self.caps_lock {
self.config.caps_lock_color
} else {
@@ -43,10 +49,11 @@ impl Renderer {
self.context.new_path();
self.context.set_source_rgba(r, g, b, a * self.fade_alpha);
self.context.set_line_width(thickness);
self.context
.arc(center_x, center_y, radius, 0.0, 2.0 * std::f64::consts::PI);
self.context.stroke().unwrap();
self.context.set_line_join(cairo::LineJoin::Round);
ring_shape::build_ring_path(&self.context, center_x, center_y, radius, shape);
render_try!(self.context.stroke());
// Separator line through center
let (r, g, b, a) = self.config.separator_color;
if a > 0.0 {
self.context.new_path();
@@ -54,7 +61,7 @@ impl Renderer {
self.context.set_line_width(1.0);
self.context.move_to(center_x - radius, center_y);
self.context.line_to(center_x + radius, center_y);
self.context.stroke().unwrap();
render_try!(self.context.stroke());
}
}
@@ -66,42 +73,78 @@ impl Renderer {
let center_y = self.height as f64 / 2.0;
let radius = self.config.indicator_radius as f64;
let thickness = self.config.indicator_thickness as f64;
let shape = self.config.ring_shape;
self.context.new_path();
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
let max_dots = self.config.max_dots as f64;
let dot_radius = radius - thickness - 10.0;
let t_offset = ring_shape::top_centre_offset(shape);
let count = self.password_display.len();
if count == 0 {
return;
}
let dot_radius = radius - thickness - 10.0;
let angle_step = (360.0 / 24.0_f64).to_radians();
for i in 0..count {
let angle = (i as f64 * angle_step) - std::f64::consts::FRAC_PI_2;
let x = center_x + dot_radius * angle.cos();
let y = center_y + dot_radius * angle.sin();
if self.peeking {
// Draw each character at the same ring-perimeter positions as dots
self.context.set_font_size(14.0);
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
for (i, ch) in self.password_display.chars().enumerate() {
let t = (i as f64 / max_dots) + t_offset;
let (x, y) = ring_shape::perimeter_point(center_x, center_y, dot_radius, shape, t);
let mut ch_str = String::new();
ch_str.push(ch);
let te = render_try!(self.context.text_extents(&ch_str));
self.context.new_path();
self.context.move_to(
x - te.x_bearing() - te.width() / 2.0,
y - te.y_bearing() - te.height() / 2.0,
);
render_try!(self.context.show_text(&ch_str));
}
} else {
self.context.new_path();
self.context.arc(x, y, 4.0, 0.0, 2.0 * std::f64::consts::PI);
self.context.fill().unwrap();
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
for i in 0..count {
let t = (i as f64 / max_dots) + t_offset;
let (x, y) = ring_shape::perimeter_point(center_x, center_y, dot_radius, shape, t);
self.context.new_path();
self.context.arc(x, y, 4.0, 0.0, 2.0 * std::f64::consts::PI);
render_try!(self.context.fill());
}
}
if self.fade_alpha > 0.0 && self.cursor_position > 0 {
let cursor_angle =
((self.cursor_position as f64 - 0.5) * angle_step) - std::f64::consts::FRAC_PI_2;
let x1 = center_x + (dot_radius - 8.0) * cursor_angle.cos();
let y1 = center_y + (dot_radius - 8.0) * cursor_angle.sin();
let x2 = center_x + (dot_radius + 8.0) * cursor_angle.cos();
let y2 = center_y + (dot_radius + 8.0) * cursor_angle.sin();
// Cursor indicator (shared between peek and dot modes).
if self.fade_alpha > 0.0 {
// At position 0, place the cursor squarely at the first-dot position
// (t_offset) instead of subtracting 0.5, which would push t negative
// and cause perimeter_point to wrap it to the far end of the ring.
let cursor_t = if self.cursor_position == 0 {
t_offset
} else {
((self.cursor_position as f64 - 0.5) / max_dots) + t_offset
};
let (cx, cy) =
ring_shape::perimeter_point(center_x, center_y, dot_radius, shape, cursor_t);
let dx = cx - center_x;
let dy = cy - center_y;
let len = (dx * dx + dy * dy).sqrt().max(1.0);
let nx = dx / len;
let ny = dy / len;
let x1 = cx - 8.0 * nx;
let y1 = cy - 8.0 * ny;
let x2 = cx + 8.0 * nx;
let y2 = cy + 8.0 * ny;
self.context.new_path();
self.context.set_source_rgba(0.0, 0.8, 1.0, self.fade_alpha);
self.context.set_line_width(2.0);
self.context.move_to(x1, y1);
self.context.line_to(x2, y2);
self.context.stroke().unwrap();
render_try!(self.context.stroke());
}
}
@@ -114,9 +157,9 @@ impl Renderer {
self.context.set_source_rgba(r, g, b, a * self.fade_alpha);
self.context.set_font_size(24.0);
let text = "Caps Lock";
let te = self.context.text_extents(text).unwrap();
let te = render_try!(self.context.text_extents(text));
self.context
.move_to(center_x - te.width() / 2.0, center_y - radius - 10.0);
self.context.show_text(text).unwrap();
render_try!(self.context.show_text(text));
}
}
+77 -75
View File
@@ -7,7 +7,6 @@ impl Renderer {
let center_x = self.width as f64 / 2.0;
let start_y = self.height as f64 - 120.0;
let art_size = 56.0;
let spacing = 80.0;
if self.config.show_album_art && self.system_status.media_art_url != self.last_art_url {
self.last_art_url = self.system_status.media_art_url.clone();
@@ -16,45 +15,32 @@ impl Renderer {
if let Ok(img) = image::load_from_memory(data) {
let img = img.to_rgba8();
let (w, h) = img.dimensions();
let mut surface =
ImageSurface::create(Format::ARgb32, w as i32, h as i32).unwrap();
if let Ok(mut surface) =
ImageSurface::create(Format::ARgb32, w as i32, h as i32)
{
let mut surface_data = surface.data().unwrap();
for y in 0..h {
for x in 0..w {
let pixel = img.get_pixel(x, y);
let idx = ((y * w + x) * 4) as usize;
surface_data[idx] = pixel[2];
surface_data[idx + 1] = pixel[1];
surface_data[idx + 2] = pixel[0];
surface_data[idx + 3] = pixel[3];
if let Ok(mut surface_data) = surface.data() {
for y in 0..h {
for x in 0..w {
let pixel = img.get_pixel(x, y);
let idx = ((y * w + x) * 4) as usize;
surface_data[idx] = pixel[2];
surface_data[idx + 1] = pixel[1];
surface_data[idx + 2] = pixel[0];
surface_data[idx + 3] = pixel[3];
}
}
} else {
log::error!("Failed to access album art surface data");
}
self.media_art_surface = Some(surface);
} else {
log::error!("Failed to create album art surface");
}
self.media_art_surface = Some(surface);
}
}
}
let has_art = self.config.show_album_art && self.media_art_surface.is_some();
let text_x = if has_art {
center_x - spacing / 2.0
} else {
center_x
};
let art_x = center_x - spacing - art_size / 2.0;
if has_art {
if let Some(ref art) = self.media_art_surface {
self.context.save().unwrap();
let scale = art_size / art.width() as f64;
self.context.translate(art_x, start_y);
self.context.scale(scale, scale);
self.context.set_source_surface(art, 0.0, 0.0).unwrap();
self.context.paint_with_alpha(self.fade_alpha).unwrap();
self.context.restore().unwrap();
}
}
self.context.new_path();
self.context
@@ -67,65 +53,81 @@ impl Renderer {
title.clone()
};
let te = self.context.text_extents(&display_text).unwrap();
self.context
.move_to(text_x - te.width() / 2.0, start_y + 20.0);
self.context.show_text(&display_text).unwrap();
let te = render_try!(self.context.text_extents(&display_text));
// Center art + text as a group with 16px gap between them
let art_text_gap = 16.0;
let group_width = te.width() + art_size + art_text_gap;
let group_start_x = center_x - group_width / 2.0;
let art_x = group_start_x;
let text_center_x = art_x + art_size + art_text_gap + te.width() / 2.0;
if has_art {
if let Some(ref art) = self.media_art_surface {
render_try!(self.context.save());
let scale = art_size / art.width() as f64;
self.context.translate(art_x, start_y);
self.context.scale(scale, scale);
render_try!(self.context.set_source_surface(art, 0.0, 0.0));
render_try!(self.context.paint_with_alpha(self.fade_alpha));
render_try!(self.context.restore());
}
}
self.context
.move_to(text_center_x - te.width() / 2.0, start_y + 20.0);
render_try!(self.context.show_text(&display_text));
// All media buttons on one row, evenly spaced.
// Each gets a 24×24 hit area, matching draw_icon_at's target_size.
let btn_size = 24.0;
let btn_gap = 48.0;
let btn_y = start_y + 50.0;
// Layout: prev | play_pause | next (centered as a group)
let total_buttons: f64 = (self.media_prev_icon_surface.is_some() as u32
+ 1
+ self.media_next_icon_surface.is_some() as u32)
as f64;
let group_width = (total_buttons - 1.0) * btn_gap + btn_size;
let group_start_x = center_x - group_width / 2.0;
let mut btn_x = group_start_x;
if let Some(ref icon) = self.media_prev_icon_surface {
self.draw_icon_at(btn_x, btn_y - btn_size / 2.0, icon);
self.media_rects
.push(("prev", btn_x, btn_y - btn_size / 2.0, btn_size, btn_size));
btn_x += btn_gap;
}
// Play/pause — always present (at least one of play/pause icon should load)
if self.system_status.media_playing {
if let Some(ref icon) = self.media_pause_icon_surface {
let pause_y = start_y + 40.0;
let rx = center_x - icon.width() as f64 / 2.0;
let ry = pause_y - icon.height() as f64 / 2.0;
self.draw_icon_at(rx, ry, icon);
self.draw_icon_at(btn_x, btn_y - btn_size / 2.0, icon);
self.media_rects.push((
"play_pause",
rx,
ry,
icon.width() as f64,
icon.height() as f64,
btn_x,
btn_y - btn_size / 2.0,
btn_size,
btn_size,
));
}
} else if let Some(ref icon) = self.media_play_icon_surface {
let play_y = start_y + 40.0;
let rx = center_x - icon.width() as f64 / 2.0;
let ry = play_y - icon.height() as f64 / 2.0;
self.draw_icon_at(rx, ry, icon);
self.draw_icon_at(btn_x, btn_y - btn_size / 2.0, icon);
self.media_rects.push((
"play_pause",
rx,
ry,
icon.width() as f64,
icon.height() as f64,
btn_x,
btn_y - btn_size / 2.0,
btn_size,
btn_size,
));
}
let controls_y = start_y + 65.0;
let icon_size = 20.0;
let icon_spacing = 40.0;
if let Some(ref icon) = self.media_prev_icon_surface {
let rx = center_x - icon_spacing - icon_size / 2.0;
let ry = controls_y - icon_size / 2.0;
self.draw_icon_at(rx, ry, icon);
self.media_rects
.push(("prev", rx, ry, icon_size, icon_size));
}
if let Some(ref icon) = self.media_stop_icon_surface {
let rx = center_x - icon_size / 2.0;
let ry = controls_y - icon_size / 2.0;
self.draw_icon_at(rx, ry, icon);
self.media_rects
.push(("stop", rx, ry, icon_size, icon_size));
}
btn_x += btn_gap;
if let Some(ref icon) = self.media_next_icon_surface {
let rx = center_x + icon_spacing - icon_size / 2.0;
let ry = controls_y - icon_size / 2.0;
self.draw_icon_at(rx, ry, icon);
self.draw_icon_at(btn_x, btn_y - btn_size / 2.0, icon);
self.media_rects
.push(("next", rx, ry, icon_size, icon_size));
.push(("next", btn_x, btn_y - btn_size / 2.0, btn_size, btn_size));
}
}
}
+31
View File
@@ -4,9 +4,24 @@ use std::time::Instant;
use crate::config::Config;
use crate::system::SystemStatus;
/// Log cairo errors and return early instead of propagating panics.
/// Defined once here and available to all render submodules.
macro_rules! render_try {
($expr:expr) => {
match $expr {
Ok(v) => v,
Err(e) => {
log::error!("cairo error: {:?}", e);
return;
}
}
};
}
mod feedback;
mod indicator;
mod media_bar;
pub(crate) mod ring_shape;
mod status_bar;
pub struct Renderer {
@@ -19,12 +34,15 @@ pub struct Renderer {
pub(crate) wrong_password_shown: bool,
pub(crate) key_highlight_shown: bool,
pub(crate) cleared_feedback_shown: bool,
pub(crate) verifying_shown: bool,
pub(crate) wrong_password_start: Option<Instant>,
pub(crate) key_highlight_start: Option<Instant>,
pub(crate) cleared_feedback_start: Option<Instant>,
pub(crate) verifying_start: Option<Instant>,
pub(crate) key_highlight_angle: f64,
pub(crate) background: Option<ImageSurface>,
pub(crate) password_display: String,
pub(crate) peeking: bool,
pub(crate) cursor_position: usize,
pub(crate) uptime_cache: String,
pub(crate) last_uptime_update: Option<Instant>,
@@ -60,12 +78,15 @@ impl Renderer {
wrong_password_shown: false,
key_highlight_shown: false,
cleared_feedback_shown: false,
verifying_shown: false,
wrong_password_start: None,
key_highlight_start: None,
cleared_feedback_start: None,
verifying_start: None,
key_highlight_angle: 0.0,
background: None,
password_display: String::new(),
peeking: false,
cursor_position: 0,
uptime_cache: String::new(),
last_uptime_update: None,
@@ -108,6 +129,12 @@ impl Renderer {
pub fn set_password_display(&mut self, length: usize) {
self.password_display = ".".repeat(length);
self.peeking = false;
}
pub fn peek_password(&mut self, password: &str) {
self.password_display = password.to_string();
self.peeking = true;
}
pub fn set_cursor_position(&mut self, position: usize) {
@@ -193,6 +220,10 @@ impl Renderer {
self.draw_caps_lock_indicator();
}
if self.verifying_shown {
self.draw_verifying_feedback();
}
if self.wrong_password_shown {
self.draw_wrong_password_feedback();
}
+257
View File
@@ -0,0 +1,257 @@
use cairo::Context;
use crate::config::RingShape;
/// Number of linear segments used to approximate curved portions of a shape.
/// Higher = smoother, lower = faster.
const SEGMENTS: usize = 120;
/// Return the (x, y) point on the shape's perimeter at normalized position `t` ∈ [0, 1].
/// `t = 0` is a reference point (rightmost for most shapes); `t` increases clockwise.
/// `r` is the shape's characteristic radius (distance from center to side/vertex).
pub(crate) fn perimeter_point(cx: f64, cy: f64, r: f64, shape: RingShape, t: f64) -> (f64, f64) {
// Normalize t to [0, 1). Rust's % preserves sign, and the shape-specific
// functions use floor/truncation that break on negative values.
let t = t - t.floor();
match shape {
RingShape::Circle => {
let angle = t * 2.0 * std::f64::consts::PI;
(cx + r * angle.cos(), cy + r * angle.sin())
}
RingShape::Square => square_perimeter_point(cx, cy, r, t),
RingShape::Diamond => diamond_perimeter_point(cx, cy, r, t),
RingShape::Hexagon => hexagon_perimeter_point(cx, cy, r, t),
RingShape::Pill => pill_perimeter_point(cx, cy, r, t),
}
}
/// Right-top-right-bottom-left-bottom-left-top order (clockwise from right).
fn square_perimeter_point(cx: f64, cy: f64, r: f64, t: f64) -> (f64, f64) {
let t = t % 1.0;
let side = (t * 4.0).floor() as u32;
let local = t * 4.0 - side as f64;
match side {
0 => {
// Right side: top → bottom
(cx + r, cy - r + local * 2.0 * r)
}
1 => {
// Bottom side: right → left
(cx + r - local * 2.0 * r, cy + r)
}
2 => {
// Left side: bottom → top
(cx - r, cy + r - local * 2.0 * r)
}
_ => {
// Top side: left → right
(cx - r + local * 2.0 * r, cy - r)
}
}
}
/// Right-bottom-left-top order (clockwise from right).
fn diamond_perimeter_point(cx: f64, cy: f64, r: f64, t: f64) -> (f64, f64) {
let t = t % 1.0;
let side = (t * 4.0).floor() as u32;
let local = t * 4.0 - side as f64;
match side {
0 => {
// Right to bottom
(cx + r - local * r, cy + local * r)
}
1 => {
// Bottom to left
(cx - local * r, cy + r - local * r)
}
2 => {
// Left to top
(cx - r + local * r, cy - local * r)
}
_ => {
// Top to right
(cx + local * r, cy - r + local * r)
}
}
}
/// 0: right → bottom-right (vertex to vertex)
/// 1: bottom edge (right → left)
/// 2: bottom-left → left
/// 3: left → top-left
/// 4: top edge (left → right)
/// 5: top-right → right
fn hexagon_perimeter_point(cx: f64, cy: f64, r: f64, t: f64) -> (f64, f64) {
let t = t % 1.0;
let side = (t * 6.0).floor() as u32;
let local = t * 6.0 - side as f64;
// Shared helper for edges between two vertices
let vert =
|angle_rad: f64| -> (f64, f64) { (cx + r * angle_rad.cos(), cy + r * angle_rad.sin()) };
// Vertices clockwise from right (angle = 0)
let v = [
vert(0.0), // V0: right
vert(std::f64::consts::PI * (1.0 / 3.0)), // V1: bottom-right
vert(std::f64::consts::PI * (2.0 / 3.0)), // V2: bottom-left
vert(std::f64::consts::PI), // V3: left
vert(std::f64::consts::PI * (4.0 / 3.0)), // V4: top-left
vert(std::f64::consts::PI * (5.0 / 3.0)), // V5: top-right
];
let (x0, y0) = v[side as usize];
let (x1, y1) = v[((side + 1) % 6) as usize];
(x0 + local * (x1 - x0), y0 + local * (y1 - y0))
}
/// Clockwise from top-right corner: right cap (downward) → bottom straight
/// (leftward) → left cap (upward) → top straight (rightward).
///
/// The pill is a stadium / capsule: cap radius = r, straight-section length = 2r,
/// total width = 4r, total height = 2r.
fn pill_perimeter_point(cx: f64, cy: f64, r: f64, t: f64) -> (f64, f64) {
let t = t % 1.0;
let total_p = 4.0 + 2.0 * std::f64::consts::PI; // 4r + 2πr, normalised by r
let straights = 2.0 / total_p; // each straight segment's t fraction
let caps = std::f64::consts::PI / total_p; // each cap's t fraction
if t < caps {
// Right cap: semicircle, top → bottom, centred at (r, 0)
let local = t / caps;
let angle = -std::f64::consts::PI / 2.0 + local * std::f64::consts::PI;
(cx + r + r * angle.cos(), cy + r * angle.sin())
} else if t < caps + straights {
// Bottom straight: right → left
let local = (t - caps) / straights;
(cx + r - local * 2.0 * r, cy + r)
} else if t < caps + straights + caps {
// Left cap: semicircle, bottom → top, centred at (-r, 0)
let local = (t - caps - straights) / caps;
let angle = std::f64::consts::PI / 2.0 + local * std::f64::consts::PI;
(cx - r + r * angle.cos(), cy + r * angle.sin())
} else {
// Top straight: left → right
let local = (t - 2.0 * caps - straights) / straights;
(cx - r + local * 2.0 * r, cy - r)
}
}
/// Build the full closed path of the shape outline (at radius `r`).
/// Call `stroke()` after this to draw the ring.
pub(crate) fn build_ring_path(ctx: &Context, cx: f64, cy: f64, r: f64, shape: RingShape) {
match shape {
RingShape::Circle => {
ctx.arc(cx, cy, r, 0.0, 2.0 * std::f64::consts::PI);
}
RingShape::Square | RingShape::Diamond | RingShape::Hexagon | RingShape::Pill => {
let (x0, y0) = perimeter_point(cx, cy, r, shape, 0.0);
ctx.move_to(x0, y0);
// Subdivide perimeter into enough segments for smooth rendering
let n = 80;
for i in 1..=n {
let pt = i as f64 / n as f64;
let (x, y) = perimeter_point(cx, cy, r, shape, pt);
ctx.line_to(x, y);
}
ctx.close_path();
}
}
}
/// Build a partial path along the shape perimeter from normalized position
/// `t_start` to `t_end`. Call `stroke()` after this to draw a sector.
pub(crate) fn build_sector_path(
ctx: &Context,
cx: f64,
cy: f64,
r: f64,
shape: RingShape,
t_start: f64,
t_end: f64,
) {
match shape {
RingShape::Circle => {
let a_start = t_start * 2.0 * std::f64::consts::PI;
let a_end = t_end * 2.0 * std::f64::consts::PI;
ctx.arc(cx, cy, r, a_start, a_end);
}
RingShape::Square | RingShape::Diamond | RingShape::Hexagon | RingShape::Pill => {
let (x0, y0) = perimeter_point(cx, cy, r, shape, t_start);
ctx.move_to(x0, y0);
for i in 1..=SEGMENTS {
let t = t_start + (t_end - t_start) * (i as f64 / SEGMENTS as f64);
let (x, y) = perimeter_point(cx, cy, r, shape, t);
ctx.line_to(x, y);
}
}
}
}
/// Build the filled interior path (inset from outer ring by `thickness / 2`).
/// Call `fill()` after this.
pub(crate) fn build_fill_path(
ctx: &Context,
cx: f64,
cy: f64,
radius: f64,
thickness: f64,
shape: RingShape,
) {
let inner_r = (radius - thickness / 2.0).max(0.0);
if inner_r <= 0.0 {
return;
}
build_ring_path(ctx, cx, cy, inner_r, shape);
}
/// Check whether a point `(px, py)` lies inside the shape's fill area.
/// This is used for hittesting (e.g. click & hold to peek) so the clickable
/// region matches what the user sees on screen.
pub(crate) fn point_in_shape(cx: f64, cy: f64, r: f64, shape: RingShape, px: f64, py: f64) -> bool {
let dx = (px - cx).abs();
let dy = (py - cy).abs();
match shape {
RingShape::Circle => dx * dx + dy * dy <= r * r,
RingShape::Square => dx <= r && dy <= r,
RingShape::Diamond => dx + dy <= r,
RingShape::Hexagon => {
// Regular hexagon, vertex at (r, 0), edge slopes at ±60°.
// For |dx| ≥ r/2 the sloping edge bounds: |dy| ≤ √3 (r |dx|).
// For |dx| ≤ r/2 the flat top bounds: |dy| ≤ √3 r / 2.
let sqrt3 = 3.0_f64.sqrt();
if dx >= r / 2.0 {
dy <= sqrt3 * (r - dx)
} else {
dy <= r * sqrt3 / 2.0
}
}
RingShape::Pill => {
// Pill = centre rectangle (2r × 2r) + semicircular caps of
// radius r at each end. Total width 4r, height 2r.
if dx <= r {
dy <= r
} else {
let ex = dx - r;
ex * ex + dy * dy <= r * r
}
}
}
}
/// Return the normalized `t` offset that places the first password dot at the
/// visual top-centre of the shape. May be negative; callers should NOT wrap.
pub(crate) fn top_centre_offset(shape: RingShape) -> f64 {
match shape {
// Circle/Diamond: top at t=0.75 → offset -(1-0.75) = -0.25
RingShape::Circle | RingShape::Diamond => -0.25,
// Square: top edge centre at t=0.875 → offset -(1-0.875) = -0.125
RingShape::Square => -0.125,
// Hexagon: top edge centre at t=0.75 → offset -0.25
RingShape::Hexagon => -0.25,
// Pill: top straight centre at t = 1 - 1/(4+2π) ≈ 0.9027
RingShape::Pill => -1.0 / (4.0 + 2.0 * std::f64::consts::PI),
}
}
+32 -26
View File
@@ -3,7 +3,7 @@ use cairo::{Format, ImageSurface};
impl Renderer {
pub(crate) fn load_icons(&mut self) {
log::info!("Attempting to load status icons...");
log::debug!("Attempting to load status icons...");
let wifi_names = [
"network-wireless-signal-excellent-symbolic",
"network-wireless-signal-excellent",
@@ -25,7 +25,7 @@ impl Renderer {
.unwrap_or_default();
if !wifi_path.is_empty() {
log::info!("Resolved WiFi icon path: {}", wifi_path);
log::debug!("Resolved WiFi icon path: {}", wifi_path);
self.wifi_icon_surface = self.load_icon(&wifi_path);
}
@@ -50,7 +50,7 @@ impl Renderer {
.unwrap_or_default();
if !bt_path.is_empty() {
log::info!("Resolved Bluetooth icon path: {}", bt_path);
log::debug!("Resolved Bluetooth icon path: {}", bt_path);
self.bluetooth_icon_surface = self.load_icon(&bt_path);
}
@@ -77,7 +77,7 @@ impl Renderer {
.unwrap_or_default();
if !batt_path.is_empty() {
log::info!("Resolved Battery icon path: {}", batt_path);
log::debug!("Resolved Battery icon path: {}", batt_path);
self.battery_icon_surface = self.load_icon(&batt_path);
}
@@ -353,27 +353,27 @@ impl Renderer {
self.context.new_path();
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
self.context.set_font_size(48.0);
let te = self.context.text_extents(&time_str).unwrap();
let te = render_try!(self.context.text_extents(&time_str));
self.context
.move_to(center_x - te.width() / 2.0, center_y + te.height() / 4.0);
self.context.show_text(&time_str).unwrap();
render_try!(self.context.show_text(&time_str));
self.context.new_path();
self.context.set_font_size(14.0);
let de = self.context.text_extents(&date_str).unwrap();
let de = render_try!(self.context.text_extents(&date_str));
self.context.move_to(
center_x - de.width() / 2.0,
center_y + te.height() / 4.0 + 25.0,
);
self.context.show_text(&date_str).unwrap();
render_try!(self.context.show_text(&date_str));
self.context.new_path();
let ue = self.context.text_extents(&self.uptime_cache).unwrap();
let ue = render_try!(self.context.text_extents(&self.uptime_cache));
self.context.move_to(
center_x - ue.width() / 2.0,
center_y + te.height() / 4.0 + 43.0,
);
self.context.show_text(&self.uptime_cache).unwrap();
render_try!(self.context.show_text(&self.uptime_cache));
}
pub(crate) fn draw_network(&self) {
@@ -392,13 +392,13 @@ impl Renderer {
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
self.context.set_font_size(16.0);
self.context.move_to(text_x, y);
self.context.show_text(ssid).unwrap();
render_try!(self.context.show_text(ssid));
} else {
self.context.new_path();
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
self.context.set_font_size(16.0);
self.context.move_to(x, y);
self.context.show_text(ssid).unwrap();
render_try!(self.context.show_text(ssid));
}
}
}
@@ -418,7 +418,7 @@ impl Renderer {
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
self.context.set_font_size(16.0);
self.context.move_to(text_x, y);
self.context.show_text(&battery_text).unwrap();
render_try!(self.context.show_text(&battery_text));
} else {
self.draw_battery_icon_at(
x,
@@ -433,7 +433,7 @@ impl Renderer {
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
self.context.set_font_size(16.0);
self.context.move_to(x + icon_width + 10.0, y);
self.context.show_text(&battery_text).unwrap();
render_try!(self.context.show_text(&battery_text));
}
}
}
@@ -462,7 +462,7 @@ impl Renderer {
.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha * alpha_mult);
self.context.set_font_size(14.0);
self.context.move_to(text_x, y);
self.context.show_text(&status_text).unwrap();
render_try!(self.context.show_text(&status_text));
}
}
@@ -478,7 +478,7 @@ impl Renderer {
self.context.set_font_size(16.0);
let text = format!("Layout: {}", layout);
self.context.move_to(x, y);
self.context.show_text(&text).unwrap();
render_try!(self.context.show_text(&text));
}
}
}
@@ -490,8 +490,12 @@ impl Renderer {
(target_size / surface.width() as f64).min(target_size / surface.height() as f64);
self.context.translate(x, y);
self.context.scale(scale, scale);
self.context.set_source_surface(surface, 0.0, 0.0).unwrap();
self.context.paint_with_alpha(self.fade_alpha).unwrap();
if let Err(e) = self.context.set_source_surface(surface, 0.0, 0.0) {
log::error!("cairo error: {:?}", e);
}
if let Err(e) = self.context.paint_with_alpha(self.fade_alpha) {
log::error!("cairo error: {:?}", e);
}
self.context.restore().unwrap();
}
@@ -508,10 +512,12 @@ impl Renderer {
(target_size / surface.width() as f64).min(target_size / surface.height() as f64);
self.context.translate(x, y);
self.context.scale(scale, scale);
self.context.set_source_surface(surface, 0.0, 0.0).unwrap();
self.context
.paint_with_alpha(self.fade_alpha * alpha)
.unwrap();
if let Err(e) = self.context.set_source_surface(surface, 0.0, 0.0) {
log::error!("cairo error: {:?}", e);
}
if let Err(e) = self.context.paint_with_alpha(self.fade_alpha * alpha) {
log::error!("cairo error: {:?}", e);
}
self.context.restore().unwrap();
}
@@ -529,11 +535,11 @@ impl Renderer {
self.context.set_source_rgba(1.0, 1.0, 1.0, alpha * 0.5);
self.context.set_line_width(2.0);
self.context.rectangle(x, y, width, height);
self.context.stroke().unwrap();
render_try!(self.context.stroke());
self.context.new_path();
self.context
.rectangle(x + width, y + height / 4.0, 3.0, height / 2.0);
self.context.fill().unwrap();
render_try!(self.context.fill());
let fill_width = (width - 4.0) * (percent / 100.0);
self.context.new_path();
if percent < 20.0 {
@@ -543,7 +549,7 @@ impl Renderer {
}
self.context
.rectangle(x + 2.0, y + 2.0, fill_width, height - 4.0);
self.context.fill().unwrap();
render_try!(self.context.fill());
if charging {
self.context.new_path();
self.context.set_source_rgba(1.0, 1.0, 0.0, alpha);
@@ -556,7 +562,7 @@ impl Renderer {
self.context.line_to(bx - 1.0, by - 3.0);
self.context.line_to(bx + 1.0, by - 3.0);
self.context.close_path();
self.context.fill().unwrap();
render_try!(self.context.fill());
}
}
}
+12 -235
View File
@@ -4,7 +4,7 @@
use anyhow::{Context, Result};
use cairo::ImageSurface;
use log::debug;
use log::warn;
use smithay_client_toolkit::shm::{slot::Buffer, slot::SlotPool};
use std::sync::Mutex;
use wayland_client::globals::GlobalList;
@@ -36,246 +36,19 @@ impl Screenshot {
/// Apply configured visual effects to the screenshot.
pub fn apply_effects(&mut self, config: &Config) -> Result<()> {
if let Some((radius, times)) = config.effect_blur {
self.apply_blur(radius, times)?;
crate::effects::apply_blur(&mut self.surface, radius, times)?;
}
if let Some((base, factor)) = config.effect_vignette {
self.apply_vignette(base, factor);
crate::effects::apply_vignette(&mut self.surface, base, factor)?;
}
if let Some(pixel_size) = config.effect_pixelate {
self.apply_pixelate(pixel_size);
crate::effects::apply_pixelate(&mut self.surface, pixel_size)?;
}
if let Some(angle) = config.effect_swirl {
self.apply_swirl(angle);
}
if let Some(factor) = config.effect_melting {
self.apply_melting(factor);
crate::effects::apply_swirl(&mut self.surface, angle)?;
}
Ok(())
}
/// Apply a swirl effect.
pub fn apply_swirl(&mut self, angle: f32) {
let width = self.surface.width();
let height = self.surface.height();
let center_x = width as f32 / 2.0;
let center_y = height as f32 / 2.0;
let radius = center_x.min(center_y);
let stride = self.surface.stride() as usize;
let mut data = vec![0u8; stride * height as usize];
self.surface
.with_data(|src| data.copy_from_slice(src))
.unwrap();
let original = data.clone();
for y in 0..height {
for x in 0..width {
let dx = x as f32 - center_x;
let dy = y as f32 - center_y;
let d = (dx * dx + dy * dy).sqrt();
if d < radius {
let percent = (radius - d) / radius;
let theta = percent * percent * angle;
let s = theta.sin();
let c = theta.cos();
let nx = (c * dx - s * dy + center_x) as i32;
let ny = (s * dx + c * dy + center_y) as i32;
if nx >= 0 && nx < width && ny >= 0 && ny < height {
let src_idx = (ny as usize * stride) + (nx as usize * 4);
let dst_idx = (y as usize * stride) + (x as usize * 4);
data[dst_idx..dst_idx + 4].copy_from_slice(&original[src_idx..src_idx + 4]);
}
}
}
}
let mut surface_data = self.surface.data().unwrap();
surface_data.copy_from_slice(&data);
}
/// Apply a melting effect (vertical smear).
pub fn apply_melting(&mut self, factor: f32) {
let width = self.surface.width();
let height = self.surface.height();
let stride = self.surface.stride() as usize;
let mut data = vec![0u8; stride * height as usize];
self.surface
.with_data(|src| data.copy_from_slice(src))
.unwrap();
use rand::Rng;
let mut rng = rand::thread_rng();
for x in 0..width {
let mut melt_amount = 0.0;
for y in 0..height {
melt_amount += rng.gen_range(0.0..factor);
let src_y = (y as f32 - melt_amount).max(0.0) as i32;
let src_idx = (src_y as usize * stride) + (x as usize * 4);
let dst_idx = (y as usize * stride) + (x as usize * 4);
// Copy the pixel from above to create a smear
let pixel = [
data[src_idx],
data[src_idx + 1],
data[src_idx + 2],
data[src_idx + 3],
];
data[dst_idx..dst_idx + 4].copy_from_slice(&pixel);
}
}
let mut surface_data = self.surface.data().unwrap();
surface_data.copy_from_slice(&data);
}
/// Pixelate the surface.
pub fn apply_pixelate(&mut self, pixel_size: u32) {
if pixel_size <= 1 {
return;
}
let width = self.surface.width();
let height = self.surface.height();
let stride = self.surface.stride() as usize;
let mut data = vec![0u8; stride * height as usize];
self.surface
.with_data(|src| data.copy_from_slice(src))
.unwrap();
for y in (0..height).step_by(pixel_size as usize) {
for x in (0..width).step_by(pixel_size as usize) {
let mut r = 0u32;
let mut g = 0u32;
let mut b = 0u32;
let mut count = 0u32;
// Average pixels in the block
for py in 0..pixel_size {
for px in 0..pixel_size {
let cur_x = x + px as i32;
let cur_y = y + py as i32;
if cur_x < width && cur_y < height {
let index = (cur_y as usize * stride) + (cur_x as usize * 4);
r += data[index] as u32;
g += data[index + 1] as u32;
b += data[index + 2] as u32;
count += 1;
}
}
}
if count > 0 {
let r = (r / count) as u8;
let g = (g / count) as u8;
let b = (b / count) as u8;
// Fill the block
for py in 0..pixel_size {
for px in 0..pixel_size {
let cur_x = x + px as i32;
let cur_y = y + py as i32;
if cur_x < width && cur_y < height {
let index = (cur_y as usize * stride) + (cur_x as usize * 4);
data[index] = r;
data[index + 1] = g;
data[index + 2] = b;
}
}
}
}
}
}
let mut surface_data = self.surface.data().unwrap();
surface_data.copy_from_slice(&data);
}
/// Apply a Gaussian blur effect.
pub fn apply_blur(&mut self, radius: u32, times: u32) -> Result<()> {
if radius == 0 || times == 0 {
return Ok(());
}
let width = self.surface.width();
let height = self.surface.height();
let stride = self.surface.stride() as usize;
let mut data = vec![0u8; stride * height as usize];
self.surface
.with_data(|src| data.copy_from_slice(src))
.context("Failed to get surface data")?;
// Convert to image::RgbaImage for processing
let mut img: image::ImageBuffer<image::Rgba<u8>, Vec<u8>> =
image::ImageBuffer::from_raw(width as u32, height as u32, data)
.context("Failed to create image buffer")?;
for _ in 0..times {
let mut rgb_data: Vec<[u8; 3]> =
Vec::with_capacity((width as usize) * (height as usize));
for pixel in img.pixels() {
rgb_data.push([pixel[0], pixel[1], pixel[2]]);
}
fastblur::gaussian_blur(
&mut rgb_data,
width as usize,
height as usize,
radius as f32,
);
for (i, pixel) in img.pixels_mut().enumerate() {
pixel[0] = rgb_data[i][0];
pixel[1] = rgb_data[i][1];
pixel[2] = rgb_data[i][2];
}
}
// Copy back to surface
let new_data = img.into_raw();
let mut surface_data = self.surface.data()?;
surface_data.copy_from_slice(&new_data);
Ok(())
}
/// Apply a vignette effect (darken edges).
pub fn apply_vignette(&mut self, base: f32, factor: f32) {
let width = self.surface.width();
let height = self.surface.height();
let center_x = width as f32 / 2.0;
let center_y = height as f32 / 2.0;
let max_distance = (center_x * center_x + center_y * center_y).sqrt();
let stride = self.surface.stride() as usize;
let mut data = vec![0u8; stride * height as usize];
self.surface
.with_data(|src| data.copy_from_slice(src))
.unwrap();
for y in 0..height {
for x in 0..width {
let dx = x as f32 - center_x;
let dy = y as f32 - center_y;
let distance = (dx * dx + dy * dy).sqrt();
let vignette_factor = base + (1.0 - base) * (distance / max_distance).powf(factor);
let index = ((y * width + x) * 4) as usize;
for i in 0..3 {
let value = data[index + i] as f32 * vignette_factor;
data[index + i] = value.clamp(0.0, 255.0) as u8;
}
}
}
let mut surface_data = self.surface.data().unwrap();
surface_data.copy_from_slice(&data);
}
}
#[derive(Clone)]
@@ -312,7 +85,7 @@ impl ScreenshotManager {
.ok();
if manager.is_none() {
debug!("zwlr_screencopy_manager_v1 not available");
warn!("zwlr_screencopy_manager_v1 not available — backgrounds will not be captured");
}
Ok(Self { manager })
@@ -361,11 +134,15 @@ impl ScreenshotManager {
let raw_data = {
let mut data = vec![0u8; (info.width * info.height * 4) as usize];
let canvas_end = canvas.len();
for row in 0..height {
let src_offset = row * stride;
let dst_offset = row * pixel_width;
data[dst_offset..dst_offset + pixel_width]
.copy_from_slice(&canvas[src_offset..src_offset + pixel_width]);
let copy_end = (src_offset + pixel_width).min(canvas_end);
if copy_end > src_offset {
data[dst_offset..dst_offset + pixel_width]
.copy_from_slice(&canvas[src_offset..copy_end]);
}
}
data
};
+86 -49
View File
@@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use zbus::Connection;
#[derive(Clone, Default)]
#[derive(Clone, Default, PartialEq)]
pub struct SystemStatus {
pub battery_percent: Option<f64>,
pub is_charging: bool,
@@ -21,22 +21,30 @@ pub struct SystemStatus {
}
#[derive(Debug, Clone, Copy)]
pub enum SystemCommand {
pub enum BackendCommand {
PowerOff,
Reboot,
Suspend,
MediaPlayPause,
MediaStop,
MediaNext,
MediaPrev,
}
pub struct SystemManager {
status: Arc<Mutex<SystemStatus>>,
cmd_tx: mpsc::UnboundedSender<SystemCommand>,
cmd_tx: mpsc::UnboundedSender<BackendCommand>,
}
impl SystemManager {
pub fn new() -> Self {
pub fn new(config: &crate::config::Config) -> Self {
let poll_interval = tokio::time::Duration::from_secs(config.system_poll_interval);
let reconnect_delay = tokio::time::Duration::from_secs(config.dbus_reconnect_delay);
let command_timeout = tokio::time::Duration::from_secs(config.command_timeout);
let status = Arc::new(Mutex::new(SystemStatus::default()));
let s_clone = status.clone();
let (cmd_tx, mut cmd_rx) = mpsc::unbounded_channel::<SystemCommand>();
let (cmd_tx, mut cmd_rx) = mpsc::unbounded_channel::<BackendCommand>();
std::thread::spawn(move || {
let rt = match tokio::runtime::Runtime::new() {
@@ -49,7 +57,7 @@ impl SystemManager {
rt.block_on(async {
let mut conn: Option<Connection> = None;
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(2));
let mut interval = tokio::time::interval(poll_interval);
let mut last_art_url: Option<String> = None;
let mut last_art_data: Option<Arc<Vec<u8>>> = None;
@@ -59,7 +67,9 @@ impl SystemManager {
Ok(c) => conn = Some(c),
Err(e) => {
error!("Failed to connect to system DBus: {}", e);
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
tokio::time::sleep(reconnect_delay).await;
interval = tokio::time::interval(poll_interval);
continue;
}
}
}
@@ -244,26 +254,69 @@ impl SystemManager {
}
}
}
Some(command) = cmd_rx.recv() => {
if let Some(ref c) = conn {
let method = match command {
SystemCommand::PowerOff => "PowerOff",
SystemCommand::Reboot => "Reboot",
SystemCommand::Suspend => "Suspend",
};
debug!("Executing system command: {}", method);
let result = tokio::time::timeout(
tokio::time::Duration::from_secs(5),
c.call_method(
Some("org.freedesktop.login1"),
"/org/freedesktop/login1",
Some("org.freedesktop.login1.Manager"),
method,
&(true),
)
).await;
if result.is_err() {
error!("System command {} timed out", method);
Some(cmd) = cmd_rx.recv() => {
match cmd {
BackendCommand::PowerOff
| BackendCommand::Reboot
| BackendCommand::Suspend => {
if let Some(ref c) = conn {
// Safety: only PowerOff/Reboot/Suspend reach this
// branch due to the outer match arm.
let method = match cmd {
BackendCommand::PowerOff => "PowerOff",
BackendCommand::Reboot => "Reboot",
BackendCommand::Suspend => "Suspend",
BackendCommand::MediaPlayPause
| BackendCommand::MediaStop
| BackendCommand::MediaNext
| BackendCommand::MediaPrev => {
unreachable!("media command in power branch: {:?}", cmd)
}
};
debug!("Executing system command: {}", method);
let result = tokio::time::timeout(
command_timeout,
c.call_method(
Some("org.freedesktop.login1"),
"/org/freedesktop/login1",
Some("org.freedesktop.login1.Manager"),
method,
&(true),
)
).await;
if result.is_err() {
error!("System command {} timed out", method);
}
}
}
BackendCommand::MediaPlayPause
| BackendCommand::MediaStop
| BackendCommand::MediaNext
| BackendCommand::MediaPrev => {
let action = cmd;
// Fire-and-forget: don't block the polling loop on MPRIS.
tokio::task::spawn_blocking(move || {
match PlayerFinder::new() {
Ok(finder) => {
match finder.find_active() {
Ok(player) => {
let result = match action {
BackendCommand::MediaPlayPause => player.play_pause(),
BackendCommand::MediaStop => player.stop(),
BackendCommand::MediaNext => player.next(),
BackendCommand::MediaPrev => player.previous(),
_ => Ok(()),
};
if let Err(e) = result {
error!("MPRIS command {action:?} failed: {e}");
}
}
Err(e) => error!("No active MPRIS player found: {e}"),
}
}
Err(e) => error!("Failed to create MPRIS PlayerFinder: {e}"),
}
});
}
}
}
@@ -276,42 +329,26 @@ impl SystemManager {
}
pub fn get_status(&self) -> SystemStatus {
self.status.lock().unwrap().clone()
self.status.lock().map(|s| s.clone()).unwrap_or_default()
}
pub fn send_command(&self, cmd: SystemCommand) {
pub fn send_command(&self, cmd: BackendCommand) {
let _ = self.cmd_tx.send(cmd);
}
pub fn media_play_pause(&self) {
if let Ok(finder) = PlayerFinder::new() {
if let Ok(player) = finder.find_active() {
let _ = player.play_pause();
}
}
let _ = self.cmd_tx.send(BackendCommand::MediaPlayPause);
}
pub fn media_stop(&self) {
if let Ok(finder) = PlayerFinder::new() {
if let Ok(player) = finder.find_active() {
let _ = player.stop();
}
}
let _ = self.cmd_tx.send(BackendCommand::MediaStop);
}
pub fn media_next(&self) {
if let Ok(finder) = PlayerFinder::new() {
if let Ok(player) = finder.find_active() {
let _ = player.next();
}
}
let _ = self.cmd_tx.send(BackendCommand::MediaNext);
}
pub fn media_prev(&self) {
if let Ok(finder) = PlayerFinder::new() {
if let Ok(player) = finder.find_active() {
let _ = player.previous();
}
}
let _ = self.cmd_tx.send(BackendCommand::MediaPrev);
}
}