start playing around
This commit is contained in:
parent
c079eb855d
commit
19681a0db8
3 changed files with 442 additions and 211 deletions
559
Cargo.lock
generated
559
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -46,5 +46,9 @@ simple-signal = "1.1"
|
||||||
dashmap = "3.11"
|
dashmap = "3.11"
|
||||||
unescape = "0.1"
|
unescape = "0.1"
|
||||||
|
|
||||||
|
tokio = { version = "1.0", features = ["full"] }
|
||||||
|
async-stream = "0.3"
|
||||||
|
futures-core = "0.3"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_assertions = "0.6.1"
|
pretty_assertions = "0.6.1"
|
||||||
|
|
|
@ -6,6 +6,10 @@ use std::{
|
||||||
os::unix::{io::AsRawFd, net},
|
os::unix::{io::AsRawFd, net},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
use tokio::{
|
||||||
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
|
sync::mpsc::*,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn initialize_server(should_detach: bool, action: opts::ActionWithServer) -> Result<()> {
|
pub fn initialize_server(should_detach: bool, action: opts::ActionWithServer) -> Result<()> {
|
||||||
let _ = std::fs::remove_file(&*crate::IPC_SOCKET_PATH);
|
let _ = std::fs::remove_file(&*crate::IPC_SOCKET_PATH);
|
||||||
|
@ -31,10 +35,12 @@ pub fn initialize_server(should_detach: bool, action: opts::ActionWithServer) ->
|
||||||
let eww_config = config::EwwConfig::read_from_file(&config_file_path)?;
|
let eww_config = config::EwwConfig::read_from_file(&config_file_path)?;
|
||||||
|
|
||||||
gtk::init()?;
|
gtk::init()?;
|
||||||
let (evt_send, evt_recv) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
// let (evt_send, evt_recv) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
||||||
|
let (ui_send, ui_recv) = tokio::sync::mpsc::unbounded_channel();
|
||||||
|
let glib_context = glib::MainContext::default();
|
||||||
|
|
||||||
log::info!("Initializing script var handler");
|
log::info!("Initializing script var handler");
|
||||||
let script_var_handler = script_var_handler::ScriptVarHandler::new(evt_send.clone())?;
|
let script_var_handler = script_var_handler::ScriptVarHandler::new(ui_send.clone())?;
|
||||||
|
|
||||||
let mut app = app::App {
|
let mut app = app::App {
|
||||||
eww_state: EwwState::from_default_vars(eww_config.generate_initial_state()?),
|
eww_state: EwwState::from_default_vars(eww_config.generate_initial_state()?),
|
||||||
|
@ -42,7 +48,7 @@ pub fn initialize_server(should_detach: bool, action: opts::ActionWithServer) ->
|
||||||
windows: HashMap::new(),
|
windows: HashMap::new(),
|
||||||
css_provider: gtk::CssProvider::new(),
|
css_provider: gtk::CssProvider::new(),
|
||||||
script_var_handler,
|
script_var_handler,
|
||||||
app_evt_send: evt_send.clone(),
|
app_evt_send: ui_send.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(screen) = gdk::Screen::get_default() {
|
if let Some(screen) = gdk::Screen::get_default() {
|
||||||
|
@ -65,47 +71,83 @@ pub fn initialize_server(should_detach: bool, action: opts::ActionWithServer) ->
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
run_server_thread(evt_send.clone())?;
|
// run_server_thread(ui_send.clone())?;
|
||||||
let _hotwatch = run_filewatch_thread(&config_file_path, &scss_file_path, evt_send)?;
|
// let _hotwatch = run_filewatch_thread(&config_file_path, &scss_file_path, evt_send.clone())?;
|
||||||
|
|
||||||
evt_recv.attach(None, move |msg| {
|
std::thread::spawn(move || {
|
||||||
app.handle_command(msg);
|
let rt = tokio::runtime::Runtime::new().expect("REEEEEEEEE");
|
||||||
glib::Continue(true)
|
rt.block_on(async {
|
||||||
|
// let _ = tokio::try_join!(async_part(evt_send));
|
||||||
|
let _ = async_part(ui_send.clone()).await;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
glib_context.spawn_local(async move {
|
||||||
|
while let Some(event) = ui_recv.recv().await {
|
||||||
|
app.handle_command(event);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// evt_recv.attach(None, move |msg| {
|
||||||
|
// app.handle_command(msg);
|
||||||
|
// glib::Continue(true)
|
||||||
|
//});
|
||||||
|
|
||||||
gtk::main();
|
gtk::main();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_server_thread(evt_send: glib::Sender<app::EwwCommand>) -> Result<()> {
|
async fn async_part(evt_send: UnboundedSender<app::EwwCommand>) -> Result<()> {
|
||||||
std::thread::spawn(move || {
|
Ok(())
|
||||||
let result: Result<_> = try {
|
}
|
||||||
log::info!("Starting up eww server");
|
|
||||||
let listener = net::UnixListener::bind(&*crate::IPC_SOCKET_PATH)?;
|
// async fn async_server_accept() -> Result<impl futures_core::stream::Stream<Item = tokio::net::UnixStream>> {
|
||||||
for stream in listener.incoming() {
|
// let listener = tokio::net::UnixListener::bind(&*crate::IPC_SOCKET_PATH)?;
|
||||||
|
// let stream = async_stream::stream! {
|
||||||
|
// loop {
|
||||||
|
// match listener.accept().await {
|
||||||
|
// Ok((mut stream, _addr)) => yield stream,
|
||||||
|
// Err(e) => {
|
||||||
|
// eprintln!("Failed to connect to client: {:?}", e);
|
||||||
|
//};
|
||||||
|
// Ok(stream)
|
||||||
|
//}
|
||||||
|
|
||||||
|
async fn async_server(evt_send: UnboundedSender<app::EwwCommand>) -> Result<()> {
|
||||||
|
let listener = tokio::net::UnixListener::bind(&*crate::IPC_SOCKET_PATH)?;
|
||||||
|
loop {
|
||||||
|
match listener.accept().await {
|
||||||
|
Ok((mut stream, _addr)) => {
|
||||||
try_logging_errors!("handling message from IPC client" => {
|
try_logging_errors!("handling message from IPC client" => {
|
||||||
let mut stream = stream?;
|
let (mut stream_read, mut stream_write) = stream.split();
|
||||||
let action: opts::ActionWithServer = bincode::deserialize_from(&stream)
|
|
||||||
.context("Failed to read or deserialize message from client")?;
|
let action: opts::ActionWithServer = {
|
||||||
|
let mut raw_message = Vec::<u8>::new();
|
||||||
|
stream_read.read_to_end(&mut raw_message);
|
||||||
|
bincode::deserialize(&raw_message).context("Failed to parse client message")?
|
||||||
|
};
|
||||||
|
|
||||||
log::info!("received command from IPC: {:?}", &action);
|
log::info!("received command from IPC: {:?}", &action);
|
||||||
|
|
||||||
|
// TODO clean up the maybe_response_recv
|
||||||
let (command, maybe_response_recv) = action.into_eww_command();
|
let (command, maybe_response_recv) = action.into_eww_command();
|
||||||
|
|
||||||
evt_send.send(command)?;
|
evt_send.send(command)?;
|
||||||
|
|
||||||
if let Some(response_recv) = maybe_response_recv {
|
if let Some(response_recv) = maybe_response_recv {
|
||||||
if let Ok(response) = response_recv.recv_timeout(std::time::Duration::from_millis(100)) {
|
if let Ok(response) = response_recv.recv_timeout(std::time::Duration::from_millis(100)) {
|
||||||
let result = &stream.write_all(response.as_bytes());
|
let result = &stream_write.write_all(response.as_bytes()).await;
|
||||||
crate::print_result_err!("Sending text response to ipc client", &result);
|
crate::print_result_err!("Sending text response to ipc client", &result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
Err(e) => {
|
||||||
if let Err(err) = result {
|
eprintln!("Failed to connect to client: {:?}", e);
|
||||||
eprintln!("error in server thread: {}", err);
|
}
|
||||||
std::process::exit(1);
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_filewatch_thread<P: AsRef<Path>>(
|
fn run_filewatch_thread<P: AsRef<Path>>(
|
||||||
|
|
Loading…
Add table
Reference in a new issue