Removed jq dependency, bump version to 0.3.0

This commit is contained in:
Benjamin Bädorf 2020-11-19 00:45:36 +01:00
parent 981d4258ab
commit 93a38220e2
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C
3 changed files with 141 additions and 45 deletions

83
Cargo.lock generated
View file

@ -1,6 +1,89 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "itoa"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde_derive 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_derive"
version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_json"
version = "1.0.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "wlstreamer" name = "wlstreamer"
version = "0.2.0" version = "0.2.0"
dependencies = [
"serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
"checksum proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
"checksum quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
"checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
"checksum serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
"checksum serde_derive 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e"
"checksum serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)" = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95"
"checksum syn 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac"
"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"

View file

@ -1,7 +1,9 @@
[package] [package]
name = "wlstreamer" name = "wlstreamer"
version = "0.2.0" version = "0.3.0"
authors = ["Benjamin Bädorf <hello@benjaminbaedorf.eu>"] authors = ["Benjamin Bädorf <hello@benjaminbaedorf.eu>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
serde = { version = "1.0", features = ["derive"]}
serde_json = "1.0"

View file

@ -1,12 +1,27 @@
use serde::{Deserialize, Serialize};
use std::env; use std::env;
use std::io::{BufRead, BufReader, Error, ErrorKind, Write}; use std::io::{BufRead, BufReader, Error};
use std::process::{Child, Command, Stdio}; use std::process::{Child, Command, Stdio};
#[derive(Serialize, Deserialize, Debug)]
struct SwayWorkspace {
id: u32,
name: String,
focus: Vec<u32>,
output: String,
focused: bool,
visible: bool,
num: u32,
#[serde(rename = "type")]
type_name: String,
representation: String,
}
struct Config { struct Config {
current_screen: String, current_screen: String,
output: String, output: String,
screen_blacklist: Vec<String>, screen_blacklist: Vec<String>,
workspace_blacklist: Vec<String>, workspace_blacklist: Vec<u32>,
verbose: bool, verbose: bool,
} }
@ -63,14 +78,15 @@ fn record_screen(config: &mut Config, valid_screens: &Vec<String>) -> Result<Chi
return Ok(cmd); return Ok(cmd);
} else { } else {
let index = get_screen_index(&valid_screens[0]);
let output_str = format!("--file={}", config.output.as_str()); let output_str = format!("--file={}", config.output.as_str());
let screen_str = format!("-o{}", valid_screens[0]);
println!("Outputting to {}", config.output.as_str()); println!("Outputting to {}", config.output.as_str());
let mut cmd = Command::new("wf-recorder") let cmd = Command::new("wf-recorder")
.args(&[ .args(&[
"--muxer=v4l2", "--muxer=v4l2",
"--codec=rawvideo", "--codec=rawvideo",
"--pixel-format=yuyv422", "--pixel-format=yuyv422",
screen_str.as_str(),
output_str.as_str(), output_str.as_str(),
]) ])
.stdin(Stdio::piped()) .stdin(Stdio::piped())
@ -86,11 +102,6 @@ fn record_screen(config: &mut Config, valid_screens: &Vec<String>) -> Result<Chi
}) })
.spawn()?; .spawn()?;
cmd.stdin
.as_mut()
.ok_or(Error::new(ErrorKind::Other, "Recorder process failed"))?
.write_all(index.as_bytes())?;
config.current_screen = valid_screens[0].as_str().to_string(); config.current_screen = valid_screens[0].as_str().to_string();
return Ok(cmd); return Ok(cmd);
@ -98,47 +109,45 @@ fn record_screen(config: &mut Config, valid_screens: &Vec<String>) -> Result<Chi
} }
fn get_valid_screens_for_recording(config: &Config) -> Vec<String> { fn get_valid_screens_for_recording(config: &Config) -> Vec<String> {
let mut command = let mut command = "swaymsg -t get_workspaces";
"swaymsg -t get_workspaces | jq -r 'sort_by(.focused != true) | map(select(.visible"
.to_string();
for screen in &config.screen_blacklist {
command.push_str(" and .output != \"");
command.push_str(screen.as_str());
command.push_str("\"");
}
for workspace in &config.workspace_blacklist {
command.push_str(" and .num != ");
command.push_str(workspace.as_str());
}
command.push_str(")) | map(.output) | .[]'");
let output = Command::new("sh") let output = Command::new("sh")
.args(&["-c", command.as_str()]) .args(&["-c", command])
.output() .output()
.expect("Couldn't get current focus"); .expect("Couldn't get current focus");
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 from get_workspaces"); let stdout_string =
let valid_screens = stdout String::from_utf8(output.stdout).expect("Invalid UTF-8 from get_workspaces");
.split('\n') let mut workspaces: Vec<SwayWorkspace> =
.map(|s| s.to_string()) serde_json::from_str(stdout_string.as_str()).expect("Invalid json from get_workspaces");
.filter(|s| s != "")
.collect();
println!("Found recordable screens: {:?}", valid_screens); if config.verbose {
println!("Found workspaces");
return valid_screens; for elem in workspaces.iter() {
println!("{:?}", elem);
}
} }
fn get_screen_index(screen: &String) -> String { workspaces = workspaces
let mut command = "swaymsg -t get_outputs | jq -r 'map(.name==\"".to_string(); .into_iter()
command.push_str(screen.as_str()); .filter(|w| {
command.push_str("\") | index(true) + 1'"); w.visible
let output = Command::new("sh") && !config
.args(&["-c", command.as_str()]) .screen_blacklist
.output() .iter()
.expect("Couldn't get screen index"); .any(|screen| screen.eq(&w.output))
&& !config.workspace_blacklist.iter().any(|&num| num == w.num)
})
.collect();
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 from get_workspaces"); if config.verbose {
return stdout; println!("Filtered workspaces");
for elem in workspaces.iter() {
println!("{:?}", elem);
}
}
workspaces.sort_by_key(|w| w.focused);
return workspaces.into_iter().map(|w| w.output).collect();
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -160,7 +169,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let arg = &args[i]; let arg = &args[i];
if arg == "--not-ws" { if arg == "--not-ws" {
i += 1; i += 1;
config.workspace_blacklist.push(args[i].clone()); config
.workspace_blacklist
.push(args[i].clone().parse::<u32>().unwrap());
} else if arg == "--not-screen" { } else if arg == "--not-screen" {
i += 1; i += 1;
config.screen_blacklist.push(args[i].clone()); config.screen_blacklist.push(args[i].clone());