mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-03 09:31:26 +03:00
enable conditional compilation for all backends
To reduce binary size one can now enable only specific backends, e.g. wayland or x11 via cargo features Additionally adds stubs for libei and xdg-desktop-portal backends
This commit is contained in:
107
src/main.rs
107
src/main.rs
@@ -1,34 +1,11 @@
|
||||
use std::{net::SocketAddr, sync::mpsc, thread};
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::env;
|
||||
use std::sync::mpsc;
|
||||
|
||||
use lan_mouse::{
|
||||
client::{ClientManager, Position},
|
||||
config, dns, event, request,
|
||||
client::ClientManager,
|
||||
consumer, producer,
|
||||
config, event, request,
|
||||
};
|
||||
|
||||
#[cfg(windows)]
|
||||
use lan_mouse::backend::windows;
|
||||
|
||||
#[cfg(unix)]
|
||||
use lan_mouse::backend::{wayland, x11, Backend};
|
||||
|
||||
fn add_client(client_manager: &mut ClientManager, client: &config::Client, pos: Position) {
|
||||
let ip = match client.ip {
|
||||
Some(ip) => ip,
|
||||
None => match &client.host_name {
|
||||
Some(host_name) => match dns::resolve(host_name) {
|
||||
Ok(ip) => ip,
|
||||
Err(e) => panic!("{}", e),
|
||||
},
|
||||
None => panic!("neither ip nor hostname specified"),
|
||||
},
|
||||
};
|
||||
let addr = SocketAddr::new(ip, client.port.unwrap_or(42069));
|
||||
client_manager.add_client(addr, pos);
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
// parse config file
|
||||
let config = config::Config::new("config.toml").unwrap();
|
||||
@@ -42,83 +19,15 @@ pub fn main() {
|
||||
// event channel for consuming events
|
||||
let (consume_tx, consume_rx) = mpsc::sync_channel(128);
|
||||
|
||||
let mut client_manager = ClientManager::new();
|
||||
|
||||
// add clients from config
|
||||
for client in vec![
|
||||
&config.client.left,
|
||||
&config.client.right,
|
||||
&config.client.top,
|
||||
&config.client.bottom,
|
||||
] {
|
||||
if let Some(client) = client {
|
||||
let pos = match client {
|
||||
client if Some(client) == config.client.left.as_ref() => Position::Left,
|
||||
client if Some(client) == config.client.right.as_ref() => Position::Right,
|
||||
client if Some(client) == config.client.top.as_ref() => Position::Top,
|
||||
client if Some(client) == config.client.bottom.as_ref() => Position::Bottom,
|
||||
_ => panic!(),
|
||||
};
|
||||
add_client(&mut client_manager, client, pos);
|
||||
}
|
||||
}
|
||||
// create client manager
|
||||
let mut client_manager = ClientManager::new(&config);
|
||||
|
||||
// start receiving client connection requests
|
||||
let (request_server, request_thread) = request::Server::listen(port).unwrap();
|
||||
|
||||
let clients = client_manager.get_clients();
|
||||
|
||||
#[cfg(unix)]
|
||||
let backend = match env::var("XDG_SESSION_TYPE") {
|
||||
Ok(session_type) => match session_type.as_str() {
|
||||
"x11" => Backend::X11,
|
||||
"wayland" => Backend::WAYLAND,
|
||||
_ => panic!("unknown XDG_SESSION_TYPE"),
|
||||
},
|
||||
Err(_) => panic!("could not detect session type"),
|
||||
};
|
||||
|
||||
#[cfg(windows)]
|
||||
println!("using backend: windows");
|
||||
|
||||
#[cfg(unix)]
|
||||
println!(
|
||||
"using backend: {}",
|
||||
match backend {
|
||||
Backend::X11 => "x11",
|
||||
Backend::WAYLAND => "wayland",
|
||||
}
|
||||
);
|
||||
|
||||
// start producing and consuming events
|
||||
let event_producer = thread::Builder::new()
|
||||
.name("event producer".into())
|
||||
.spawn(move || {
|
||||
#[cfg(windows)]
|
||||
windows::producer::run(produce_tx, request_server, clients);
|
||||
|
||||
#[cfg(unix)]
|
||||
match backend {
|
||||
Backend::X11 => x11::producer::run(produce_tx, request_server, clients),
|
||||
Backend::WAYLAND => wayland::producer::run(produce_tx, request_server, clients),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let clients = client_manager.get_clients();
|
||||
let event_consumer = thread::Builder::new()
|
||||
.name("event consumer".into())
|
||||
.spawn(move || {
|
||||
#[cfg(windows)]
|
||||
windows::consumer::run(consume_rx, clients);
|
||||
|
||||
#[cfg(unix)]
|
||||
match backend {
|
||||
Backend::X11 => x11::consumer::run(consume_rx, clients),
|
||||
Backend::WAYLAND => wayland::consumer::run(consume_rx, clients),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
let event_producer = producer::start(produce_tx, client_manager.get_clients(), request_server);
|
||||
let event_consumer = consumer::start(consume_rx, client_manager.get_clients(), config.backend);
|
||||
|
||||
// start sending and receiving events
|
||||
let event_server = event::server::Server::new(port);
|
||||
|
||||
Reference in New Issue
Block a user