refactor: Phase2 Task3 — ScreenPlugin重构为thin wrapper(通过DeviceCommand转发)

This commit is contained in:
showen
2026-03-13 07:40:06 +08:00
parent 5310a92633
commit bf41c4513f
4 changed files with 89 additions and 93 deletions

View File

@@ -1,109 +1,58 @@
//! ScreenPlugin — 屏幕管理
//! ScreenPlugin — 屏幕管理Thin Wrapper
//!
//! 唤醒锁systemd-inhibit、光标隐藏unclutter
//! 本插件现在是 DevicePlugin 的 thin wrapper通过 DeviceCommand 消息
//! 调用 DevicePlugin 实现防息屏和光标隐藏功能。
//!
//! 历史v0.1.0 直接调用 systemd-inhibit 和 unclutter
//! v0.2.0 迁移到 DevicePlugin2026-03-13
use crate::core::{message::Message, plugin::*};
use crate::core::{
message::{Destination, DeviceCommand, Envelope, Message},
plugin::*,
};
use anyhow::Result;
use std::process::{Child, Command, Stdio};
pub struct ScreenPlugin {
ctx: Option<PluginContext>,
wake_lock_child: Option<Child>,
cursor_hidden: bool,
}
impl ScreenPlugin {
pub fn new() -> Self {
Self {
ctx: None,
wake_lock_child: None,
cursor_hidden: false,
Self { ctx: None }
}
fn start_wake_lock(&self) {
if let Some(ctx) = &self.ctx {
let envelope = Envelope {
from: self.id().to_string(),
to: Destination::Plugin("device".to_string()),
message: Message::DeviceCommand(DeviceCommand::SetSleepInhibit(true)),
};
let _ = ctx.tx.send(envelope);
}
}
#[cfg(target_os = "linux")]
fn start_wake_lock(&mut self) {
if self.wake_lock_child.is_some() {
return;
}
match Command::new("systemd-inhibit")
.arg("--what=idle:sleep")
.arg("--mode=block")
.arg("--who=ShowenV2")
.arg("--why=Prevent screen lock during playback")
.arg("sleep")
.arg("infinity")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
{
Ok(child) => self.wake_lock_child = Some(child),
Err(err) => eprintln!("[ScreenPlugin] 启动防息屏失败: {err}"),
fn stop_wake_lock(&self) {
if let Some(ctx) = &self.ctx {
let envelope = Envelope {
from: self.id().to_string(),
to: Destination::Plugin("device".to_string()),
message: Message::DeviceCommand(DeviceCommand::SetSleepInhibit(false)),
};
let _ = ctx.tx.send(envelope);
}
}
#[cfg(not(target_os = "linux"))]
fn start_wake_lock(&mut self) {}
#[cfg(target_os = "linux")]
fn stop_wake_lock(&mut self) {
if let Some(mut child) = self.wake_lock_child.take() {
if let Err(err) = child.kill() {
eprintln!("[ScreenPlugin] 停止防息屏失败: {err}");
}
let _ = child.wait();
fn set_cursor_hidden(&self, hidden: bool) {
if let Some(ctx) = &self.ctx {
let envelope = Envelope {
from: self.id().to_string(),
to: Destination::Plugin("device".to_string()),
message: Message::DeviceCommand(DeviceCommand::SetCursorVisible(!hidden)),
};
let _ = ctx.tx.send(envelope);
}
}
#[cfg(not(target_os = "linux"))]
fn stop_wake_lock(&mut self) {}
#[cfg(target_os = "linux")]
fn set_cursor_hidden(&mut self, hidden: bool) {
if hidden == self.cursor_hidden {
return;
}
if hidden {
let _ = Command::new("pkill")
.args(["-f", "unclutter"])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
match Command::new("unclutter")
.arg("-idle")
.arg("0")
.arg("-root")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
{
Ok(_) => self.cursor_hidden = true,
Err(err) => eprintln!("[ScreenPlugin] 隐藏光标失败: {err}"),
}
} else {
match Command::new("pkill")
.args(["-f", "unclutter"])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
{
Ok(_) => self.cursor_hidden = false,
Err(err) => eprintln!("[ScreenPlugin] 恢复光标失败: {err}"),
}
}
}
#[cfg(not(target_os = "linux"))]
fn set_cursor_hidden(&mut self, hidden: bool) {
self.cursor_hidden = hidden;
}
}
impl Default for ScreenPlugin {
@@ -121,13 +70,13 @@ impl Plugin for ScreenPlugin {
PluginInfo {
name: "Screen Manager".to_string(),
version: "0.2.0".to_string(),
description: "屏幕唤醒锁 + 光标管理".to_string(),
platform: Platform::Linux,
description: "屏幕唤醒锁 + 光标管理Thin Wrapper".to_string(),
platform: Platform::Any,
}
}
fn dependencies(&self) -> Vec<String> {
vec![]
vec!["device".to_string()]
}
fn init(&mut self, ctx: PluginContext) -> Result<()> {