added api for swaymsg to greg-ng (untested)

This commit is contained in:
Adrian Gunnar Lauterer 2025-01-24 21:42:31 +01:00
parent 74f5316121
commit 0769229419
Signed by: adriangl
GPG Key ID: D33368A59745C2F0
6 changed files with 75 additions and 0 deletions

23
Cargo.lock generated
View File

@ -625,6 +625,7 @@ dependencies = [
"sd-notify",
"serde",
"serde_json",
"swayipc",
"systemd-journal-logger",
"tempfile",
"tokio",
@ -1501,6 +1502,28 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "swayipc"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b8c50cb2e98e88b52066a35ef791fffd8f6fa631c3a4983de18ba41f718c736"
dependencies = [
"serde",
"serde_json",
"swayipc-types",
]
[[package]]
name = "swayipc-types"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "551233c60323e87cfb8194c21cc44577ab848d00bb7fa2d324a2c7f52609eaff"
dependencies = [
"serde",
"serde_json",
"thiserror 1.0.69",
]
[[package]]
name = "syn"
version = "2.0.90"

View File

@ -9,6 +9,7 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
swayipc = "3.0"
anyhow = "1.0.82"
axum = { version = "0.7.7", features = ["macros", "ws"] }
clap = { version = "4.4.1", features = ["derive"] }

View File

@ -11,6 +11,8 @@ in
mpvPackage = lib.mkPackageOption pkgs "mpv" { };
enableSway = lib.mkEnableOption "sway as the main window manager";
enableFirefox = lib.mkEnableOption "include fiorefox browser for external urls";
enablePipewire = lib.mkEnableOption "pipewire" // { default = true; };
@ -166,6 +168,14 @@ in
wrapperFeatures.gtk = true;
};
(lib.mkIf (cfg.enable && cfg.enableFirefox) {
programs.firefox = {
enable = true;
preferences = {
media.autoplay.default = "0";
};
};
xdg.portal = {
enable = true;
wlr.enable = true;
@ -176,6 +186,7 @@ in
users.greg = {
isNormalUser = true;
group = "greg";
extraGroups [ "audio" "video" "input" ];
uid = 2000;
description = "loud gym bro";
};

View File

@ -170,3 +170,15 @@ pub async fn playlist_set_looping(mpv: Mpv, r#loop: bool) -> anyhow::Result<()>
.await
.map_err(|e| e.into())
}
use swayipc::{Connection, Fallible};
pub async fn run_sway_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()))?
}

View File

@ -32,6 +32,7 @@ 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_command))
.with_state(mpv)
}
@ -401,3 +402,23 @@ async fn playlist_set_looping(
) -> RestResponse {
base::playlist_set_looping(mpv, query.r#loop).await.into()
}
#[derive(serde::Deserialize, utoipa::IntoParams)]
struct SwayCommandArgs {
command: String,
}
/// Execute a sway command
#[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_command(Query(query): Query<SwayCommandArgs>) -> RestResponse {
base::run_sway_command(query.command).await.map_err(anyhow::Error::new).into()
}

View File

@ -25,6 +25,8 @@ use tokio::{select, sync::watch};
use crate::util::IdPool;
use super::base;
#[derive(Debug, Clone)]
struct WebsocketState {
mpv: Mpv,
@ -355,6 +357,7 @@ pub enum WSCommand {
Shuffle,
SetSubtitleTrack { track: Option<usize> },
SetLooping { value: bool },
SwayCommand { command: String },
}
async fn handle_message(
@ -445,5 +448,9 @@ async fn handle_message(
.await?;
Ok(None)
}
WSCommand::SwayCommand { command } => {
base::run_sway_command(command).await?;
Ok(None)
}
}
}