Swayidle alternative to handle wayland idle notifications, sleep and lock events in Rust with Lua scripting based configuration language

Overview

swayidle-rs

This is intended as a replacement of sway's idle management daemon. I use it as a tool to understand rust message passing and state management. It aims to have the following goals:

  • automatic setup of xdg config folder
  • lua scripting to define idle management with a lua config
  • automatic script reloading
  • handling of AC and battery based idle timeouts
  • screen locking with systemd locking

I am now using it as a replacement for any ac and battery power daemon, since I can run tasks with it.

Install

Until packaging is added you can install sleepwatcher using cargo.

cargo install --git https://github.com/fishman/sleepwatcher-rs

Debug

sleepwatcher-rs uses env_logger. You can enable logging by setting the RUST_LOG environment variable:

RUST_LOG=debug sleepwatcher-rs

Default config

The default config is written to ~/.config/sleepwatcher-rs/idle_config.lua on startup if the folder and file does not exist yet.

Syntax

Lua is configured to be sandboxed, so no library functions can be used and only functions exposed inside the Rust can be used.

Important distinction between Helpers:run and Helpers:run_once. run_once will check if a process of that name is already running and won't spawn a new one in that case. This may be useful, when a screen locker can create race conditions if spawned twice.

Originally I wanted to reload the config whenever the AC adaptor is plugged in and out, but due to the timeout issue described below, you can check for the on_battery state in functions.

Helpers:log("Loading idle_config.lua")

function LockScreen()
  Helpers:log("Locking Screen")
  IdleNotifier:run_once("swaylock -f")
end

function ScreenLockBattery(event)
  if event == "idled" and Helpers:on_battery() then
    LockScreen()
  end
end

DbusHandler:PrepareSleep("LockScreen")
DbusHandler:LockHandler("LockHandler")
DbusHandler:UnlockHandler("UnlockHandler")
IdleNotifier:get_notification(300,  "ScreenLockBattery")

get_notification creates a Wayland idle timeout handler. It uses the ext-idle-notify-v1 protocol. It is not (yet) possible to create callback functions, so the function calls are made by specifying the name of the function.

PrepareSleep, LockScreen, UnlockScreen, are dbus signals from the org.freedesktop.logind.manager and org.freedesktop.logind.session.

Known issues

  • sleepwatcher-rs should automatically reload the config when ~/.config/sleepwatcher-rs/idle_config.lua is changed. However, due to an unknown reason the first trigger after reload still follows the old timeout and the next trigger is therefore equal to the rest of the previous timeout+the new timeout setting.

TODOS

  • build CI
  • AUR package
  • use more idiomatic lua syntax
  • add tests
  • enable callbacks for lua code. it currently complains about lua functions not supporting Send. It's related to the way the user methods are implemented
  • duplicate state information
  • find a way to remove the std:🧡:spawn that used within the lua thread to run_once
  • add idle hints
  • improve return and error handling
  • turn wayland_run spawn_blocking into async
You might also like...
xdotool-like for KDE Wayland

kdotool - a xdotool clone for KDE Wayland Introduction Wayland, for security concerns, removed most of the X11 APIs that xdotool uses to simulate user

A scripting language that allows complex key remapping on Linux.

Map2 A scripting language that allows complex key remapping on Linux, written in Rust. All of the functionality related to interacting with graphical

Quinine is a Rust library that implements atomic, lock-free, but write-once versions of containers like `Box` or `Arc`

Quinine is a Rust library that implements atomic, lock-free, but write-once versions of containers like `Box` or `Arc`

A dynamic output configuration tool that automatically detects and configures connected outputs based on a set of profiles.

shikane A dynamic output configuration tool that automatically detects and configures connected outputs based on a set of profiles. Each profile speci

A lock-free thread-owned queue whereby tasks are taken by stealers in entirety via buffer swapping

Swap Queue A lock-free thread-owned queue whereby tasks are taken by stealers in entirety via buffer swapping. This is meant to be used [thread_local]

global allocator that provides hooks for tracking allocation events

tracking-allocator A GlobalAlloc-compatible allocator implementation that provides the ability to track allocation events. examples As allocators are

Simple async library for triggering IFTTT events using webhooks.

IFTTT Webhook A simple Rust async library for triggering IFTTT events using webhooks. Installation Installation can be performed using cargo add: carg

πŸ₯… Dead simple webhook worker for Sentry to output events in a Discord channel
πŸ₯… Dead simple webhook worker for Sentry to output events in a Discord channel

πŸ₯… Sentry Webhook Dead simple webhook worker for Sentry to output events in a Discord channel Why? This is just a simple Rust HTTP service to do so, t

Recognize gestures by touch events

Gesture Recognizer This crate provides abstract API to recognize and handle simple gestures. At now three type of gestures are supported: Move by one

Owner
Reza Jelveh
Reza Jelveh
Rust implementation for Wlroots (Sway, Wayfire, Hikari, River, etc.) of Gnome Screenshot and Idle DBUS Server, which Upwork uses to capture the screen as proof of work.

?? upwork-wlroots-bridge ?? Rust Implementation for Wlroots (Sway, Wayfire, Hikari, River, etc.) of Gnome Screenshot and Idle DBUS Server (with extra

Daniel Moretti V. 4 Jan 2, 2023
A language server for lua written in rust

lua-analyzer lua-analyzer is a lsp server for lua. This is mostly for me to learn the lsp protocol and language analysis so suggestions are helpful. T

null 61 Dec 11, 2022
Dynamic key remapper for X11 and Wayland

???????????? ⌨️ xremap is a key remapper for Linux. Unlike xmodmap, it supports app-specific remapping and Wayland. Concept Fast - Xremap is written i

Takashi Kokubun 643 Jan 8, 2023
Steno for Wayland

WayPlover Steno for Wayland Description A Steno Stroke Interpreter for Wayland. Open Steno Project Learn Plover Usage wayplover --port /dev/ttyACM0 --

Travis Davis 9 Oct 23, 2022
A wallpaper daemon for Wayland compositors

Paper A wallpaper daemon for Wayland compositors implementing the layer-shell protocol. Features Supports png and jpg format Tiled wallpapers Bordered

snakedye 20 Nov 26, 2022
Turn off monitors to save power (for Wayland)

Same as xset dpms force off, but for Wayland. It requires zwlr_output_power_manager_v1 and org_kde_kwin_idle support from the Wayland compositer. wlro

依云 15 Dec 8, 2022
Wallpaper daemon for Wayland

wpaperd wpaperd is a minimal wallpaper daemon for Wayland. It allows the user to choose a different image for each output (aka for each monitor) just

Danilo Spinella 53 Dec 28, 2022
Default implementation of the Wayland protocol for use with wl

Wayland An implementation of core Wayland interfaces and convenience functions for accelerating the development of Wayland clients and servers using t

AidoP 1 Jan 24, 2022
A simple clipboard manager for wayland.

Wpilman A simple clipboard manager for wayland. Installation Just compile it yourself or install the AUR package: paru -S wlipman-git # or yay -S wlip

null 3 Jan 13, 2023
Wayland clipboard manager that will make you clap πŸ‘

Clapboard - clipboard manager that makes you clap ?? Clapboard is a simple clipboard manager for Wayland, built in Rust. It saves a history of your cl

Yo'av Moshe 18 Jan 28, 2023