Skip to content

Commit

Permalink
restream.rs: use cli arguments instead of discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
rien committed Jan 28, 2021
1 parent a24f94c commit 45a6648
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions reStream.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,22 @@ case "$rm_version" in
"reMarkable 1.0")
width=1408
height=1872
bytes_per_pixel=2
fb_file="/dev/fb0"
pixel_format="rgb565le"
;;
"reMarkable 2.0")
if ssh_cmd "[ -f /dev/shm/swtfb.01 ]"; then
width=1404
height=1872
bytes_per_pixel=2
fb_file="/dev/shm/swtfb.01"
pixel_format="rgb565le"
else
width=1872
height=1404
bytes_per_pixel=1
fb_file=":mem:"
pixel_format="gray8"
video_filters="$video_filters,transpose=2"
fi
Expand Down Expand Up @@ -191,8 +197,10 @@ fi

set -e # stop if an error occurs

restream_rs='PATH="$PATH:/opt/bin/:." restream'
restream_options="-h $height -w $width -b $bytes_per_pixel -f $fb_file"

# shellcheck disable=SC2089
restream_rs="PATH=\"\$PATH:/opt/bin/:.\" restream $restream_options"
if $unsecure_connection; then
listen_port=16789
ssh_cmd "$restream_rs --listen $listen_port" &
Expand All @@ -202,7 +210,7 @@ else
receive_cmd="ssh_cmd $restream_rs"
fi

# shellcheck disable=SC2086
# shellcheck disable=SC2086,SC2090
$receive_cmd \
| $decompress \
| $host_passthrough \
Expand Down
70 changes: 41 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use lz_fear::CompressionSettings;

use std::default::Default;
use std::fs::File;
use std::path::Path;
use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write};
use std::net::{TcpStream, TcpListener};
use std::process::Command;
Expand All @@ -26,36 +25,55 @@ pub struct Opts {
about = "Listen for an (unsecure) TCP connection to send the data to which reduces some load on the reMarkable and improves fps."
)]
listen: Option<usize>,

#[clap(
long,
name = "height",
short = 'h',
about = "Height (in pixels) of the framebuffer."
)]
height: usize,

#[clap(
long,
name = "width",
short = 'w',
about = "Width (in pixels) of the framebuffer."
)]
width: usize,

#[clap(
long,
name = "bytes",
short = 'b',
about = "How many bytes represent one pixel in the framebuffer."
)]
bytes_per_pixel: usize,

#[clap(
long,
name = "path",
short = 'f',
about = "File containing the framebuffer data. If this equals the string ':mem:' it will try to read the framebuffer from xochitl's process memory (rM2 only)."
)]
file: String,

}

fn main() -> Result<()> {
let ref opts: Opts = Opts::parse();

let version = remarkable_version()?;
let height = 1872;
let streamer = if version == "reMarkable 1.0\n" {
let width = 1408;
let bytes_per_pixel = 2;
ReStreamer::init("/dev/fb0", 0, width, height, bytes_per_pixel)?
} else if version == "reMarkable 2.0\n" {
let width = 1404;
if Path::new("/dev/shm/swtfb.01").exists() {
let bytes_per_pixel = 2;
ReStreamer::init("/dev/shm/swtfb.01", 0, width, height, bytes_per_pixel)?
} else {
let bytes_per_pixel = 1;
let pid = xochitl_pid()?;
let offset = rm2_fb_offset(pid)?;
let mem = format!("/proc/{}/mem", pid);
ReStreamer::init(&mem, offset, width, height, bytes_per_pixel)?
}
let (file, offset) = if opts.file == ":mem:" {
let pid = xochitl_pid()?;
let offset = rm2_fb_offset(pid)?;
let mem = format!("/proc/{}/mem", pid);
(mem, offset)
} else {
Err(anyhow!(
"Unknown reMarkable version: {}\nPlease open a feature request to support your device.",
version
))?
(opts.file.to_owned(), 0)
};

let streamer = ReStreamer::init(&file, offset, opts.width, opts.height, opts.bytes_per_pixel)?;

let stdout = std::io::stdout();
let data_target: Box<dyn Write> = if let Some(port) = opts.listen {
Box::new(listen_timeout(port, Duration::from_secs(3))?)
Expand Down Expand Up @@ -85,12 +103,6 @@ fn listen_timeout(port: usize, timeout: Duration) -> Result<TcpStream> {
Ok(conn)
}

fn remarkable_version() -> Result<String> {
let content = std::fs::read("/sys/devices/soc0/machine")
.context("Failed to read /sys/devices/soc0/machine")?;
Ok(String::from_utf8(content)?)
}

fn xochitl_pid() -> Result<usize> {
let output = Command::new("/bin/pidof")
.args(&["xochitl"])
Expand Down

0 comments on commit 45a6648

Please sign in to comment.