From 41a5bebb1d28b7c830a36954327f79ff0c7b24c8 Mon Sep 17 00:00:00 2001 From: Aleksander Date: Mon, 29 Jan 2024 18:05:04 +0100 Subject: [PATCH] Port changes from -x repo --- Cargo.lock | 7 ++++++ Cargo.toml | 1 + src/backend/openvr/overlay.rs | 2 ++ src/config.rs | 16 ------------ src/graphics.rs | 5 ++++ src/overlays/keyboard.rs | 46 +++++++++++++++++++++++------------ src/overlays/screen.rs | 26 ++++++++++---------- src/overlays/watch.rs | 2 +- src/res/config.yaml | 12 --------- src/state.rs | 16 ------------ 10 files changed, 59 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce4fe65c..e4931d6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4205,6 +4205,7 @@ dependencies = [ "vulkano-shaders", "winit", "wlx-capture", + "xdg", ] [[package]] @@ -4256,6 +4257,12 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a0ccd7b4a5345edfcd0c3535718a4e9ff7798ffc536bb5b5a0e26ff84732911" +[[package]] +name = "xdg" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213b7324336b53d2414b2db8537e56544d981803139155afa84f76eeebb7a546" + [[package]] name = "xdg-home" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index cc69ddc0..581d08c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ vulkano = { git = "https://github.com/vulkano-rs/vulkano", rev = "94f50f1" } vulkano-shaders = { git = "https://github.com/vulkano-rs/vulkano", rev = "94f50f1" } winit = "0.29.10" wlx-capture = { git = "https://github.com/galister/wlx-capture" } +xdg = "2.5.2" [features] openvr = ["dep:ovr_overlay"] diff --git a/src/backend/openvr/overlay.rs b/src/backend/openvr/overlay.rs index 23eb38cc..572a07d2 100644 --- a/src/backend/openvr/overlay.rs +++ b/src/backend/openvr/overlay.rs @@ -232,8 +232,10 @@ impl OverlayData { texture.m_nWidth, texture.m_nHeight, image.usage() + ); if let Err(e) = overlay.set_image_vulkan(handle, &mut texture) { panic!("Failed to set overlay texture: {}", e); } + log::info!("{}: Uploaded texture", self.state.name); } } diff --git a/src/config.rs b/src/config.rs index b20b700a..42e065f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,14 +10,6 @@ pub fn def_pw_tokens() -> Vec<(String, String)> { Vec::new() } -fn def_grab_threshold() -> f32 { - 0.6 -} - -fn def_trigger_threshold() -> f32 { - 0.65 -} - fn def_click_freeze_time_ms() -> u32 { 300 } @@ -32,12 +24,6 @@ fn def_one() -> f32 { #[derive(Deserialize, Serialize)] pub struct GeneralConfig { - #[serde(default = "def_grab_threshold")] - pub grab_threshold: f32, - - #[serde(default = "def_trigger_threshold")] - pub trigger_threshold: f32, - #[serde(default = "def_click_freeze_time_ms")] pub click_freeze_time_ms: u32, @@ -74,8 +60,6 @@ impl GeneralConfig { } fn post_load(&self) { - GeneralConfig::sanitize_range("grab_threshold", self.grab_threshold, 0.0, 1.0); - GeneralConfig::sanitize_range("trigger_threshold", self.trigger_threshold, 0.0, 1.0); GeneralConfig::sanitize_range("keyboard_scale", self.keyboard_scale, 0.0, 5.0); GeneralConfig::sanitize_range("desktop_view_scale", self.desktop_view_scale, 0.0, 5.0); GeneralConfig::sanitize_range("watch_scale", self.watch_scale, 0.0, 5.0); diff --git a/src/graphics.rs b/src/graphics.rs index 559b256f..30b8c680 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -9,6 +9,7 @@ use std::{ use ash::vk::{self, SubmitInfo}; use smallvec::{smallvec, SmallVec}; +use vulkano::instance::InstanceCreateFlags; use vulkano::{ buffer::{ allocator::{SubbufferAllocator, SubbufferAllocatorCreateInfo}, @@ -25,6 +26,8 @@ use vulkano::{ descriptor_set::{ allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet, }, + device::physical::PhysicalDeviceType, + device::Features, device::{ physical::PhysicalDevice, Device, DeviceCreateInfo, DeviceExtensions, Queue, QueueCreateInfo, QueueFlags, @@ -37,6 +40,7 @@ use vulkano::{ Image, ImageCreateInfo, ImageLayout, ImageTiling, ImageType, ImageUsage, SampleCount, SubresourceLayout, }, + instance::InstanceExtensions, instance::{Instance, InstanceCreateInfo}, memory::{ allocator::{ @@ -72,6 +76,7 @@ use vulkano::{ }, DeviceSize, VulkanLibrary, VulkanObject, }; + use wlx_capture::frame::{ DmabufFrame, FourCC, DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888, diff --git a/src/overlays/keyboard.rs b/src/overlays/keyboard.rs index 31b2c95b..152e941e 100644 --- a/src/overlays/keyboard.rs +++ b/src/overlays/keyboard.rs @@ -11,12 +11,12 @@ use crate::{ config, gui::{color_parse, CanvasBuilder, Control}, hid::{KeyModifier, VirtualKey, KEYS_TO_MODS}, - state::AppState, + state::{AppSession, AppState}, }; use glam::{vec2, vec3a, Affine2}; use once_cell::sync::Lazy; use regex::Regex; -use rodio::{Decoder, OutputStream, Source}; +use rodio::{Decoder, OutputStream, OutputStreamHandle, Source}; use serde::{Deserialize, Serialize}; const PIXELS_PER_UNIT: f32 = 80.; @@ -35,6 +35,8 @@ where modifiers: 0, processes: vec![], audio_stream: None, + first_try: true, + audio_handle: None, }; let mut canvas = CanvasBuilder::new( @@ -107,7 +109,7 @@ where let interaction_transform = Affine2::from_translation(vec2(0.5, 0.5)) * Affine2::from_scale(vec2(1., -size.x as f32 / size.y as f32)); - let width = LAYOUT.row_size * 0.05; + let width = LAYOUT.row_size * 0.05 * app.session.config.keyboard_scale; OverlayData { state: OverlayState { @@ -132,7 +134,7 @@ fn key_press( ) { match control.state.as_mut() { Some(KeyButtonData::Key { vk, pressed }) => { - data.key_click(); + data.key_click(&app.session); app.hid_provider.send_key(*vk as _, true); *pressed = true; } @@ -143,12 +145,12 @@ fn key_press( }) => { *sticky = data.modifiers & *modifier == 0; data.modifiers |= *modifier; - data.key_click(); + data.key_click(&app.session); app.hid_provider.set_modifiers(data.modifiers); *pressed = true; } Some(KeyButtonData::Macro { verbs }) => { - data.key_click(); + data.key_click(&app.session); for (vk, press) in verbs { app.hid_provider.send_key(*vk as _, *press); } @@ -158,7 +160,7 @@ fn key_press( data.processes .retain_mut(|child| !matches!(child.try_wait(), Ok(Some(_)))); - data.key_click(); + data.key_click(&app.session); if let Ok(child) = Command::new(program).args(args).spawn() { data.processes.push(child); } @@ -208,19 +210,31 @@ struct KeyboardData { modifiers: KeyModifier, processes: Vec, audio_stream: Option, + audio_handle: Option, + first_try: bool, } impl KeyboardData { - fn key_click(&mut self) { - let wav = include_bytes!("../res/421581.wav"); - let cursor = Cursor::new(wav); - let source = Decoder::new_wav(cursor).unwrap(); - self.audio_stream = None; - if let Ok((stream, handle)) = OutputStream::try_default() { + fn key_click(&mut self, session: &AppSession) { + if !session.config.keyboard_sound_enabled { + return; + } + + if self.audio_stream.is_none() && self.first_try { + self.first_try = false; + if let Ok((stream, handle)) = OutputStream::try_default() { + self.audio_stream = Some(stream); + self.audio_handle = Some(handle); + } else { + log::error!("Failed to open audio stream"); + } + } + + if let Some(handle) = &self.audio_handle { + let wav = include_bytes!("../res/421581.wav"); + let cursor = Cursor::new(wav); + let source = Decoder::new_wav(cursor).unwrap(); let _ = handle.play_raw(source.convert_samples()); - self.audio_stream = Some(stream); - } else { - log::error!("Failed to play key click"); } } } diff --git a/src/overlays/screen.rs b/src/overlays/screen.rs index 01466a0e..fe3f8a37 100644 --- a/src/overlays/screen.rs +++ b/src/overlays/screen.rs @@ -1,7 +1,7 @@ use core::slice; use serde::{Deserialize, Serialize}; use std::{ - collections::BTreeMap, + collections::HashMap, error::Error, f32::consts::PI, ops::Deref, @@ -84,9 +84,6 @@ impl InteractionHandler for ScreenInteractionHandler { } } fn on_pointer(&mut self, app: &mut AppState, hit: &PointerHit, pressed: bool) { - let pos = self.mouse_transform.transform_point2(hit.uv); - app.hid_provider.mouse_move(pos); - let btn = match hit.mode { PointerMode::Right => MOUSE_RIGHT, PointerMode::Middle => MOUSE_MIDDLE, @@ -94,11 +91,14 @@ impl InteractionHandler for ScreenInteractionHandler { }; if pressed { - self.next_move = - Instant::now() + Duration::from_millis(app.session.click_freeze_time_ms); + self.next_move = Instant::now() + + Duration::from_millis(app.session.config.click_freeze_time_ms as u64); } app.hid_provider.send_button(btn, pressed); + + let pos = self.mouse_transform.transform_point2(hit.uv); + app.hid_provider.mouse_move(pos); } fn on_scroll(&mut self, app: &mut AppState, _hit: &PointerHit, delta: f32) { let millis = (1. - delta.abs()) * delta; @@ -350,7 +350,7 @@ impl OverlayRenderer for ScreenRenderer { fn try_create_screen( wl: &WlxClient, id: u32, - pw_token_store: &mut BTreeMap, + pw_token_store: &mut HashMap, session: &AppSession, ) -> Option> where @@ -435,7 +435,7 @@ where want_visible: session.show_screens.iter().any(|s| s == &*output.name), show_hide: true, grabbable: true, - spawn_scale: 1.5, + spawn_scale: 1.5 * session.config.desktop_view_scale, spawn_point: vec3a(0., 0.5, -1.), spawn_rotation: Quat::from_axis_angle(axis, angle), interaction_transform, @@ -462,7 +462,7 @@ fn get_pw_token_path() -> PathBuf { path } -pub fn save_pw_token_config(tokens: &BTreeMap) -> Result<(), Box> { +pub fn save_pw_token_config(tokens: &HashMap) -> Result<(), Box> { let mut conf = TokenConf::default(); for (name, token) in tokens { @@ -475,8 +475,8 @@ pub fn save_pw_token_config(tokens: &BTreeMap) -> Result<(), Box Ok(()) } -pub fn load_pw_token_config() -> Result, Box> { - let mut map: BTreeMap = BTreeMap::new(); +pub fn load_pw_token_config() -> Result, Box> { + let mut map: HashMap = HashMap::new(); let yaml = std::fs::read_to_string(get_pw_token_path())?; let conf: TokenConf = serde_yaml::from_str(yaml.as_str())?; @@ -496,10 +496,10 @@ where let wl = WlxClient::new().unwrap(); // Load existing Pipewire tokens from file - let mut pw_tokens: BTreeMap = if let Ok(conf) = load_pw_token_config() { + let mut pw_tokens: HashMap = if let Ok(conf) = load_pw_token_config() { conf } else { - BTreeMap::new() + HashMap::new() }; let pw_tokens_copy = pw_tokens.clone(); diff --git a/src/overlays/watch.rs b/src/overlays/watch.rs index 12383d08..f81f1566 100644 --- a/src/overlays/watch.rs +++ b/src/overlays/watch.rs @@ -169,7 +169,7 @@ where name: "Watch".into(), size: (400, 200), want_visible: true, - spawn_scale: 0.065, + spawn_scale: 0.065 * state.session.config.watch_scale, spawn_point: state.session.watch_pos.into(), spawn_rotation: state.session.watch_rot, interaction_transform, diff --git a/src/res/config.yaml b/src/res/config.yaml index 4652e754..523a5f47 100644 --- a/src/res/config.yaml +++ b/src/res/config.yaml @@ -1,15 +1,3 @@ -# Grab threshold for changing position of overlays -# Allowed values: 0.0 - 1.0 -# Default - 0.6 -# -# 1.0 - The hand must grab tighter to start dragging, recommended on Index controller -grab_threshold: 0.6 - -# Threshold for registering trigger (click) event -# Allowed values: 0.0 - 1.0 -# Default - 0.65 -trigger_threshold: 0.65 - # For how much time mouse motion events should be stopped after clicking? # Prevents accidental dragging various GUI elements or links, making it easier to click # Default: 300 diff --git a/src/state.rs b/src/state.rs index 0baf9700..7ae391d1 100644 --- a/src/state.rs +++ b/src/state.rs @@ -80,13 +80,6 @@ pub struct AppSession { pub config: GeneralConfig, pub show_screens: Vec, - pub show_keyboard: bool, - pub keyboard_volume: f32, - - pub screen_flip_h: bool, - pub screen_flip_v: bool, - pub screen_invert_color: bool, - pub screen_max_res: [u32; 2], pub watch_hand: usize, pub watch_pos: Vec3, @@ -100,8 +93,6 @@ pub struct AppSession { pub color_shift: Vec3, pub color_alt: Vec3, pub color_grab: Vec3, - - pub click_freeze_time_ms: u64, } impl AppSession { @@ -114,12 +105,6 @@ impl AppSession { config_root_path, config, show_screens: vec!["DP-3".to_string()], - keyboard_volume: 0.5, - show_keyboard: false, - screen_flip_h: false, - screen_flip_v: false, - screen_invert_color: false, - screen_max_res: [2560, 1440], capture_method: "auto".to_string(), primary_hand: 1, watch_hand: 0, @@ -145,7 +130,6 @@ impl AppSession { y: 0., z: 0., }, - click_freeze_time_ms: 300, } } }