Initial commit

This commit is contained in:
Andre Henriques 2023-05-10 20:46:25 +01:00
commit 92cedda692
6 changed files with 1525 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
/target
result
# Added by cargo
#
# already existing elements were commented out
#/target

1282
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "lightcontroll-discord"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pipewire = "0.6.0"
libspa = "0.6.0"
regex = "1.7.1"
reqwest = { version = "0.11", features = ["blocking", "json"] }

81
flake.lock Normal file
View File

@ -0,0 +1,81 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1681202837,
"narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "cfacdce06f30d2b68473a46042957675eebb3401",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1683741689,
"narHash": "sha256-VY6gjqAFQe0Xyz+olc979zbsW9dC4VG+mINGffFKVEw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f431ee4a85cb985075b4ed27596913e8087f4264",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1683708507,
"narHash": "sha256-i5zgWcuyZcNBnlrzVjhpAFQdJWr4OyhvQ1owAEHFFKw=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "ed55dc022aa23ed3c42f383cf1782290b3b939d5",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

59
flake.nix Normal file
View File

@ -0,0 +1,59 @@
{
description = "monitors discord to controll lights";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
rust-overlay.url = "github:oxalica/rust-overlay";
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, rust-overlay }:
let
# Systems supported
allSystems = [
"x86_64-linux" # 64-bit Intel/AMD Linux
"aarch64-linux" # 64-bit ARM Linux
"x86_64-darwin" # 64-bit Intel macOS
"aarch64-darwin" # 64-bit ARM macOS
];
# Helper to provide system-specific attributes
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
pkgs = import nixpkgs { inherit system; };
});
in
{
packages = forAllSystems ({ pkgs }: {
default = let
stdenv = pkgs.stdenv;
lib = pkgs.lib;
in
pkgs.rustPlatform.buildRustPackage {
pname = "lightcontroll-discord";
version = "0.1";
src = ./.;
LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib";
# doCheck = false;
# RUST_BACKTRACE=1;
buildInputs = with pkgs; [
pipewire
libspatialaudio
openssl
];
nativeBuildInputs = with pkgs; [
pkg-config
clang
llvmPackages.libclang
];
cargoLock = {
lockFile = ./Cargo.lock;
};
#installPhase = ''
# mkdir -p $out/bin
# #cp ./target/release/pipewire-autoconnect $out/bin
#'';
};
});
};
}

82
src/main.rs Normal file
View File

@ -0,0 +1,82 @@
use std::{rc::Rc, sync::{Mutex, Arc}};
use libspa::ReadableDict;
use pipewire::{MainLoop, Context};
use regex::Regex;
fn deal_with_node(
port: &pipewire::registry::GlobalObject<libspa::ForeignDict>,
ids: AVec
) {
let re = Regex::new(r"WEBRTC VoiceEngine").unwrap();
if let Some(props) = &port.props {
if let (Some(alias), Some(id)) = (props.get("port.alias"), props.get("node.id")) {
if re.is_match(alias) {
let mut a = ids.lock().ok().unwrap();
a.push(id.to_string().parse::<u32>().unwrap());
drop(a);
change(true);
}
}
}
}
type AVec = Arc<Mutex<Vec<u32>>>;
fn deal_remove_node(
id: u32,
ids: AVec
) {
if ids.lock().ok().unwrap().len() == 0 {
return;
}
let ids_clone_c = ids.lock().ok().unwrap().clone();
let idsa: Vec<u32> = ids_clone_c.into_iter().filter(|&f| f != id).collect::<Vec<u32>>();
if idsa.len() == 0 {
change(false);
}
let mut ids_clone = ids.lock().ok().unwrap();
*ids_clone = idsa;
drop(ids_clone);
}
fn change(on: bool) {
println!("change status {}", on);
if on {
let _resp = reqwest::blocking::get("http://aether:3000/start/stop");
} else {
let _resp = reqwest::blocking::get("http://aether:3000/stop/stop");
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Hello, world!");
let main_loop = MainLoop::new()?;
let context = Context::new(&main_loop)?;
let core = Rc::new(context.connect(None)?);
let ids: AVec = Arc::new(Mutex::new(Vec::new()));
let cpy = ids.clone();
let registry = core.get_registry()?;
let _listener = registry
.add_listener_local()
.global(move |global| match global.type_ {
pipewire::types::ObjectType::Port => deal_with_node(global, cpy.clone()),
_ => ()
})
.global_remove(move |id| deal_remove_node(id, ids.clone()))
.register();
main_loop.run();
Ok(())
}