splitting app

irc-tor
lza_menace 2 years ago
parent 190bf16af7
commit 6a193faf8e

@ -0,0 +1,80 @@
use eframe::egui;
use crate::player::Player;
pub struct App {
pub player: Player,
pub tor_started: bool,
pub to_data: String
}
impl Default for App {
fn default() -> Self {
Self {
tor_started: false,
to_data: "".to_owned(),
player: Player::default()
}
}
}
impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("lza gui");
ui.label("Made by ya boi, lza_menace");
ui.hyperlink("https://lzahq.tech");
// ui.separator();
ui.horizontal(|ui| {
if ui.button("▶").clicked() {
// If stream thread is done, start again
if self.player.stream_thread.is_finished() {
ui.label("Starting stream...");
std::thread::sleep(std::time::Duration::from_secs(3));
self.player.stream_thread = self.player.start_radio_stream();
}
if self.player.sink.len() != 1 {
let source: rodio::Decoder<std::io::BufReader<std::fs::File>> = self.player.get_radio_source();
let _ = &self.player.sink.append(source);
}
let _ = &self.player.sink.play();
}
if ui.button("■").clicked() {
let _ = &self.player.sink.pause();
}
if ui.button("status").clicked() {
println!(
"is_paused: {}. empty: {}. len: {}. volume: {}.",
&self.player.sink.is_paused(),
&self.player.sink.empty(),
&self.player.sink.len(),
&self.player.sink.volume()
)
}
});
// ui.separator();
// ui.heading("Tor");
// if self.tor_started {
// ui.label("Tor started.");
// if ui.button("Clear Tor logs").clicked() {
// let _r: std::io::Result<()> = clear_tor_log();
// }
// ui.collapsing("View Tor logs:", |ui| {
// egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| {
// let contents = std::fs::read_to_string(TOR_LOG.to_string()).unwrap();
// ui.label(contents);
// });
// });
// } else {
// if ui.button("Start Tor").clicked() {
// let _t: std::thread::JoinHandle<Result<u8, libtorError>> = start_tor();
// self.tor_started = true;
// }
// }
});
}
}

@ -1,11 +1,17 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#[allow(dead_code)]
pub use crate::player::Player;
pub use crate::app::App;
use std::{thread, io, fs, time};
use libtor::{Tor, TorFlag, Error as libtorError, log as libtorLog};
use eframe::egui;
#[allow(unused_imports)]
use rodio::{Decoder, OutputStream, source::Source, Sink};
mod app;
mod player;
// "http://wowradiod6mhb4ignjqy5ghf5l42yh2yeumgeq3yi7gn7yqy3efhe5ad.onion"
// "http://wowradiof5wx62w4avdk5fwbvcoea2z4ul2q3awffn5vmfaa3vhlgiid.onion"
@ -52,118 +58,17 @@ fn get_wow_price() -> String {
return res
}
fn start_radio_stream() -> thread::JoinHandle<()> {
let t: Result<thread::JoinHandle<()>, io::Error> = thread::Builder::new().name("radio_stream".to_string()).spawn(move || {
let mut res: reqwest::blocking::Response = reqwest::blocking::get("https://radio.wownero.com/wow.ogg").expect("request failed");
let mut out: fs::File = fs::File::create(RADIO_STREAM).expect("failed to create file");
io::copy(&mut res, &mut out).expect("failed to copy content");
});
return t.unwrap()
}
fn get_radio_source() -> Decoder<io::BufReader<fs::File>> {
// let _: fs::File = fs::File::create(RADIO_STREAM).expect("failed to create file");
let file: io::BufReader<fs::File> = io::BufReader::new(fs::File::open(RADIO_STREAM).unwrap());
let source: Decoder<io::BufReader<fs::File>> = Decoder::new(file).unwrap();
return source
}
fn get_radio_sink() -> Sink {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
return sink
}
struct LzaGUI {
tor_started: bool,
to_data: String,
sink: Sink,
stream_thread: thread::JoinHandle<()>
}
impl Default for LzaGUI {
fn default() -> Self {
Self {
tor_started: false,
to_data: "".to_owned(),
sink: get_radio_sink(),
stream_thread: thread::spawn(|| {})
}
}
}
impl eframe::App for LzaGUI {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("lza gui");
ui.label("Made by ya boi, lza_menace");
ui.hyperlink("https://lzahq.tech");
// ui.separator();
ui.horizontal(|ui| {
if ui.button("▶").clicked() {
// If stream thread is done, start again
if self.stream_thread.is_finished() {
self.stream_thread = start_radio_stream();
}
thread::sleep(time::Duration::from_secs(3));
let source: Decoder<io::BufReader<fs::File>> = get_radio_source();
let _ = &self.sink.append(source);
let _ = &self.sink.play();
}
if ui.button("■").clicked() {
let _ = &self.sink.stop();
}
if ui.button("status").clicked() {
println!(
"is_paused: {}. empty: {}. len: {}. volume: {}.",
&self.sink.is_paused(),
&self.sink.empty(),
&self.sink.len(),
&self.sink.volume()
)
}
});
ui.separator();
if ui.button("Get TO Market Data").clicked() {
println!("Getting TO market data");
self.to_data = get_wow_price();
}
ui.label(&self.to_data);
ui.separator();
ui.heading("Tor");
if self.tor_started {
ui.label("Tor started.");
if ui.button("Clear Tor logs").clicked() {
let _r: io::Result<()> = clear_tor_log();
}
ui.collapsing("View Tor logs:", |ui| {
egui::ScrollArea::vertical().stick_to_bottom(true).show(ui, |ui| {
let contents = fs::read_to_string(TOR_LOG.to_string()).unwrap();
ui.label(contents);
});
});
} else {
if ui.button("Start Tor").clicked() {
let _t: thread::JoinHandle<Result<u8, libtorError>> = start_tor();
self.tor_started = true;
}
}
});
}
}
fn main() {
let options = eframe::NativeOptions::default();
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
let sink: rodio::Sink = rodio::Sink::try_new(&stream_handle).unwrap();
let player: player::Player = player::Player::new(sink, stream_handle);
let mut app: app::App = app::App::default();
app.player = player;
let options: eframe::NativeOptions = eframe::NativeOptions::default();
eframe::run_native(
"lza gui",
options,
Box::new(|_cc| Box::new(LzaGUI::default())),
Box::new(|_cc| Box::new(app)),
);
let _ = cleanup();
}

@ -0,0 +1,51 @@
pub struct Player {
pub sink: rodio::Sink,
pub stream_thread: std::thread::JoinHandle<()>,
pub stream_handle: rodio::OutputStreamHandle,
pub volume: f32
}
impl Player {
pub fn new(sink: rodio::Sink, stream_handle: rodio::OutputStreamHandle) -> Self {
Self {
sink,
stream_handle,
stream_thread: std::thread::spawn(|| {}),
volume: 1.0
}
}
pub fn default() -> Self {
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&stream_handle).unwrap();
Self::new(sink, stream_handle)
}
pub fn start_radio_stream(&self) -> std::thread::JoinHandle<()> {
let t: Result<std::thread::JoinHandle<()>, std::io::Error> = std::thread::Builder::new().name("radio_stream".to_string()).spawn(move || {
let mut res: reqwest::blocking::Response = reqwest::blocking::get("https://radio.wownero.com/wow.ogg").expect("request failed");
let mut out: std::fs::File = std::fs::File::create(crate::RADIO_STREAM).expect("failed to create file");
std::io::copy(&mut res, &mut out).expect("failed to copy content");
});
return t.unwrap()
}
pub fn get_radio_source(&self) -> rodio::Decoder<std::io::BufReader<std::fs::File>> {
let file: std::io::BufReader<std::fs::File> = std::io::BufReader::new(std::fs::File::open(crate::RADIO_STREAM).unwrap());
let source: rodio::Decoder<std::io::BufReader<std::fs::File>> = rodio::Decoder::new(file).unwrap();
return source
}
pub fn get_radio_sink(&self) -> rodio::Sink {
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
let sink: rodio::Sink = rodio::Sink::try_new(&stream_handle).unwrap();
return sink
}
}