I need to commit more often. First commit BTW

This commit is contained in:
vandechat96
2023-08-03 18:39:33 +02:00
commit e6f53e0d83
24 changed files with 4707 additions and 0 deletions

14
.gitignore vendored Normal file
View File

@@ -0,0 +1,14 @@
### Rust template
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
.idea

4368
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

7
Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[workspace]
members = [
'deckui',
'deckapp',
'lib'
]

2
buildui.sh Normal file
View File

@@ -0,0 +1,2 @@
cross build --release -p deckui --target=aarch64-unknown-linux-gnu && scp -i ~/.ssh/onekey ./target/aarch64-unknown-linux-gnu/release/deckui rockpro64:/home/jika/deckui2 && echo "Uploaded"

14
deckapp/Cargo.toml Normal file
View File

@@ -0,0 +1,14 @@
[package]
name = "deckapp"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lib = {path = "../lib/"}
anyhow = "1.0"
log = "0.4"
env_logger = "0.10"

17
deckapp/src/main.rs Normal file
View File

@@ -0,0 +1,17 @@
use lib::{start_client, start_server, ServerMessage, ClientMessage};
use log::info;
fn main() {
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);
let (sender_s, receiver_c) = start_server().unwrap();
let (sender_c, receiver_s) = start_client().unwrap();
sender_s.send(ServerMessage::NowPlaying("Hello".to_string())).unwrap();
sender_c.send(ClientMessage::PlayPause).unwrap();
loop{
}
}

20
deckui/Cargo.toml Normal file
View File

@@ -0,0 +1,20 @@
[package]
name = "deckui"
version = "0.1.0"
build = "build.rs"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[package.metadata.cross.build]
default-target = "aarch64-unknown-linux-gnu"
[[bin]]
path = "src/main.rs"
name = "deckui"
[dependencies]
slint = "1.0"
[build-dependencies]
slint-build = "1.0"

3
deckui/build.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
slint_build::compile("ui/appwindow.slint").unwrap();
}

BIN
deckui/icons/back.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

BIN
deckui/icons/loop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

BIN
deckui/icons/pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

BIN
deckui/icons/play-pause.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
deckui/icons/play.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
deckui/icons/rec.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
deckui/icons/shuffle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
deckui/icons/skip.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
deckui/icons/stop.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

19
deckui/src/main.rs Normal file
View File

@@ -0,0 +1,19 @@
slint::include_modules!();
fn main() -> Result<(), slint::PlatformError> {
let ui = AppWindow::new()?;
let ui_handle = ui.as_weak();
ui.global::<MediaLogic>().on_play_pause(move || {
ui_handle.unwrap().set_now_playing("a".into());
});
ui.global::<MediaLogic>().on_next(move || {
});
ui.global::<MediaLogic>().on_previous(move || {
});
ui.run()
}

24
deckui/ui/appwindow.slint Normal file
View File

@@ -0,0 +1,24 @@
import { Button, VerticalBox } from "std-widgets.slint";
import { MediaControls, MediaLogic } from "widgets/mediacontrols.slint";
export { MediaLogic }
export component AppWindow inherits Window {
background: white;
in-out property <string> now_playing: "test";
VerticalLayout {
alignment: center;
MediaControls { }
Text {
horizontal-alignment: center;
color: black;
text: root.now_playing;
font-size:50px;
font-weight: 600;
}
}
}

View File

@@ -0,0 +1,19 @@
export component Clickable inherits TouchArea {
in property <image> icon;
callback action;
Image {
width: 128px;
height: 128px;
colorize: root.pressed ? #4D4D4D: black;
source: icon;
}
clicked => {
action();
}
}

View File

@@ -0,0 +1,36 @@
import { Clickable } from "clickable.slint";
import { HorizontalBox } from "std-widgets.slint";
export global MediaLogic {
pure callback play-pause();
pure callback next();
pure callback previous();
}
export component MediaControls inherits Clickable {
in-out property <bool> playing: false;
HorizontalBox {
width: 450px;
Clickable {
icon: @image-url("../../icons/back.png");
}
Clickable {
icon: playing ? @image-url("../../icons/pause.png"): @image-url("../../icons/play.png");
action => {
root.playing = !root.playing;
MediaLogic.play-pause();
}
}
Clickable {
icon: @image-url("../../icons/skip.png");
}
}
}

16
lib/Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "lib"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0"
thiserror = "1.0"
log = "0.4"
crossbeam = "0.8"
serde = { version = "1.0", features = ["derive"] }
rmp-serde = "1.1"

140
lib/src/lib.rs Normal file
View File

@@ -0,0 +1,140 @@
use std::{
io::{self, Read, Write},
net::{TcpListener, TcpStream},
result::Result::Ok,
thread,
time::Duration,
};
use anyhow::Result;
use crossbeam::channel::{self, Receiver, Sender};
use log::{error, info};
use rmp_serde::{Deserializer, Serializer};
use serde::{Deserialize, Serialize};
pub fn create_listener(address: &str, port: u16) -> TcpListener {
TcpListener::bind((address, port)).unwrap_or_else(|e| {
if e.kind() == io::ErrorKind::AddrInUse {
info!("Port {} is already being used by another program", port);
std::process::exit(1);
} else if e.kind() == io::ErrorKind::AddrNotAvailable {
info!("Address {} not available", address);
std::process::exit(1);
} else {
panic!("{:?}", e);
}
})
}
#[derive(Serialize, Deserialize, Debug)]
pub enum ClientMessage {
Next,
Previous,
PlayPause,
}
#[derive(Serialize, Deserialize, Debug)]
pub enum ServerMessage {
NowPlaying(String),
}
pub fn start_server() -> Result<(Sender<ServerMessage>, Receiver<ClientMessage>)> {
let (sender_c, reciever_c) = channel::bounded(4);
let (sender_s, reciever_s) = channel::bounded(4);
let server = create_listener("0.0.0.0", 6487);
info!("Listening on port {}", 6487);
server.set_nonblocking(true)?;
thread::spawn(move || {
for stream_result in server.incoming() {
match stream_result {
Ok(stream) => {
let reciever_s = reciever_s.clone();
let sender_c = sender_c.clone();
thread::spawn(move || {
if let Err(error) = handle_connection(stream, reciever_s, sender_c,"Client") {
error!("error while handling stream: {}", error);
}
})
} // Spawn a new thread, ignore the return value because we don't need to join threads
_ => continue,
};
}
});
Ok((sender_s, reciever_c))
}
pub fn start_client() -> Result<(Sender<ClientMessage>, Receiver<ServerMessage>)> {
let (sender_c, reciever_c) = channel::bounded(4);
let (sender_s, reciever_s) = channel::bounded(4);
info!("Connecting");
let server = TcpStream::connect("127.0.0.1:6487")?;
info!("Connected successfully to {}", "127.0.0.1:6487");
server.set_nonblocking(true)?;
thread::spawn(move || {
handle_connection(server, reciever_c, sender_s, "Server") });
Ok((sender_c, reciever_s))
}
fn handle_connection<R,S>(
mut stream: TcpStream,
reciever: Receiver<R>,
sender: Sender<S>,
origin: &str,
) -> Result<()>
where R : Serialize
{
loop {
thread::sleep(Duration::from_millis(200));
if !reciever.is_empty() {
let msg: R = reciever.recv()?;
let mut msg_buf = Vec::new();
msg.serialize(&mut Serializer::new(&mut msg_buf))?;
stream.write(&msg_buf)?;
}
handle_read(&mut stream, origin)?;
}
}
fn handle_read(stream: &mut TcpStream, origin: &str) -> Result<()> {
let mut byte_read = 0;
let mut read_buff = [0u8; 4096];
if byte_read == 0 {
byte_read = match stream.read(&mut read_buff) {
Ok(0) => return Ok(()), // Socket closed
Ok(n) => n,
Err(e) if e.kind() == io::ErrorKind::WouldBlock => 0, // If there is no data, return 0 bytes written
Err(_) => return Ok(()),
};
}
// Write data from tunnel to stream
if byte_read > 0 {
info!("[{origin}] {:?}", &read_buff[0..byte_read]);
if origin == "Server" {
let a: ServerMessage = rmp_serde::from_slice(&read_buff[0..byte_read]).unwrap();
info!("{:?}", a);
} else {
let a: ClientMessage = rmp_serde::from_slice(&read_buff[0..byte_read]).unwrap();
info!("{:?}", a);
}
//sender_c.send(msg);
}
Ok(())
}

8
runcc.yml Normal file
View File

@@ -0,0 +1,8 @@
commands:
lib: |
cargo watch -d 2 -w lib -x "build -p lib"
deckui: |
cargo watch -d 2 -w deckui -- sh ./buildui.sh
deckapp: |
cargo watch -d 2 -w deckapp -w lib -x "run -p deckapp"