From 1851c56e407cac72ca104cc5e75692cd9c648986 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Mon, 8 Aug 2022 23:12:23 +0300 Subject: rewrite using latest walayd libs & implement HiDPI --- src/status_cmd.rs | 71 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 31 deletions(-) (limited to 'src/status_cmd.rs') diff --git a/src/status_cmd.rs b/src/status_cmd.rs index a229200..4d9c453 100644 --- a/src/status_cmd.rs +++ b/src/status_cmd.rs @@ -1,18 +1,22 @@ -use std::io::{BufRead, BufReader, Result, Write}; +use std::io::{BufRead, BufReader, Write}; use std::os::unix::prelude::AsRawFd; use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio}; -use smithay_client_toolkit::reexports::calloop::{self, LoopHandle}; +use anyhow::Result; + +use tokio::io::unix::AsyncFd; +use tokio::io::Interest; use crate::i3bar_protocol::{Block, Event, Protocol}; -use crate::BarState; +#[derive(Debug)] pub struct StatusCmd { - child: Child, + pub child: Child, output: BufReader, input: ChildStdin, protocol: Protocol, buf: Vec, + pub async_fd: AsyncFd, } impl StatusCmd { @@ -24,17 +28,22 @@ impl StatusCmd { .spawn()?; let output = child.stdout.take().unwrap(); let input = child.stdin.take().unwrap(); + let async_fd = AsyncFd::with_interest(output.as_raw_fd(), Interest::READABLE)?; Ok(Self { child, output: BufReader::new(output), input, protocol: Protocol::Unknown, buf: Vec::new(), + async_fd, }) } pub fn notify_available(&mut self) -> Result>> { let buf = self.output.fill_buf()?; + if buf.is_empty() { + bail!("status command exited"); + } if self.buf.is_empty() { let rem = self.protocol.process_new_bytes(buf)?; if !rem.is_empty() { @@ -57,35 +66,35 @@ impl StatusCmd { pub fn send_click_event(&mut self, event: &Event) -> Result<()> { if self.protocol.supports_clicks() { - writeln!(self.input, "{}", serde_json::to_string(event).unwrap(),)?; + writeln!(self.input, "{}", serde_json::to_string(event).unwrap())?; } Ok(()) } - pub fn quick_insert(&self, handle: LoopHandle) { - handle - .insert_source( - calloop::generic::Generic::new( - self.output.get_ref().as_raw_fd(), - calloop::Interest { - readable: true, - writable: false, - }, - calloop::Mode::Level, - ), - move |ready, _, bar_state| { - if ready.readable { - bar_state.notify_available()?; - Ok(calloop::PostAction::Continue) - } else { - bar_state.set_error("error reading from status command"); - if let Some(mut child) = bar_state.status_cmd.take() { - let _ = child.child.kill(); - } - Ok(calloop::PostAction::Remove) - } - }, - ) - .expect("failed to inser calloop source"); - } + // pub fn quick_insert(&self, handle: LoopHandle) { + // handle + // .insert_source( + // calloop::generic::Generic::new( + // self.output.get_ref().as_raw_fd(), + // calloop::Interest { + // readable: true, + // writable: false, + // }, + // calloop::Mode::Level, + // ), + // move |ready, _, bar_state| { + // if ready.readable { + // bar_state.notify_available()?; + // Ok(calloop::PostAction::Continue) + // } else { + // bar_state.set_error("error reading from status command"); + // if let Some(mut child) = bar_state.status_cmd.take() { + // let _ = child.child.kill(); + // } + // Ok(calloop::PostAction::Remove) + // } + // }, + // ) + // .expect("failed to inser calloop source"); + // } } -- cgit v1.3.1