sway + input #40

Open
adriangl wants to merge 6 commits from syncope into main
7 changed files with 327 additions and 64 deletions
Showing only changes of commit 20c53ed153 - Show all commits

10
Cargo.lock generated
View File

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "addr2line"
@ -620,8 +620,10 @@ dependencies = [
"clap-verbosity-flag",
"env_logger",
"futures",
"lazy_static",
"log",
"mpvipc-async",
"regex",
"sd-notify",
"serde",
"serde_json",
@ -988,6 +990,12 @@ dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.168"

View File

@ -9,6 +9,8 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.5.4"
lazy_static = "1.4.0"
url = "2.2.2"
swayipc = "3.0"
anyhow = "1.0.82"

View File

@ -13,6 +13,8 @@ in
enableSway = lib.mkEnableOption "sway as the main window manager";
enableFirefox = lib.mkEnableOption "include fiorefox browser for external urls";
enableInput = lib.mkEnableOption "enable input maipulation with ydotool,";
enablePipewire = lib.mkEnableOption "pipewire" // { default = true; };
@ -162,6 +164,11 @@ in
pulse.enable = true;
};
})
(lib.mkIf (cfg.enable && cfg.enableInput) {
programs.ydotool = {
enable = true;
};
})
(lib.mkIf (cfg.enable && cfg.enableSway) {
programs.sway = {
enable = true;
@ -188,7 +195,7 @@ in
users.greg = {
isNormalUser = true;
group = "greg";
extraGroups [ "audio" "video" "input" ];
extraGroups [ "audio" "video" "input" "ydotool" ];
uid = 2000;
description = "loud gym bro";
};

View File

@ -1,8 +1,8 @@
mod base;
mod rest_wrapper_v1;
// mod rest_wrapper_v1;
mod rest_wrapper_v2;
mod websocket_v1;
pub use rest_wrapper_v1::{rest_api_docs as rest_api_docs_v1, rest_api_routes as rest_api_routes_v1};
// pub use rest_wrapper_v1::{rest_api_docs as rest_api_docs_v1, rest_api_routes as rest_api_routes_v1};
pub use rest_wrapper_v2::{rest_api_docs as rest_api_docs_v2, rest_api_routes as rest_api_routes_v2};
pub use websocket_v1::websocket_api;

View File

@ -173,16 +173,15 @@ pub async fn playlist_set_looping(mpv: Mpv, r#loop: bool) -> anyhow::Result<()>
use swayipc::{Connection, Fallible};
pub async fn sway_run_command(command: String) -> Fallible<()> {
tokio::task::spawn_blocking(move || -> Fallible<()> {
let mut connection = Connection::new()?;
connection.run_command(&command)?;
Ok(())
})
.await
.map_err(|e| swayipc::Error::CommandFailed(e.to_string()))?
}
// pub async fn sway_run_command(command: String) -> Fallible<()> {
// tokio::task::spawn_blocking(move || -> Fallible<()> {
// let mut connection = Connection::new()?;
// connection.run_command(&command)?;
// Ok(())
// })
// .await
// .map_err(|e| swayipc::Error::CommandFailed(e.to_string()))?
// }
//only to check if workspace exists.
fn get_workspace_names(connection: &mut Connection) -> Fallible<Vec<String>> {
@ -225,6 +224,7 @@ pub async fn sway_change_workspace(workspace: String) -> Fallible<()> {
}
use url::Url;
pub async fn sway_launch_browser(url: &str) -> Fallible<()> {
// Validate URL
let url = Url::parse(url)
@ -237,7 +237,19 @@ pub async fn sway_launch_browser(url: &str) -> Fallible<()> {
tokio::task::spawn_blocking(move || -> Fallible<()> {
let mut connection = Connection::new()?;
connection.run_command(&format!("exec xdg-open {}", url))?;
// connection.run_command(&format!("exec xdg-open {}", url))?;
connection.run_command(&format!("exec firefox --kiosk {}", url))?; //moved to firefox to pin in kiosk mode. potentially add --new-window
// let browser_output = std::process::Command::new("xdg-settings")
// .arg("get")
// .arg("default-web-browser")
// .output()?;
// let default_browser = String::from_utf8(browser_output.stdout)?
// .trim()
// .trim_end_matches(".desktop")
// .to_string();
// connection.run_command(&format!("exec {} --kiosk {}", default_browser, url))?; // set default browser in kiosk mode
Ok(())
})
.await
@ -255,21 +267,45 @@ pub async fn sway_close_workspace(workspace: String) -> Fallible<()> {
));
}
// Get workspace tree and find all nodes in target workspace
let tree = connection.get_tree()?;
let workspace_nodes = tree
.nodes
.iter()
.flat_map(|output| &output.nodes) // Get workspaces
.find(|ws| ws.name.as_ref().map_or(false, |name| name == &workspace));
// // Get workspace tree and find all nodes in target workspace
// let tree = connection.get_tree()?;
// let workspace_nodes = tree
// .nodes
// .iter()
// .flat_map(|output| &output.nodes) // Get workspaces
// .find(|ws| ws.name.as_ref().map_or(false, |name| name == &workspace));
// Kill all nodes in workspace if found
if let Some(ws) = workspace_nodes {
for container in ws.nodes.iter() {
// Close each container in the workspace
connection.run_command(&format!("[con_id={}] kill", container.id))?;
}
}
// // Kill all nodes in workspace if found
// if let Some(ws) = workspace_nodes {
// for container in ws.nodes.iter() {
// // Close each container in the workspace
// connection.run_command(&format!("[con_id={}] kill", container.id))?;
// }
// }
// Get workspace tree and find all nodes in target workspace
let tree = connection.get_tree()?;
let workspace_nodes = tree
.nodes
.iter()
.flat_map(|output| &output.nodes) // Get workspaces
.find(|ws| ws.name.as_ref().map_or(false, |name| name == &workspace));
// Kill all non-MPV nodes in workspace if found
if let Some(ws) = workspace_nodes {
for container in ws.nodes.iter() {
// Check if window is MPV
let is_mpv = container.window_properties.as_ref()
.map(|props| props.class.as_ref()
.map_or(false, |class| class == "mpv"))
.unwrap_or(false);
if !is_mpv {
// Close container only if not MPV
connection.run_command(&format!("[con_id={}] kill", container.id))?;
}
}
}
Ok(())
})
@ -277,3 +313,115 @@ pub async fn sway_close_workspace(workspace: String) -> Fallible<()> {
.map_err(|e| swayipc::Error::CommandFailed(e.to_string()))?
}
use regex::Regex;
use lazy_static::lazy_static;
lazy_static! {
static ref KEYPRESS_PATTERN: Regex = Regex::new(r"^(\d+:[01]\s*)+$").unwrap();
static ref CLEANUP_PATTERN: Regex = Regex::new(r"[^0-9: \t]").unwrap();
}
fn validate_keypress_string(input: &str) -> Fallible<String> {
let cleaned = CLEANUP_PATTERN.replace_all(input, "").to_string();
let cleaned = cleaned.trim();
if !KEYPRESS_PATTERN.is_match(cleaned) {
return Err(swayipc::Error::CommandFailed(
"Invalid keypress format. Expected 'number:1 number:0'".into()
));
}
Ok(cleaned.to_string())
}
//to simulate keypresses 42:1 38:1 38:0 24:1 24:0 38:1 38:0 42:0 -> LOL
pub async fn sway_input(keys: String) -> Fallible<()> {
let validated_input = validate_keypress_string(&keys)?;
tokio::task::spawn_blocking(move || -> Fallible<()> {
let mut connection = Connection::new()?;
connection.run_command(&format!("exec ydotool key {}", validated_input))?;
Ok(())
})
.await
.map_err(|e| swayipc::Error::CommandFailed(e.to_string()))?
}
// simulate mouse movement
pub async fn sway_mouse_move(x: i32, y: i32) -> Fallible<()> {
tokio::task::spawn_blocking(move || -> Fallible<()> {
let mut connection = Connection::new()?;
connection.run_command(&format!("exec ydotool mousemove -x {} -y {}", x, y))?;
Ok(())
})
.await
.map_err(|e| swayipc::Error::CommandFailed(e.to_string()))?
}
//simulate scroll
pub async fn sway_mouse_scroll(x: i32, y: i32) -> Fallible<()> {
tokio::task::spawn_blocking(move || -> Fallible<()> {
let mut connection = Connection::new()?;
connection.run_command(&format!("exec ydotool mousemove -w -x {} -y {}", x, y))?;
Ok(())
})
.await
.map_err(|e| swayipc::Error::CommandFailed(e.to_string()))?
}
#[derive(Debug)]
pub enum MouseButton {
Left,
Right,
Middle,
Side,
Extra,
Forward,
Back,
Task,
}
impl MouseButton {
fn to_base_value(&self) -> u8 {
match self {
MouseButton::Left => 0x00,
MouseButton::Right => 0x01,
MouseButton::Middle => 0x02,
MouseButton::Side => 0x03,
MouseButton::Extra => 0x04,
MouseButton::Forward => 0x05,
MouseButton::Back => 0x06,
MouseButton::Task => 0x07,
}
}
fn from_str(s: &str) -> Fallible<Self> {
match s.to_uppercase().as_str() {
"MIDDLE" => Ok(MouseButton::Middle),
"SIDE" => Ok(MouseButton::Side),
"EXTRA" => Ok(MouseButton::Extra),
"FORWARD" => Ok(MouseButton::Forward),
"BACK" => Ok(MouseButton::Back),
"TASK" => Ok(MouseButton::Task),
_ => Err(swayipc::Error::CommandFailed(format!("Invalid mouse button: {}", s))),
}
}
fn click_value(&self) -> u8 {
self.to_base_value() | 0xC0 // Combines DOWN (0x40) and UP (0x80)
}
}
pub async fn sway_mouse_click(button: String) -> Fallible<()> {
let mouse_button = MouseButton::from_str(&button)?;
let click_value = mouse_button.click_value();
tokio::task::spawn_blocking(move || -> Fallible<()> {
let mut connection = Connection::new()?;
connection.run_command(&format!("exec ydotool click {:#04x}", click_value))?;
Ok(())
})
.await
.map_err(|e| swayipc::Error::CommandFailed(e.to_string()))?
}

View File

@ -11,6 +11,7 @@ use serde_json::{json, Value};
use utoipa::OpenApi;
use utoipa_axum::{router::OpenApiRouter, routes};
use utoipa_swagger_ui::SwaggerUi;
use futures::FutureExt;
use super::base;
@ -32,11 +33,14 @@ pub fn rest_api_routes(mpv: Mpv) -> Router {
.route("/playlist/shuffle", post(shuffle))
.route("/playlist/loop", get(playlist_get_looping))
.route("/playlist/loop", post(playlist_set_looping))
.route("/sway/command", post(sway_run_command))
// .route("/sway/command", post(sway_run_command))
.route("/sway/workspace/close", post(sway_close_workspace_handler))
.route("/sway/workspace/change", post(sway_change_workspace_handler))
.route("/sway/workspace/list", get(sway_get_workspace_names_handler))
.route("/sway/browser/launch", post(sway_launch_browser_handler))
.route("/sway/input/keys", post(sway_input_handler))
.route("/sway/input/mouse", post(sway_mouse_move_handler))
.route("/sway/input/scroll", post(sway_mouse_scroll_handler))
.with_state(mpv)
}
@ -408,72 +412,71 @@ async fn playlist_set_looping(
}
/// Execute a sway command TODO: restrict to switching workspace, etc instead of arbitrary commands
#[derive(serde::Deserialize, utoipa::IntoParams)]
struct SwayCommandArgs {
#[derive(serde::Deserialize, utoipa::ToSchema)]
struct SwayCommandBody {
command: String,
}
#[utoipa::path(
post,
path = "/sway/command",
params(SwayCommandArgs),
responses(
(status = 200, description = "Success", body = EmptySuccessResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
)
)]
async fn sway_run_command(Query(query): Query<SwayCommandArgs>) -> RestResponse {
base::sway_run_command(query.command).await.map_err(anyhow::Error::new).into()
}
// #[utoipa::path(
// post,
// path = "/sway/command",
// request_body = SwayCommandBody,
// responses(
// (status = 200, description = "Success", body = EmptySuccessResponse),
// (status = 500, description = "Internal server error", body = ErrorResponse),
// )
// )]
// async fn sway_run_command(Json(body): Json<SwayCommandBody>) -> RestResponse {
// base::sway_run_command(body.command).await.map_err(anyhow::Error::new).into()
// }
#[derive(serde::Deserialize, utoipa::IntoParams)]
struct SwayBrowserArgs {
#[derive(serde::Deserialize, utoipa::ToSchema)]
struct SwayBrowserBody {
url: String,
}
#[utoipa::path(
post,
path = "/sway/browser/launch",
params(SwayBrowserArgs),
path = "/sway/browser/launch",
request_body = SwayBrowserBody,
responses(
(status = 200, description = "Success", body = EmptySuccessResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
)
)]
async fn sway_launch_browser_handler(Query(query): Query<SwayBrowserArgs>) -> RestResponse {
base::sway_launch_browser(&query.url).await.map_err(anyhow::Error::new).into()
async fn sway_launch_browser_handler(Json(body): Json<SwayBrowserBody>) -> RestResponse {
base::sway_launch_browser(&body.url).await.map_err(anyhow::Error::new).into()
}
#[derive(serde::Deserialize, utoipa::IntoParams)]
struct SwayWorkspaceArgs {
#[derive(serde::Deserialize, utoipa::ToSchema)]
struct SwayWorkspaceBody {
workspace: String,
}
#[utoipa::path(
post,
path = "/sway/workspace/close",
params(SwayWorkspaceArgs),
request_body = SwayWorkspaceBody,
responses(
(status = 200, description = "Success", body = EmptySuccessResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
)
)]
async fn sway_close_workspace_handler(Query(query): Query<SwayWorkspaceArgs>) -> RestResponse {
base::sway_close_workspace(query.workspace).await.map_err(anyhow::Error::new).into()
async fn sway_close_workspace_handler(Json(body): Json<SwayWorkspaceBody>) -> RestResponse {
base::sway_close_workspace(body.workspace).await.map_err(anyhow::Error::new).into()
}
#[utoipa::path(
post,
path = "/sway/workspace/change",
params(SwayWorkspaceArgs),
path = "/sway/workspace/change",
request_body = SwayWorkspaceBody,
responses(
(status = 200, description = "Success", body = EmptySuccessResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
)
)]
async fn sway_change_workspace_handler(Query(query): Query<SwayWorkspaceArgs>) -> RestResponse {
base::sway_change_workspace(query.workspace).await.map_err(anyhow::Error::new).into()
async fn sway_change_workspace_handler(Json(body): Json<SwayWorkspaceBody>) -> RestResponse {
base::sway_change_workspace(body.workspace).await.map_err(anyhow::Error::new).into()
}
#[utoipa::path(
@ -491,3 +494,78 @@ async fn sway_get_workspace_names_handler() -> RestResponse {
.into()
}
#[derive(serde::Deserialize, utoipa::ToSchema)]
struct KeyboardInput {
keys: String,
}
#[utoipa::path(
post,
path = "/sway/input/keys",
request_body = KeyboardInput,
responses(
(status = 200, description = "Success", body = Vec<String>),
(status = 500, description = "Internal server error", body = ErrorResponse),
)
)]
async fn sway_input_handler(
Json(payload): Json<KeyboardInput>
) -> RestResponse {
base::sway_input(payload.keys)
.await
.map(|_| json!({}))
.map_err(anyhow::Error::new)
.into()
}
#[utoipa::path(
post,
path = "/sway/input/mouse",
request_body = MouseMove,
responses(
(status = 200, description = "Success", body = Vec<String>),
(status = 500, description = "Internal server error", body = ErrorResponse),
)
)]
async fn sway_mouse_move_handler(
Json(payload): Json<MouseMove>
) -> RestResponse {
base::sway_mouse_move(payload.x, payload.y)
.await
.map(|_| json!({}))
.map_err(anyhow::Error::new)
.into()
}
#[utoipa::path(
post,
path = "/sway/input/scroll",
request_body = MouseMove,
responses(
(status = 200, description = "Success", body = Vec<String>),
(status = 500, description = "Internal server error", body = ErrorResponse),
)
)]
async fn sway_mouse_scroll_handler(
Json(payload): Json<MouseMove>
) -> RestResponse {
base::sway_mouse_scroll(payload.x, payload.y)
.await
.map(|_| json!({}))
.map_err(anyhow::Error::new)
.into()
}
#[derive(serde::Deserialize, utoipa::ToSchema)]
struct MouseMove {
x: i32,
y: i32,
}

View File

@ -357,7 +357,11 @@ pub enum WSCommand {
Shuffle,
SetSubtitleTrack { track: Option<usize> },
SetLooping { value: bool },
SwayCommand { command: String },
// SwayCommand { command: String },
SwayLaunchBrowser { url: String },
SwayCloseWorkspace { workspace: String },
SwayChangeWorkspace { workspace: String },
SwayGetWorkspaces,
}
async fn handle_message(
@ -448,9 +452,25 @@ async fn handle_message(
.await?;
Ok(None)
}
WSCommand::SwayCommand { command } => {
base::sway_run_command(command).await?;
// WSCommand::SwayCommand { command } => {
// base::sway_run_command(command).await?;
// Ok(None)
// }
WSCommand::SwayLaunchBrowser { url } => {
base::sway_launch_browser(&url).await?;
Ok(None)
}
WSCommand::SwayCloseWorkspace { workspace } => {
base::sway_close_workspace(workspace).await?;
Ok(None)
}
WSCommand::SwayChangeWorkspace { workspace } => {
base::sway_change_workspace(workspace).await?;
Ok(None)
}
WSCommand::SwayGetWorkspaces => {
let workspaces = base::sway_get_workspace_names().await?;
Ok(Some(json!(workspaces)))
}
}
}