mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-03-17 00:16:50 +08:00
init
This commit is contained in:
130
src/hid/backend.rs
Normal file
130
src/hid/backend.rs
Normal file
@@ -0,0 +1,130 @@
|
||||
//! HID backend trait definition
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::types::{KeyboardEvent, MouseEvent};
|
||||
use crate::error::Result;
|
||||
|
||||
/// Default CH9329 baud rate
|
||||
fn default_ch9329_baud_rate() -> u32 {
|
||||
9600
|
||||
}
|
||||
|
||||
/// HID backend type
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "lowercase")]
|
||||
pub enum HidBackendType {
|
||||
/// USB OTG gadget mode
|
||||
Otg,
|
||||
/// CH9329 serial HID controller
|
||||
Ch9329 {
|
||||
/// Serial port path
|
||||
port: String,
|
||||
/// Baud rate (default: 9600)
|
||||
#[serde(default = "default_ch9329_baud_rate")]
|
||||
baud_rate: u32,
|
||||
},
|
||||
/// No HID backend (disabled)
|
||||
None,
|
||||
}
|
||||
|
||||
impl Default for HidBackendType {
|
||||
fn default() -> Self {
|
||||
Self::None
|
||||
}
|
||||
}
|
||||
|
||||
impl HidBackendType {
|
||||
/// Check if OTG backend is available on this system
|
||||
pub fn otg_available() -> bool {
|
||||
// Check for USB gadget support
|
||||
std::path::Path::new("/sys/class/udc").exists()
|
||||
}
|
||||
|
||||
/// Detect the best available backend
|
||||
pub fn detect() -> Self {
|
||||
// Check for OTG gadget support
|
||||
if Self::otg_available() {
|
||||
return Self::Otg;
|
||||
}
|
||||
|
||||
// Check for common CH9329 serial ports
|
||||
let common_ports = [
|
||||
"/dev/ttyUSB0",
|
||||
"/dev/ttyUSB1",
|
||||
"/dev/ttyAMA0",
|
||||
"/dev/serial0",
|
||||
];
|
||||
|
||||
for port in &common_ports {
|
||||
if std::path::Path::new(port).exists() {
|
||||
return Self::Ch9329 {
|
||||
port: port.to_string(),
|
||||
baud_rate: 9600, // Use default baud rate for auto-detection
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Self::None
|
||||
}
|
||||
|
||||
/// Get backend name as string
|
||||
pub fn name_str(&self) -> &str {
|
||||
match self {
|
||||
Self::Otg => "otg",
|
||||
Self::Ch9329 { .. } => "ch9329",
|
||||
Self::None => "none",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// HID backend trait
|
||||
#[async_trait]
|
||||
pub trait HidBackend: Send + Sync {
|
||||
/// Get backend name
|
||||
fn name(&self) -> &'static str;
|
||||
|
||||
/// Initialize the backend
|
||||
async fn init(&self) -> Result<()>;
|
||||
|
||||
/// Send a keyboard event
|
||||
async fn send_keyboard(&self, event: KeyboardEvent) -> Result<()>;
|
||||
|
||||
/// Send a mouse event
|
||||
async fn send_mouse(&self, event: MouseEvent) -> Result<()>;
|
||||
|
||||
/// Reset all inputs (release all keys/buttons)
|
||||
async fn reset(&self) -> Result<()>;
|
||||
|
||||
/// Shutdown the backend
|
||||
async fn shutdown(&self) -> Result<()>;
|
||||
|
||||
/// Check if backend supports absolute mouse positioning
|
||||
fn supports_absolute_mouse(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Get screen resolution (for absolute mouse)
|
||||
fn screen_resolution(&self) -> Option<(u32, u32)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Set screen resolution (for absolute mouse)
|
||||
fn set_screen_resolution(&mut self, _width: u32, _height: u32) {}
|
||||
}
|
||||
|
||||
/// HID backend information
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct HidBackendInfo {
|
||||
/// Backend name
|
||||
pub name: String,
|
||||
/// Backend type
|
||||
pub backend_type: String,
|
||||
/// Is initialized
|
||||
pub initialized: bool,
|
||||
/// Supports absolute mouse
|
||||
pub absolute_mouse: bool,
|
||||
/// Screen resolution (if absolute mouse)
|
||||
pub resolution: Option<(u32, u32)>,
|
||||
}
|
||||
Reference in New Issue
Block a user