Skip to content

Commit

Permalink
use libc directly
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin committed Mar 30, 2024
1 parent 310c875 commit c6da876
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ authors = ["MaxVerevkin <[email protected]>"]
[dependencies]
anyhow = "1"
memchr = "2"
nix = { version = "0.28", default-features = false, features = ["fs"] }
pangocairo = "0.19"
serde_json = "1"
serde = { version = "1", features = ["derive"] }
Expand Down
26 changes: 19 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ mod text;
mod utils;
mod wm_info_provider;

use std::io::ErrorKind;
use std::os::fd::AsRawFd;
use std::io::{self, ErrorKind};
use std::os::fd::{AsRawFd, RawFd};
use std::path::PathBuf;

use clap::Parser;
use event_loop::EventLoop;
use nix::fcntl::OFlag;
use signal_hook::consts::*;
use wayrs_client::{Connection, IoMode};

use event_loop::EventLoop;
use state::State;

#[derive(Parser)]
Expand All @@ -41,16 +40,20 @@ struct Cli {
fn main() -> anyhow::Result<()> {
let args = Cli::parse();

let (sig_read, sig_write) = nix::unistd::pipe2(OFlag::O_NONBLOCK | OFlag::O_CLOEXEC)?;
let [sig_read, sig_write] = pipe(libc::O_NONBLOCK | libc::O_CLOEXEC)?;
signal_hook::low_level::pipe::register(SIGUSR1, sig_write)?;

let (mut conn, globals) = Connection::connect_and_collect_globals()?;
let mut el = EventLoop::new();
let mut state = State::new(&mut conn, &globals, &mut el, args.config.as_deref());
conn.flush(IoMode::Blocking)?;

el.register_with_fd(sig_read.as_raw_fd(), move |ctx| {
nix::unistd::read(sig_read.as_raw_fd(), &mut [0; 1])?;
el.register_with_fd(sig_read, move |ctx| {
let mut buf = [0u8];
assert_eq!(
unsafe { libc::read(sig_read, buf.as_mut_ptr().cast(), 1) },
1
);
ctx.state.toggle_visibility(ctx.conn);
Ok(event_loop::Action::Keep)
});
Expand Down Expand Up @@ -103,3 +106,12 @@ fn main() -> anyhow::Result<()> {
el.run(&mut conn, &mut state)?;
unreachable!();
}

fn pipe(flags: libc::c_int) -> io::Result<[RawFd; 2]> {
let mut fds = [0; 2];
if unsafe { libc::pipe2(fds.as_mut_ptr(), flags) } == -1 {
Err(io::Error::last_os_error())
} else {
Ok(fds)
}
}
9 changes: 4 additions & 5 deletions src/status_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{BufWriter, ErrorKind, Write};
use std::io::{self, BufWriter, ErrorKind, Write};
use std::os::unix::io::AsRawFd;
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};

Expand All @@ -25,10 +25,9 @@ impl StatusCmd {
.spawn()?;
let output = child.stdout.take().unwrap();
let input = BufWriter::new(child.stdin.take().unwrap());
nix::fcntl::fcntl(
output.as_raw_fd(),
nix::fcntl::F_SETFL(nix::fcntl::OFlag::O_NONBLOCK),
)?;
if unsafe { libc::fcntl(output.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK) } == -1 {
return Err(io::Error::last_os_error().into());
}
Ok(Self {
child,
output,
Expand Down

0 comments on commit c6da876

Please sign in to comment.