First working version with all options except grace
This commit is contained in:
+17
-13
@@ -57,6 +57,10 @@ pub struct Config {
|
||||
#[arg(long)]
|
||||
pub debug: bool,
|
||||
|
||||
/// Write verbose logs to ~/.wayrustlock.log
|
||||
#[arg(long)]
|
||||
pub log_file: bool,
|
||||
|
||||
/// Show screen temporarily when a key is pressed (like swaylock-effects peek)
|
||||
#[arg(long)]
|
||||
pub temp_screenshot: bool,
|
||||
@@ -64,23 +68,23 @@ pub struct Config {
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Self {
|
||||
let mut config = Config::parse();
|
||||
let cli_config = Config::parse();
|
||||
|
||||
// Use path from CLI if provided, otherwise default
|
||||
let config_path = cli_config.config.clone().unwrap_or_else(|| {
|
||||
let mut path = std::path::PathBuf::from(std::env::var("HOME").unwrap_or_default());
|
||||
path.push(".config/wayrustlock/config.toml");
|
||||
path
|
||||
});
|
||||
|
||||
if let Some(config_path) = &config.config {
|
||||
if let Ok(file_content) = std::fs::read_to_string(config_path) {
|
||||
if let Ok(file_config) = toml::from_str::<Config>(&file_content) {
|
||||
config = file_config;
|
||||
} else {
|
||||
eprintln!(
|
||||
"Warning: Failed to parse config file {}",
|
||||
config_path.display()
|
||||
);
|
||||
if config_path.exists() {
|
||||
if let Ok(file_content) = std::fs::read_to_string(&config_path) {
|
||||
if let Ok(_file_config) = toml::from_str::<Config>(&file_content) {
|
||||
return cli_config;
|
||||
}
|
||||
} else {
|
||||
eprintln!("Warning: Config file {} not found", config_path.display());
|
||||
}
|
||||
}
|
||||
|
||||
config
|
||||
cli_config
|
||||
}
|
||||
}
|
||||
|
||||
+5
-16
@@ -9,6 +9,7 @@ pub struct InputHandler {
|
||||
key_highlight_timer: Option<std::time::Instant>,
|
||||
temp_screenshot_timer: Option<std::time::Instant>,
|
||||
temp_screenshot_active: bool,
|
||||
caps_lock: bool,
|
||||
}
|
||||
|
||||
impl InputHandler {
|
||||
@@ -21,6 +22,7 @@ impl InputHandler {
|
||||
key_highlight_timer: None,
|
||||
temp_screenshot_timer: None,
|
||||
temp_screenshot_active: false,
|
||||
caps_lock: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +33,9 @@ impl InputHandler {
|
||||
state: wayland_client::protocol::wl_keyboard::KeyState,
|
||||
modifiers: smithay_client_toolkit::seat::keyboard::Modifiers,
|
||||
) -> InputAction {
|
||||
// Update Caps Lock state
|
||||
self.caps_lock = modifiers.caps_lock;
|
||||
|
||||
// Only process key press events
|
||||
if state != wayland_client::protocol::wl_keyboard::KeyState::Pressed {
|
||||
return InputAction::None;
|
||||
@@ -143,17 +148,6 @@ impl InputHandler {
|
||||
self.password_buffer.chars().map(|_| '•').collect()
|
||||
}
|
||||
|
||||
/// Get the actual password (for authentication)
|
||||
pub fn get_password(&self) -> Zeroizing<String> {
|
||||
self.password_buffer.clone()
|
||||
}
|
||||
|
||||
/// Clear the password buffer (e.g., after wrong password)
|
||||
pub fn clear_password(&mut self) {
|
||||
self.password_buffer.clear();
|
||||
self.cursor_position = 0;
|
||||
}
|
||||
|
||||
/// Set wrong password feedback timer
|
||||
pub fn set_wrong_password_feedback(&mut self) {
|
||||
self.wrong_password_timer = Some(std::time::Instant::now());
|
||||
@@ -206,11 +200,6 @@ impl InputHandler {
|
||||
false
|
||||
}
|
||||
|
||||
/// Check if temp screenshot is currently active
|
||||
pub fn is_temp_screenshot_active(&self) -> bool {
|
||||
self.temp_screenshot_active
|
||||
}
|
||||
|
||||
/// Update temp screenshot state (call periodically)
|
||||
pub fn update_temp_screenshot(&mut self) {
|
||||
if self.temp_screenshot_active && !self.should_show_temp_screenshot() {
|
||||
|
||||
+70
-260
@@ -2,29 +2,28 @@ use cairo::ImageSurface;
|
||||
use std::error::Error;
|
||||
use std::time::Instant;
|
||||
use wayland_client::protocol::{wl_output, wl_shm, wl_surface};
|
||||
use wayland_client::Proxy;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::input::{InputAction, InputHandler};
|
||||
use crate::render::Renderer;
|
||||
use crate::screenshot::Screenshot;
|
||||
use smithay_client_toolkit::seat::keyboard::KeyEvent;
|
||||
use smithay_client_toolkit::shm::slot::SlotPool;
|
||||
|
||||
/// Manages a locked surface for a single output
|
||||
pub struct LockedSurface {
|
||||
width: i32,
|
||||
height: i32,
|
||||
config: Config,
|
||||
pub renderer: Renderer,
|
||||
input_handler: InputHandler,
|
||||
background: Option<ImageSurface>,
|
||||
background_applied: bool,
|
||||
fade_alpha: f64,
|
||||
wrong_password_shown: bool,
|
||||
key_highlight_shown: bool,
|
||||
temp_screenshot_shown: bool,
|
||||
last_update: Instant,
|
||||
start_time: Instant,
|
||||
wayland_surface: Option<wl_surface::WlSurface>,
|
||||
output: wl_output::WlOutput,
|
||||
configured: bool,
|
||||
}
|
||||
|
||||
impl LockedSurface {
|
||||
@@ -42,26 +41,30 @@ impl LockedSurface {
|
||||
let renderer = Renderer::new(width, height, config.clone());
|
||||
let input_handler = InputHandler::new(config.clone());
|
||||
|
||||
// Background will be set later when screenshot is captured (if screenshots enabled)
|
||||
let background = None;
|
||||
|
||||
Some(Self {
|
||||
width,
|
||||
height,
|
||||
config: config.clone(),
|
||||
renderer,
|
||||
input_handler,
|
||||
background,
|
||||
background: None,
|
||||
background_applied: false,
|
||||
fade_alpha: 0.0,
|
||||
wrong_password_shown: false,
|
||||
key_highlight_shown: false,
|
||||
temp_screenshot_shown: false,
|
||||
last_update: Instant::now(),
|
||||
start_time: Instant::now(),
|
||||
wayland_surface: None,
|
||||
output,
|
||||
configured: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Set the configured state
|
||||
pub fn set_configured(&mut self) {
|
||||
log::debug!("LockedSurface: Configured, starting animation");
|
||||
self.configured = true;
|
||||
self.start_time = Instant::now();
|
||||
}
|
||||
|
||||
/// Check if this surface matches the given Wayland surface
|
||||
pub fn matches_surface(&self, surface: &wl_surface::WlSurface) -> bool {
|
||||
use wayland_client::Proxy;
|
||||
@@ -72,93 +75,77 @@ impl LockedSurface {
|
||||
|
||||
/// Update the surface state (called on each frame)
|
||||
pub fn update(&mut self) {
|
||||
log::debug!(
|
||||
"LockedSurface::update() called, background: {}",
|
||||
self.background.is_some()
|
||||
);
|
||||
|
||||
// Update timers
|
||||
self.input_handler.update();
|
||||
|
||||
if !self.configured {
|
||||
return;
|
||||
}
|
||||
|
||||
// Update fade animation
|
||||
if self.fade_alpha < 1.0 {
|
||||
let elapsed = self.last_update.elapsed();
|
||||
let elapsed = self.start_time.elapsed();
|
||||
let fade_duration = std::time::Duration::from_secs_f32(self.config.fade_in);
|
||||
self.fade_alpha = (elapsed.as_secs_f64() / fade_duration.as_secs_f64()).min(1.0);
|
||||
self.renderer.set_fade_alpha(self.fade_alpha);
|
||||
log::debug!("Fade alpha updated: {}", self.fade_alpha);
|
||||
let new_alpha = (elapsed.as_secs_f64() / fade_duration.as_secs_f64()).min(1.0);
|
||||
if (new_alpha - self.fade_alpha).abs() > 0.001 {
|
||||
self.fade_alpha = new_alpha;
|
||||
self.renderer.set_fade_alpha(self.fade_alpha);
|
||||
}
|
||||
}
|
||||
|
||||
// Update visual feedback
|
||||
// Check if we should show/hide wrong password feedback
|
||||
if self.input_handler.should_show_wrong_password() && !self.wrong_password_shown {
|
||||
self.renderer.show_wrong_password();
|
||||
self.wrong_password_shown = true;
|
||||
log::debug!("Showing wrong password feedback");
|
||||
} else if !self.input_handler.should_show_wrong_password() && self.wrong_password_shown {
|
||||
self.wrong_password_shown = false;
|
||||
log::debug!("Hiding wrong password feedback");
|
||||
}
|
||||
|
||||
// Check if we should show/hide key highlight feedback
|
||||
if self.input_handler.should_show_key_highlight() && !self.key_highlight_shown {
|
||||
self.renderer.show_key_highlight();
|
||||
self.key_highlight_shown = true;
|
||||
log::debug!("Showing key highlight");
|
||||
} else if !self.input_handler.should_show_key_highlight() && self.key_highlight_shown {
|
||||
self.key_highlight_shown = false;
|
||||
log::debug!("Hiding key highlight");
|
||||
}
|
||||
|
||||
// Handle temp screenshot (peek feature)
|
||||
// Check if we should show/hide temp screenshot
|
||||
if self.input_handler.should_show_temp_screenshot() && !self.temp_screenshot_shown {
|
||||
// When temp screenshot is active, we should show the actual screen
|
||||
// For now, we'll just set a different background alpha
|
||||
self.renderer.set_fade_alpha(0.3); // Semi-transparent
|
||||
self.renderer.set_fade_alpha(0.3);
|
||||
self.temp_screenshot_shown = true;
|
||||
log::debug!("Showing temp screenshot (peek)");
|
||||
} else if !self.input_handler.should_show_temp_screenshot() && self.temp_screenshot_shown {
|
||||
// Restore normal fade alpha
|
||||
self.renderer.set_fade_alpha(self.fade_alpha);
|
||||
self.temp_screenshot_shown = false;
|
||||
log::debug!("Restored normal fade alpha after peek");
|
||||
}
|
||||
|
||||
// Set background if available
|
||||
if let Some(ref background) = self.background {
|
||||
let size = (background.width(), background.height());
|
||||
log::info!(
|
||||
"✓ Applying background from self.background: {}x{}",
|
||||
size.0,
|
||||
size.1
|
||||
);
|
||||
self.renderer.set_background(background.clone());
|
||||
} else {
|
||||
log::warn!("✗ No background in self.background - will render solid color!");
|
||||
// Set background if available and not already applied
|
||||
if !self.background_applied {
|
||||
if let Some(ref background) = self.background {
|
||||
log::info!("Applying background image to renderer");
|
||||
self.renderer.set_background(background.clone());
|
||||
self.background_applied = true;
|
||||
}
|
||||
}
|
||||
|
||||
self.renderer
|
||||
.set_password_display(self.input_handler.get_display_password());
|
||||
|
||||
// Render the frame
|
||||
self.renderer.render();
|
||||
|
||||
self.last_update = Instant::now();
|
||||
}
|
||||
|
||||
/// Commit the rendered frame to the Wayland surface
|
||||
pub fn commit(&self, pool: &mut SlotPool) -> Result<(), Box<dyn Error>> {
|
||||
// Get pixel data from renderer
|
||||
if !self.configured {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let pixel_data = self.renderer.get_pixel_data()?;
|
||||
let (width, height, stride) = self.renderer.surface_info();
|
||||
|
||||
// Create buffer from pool
|
||||
let (buffer, canvas) =
|
||||
pool.create_buffer(width, height, stride, wl_shm::Format::Argb8888)?;
|
||||
|
||||
// Copy pixel data to buffer
|
||||
let copy_len = pixel_data.len().min(canvas.len());
|
||||
canvas[..copy_len].copy_from_slice(&pixel_data[..copy_len]);
|
||||
|
||||
// Attach buffer to Wayland surface and commit
|
||||
if let Some(wl_surface) = &self.wayland_surface {
|
||||
buffer.attach_to(wl_surface)?;
|
||||
wl_surface.damage_buffer(0, 0, width, height);
|
||||
@@ -168,204 +155,70 @@ impl LockedSurface {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Handle resize event from Wayland
|
||||
pub fn resize(&mut self, width: i32, height: i32) {
|
||||
if width <= 0 || height <= 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
self.width = width;
|
||||
self.height = height;
|
||||
self.renderer.resize(width, height);
|
||||
|
||||
// TODO: Re-capture screenshot if screenshots are enabled
|
||||
self.background_applied = false;
|
||||
}
|
||||
|
||||
/// Set fade alpha for animation
|
||||
pub fn set_fade_alpha(&mut self, alpha: f64) {
|
||||
self.fade_alpha = alpha.clamp(0.0, 1.0);
|
||||
self.renderer.set_fade_alpha(self.fade_alpha);
|
||||
}
|
||||
|
||||
/// Show wrong password feedback
|
||||
pub fn show_wrong_password(&mut self) {
|
||||
self.input_handler.set_wrong_password_feedback();
|
||||
}
|
||||
|
||||
/// Show key highlight feedback
|
||||
pub fn show_key_highlight(&mut self) {
|
||||
self.input_handler.set_key_highlight();
|
||||
}
|
||||
|
||||
/// Handle a key event from Wayland
|
||||
pub fn handle_key_event(
|
||||
&mut self,
|
||||
event: smithay_client_toolkit::seat::keyboard::KeyEvent,
|
||||
) -> Option<InputAction> {
|
||||
// Convert to our input handler format
|
||||
// Note: KeyEvent has fields: time, raw_code, keysym, utf8
|
||||
// We need to determine state and modifiers from context (not available in this demo)
|
||||
// For demonstration, we'll assume key press with no modifiers
|
||||
let keysym = event.keysym;
|
||||
let state = wayland_client::protocol::wl_keyboard::KeyState::Pressed;
|
||||
let modifiers = smithay_client_toolkit::seat::keyboard::Modifiers::default();
|
||||
|
||||
let action = self
|
||||
.input_handler
|
||||
.handle_key_event(keysym, state, modifiers);
|
||||
let action = self.input_handler.handle_key_event(
|
||||
event.keysym,
|
||||
wayland_client::protocol::wl_keyboard::KeyState::Pressed,
|
||||
smithay_client_toolkit::seat::keyboard::Modifiers::default(),
|
||||
);
|
||||
|
||||
match action {
|
||||
InputAction::SubmitPassword(password) => {
|
||||
// Show key highlight for visual feedback
|
||||
self.show_key_highlight();
|
||||
Some(InputAction::SubmitPassword(password))
|
||||
InputAction::PasswordChanged => {
|
||||
self.input_handler.set_key_highlight();
|
||||
self.key_highlight_shown = false;
|
||||
}
|
||||
InputAction::Cancel => Some(InputAction::Cancel),
|
||||
InputAction::TempScreenshot => Some(InputAction::TempScreenshot),
|
||||
InputAction::PasswordChanged => Some(InputAction::PasswordChanged),
|
||||
InputAction::None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Authenticate a password using PAM
|
||||
pub fn authenticate_password(&self, password: zeroize::Zeroizing<String>) -> bool {
|
||||
// Create a simple PAM conversation that provides the password
|
||||
struct SimpleConversation {
|
||||
password: Option<zeroize::Zeroizing<String>>,
|
||||
InputAction::SubmitPassword(_) => {
|
||||
self.input_handler.set_key_highlight();
|
||||
self.key_highlight_shown = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
impl pam_client::ConversationHandler for SimpleConversation {
|
||||
fn init(&mut self, _default_user: Option<impl AsRef<str>>) {}
|
||||
|
||||
fn prompt_echo_on(
|
||||
&mut self,
|
||||
_msg: &std::ffi::CStr,
|
||||
) -> Result<std::ffi::CString, pam_client::ErrorCode> {
|
||||
Err(pam_client::ErrorCode::ABORT)
|
||||
}
|
||||
|
||||
fn prompt_echo_off(
|
||||
&mut self,
|
||||
_msg: &std::ffi::CStr,
|
||||
) -> Result<std::ffi::CString, pam_client::ErrorCode> {
|
||||
if let Some(pwd) = self.password.take() {
|
||||
std::ffi::CString::new(pwd.as_str()).map_err(|_| pam_client::ErrorCode::ABORT)
|
||||
} else {
|
||||
Err(pam_client::ErrorCode::ABORT)
|
||||
}
|
||||
}
|
||||
|
||||
fn text_info(&mut self, _msg: &std::ffi::CStr) {}
|
||||
fn error_msg(&mut self, _msg: &std::ffi::CStr) {}
|
||||
fn radio_prompt(
|
||||
&mut self,
|
||||
_msg: &std::ffi::CStr,
|
||||
) -> Result<bool, pam_client::ErrorCode> {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
// Get username
|
||||
let username = match users::get_current_username() {
|
||||
Some(name) => name.to_string_lossy().into_owned(),
|
||||
None => {
|
||||
log::error!("Failed to get current username");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Create PAM context
|
||||
let service_name = &self.config.pam_service;
|
||||
let conversation = SimpleConversation {
|
||||
password: Some(password),
|
||||
};
|
||||
|
||||
let mut context =
|
||||
match pam_client::Context::new(service_name, Some(username.as_str()), conversation) {
|
||||
Ok(ctx) => ctx,
|
||||
Err(e) => {
|
||||
log::error!("Failed to initialize PAM context: {:?}", e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Authenticate
|
||||
match context.authenticate(pam_client::Flag::NONE) {
|
||||
Ok(()) => {
|
||||
log::info!("PAM authentication successful for user {}", username);
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("PAM authentication failed: {:?}", e);
|
||||
false
|
||||
}
|
||||
}
|
||||
Some(action)
|
||||
}
|
||||
|
||||
/// Get the input handler for this locked surface
|
||||
pub fn input_handler(&self) -> &InputHandler {
|
||||
&self.input_handler
|
||||
}
|
||||
|
||||
/// Get the rendered image surface for this locked surface
|
||||
pub fn as_image_surface(&self) -> &ImageSurface {
|
||||
self.renderer.as_image_surface()
|
||||
}
|
||||
|
||||
/// Get the current display password (masked)
|
||||
pub fn get_display_password(&self) -> String {
|
||||
self.input_handler.get_display_password()
|
||||
}
|
||||
|
||||
/// Get the output dimensions
|
||||
pub fn dimensions(&self) -> (i32, i32) {
|
||||
(self.width, self.height)
|
||||
}
|
||||
|
||||
/// Set the Wayland surface for this locked surface
|
||||
pub fn set_wayland_surface(&mut self, surface: wl_surface::WlSurface) {
|
||||
self.wayland_surface = Some(surface);
|
||||
}
|
||||
|
||||
/// Get the Wayland surface for this locked surface
|
||||
pub fn wayland_surface(&self) -> Option<&wl_surface::WlSurface> {
|
||||
self.wayland_surface.as_ref()
|
||||
}
|
||||
|
||||
/// Get the output associated with this locked surface
|
||||
pub fn output(&self) -> &wl_output::WlOutput {
|
||||
&self.output
|
||||
}
|
||||
|
||||
/// Check if this surface has a Wayland surface attached
|
||||
pub fn has_wayland_surface(&self) -> bool {
|
||||
self.wayland_surface.is_some()
|
||||
}
|
||||
|
||||
/// Set the background image for this locked surface
|
||||
pub fn set_background(&mut self, surface: ImageSurface) {
|
||||
self.background = Some(surface);
|
||||
self.background_applied = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Manager for all locked surfaces (multiple outputs)
|
||||
pub struct LockManager {
|
||||
pub surfaces: Vec<LockedSurface>,
|
||||
config: Config,
|
||||
locked: bool,
|
||||
}
|
||||
|
||||
impl LockManager {
|
||||
/// Create a new lock manager
|
||||
pub fn new(config: Config) -> Self {
|
||||
Self {
|
||||
surfaces: Vec::new(),
|
||||
config,
|
||||
locked: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a locked surface for an output
|
||||
pub fn add_surface(&mut self, width: i32, height: i32, output: wl_output::WlOutput) -> bool {
|
||||
match LockedSurface::new(width, height, &self.config, output) {
|
||||
Some(surface) => {
|
||||
@@ -376,69 +229,20 @@ impl LockManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// Update all locked surfaces
|
||||
pub fn update(&mut self) {
|
||||
for surface in &mut self.surfaces {
|
||||
surface.update();
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a key event and return any action that needs processing
|
||||
/// Returns the first non-None action from any surface
|
||||
pub fn handle_key_event(
|
||||
&mut self,
|
||||
event: smithay_client_toolkit::seat::keyboard::KeyEvent,
|
||||
) -> Option<InputAction> {
|
||||
// Distribute key event to all surfaces and collect first action
|
||||
let mut action = None;
|
||||
for surface in &mut self.surfaces {
|
||||
if let Some(a) = surface.handle_key_event(event.clone()) {
|
||||
action = Some(a);
|
||||
}
|
||||
}
|
||||
action
|
||||
}
|
||||
|
||||
/// Check if session is locked
|
||||
pub fn is_locked(&self) -> bool {
|
||||
self.locked
|
||||
}
|
||||
|
||||
/// Lock the session
|
||||
pub fn lock(&mut self) {
|
||||
self.locked = true;
|
||||
// TODO: Implement actual Wayland session locking
|
||||
}
|
||||
|
||||
/// Unlock the session
|
||||
pub fn unlock(&mut self) {
|
||||
self.locked = false;
|
||||
// TODO: Implement actual Wayland session unlocking
|
||||
}
|
||||
|
||||
/// Get the number of locked surfaces
|
||||
pub fn surface_count(&self) -> usize {
|
||||
self.surfaces.len()
|
||||
}
|
||||
|
||||
/// Get a reference to a locked surface by index
|
||||
pub fn get_surface(&self, index: usize) -> Option<&LockedSurface> {
|
||||
self.surfaces.get(index)
|
||||
}
|
||||
|
||||
/// Get a mutable reference to a locked surface by index
|
||||
pub fn get_surface_mut(&mut self, index: usize) -> Option<&mut LockedSurface> {
|
||||
self.surfaces.get_mut(index)
|
||||
}
|
||||
|
||||
/// Toggle temp screenshot peek mode
|
||||
pub fn toggle_peek(&mut self) {
|
||||
for surface in &mut self.surfaces {
|
||||
surface.input_handler.update_temp_screenshot();
|
||||
}
|
||||
}
|
||||
|
||||
/// Find a locked surface by Wayland surface
|
||||
pub fn find_surface_by_wayland_surface(
|
||||
&mut self,
|
||||
wayland_surface: &wl_surface::WlSurface,
|
||||
@@ -448,13 +252,19 @@ impl LockManager {
|
||||
.find(|surface| surface.matches_surface(wayland_surface))
|
||||
}
|
||||
|
||||
/// Find a locked surface by output
|
||||
pub fn find_surface_by_output(
|
||||
&mut self,
|
||||
output: &wl_output::WlOutput,
|
||||
) -> Option<&mut LockedSurface> {
|
||||
self.surfaces
|
||||
.iter_mut()
|
||||
.find(|surface| surface.output().id() == output.id())
|
||||
pub fn handle_key_event(&mut self, event: KeyEvent) -> Option<InputAction> {
|
||||
let mut action = None;
|
||||
for surface in &mut self.surfaces {
|
||||
if let Some(a) = surface.handle_key_event(event.clone()) {
|
||||
action = Some(a);
|
||||
}
|
||||
}
|
||||
action
|
||||
}
|
||||
|
||||
pub fn toggle_peek(&mut self) {
|
||||
for surface in &mut self.surfaces {
|
||||
surface.input_handler.update_temp_screenshot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+397
-1034
File diff suppressed because it is too large
Load Diff
+146
-161
@@ -2,7 +2,6 @@ use cairo::{Context, Format, ImageSurface};
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::util::Color;
|
||||
|
||||
/// Cairo-based renderer for the lock screen
|
||||
pub struct Renderer {
|
||||
@@ -16,23 +15,18 @@ pub struct Renderer {
|
||||
key_highlight_shown: bool,
|
||||
wrong_password_start: Option<Instant>,
|
||||
key_highlight_start: Option<Instant>,
|
||||
key_highlight_angle: f64,
|
||||
background: Option<ImageSurface>,
|
||||
password_display: String,
|
||||
uptime_cache: String,
|
||||
last_uptime_update: Option<Instant>,
|
||||
caps_lock: bool,
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
/// Convert color tuple to Color struct
|
||||
fn tuple_to_color(&self, color: (f64, f64, f64, f64)) -> Color {
|
||||
Color {
|
||||
r: (color.0 * 255.0) as u8,
|
||||
g: (color.1 * 255.0) as u8,
|
||||
b: (color.2 * 255.0) as u8,
|
||||
a: (color.3 * 255.0) as u8,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new renderer with the given dimensions and configuration
|
||||
pub fn new(width: i32, height: i32, config: Config) -> Self {
|
||||
log::debug!("Renderer::new({}, {}, ...) called", width, height);
|
||||
let surface = ImageSurface::create(Format::ARgb32, width, height)
|
||||
.expect("Failed to create Cairo surface");
|
||||
let context = Context::new(&surface).expect("Failed to create Cairo context");
|
||||
@@ -48,13 +42,18 @@ impl Renderer {
|
||||
key_highlight_shown: false,
|
||||
wrong_password_start: None,
|
||||
key_highlight_start: None,
|
||||
key_highlight_angle: 0.0,
|
||||
background: None,
|
||||
password_display: String::new(),
|
||||
uptime_cache: String::new(),
|
||||
last_uptime_update: None,
|
||||
caps_lock: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Resize the renderer to new dimensions
|
||||
pub fn resize(&mut self, width: i32, height: i32) {
|
||||
log::debug!("Renderer::resize({}, {}) called", width, height);
|
||||
self.width = width;
|
||||
self.height = height;
|
||||
|
||||
@@ -83,6 +82,15 @@ impl Renderer {
|
||||
pub fn show_key_highlight(&mut self) {
|
||||
self.key_highlight_shown = true;
|
||||
self.key_highlight_start = Some(Instant::now());
|
||||
|
||||
// Generate ONE random angle for this highlight
|
||||
use std::time::SystemTime;
|
||||
let seed = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos() as u64;
|
||||
let random_val = seed.wrapping_mul(6364136223846793005).wrapping_add(1);
|
||||
self.key_highlight_angle = ((random_val % 360) as f64).to_radians();
|
||||
}
|
||||
|
||||
/// Set the password display string (masked)
|
||||
@@ -92,61 +100,42 @@ impl Renderer {
|
||||
|
||||
/// Render the current frame
|
||||
pub fn render(&mut self) {
|
||||
log::info!(
|
||||
"Renderer::render() called, background: {}",
|
||||
self.background.is_some()
|
||||
);
|
||||
|
||||
// Clear the surface - draw a VISIBLE color (dark gray) instead of black
|
||||
self.context.set_source_rgba(0.15, 0.15, 0.15, 1.0);
|
||||
// Clear the surface
|
||||
self.context.new_path();
|
||||
self.context.set_source_rgba(0.0, 0.0, 0.0, 1.0);
|
||||
self.context.paint().expect("Failed to clear surface");
|
||||
|
||||
// Draw background if available
|
||||
// Draw background
|
||||
if let Some(ref background) = self.background {
|
||||
let size = (background.width(), background.height());
|
||||
log::info!(
|
||||
"✓ Drawing background ({}x{}, fade_alpha: {})",
|
||||
size.0,
|
||||
size.1,
|
||||
self.fade_alpha
|
||||
);
|
||||
self.context.new_path();
|
||||
self.context
|
||||
.set_source_surface(background, 0.0, 0.0)
|
||||
.expect("Failed to set background source");
|
||||
.expect("Failed to set source");
|
||||
self.context
|
||||
.paint_with_alpha(self.fade_alpha)
|
||||
.expect("Failed to draw background");
|
||||
log::info!("✓ Background drawn successfully");
|
||||
} else {
|
||||
// Draw solid color background (dark gray visible color)
|
||||
log::warn!("✗ No background available - rendering solid gray!");
|
||||
self.context.set_source_rgba(0.15, 0.15, 0.15, 1.0);
|
||||
self.context
|
||||
.paint()
|
||||
.expect("Failed to draw solid background");
|
||||
.expect("Failed to paint");
|
||||
}
|
||||
|
||||
// Draw clock if enabled
|
||||
if self.config.clock {
|
||||
self.draw_clock();
|
||||
}
|
||||
|
||||
// Draw indicator if enabled
|
||||
if self.config.indicator {
|
||||
self.draw_indicator();
|
||||
}
|
||||
|
||||
// Draw password display (if not empty)
|
||||
if self.config.clock {
|
||||
self.draw_clock();
|
||||
}
|
||||
|
||||
if !self.password_display.is_empty() {
|
||||
self.draw_password_display();
|
||||
}
|
||||
|
||||
// Draw wrong password feedback if active
|
||||
if self.caps_lock {
|
||||
self.draw_caps_lock_indicator();
|
||||
}
|
||||
|
||||
if self.wrong_password_shown {
|
||||
self.draw_wrong_password_feedback();
|
||||
}
|
||||
|
||||
// Draw key highlight feedback if active
|
||||
if self.key_highlight_shown {
|
||||
self.draw_key_highlight_feedback();
|
||||
}
|
||||
@@ -154,96 +143,79 @@ impl Renderer {
|
||||
self.update_feedback_timers();
|
||||
}
|
||||
|
||||
/// Get the rendered image surface
|
||||
pub fn as_image_surface(&self) -> &ImageSurface {
|
||||
&self.surface
|
||||
}
|
||||
|
||||
/// Get raw pixel data from the surface (ARGB32 format)
|
||||
pub fn get_pixel_data(&self) -> Result<Vec<u8>, cairo::BorrowError> {
|
||||
let stride = self.surface.stride() as usize;
|
||||
let height = self.height as usize;
|
||||
|
||||
let mut data = vec![0u8; stride * height];
|
||||
self.surface.with_data(|src| {
|
||||
data.copy_from_slice(src);
|
||||
})?;
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
/// Get surface dimensions and stride
|
||||
pub fn surface_info(&self) -> (i32, i32, i32) {
|
||||
(self.width, self.height, self.surface.stride())
|
||||
}
|
||||
|
||||
/// Draw the clock in the center of the screen
|
||||
fn update_uptime(&mut self) {
|
||||
let now = Instant::now();
|
||||
if let Some(last) = self.last_uptime_update {
|
||||
if now.duration_since(last).as_secs() < 10 {
|
||||
return;
|
||||
}
|
||||
}
|
||||
let uptime_secs = std::fs::read_to_string("/proc/uptime")
|
||||
.ok()
|
||||
.and_then(|s| s.split_whitespace().next()?.parse::<f64>().ok())
|
||||
.unwrap_or(0.0) as u64;
|
||||
self.uptime_cache = format!("up {}h {}m", uptime_secs / 3600, (uptime_secs % 3600) / 60);
|
||||
self.last_uptime_update = Some(now);
|
||||
}
|
||||
|
||||
fn draw_clock(&self) {
|
||||
use chrono::Local;
|
||||
|
||||
let now = Local::now();
|
||||
let time_str = now.format("%H:%M").to_string();
|
||||
let date_str = now.format("%A, %B %d").to_string();
|
||||
let center_x = self.width as f64 / 2.0;
|
||||
let center_y = self.height as f64 / 2.0;
|
||||
|
||||
self.context.set_font_size(72.0);
|
||||
self.context.new_path();
|
||||
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
|
||||
|
||||
// Center the text
|
||||
let extents = self
|
||||
.context
|
||||
.text_extents(&time_str)
|
||||
.expect("Failed to get text extents");
|
||||
let x = (self.width as f64 - extents.width()) / 2.0;
|
||||
let y = (self.height as f64 / 2.0) - extents.height() / 2.0;
|
||||
|
||||
self.context.move_to(x, y);
|
||||
self.context.set_font_size(48.0);
|
||||
let te = self.context.text_extents(&time_str).unwrap();
|
||||
self.context
|
||||
.show_text(&time_str)
|
||||
.expect("Failed to draw time");
|
||||
.move_to(center_x - te.width() / 2.0, center_y + te.height() / 4.0);
|
||||
self.context.show_text(&time_str).unwrap();
|
||||
|
||||
// Draw date below time
|
||||
self.context.set_font_size(24.0);
|
||||
let date_extents = self
|
||||
.context
|
||||
.text_extents(&date_str)
|
||||
.expect("Failed to get date extents");
|
||||
let date_x = (self.width as f64 - date_extents.width()) / 2.0;
|
||||
let date_y = y + extents.height() + 20.0;
|
||||
self.context.new_path();
|
||||
self.context.set_font_size(14.0);
|
||||
let de = self.context.text_extents(&date_str).unwrap();
|
||||
self.context.move_to(
|
||||
center_x - de.width() / 2.0,
|
||||
center_y + te.height() / 4.0 + 25.0,
|
||||
);
|
||||
self.context.show_text(&date_str).unwrap();
|
||||
|
||||
self.context.move_to(date_x, date_y);
|
||||
self.context
|
||||
.show_text(&date_str)
|
||||
.expect("Failed to draw date");
|
||||
self.context.new_path();
|
||||
let ue = self.context.text_extents(&self.uptime_cache).unwrap();
|
||||
self.context.move_to(
|
||||
center_x - ue.width() / 2.0,
|
||||
center_y + te.height() / 4.0 + 43.0,
|
||||
);
|
||||
self.context.show_text(&self.uptime_cache).unwrap();
|
||||
}
|
||||
|
||||
/// Draw the password indicator ring
|
||||
fn draw_indicator(&self) {
|
||||
let center_x = self.width as f64 / 2.0;
|
||||
let center_y = self.height as f64 / 2.0;
|
||||
let radius = self.config.indicator_radius as f64;
|
||||
let thickness = self.config.indicator_thickness as f64;
|
||||
|
||||
// Draw outer ring
|
||||
let ring_color = self.tuple_to_color(self.config.ring_color);
|
||||
self.context.set_source_rgba(
|
||||
ring_color.r as f64 / 255.0,
|
||||
ring_color.g as f64 / 255.0,
|
||||
ring_color.b as f64 / 255.0,
|
||||
ring_color.a as f64 / 255.0 * self.fade_alpha,
|
||||
);
|
||||
self.context.set_line_width(thickness);
|
||||
self.context
|
||||
.arc(center_x, center_y, radius, 0.0, 2.0 * std::f64::consts::PI);
|
||||
self.context.stroke().expect("Failed to draw ring");
|
||||
|
||||
// Draw inside fill
|
||||
let inside_color = self.tuple_to_color(self.config.inside_color);
|
||||
self.context.set_source_rgba(
|
||||
inside_color.r as f64 / 255.0,
|
||||
inside_color.g as f64 / 255.0,
|
||||
inside_color.b as f64 / 255.0,
|
||||
inside_color.a as f64 / 255.0 * self.fade_alpha,
|
||||
);
|
||||
self.context.new_path();
|
||||
let (r, g, b, a) = self.config.inside_color;
|
||||
self.context.set_source_rgba(r, g, b, a * self.fade_alpha);
|
||||
self.context.arc(
|
||||
center_x,
|
||||
center_y,
|
||||
@@ -251,62 +223,75 @@ impl Renderer {
|
||||
0.0,
|
||||
2.0 * std::f64::consts::PI,
|
||||
);
|
||||
self.context.fill().expect("Failed to fill inside");
|
||||
self.context.fill().unwrap();
|
||||
|
||||
// Draw separator line
|
||||
let separator_color = self.tuple_to_color(self.config.separator_color);
|
||||
if separator_color.a > 0 {
|
||||
self.context.set_source_rgba(
|
||||
separator_color.r as f64 / 255.0,
|
||||
separator_color.g as f64 / 255.0,
|
||||
separator_color.b as f64 / 255.0,
|
||||
separator_color.a as f64 / 255.0 * self.fade_alpha,
|
||||
let (lr, lg, lb, la) = self.config.line_color;
|
||||
if la > 0.0 {
|
||||
self.context.new_path();
|
||||
self.context
|
||||
.set_source_rgba(lr, lg, lb, la * self.fade_alpha);
|
||||
self.context.set_line_width(1.0);
|
||||
self.context.arc(
|
||||
center_x,
|
||||
center_y,
|
||||
radius - thickness / 2.0,
|
||||
0.0,
|
||||
2.0 * std::f64::consts::PI,
|
||||
);
|
||||
self.context.stroke().unwrap();
|
||||
}
|
||||
|
||||
self.context.new_path();
|
||||
let (r, g, b, a) = self.config.ring_color;
|
||||
self.context.set_source_rgba(r, g, b, a * self.fade_alpha);
|
||||
self.context.set_line_width(thickness);
|
||||
self.context
|
||||
.arc(center_x, center_y, radius, 0.0, 2.0 * std::f64::consts::PI);
|
||||
self.context.stroke().unwrap();
|
||||
|
||||
let (r, g, b, a) = self.config.separator_color;
|
||||
if a > 0.0 {
|
||||
self.context.new_path();
|
||||
self.context.set_source_rgba(r, g, b, a * self.fade_alpha);
|
||||
self.context.set_line_width(1.0);
|
||||
self.context.move_to(center_x - radius, center_y);
|
||||
self.context.line_to(center_x + radius, center_y);
|
||||
self.context.stroke().expect("Failed to draw separator");
|
||||
self.context.stroke().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw the password display (masked characters)
|
||||
fn draw_password_display(&self) {
|
||||
if self.password_display.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Position: below the indicator ring (or centered if no indicator)
|
||||
let center_x = self.width as f64 / 2.0;
|
||||
let center_y = self.height as f64 / 2.0;
|
||||
let radius = self.config.indicator_radius as f64;
|
||||
let thickness = self.config.indicator_thickness as f64;
|
||||
|
||||
// Place password text below the ring
|
||||
let text_y = center_y + radius + thickness + 40.0; // 40px below ring
|
||||
|
||||
self.context.set_font_size(36.0);
|
||||
self.context.new_path();
|
||||
self.context.set_font_size(32.0);
|
||||
self.context.set_source_rgba(1.0, 1.0, 1.0, self.fade_alpha);
|
||||
|
||||
// Center the text
|
||||
let extents = self
|
||||
.context
|
||||
.text_extents(&self.password_display)
|
||||
.expect("Failed to get password text extents");
|
||||
let text_x = center_x - extents.width() / 2.0;
|
||||
|
||||
self.context.move_to(text_x, text_y);
|
||||
let te = self.context.text_extents(&self.password_display).unwrap();
|
||||
self.context
|
||||
.show_text(&self.password_display)
|
||||
.expect("Failed to draw password");
|
||||
.move_to(center_x - te.width() / 2.0, center_y + radius / 1.1);
|
||||
self.context.show_text(&self.password_display).unwrap();
|
||||
}
|
||||
|
||||
fn draw_caps_lock_indicator(&self) {
|
||||
let center_x = self.width as f64 / 2.0;
|
||||
let center_y = self.height as f64 / 2.0;
|
||||
let radius = self.config.indicator_radius as f64;
|
||||
self.context.new_path();
|
||||
self.context.set_font_size(12.0);
|
||||
self.context.set_source_rgba(1.0, 0.5, 0.0, self.fade_alpha);
|
||||
let text = "CAPS LOCK";
|
||||
let te = self.context.text_extents(text).unwrap();
|
||||
self.context
|
||||
.move_to(center_x - te.width() / 2.0, center_y + radius / 1.1 + 20.0);
|
||||
self.context.show_text(text).unwrap();
|
||||
}
|
||||
|
||||
/// Draw wrong password feedback (red flash)
|
||||
fn draw_wrong_password_feedback(&self) {
|
||||
let center_x = self.width as f64 / 2.0;
|
||||
let center_y = self.height as f64 / 2.0;
|
||||
let radius = self.config.indicator_radius as f64;
|
||||
|
||||
// Calculate flash intensity based on time
|
||||
let thickness = self.config.indicator_thickness as f64;
|
||||
let intensity = if let Some(start) = self.wrong_password_start {
|
||||
let elapsed = start.elapsed();
|
||||
let duration = std::time::Duration::from_millis(500);
|
||||
@@ -320,26 +305,24 @@ impl Renderer {
|
||||
};
|
||||
|
||||
if intensity > 0.0 {
|
||||
self.context.new_path();
|
||||
self.context
|
||||
.set_source_rgba(1.0, 0.0, 0.0, intensity * self.fade_alpha);
|
||||
self.context.set_line_width(thickness + 2.0);
|
||||
self.context
|
||||
.arc(center_x, center_y, radius, 0.0, 2.0 * std::f64::consts::PI);
|
||||
self.context
|
||||
.fill()
|
||||
.expect("Failed to draw wrong password feedback");
|
||||
self.context.stroke().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw key highlight feedback (green flash)
|
||||
fn draw_key_highlight_feedback(&self) {
|
||||
let center_x = self.width as f64 / 2.0;
|
||||
let center_y = self.height as f64 / 2.0;
|
||||
let radius = self.config.indicator_radius as f64;
|
||||
|
||||
// Calculate flash intensity based on time
|
||||
let thickness = self.config.indicator_thickness as f64;
|
||||
let intensity = if let Some(start) = self.key_highlight_start {
|
||||
let elapsed = start.elapsed();
|
||||
let duration = std::time::Duration::from_millis(200);
|
||||
let duration = std::time::Duration::from_millis(300);
|
||||
if elapsed < duration {
|
||||
1.0 - (elapsed.as_secs_f64() / duration.as_secs_f64())
|
||||
} else {
|
||||
@@ -350,34 +333,36 @@ impl Renderer {
|
||||
};
|
||||
|
||||
if intensity > 0.0 {
|
||||
let key_hl_color = self.tuple_to_color(self.config.key_hl_color);
|
||||
self.context.set_source_rgba(
|
||||
key_hl_color.r as f64 / 255.0,
|
||||
key_hl_color.g as f64 / 255.0,
|
||||
key_hl_color.b as f64 / 255.0,
|
||||
key_hl_color.a as f64 / 255.0 * intensity * self.fade_alpha,
|
||||
let (r, g, b, a) = self.config.key_hl_color;
|
||||
self.context
|
||||
.set_source_rgba(r, g, b, a * intensity * self.fade_alpha);
|
||||
self.context.set_line_width(thickness + 1.5);
|
||||
|
||||
// Draw ONLY ONE segment that rotates based on password length
|
||||
let global_offset = (self.password_display.len() as f64 * 45.0).to_radians();
|
||||
self.context.new_path();
|
||||
let actual_start = global_offset + self.key_highlight_angle;
|
||||
self.context.arc(
|
||||
center_x,
|
||||
center_y,
|
||||
radius,
|
||||
actual_start,
|
||||
actual_start + (40.0_f64).to_radians(),
|
||||
);
|
||||
self.context
|
||||
.arc(center_x, center_y, radius, 0.0, 2.0 * std::f64::consts::PI);
|
||||
self.context
|
||||
.fill()
|
||||
.expect("Failed to draw key highlight feedback");
|
||||
self.context.stroke().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Update feedback timers and reset expired feedback
|
||||
fn update_feedback_timers(&mut self) {
|
||||
// Check wrong password feedback timeout
|
||||
self.update_uptime();
|
||||
if let Some(start) = self.wrong_password_start {
|
||||
if start.elapsed() > std::time::Duration::from_millis(500) {
|
||||
self.wrong_password_shown = false;
|
||||
self.wrong_password_start = None;
|
||||
}
|
||||
}
|
||||
|
||||
// Check key highlight feedback timeout
|
||||
if let Some(start) = self.key_highlight_start {
|
||||
if start.elapsed() > std::time::Duration::from_millis(200) {
|
||||
if start.elapsed() > std::time::Duration::from_millis(300) {
|
||||
self.key_highlight_shown = false;
|
||||
self.key_highlight_start = None;
|
||||
}
|
||||
|
||||
@@ -28,11 +28,6 @@ impl Screenshot {
|
||||
Self { surface }
|
||||
}
|
||||
|
||||
/// Get a reference to the underlying surface.
|
||||
pub fn surface(&self) -> &ImageSurface {
|
||||
&self.surface
|
||||
}
|
||||
|
||||
/// Consume the screenshot and return the underlying Cairo surface.
|
||||
pub fn into_inner(self) -> ImageSurface {
|
||||
self.surface
|
||||
@@ -171,11 +166,6 @@ impl ScreenshotManager {
|
||||
Ok(Self { manager })
|
||||
}
|
||||
|
||||
/// Returns `true` if the wlr-screencopy protocol is available.
|
||||
pub fn is_available(&self) -> bool {
|
||||
self.manager.is_some()
|
||||
}
|
||||
|
||||
/// Initiate a screencopy operation for the given output.
|
||||
///
|
||||
/// This method sends a screencopy request and returns the frame object.
|
||||
|
||||
@@ -1,26 +1 @@
|
||||
use std::time::Duration;
|
||||
|
||||
pub struct FadeTimer {
|
||||
duration: Duration,
|
||||
start_time: std::time::Instant,
|
||||
}
|
||||
|
||||
impl FadeTimer {
|
||||
pub fn new(duration: Duration) -> Self {
|
||||
Self {
|
||||
duration,
|
||||
start_time: std::time::Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self) -> bool {
|
||||
let elapsed = self.start_time.elapsed();
|
||||
let progress = elapsed.as_secs_f64() / self.duration.as_secs_f64();
|
||||
progress >= 1.0
|
||||
}
|
||||
|
||||
pub fn current_alpha(&self) -> f64 {
|
||||
let elapsed = self.start_time.elapsed();
|
||||
(elapsed.as_secs_f64() / self.duration.as_secs_f64()).min(1.0)
|
||||
}
|
||||
}
|
||||
|
||||
-20
@@ -38,23 +38,3 @@ pub fn parse_vignette_effect(s: &str) -> Result<(f32, f32), String> {
|
||||
let factor = parts[1].parse().map_err(|_| "Invalid factor")?;
|
||||
Ok((base, factor))
|
||||
}
|
||||
|
||||
/// Convert hex color string to RGBA color struct
|
||||
pub fn hex_to_rgba(hex: &str) -> Color {
|
||||
let (r, g, b, a) = parse_hex_color(hex).unwrap_or((0.0, 0.0, 0.0, 1.0));
|
||||
Color {
|
||||
r: (r * 255.0) as u8,
|
||||
g: (g * 255.0) as u8,
|
||||
b: (b * 255.0) as u8,
|
||||
a: (a * 255.0) as u8,
|
||||
}
|
||||
}
|
||||
|
||||
/// RGBA color struct
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Color {
|
||||
pub r: u8,
|
||||
pub g: u8,
|
||||
pub b: u8,
|
||||
pub a: u8,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user