refactor: Phase2 Task3 — ScreenPlugin重构为thin wrapper(通过DeviceCommand转发)
This commit is contained in:
@@ -364,11 +364,11 @@ fn video_plugin_must_have_no_dependencies() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn screen_plugin_must_have_no_dependencies() {
|
||||
fn screen_plugin_must_depend_on_device() {
|
||||
use crate::plugins::screen::ScreenPlugin;
|
||||
let plugin = ScreenPlugin::new();
|
||||
let deps = plugin.dependencies();
|
||||
assert!(deps.is_empty(), "screen plugin must have no dependencies");
|
||||
assert_eq!(deps, vec!["device"], "screen plugin must depend on device");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -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 迁移到 DevicePlugin(2026-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<()> {
|
||||
|
||||
Reference in New Issue
Block a user