2023-01-24 19:43:26 +07:00
|
|
|
use futures_util::stream::StreamExt;
|
2023-02-03 13:07:57 +07:00
|
|
|
use std::collections::HashSet;
|
2023-01-24 19:43:26 +07:00
|
|
|
use swayipc_async::{Connection, Event, EventType, WindowChange};
|
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
async fn main() {
|
2023-02-03 13:07:57 +07:00
|
|
|
let sys_dbus = Box::leak(Box::new(zbus::Connection::system().await.unwrap()));
|
|
|
|
let _ses_dbus = Box::leak(Box::new(zbus::Connection::session().await.unwrap()));
|
2023-01-24 19:43:26 +07:00
|
|
|
|
|
|
|
let mut handlers = Vec::<Box<dyn SwayIpcHandler>>::new();
|
|
|
|
for args in std::env::args().skip(1) {
|
|
|
|
handlers.push(match args.as_str() {
|
2023-02-03 13:07:57 +07:00
|
|
|
"system76-scheduler" => Box::new(System76::new(sys_dbus).await),
|
2023-01-24 19:43:26 +07:00
|
|
|
_ => panic!("handler not supported"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if handlers.is_empty() {
|
|
|
|
panic!("no handlers set up");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut subs = HashSet::new();
|
|
|
|
for handler in &mut handlers {
|
|
|
|
handler.register(&mut subs);
|
|
|
|
}
|
|
|
|
let subs = subs.into_iter().collect::<Vec<_>>();
|
2023-02-03 13:07:57 +07:00
|
|
|
loop {
|
|
|
|
drop(start(&subs, &mut handlers).await);
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 19:43:26 +07:00
|
|
|
|
2023-02-03 13:07:57 +07:00
|
|
|
async fn start(subs: &[EventType], handlers: &mut [Box<dyn SwayIpcHandler>]) -> Result<(), swayipc_async::Error> {
|
2023-01-24 19:43:26 +07:00
|
|
|
let mut events = Connection::new()
|
2023-02-03 13:07:57 +07:00
|
|
|
.await?
|
2023-01-24 19:43:26 +07:00
|
|
|
.subscribe(&subs)
|
2023-02-03 13:07:57 +07:00
|
|
|
.await?;
|
2023-01-24 19:43:26 +07:00
|
|
|
while let Some(event) = events.next().await {
|
2023-02-03 13:07:57 +07:00
|
|
|
match event {
|
|
|
|
Ok(event) => {
|
|
|
|
for handler in &mut *handlers {
|
|
|
|
handler.handle(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => match err {
|
|
|
|
swayipc_async::Error::Io(_)
|
|
|
|
| swayipc_async::Error::InvalidMagic(_)
|
|
|
|
| swayipc_async::Error::SubscriptionFailed(_)
|
|
|
|
=> return Err(err),
|
|
|
|
_ => {}
|
2023-01-24 19:43:26 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-03 13:07:57 +07:00
|
|
|
Ok(())
|
2023-01-24 19:43:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
trait SwayIpcHandler {
|
|
|
|
fn register(&mut self, subs: &mut HashSet<EventType>);
|
|
|
|
fn handle(&mut self, event: &Event);
|
|
|
|
}
|
|
|
|
|
2023-02-03 13:07:57 +07:00
|
|
|
#[zbus::dbus_proxy(
|
|
|
|
interface = "com.system76.Scheduler",
|
|
|
|
default_service = "com.system76.Scheduler",
|
|
|
|
default_path = "/com/system76/Scheduler"
|
|
|
|
)]
|
|
|
|
pub trait System76Scheduler {
|
|
|
|
/// This process will have its process group prioritized over background processes
|
|
|
|
fn set_foreground_process(&mut self, pid: u32) -> zbus::fdo::Result<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct System76<'a> {
|
|
|
|
proxy: System76SchedulerProxy<'a>,
|
2023-01-24 19:43:26 +07:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:07:57 +07:00
|
|
|
impl<'a> System76<'a> {
|
|
|
|
pub async fn new(dbus: &'a zbus::Connection) -> System76<'a> {
|
|
|
|
let proxy = System76SchedulerProxy::new(dbus).await.unwrap();
|
|
|
|
Self { proxy }
|
2023-01-24 19:43:26 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 13:07:57 +07:00
|
|
|
impl SwayIpcHandler for System76<'static> {
|
2023-01-24 19:43:26 +07:00
|
|
|
fn register(&mut self, subs: &mut HashSet<EventType>) {
|
|
|
|
subs.insert(EventType::Window);
|
|
|
|
}
|
|
|
|
fn handle(&mut self, event: &Event) {
|
|
|
|
if let Event::Window(window) = event {
|
|
|
|
if window.change != WindowChange::Focus {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Some(pid) = window.container.pid.and_then(|x| u32::try_from(x).ok()) {
|
2023-02-03 13:07:57 +07:00
|
|
|
let mut proxy = self.proxy.clone();
|
2023-01-24 19:43:26 +07:00
|
|
|
tokio::spawn(async move {
|
2023-02-03 13:07:57 +07:00
|
|
|
drop(proxy.set_foreground_process(pid).await);
|
2023-01-24 19:43:26 +07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-25 01:33:07 +07:00
|
|
|
|