1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-17 14:51:04 +02:00

refactor(keys): switch to colpetto

Switches from the `input` crate to new async `colpetto`
This commit is contained in:
Jake Stanger 2025-01-16 23:01:48 +00:00
parent f204b24ba0
commit a0231559d0
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
6 changed files with 1188 additions and 1020 deletions

View file

@ -8,17 +8,19 @@ use self::offer::{DataControlDeviceOffer, DataControlOfferHandler};
use self::source::DataControlSourceHandler;
use super::{Client, Environment, Event, Request, Response};
use crate::{Ironbar, lock, spawn, try_send};
use color_eyre::Result;
use device::DataControlDevice;
use glib::Bytes;
use nix::fcntl::{F_GETPIPE_SZ, F_SETPIPE_SZ, fcntl};
use nix::sys::epoll::{Epoll, EpollCreateFlags, EpollEvent, EpollFlags, EpollTimeout};
use rustix::event::epoll;
use rustix::event::epoll::CreateFlags;
use rustix::pipe::{fcntl_getpipe_size, fcntl_setpipe_size};
use smithay_client_toolkit::data_device_manager::WritePipe;
use std::cmp::min;
use std::ffi::c_int;
use std::fmt::{Debug, Formatter};
use std::fs::File;
use std::io::{ErrorKind, Write};
use std::os::fd::RawFd;
use std::os::fd::{AsRawFd, OwnedFd};
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::sync::Arc;
use std::{fs, io};
use tokio::io::AsyncReadExt;
@ -154,7 +156,7 @@ impl Environment {
let source = self
.data_control_device_manager_state
.create_copy_paste_source(&self.queue_handle, [INTERNAL_MIME_TYPE, &item.mime_type]);
.create_copy_paste_source(&self.queue_handle, [&item.mime_type, INTERNAL_MIME_TYPE]);
source.set_selection(&device.device);
self.copy_paste_sources.push(source);
@ -273,7 +275,7 @@ impl DataControlSourceHandler for Environment {
source: &ZwlrDataControlSourceV1,
mime: String,
write_pipe: WritePipe,
) {
) -> Result<()> {
debug!("Handler received source send request event ({mime})");
if let Some(item) = lock!(self.clipboard).clone() {
@ -294,28 +296,26 @@ impl DataControlSourceHandler for Environment {
),
};
let pipe_size = set_pipe_size(fd.as_raw_fd(), bytes.len())
.expect("Failed to increase pipe size");
let pipe_size =
set_pipe_size(fd.as_fd(), bytes.len()).expect("Failed to increase pipe size");
let mut file = File::from(fd.try_clone().expect("to be able to clone"));
debug!("Writing {} bytes", bytes.len());
let mut events = (0..16).map(|_| EpollEvent::empty()).collect::<Vec<_>>();
let epoll_event = EpollEvent::new(EpollFlags::EPOLLOUT, 0);
let epoll = epoll::create(CreateFlags::CLOEXEC)?;
epoll::add(
&epoll,
fd,
epoll::EventData::new_u64(0),
epoll::EventFlags::OUT,
)?;
let epoll_fd =
Epoll::new(EpollCreateFlags::empty()).expect("to get valid file descriptor");
epoll_fd
.add(fd, epoll_event)
.expect("to send valid epoll operation");
let mut events = epoll::EventVec::with_capacity(16);
let timeout = EpollTimeout::from(100u16);
while !bytes.is_empty() {
let chunk = &bytes[..min(pipe_size as usize, bytes.len())];
let chunk = &bytes[..min(pipe_size, bytes.len())];
epoll_fd
.wait(&mut events, timeout)
.expect("Failed to wait to epoll");
epoll::wait(&epoll, &mut events, 100u16 as c_int)?;
match file.write(chunk) {
Ok(written) => {
@ -331,9 +331,11 @@ impl DataControlSourceHandler for Environment {
debug!("Done writing");
} else {
error!("Failed to find source");
error!("Failed to find source (mime: '{mime}')");
}
}
Ok(())
}
fn cancelled(
@ -358,7 +360,7 @@ impl DataControlSourceHandler for Environment {
/// it will be clamped at this.
///
/// Returns the new size if succeeded.
fn set_pipe_size(fd: RawFd, size: usize) -> io::Result<i32> {
fn set_pipe_size(fd: BorrowedFd, size: usize) -> io::Result<usize> {
// clamp size at kernel max
let max_pipe_size = fs::read_to_string("/proc/sys/fs/pipe-max-size")
.expect("Failed to find pipe-max-size virtual kernel file")
@ -368,23 +370,24 @@ fn set_pipe_size(fd: RawFd, size: usize) -> io::Result<i32> {
let size = min(size, max_pipe_size);
let curr_size = fcntl(fd, F_GETPIPE_SZ)? as usize;
let curr_size = fcntl_getpipe_size(fd)?;
trace!("Current pipe size: {curr_size}");
let new_size = if size > curr_size {
trace!("Requesting pipe size increase to (at least): {size}");
let res = fcntl(fd, F_SETPIPE_SZ(size as i32))?;
fcntl_setpipe_size(fd, size)?;
let res = fcntl_getpipe_size(fd)?;
trace!("New pipe size: {res}");
if res < size as i32 {
if res < size {
return Err(io::Error::last_os_error());
}
res
} else {
size as i32
size
};
Ok(new_size)