Skip to content

feat: Linux播放时的休眠抑制#2773

Open
ling0412 wants to merge 5 commits intoopen-ani:mainfrom
ling0412:main
Open

feat: Linux播放时的休眠抑制#2773
ling0412 wants to merge 5 commits intoopen-ani:mainfrom
ling0412:main

Conversation

@ling0412
Copy link

@ling0412 ling0412 commented Jan 17, 2026

播放视频时阻止熄屏/休眠。linux下的休眠抑制情况比较复杂,最后选择了systemd-inhibitorg.freedesktop.ScreenSaver.Inhibit 并行的方案

只使用了systemd-inhibit, 这意味着只支持systemd发行版。

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request implements sleep prevention functionality for Linux desktop platforms during video playback. The implementation uses a dual-pronged approach combining systemd-inhibit and org.freedesktop.ScreenSaver D-Bus API to ensure reliable screen saver and sleep prevention across different Linux desktop environments.

Changes:

  • Added LinuxWindowUtils class that implements AutoCloseable and provides screen saver prevention
  • Implemented dual inhibition mechanism using systemd-inhibit for system sleep and D-Bus ScreenSaver API for display management
  • Added shutdown hook mechanism for cleanup of inhibition resources on JVM shutdown

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ling0412 ling0412 force-pushed the main branch 3 times, most recently from db49533 to 77a478f Compare January 18, 2026 03:02
@StageGuard
Copy link
Member

@WoLeo-Z 你有时间测试一下吗,我记得你用 Linux 来着

@WoLeo-Z
Copy link
Member

WoLeo-Z commented Jan 19, 2026

在我的环境下出现非预期行为,表现为 inhibit 了但是无法 uninhibit

我的一些拙劣的 debug

图片 图片

似乎是 cookie 错误. 我不太了解这个 dbus 接口

GitHub 无法上传 .pcap 文件。

https://t.me/openani_dev/63158

你可以使用 bustle 打开。


一些信息:我通过 swayidletimeout 实现自动锁屏 & 休眠。使用 bustle 监视 dbus 连接。

$ cat 1.log | grep ScreenSaver
2026-01-19 20:56:57,499 [INFO ] LinuxWindowUtils: [ScreenSaver] Inhibited (systemd + dbus)

link to #2764.

I approved gh action workflow. Built successfully on my machine.

Build with animeko-5.2.0 ❤️

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/be5afa0fcb31f0a96bf9ecba05a516c66fcd8114";

  outputs =
    { nixpkgs, ... }:
    let
      system = "x86_64-linux";
      overlayAnimekoPR2773 = final: prev: {
        animeko = prev.animeko.overrideAttrs (old: {
          patches = (old.patches or [ ]) ++ [
            (prev.fetchpatch {
              url = "https://github.com/open-ani/animeko/pull/2773.patch";
              sha256 = "sha256-02x5miYipOvMLzdEN8AeptAb5Zb7nJJhtKS8OqIkkxw=";
            })
          ];
        });
      };
      pkgs = import nixpkgs {
        inherit system;
        overlays = [ overlayAnimekoPR2773 ];
      };
    in
    {
      packages.${system}.animeko = pkgs.animeko;
    };
}

与此 PR 无关:顺便发现了一个无伤大雅的 dbus spam 噪声(图里的就是),间隔一秒轮询 color-scheme

busctl --user call :1.24 /org/freedesktop/portal/desktop org.freedesktop.impl.portal.Settings Read ss org.freedesktop.appearance color-scheme

可能由第三方库 com.jthemedetecor.OsThemeDetector 导致。

@ling0412
Copy link
Author

ling0412 commented Jan 19, 2026

似乎是 cookie 错误

谢谢你的测试,一样复现了错误,我对此dbus接口也没有足够的了解导致了实现错误。

根据 freedesktop 文档

Inhibition will stop when the UnInhibit method is called, or the application disconnects from the D-Bus session bus (which usually happens upon exit).

PR中使用了dbus-send 来试图完成功能, 但其实执行Inhibit后立马退出了,D-Bus 连接就断开了,屏保抑制也已经直接失效了,随后调用 UnInhibit 是没有意义的。

想要在不引入其他库的情况下自行实现一个长连接dbus比较困难 , 对于一个防熄屏功能来说有点太复杂了,
我也不想做一堆fallback了

我放弃实现org.freedesktop.ScreenSaver.Inhibit 删除了ScreenSaver相关部分
并专注于systemd-inhibit

这在我的所有desktop设备都可以工作,坏处就是只支持systemd发行版了

> systemd-inhibit --list
WHO               UID  USER PID    COMM            WHAT             WHY                                         MODE 
NetworkManager    0    root 1255   NetworkManager  sleep            NetworkManager needs to turn off networks   delay
Realtime Kit      0    root 1734   rtkit-daemon    sleep            Demote realtime scheduling and stop canary. delay
UPower            0    root 1840   upowerd         sleep            Pause device polling                        delay
AniVideoPlayer    1000 USER 431136 systemd-inhibit sleep:idle       Playing video                               block
DankMaterialShell 1000 USER 64527  dms             sleep            Lock before suspend                         delay
niri              1000 USER 1490   niri            handle-power-key Power key handling                          block

6 inhibitors listed.

@StageGuard
Copy link
Member

WHO 改成 Animeko 吧,这样比较容易辨识

@ling0412
Copy link
Author

WHO 改成 Animeko 吧,这样比较容易辨识

done

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 9 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants