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

refactor(wayland): update to 0.30.0

This is pretty much a rewrite of the Wayland client code for `wayland-client` and `wayland-protocols` v0.30.0, and `smithay-client-toolkit` v0.17.0
This commit is contained in:
Jake Stanger 2023-04-29 22:08:02 +01:00
parent 5c18ec8ba0
commit 7f46cb4976
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
23 changed files with 1779 additions and 1338 deletions

View file

@ -6,7 +6,7 @@ use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};
use tokio::spawn;
use tokio::sync::mpsc;
use tracing::debug;
use tracing::{debug, trace};
#[derive(Debug)]
pub enum ClipboardEvent {
@ -26,6 +26,8 @@ pub struct ClipboardClient {
impl ClipboardClient {
fn new() -> Self {
trace!("Initializing clipboard client");
let senders = Arc::new(Mutex::new(Vec::<(EventSender, usize)>::new()));
let cache = Arc::new(Mutex::new(ClipboardCache::new()));
@ -35,11 +37,21 @@ impl ClipboardClient {
let cache = cache.clone();
spawn(async move {
let mut rx = {
let (mut rx, item) = {
let wl = wayland::get_client().await;
wl.subscribe_clipboard()
};
if let Some(item) = item {
let senders = lock!(senders);
let iter = senders.iter();
for (tx, _) in iter {
try_send!(tx, ClipboardEvent::Add(item.clone()));
}
lock!(cache).insert(item, senders.len());
}
while let Ok(item) = rx.recv().await {
debug!("Received clipboard item (ID: {})", item.id);
@ -59,7 +71,6 @@ impl ClipboardClient {
let iter = senders.iter();
for (tx, sender_cache_size) in iter {
if cache_size == *sender_cache_size {
// let mut cache = lock!(cache);
let removed_id = lock!(cache)
.remove_ref_first()
.expect("Clipboard cache unexpectedly empty");
@ -83,18 +94,11 @@ impl ClipboardClient {
Self { senders, cache }
}
pub async fn subscribe(&self, cache_size: usize) -> mpsc::Receiver<ClipboardEvent> {
pub fn subscribe(&self, cache_size: usize) -> mpsc::Receiver<ClipboardEvent> {
let (tx, rx) = mpsc::channel(16);
let wl = wayland::get_client().await;
wl.roundtrip();
{
let mut cache = lock!(self.cache);
if let Some(item) = wl.get_clipboard() {
cache.insert_or_inc_ref(item);
}
let cache = lock!(self.cache);
let iter = cache.iter();
for (_, (item, _)) in iter {
@ -102,10 +106,7 @@ impl ClipboardClient {
}
}
{
let mut senders = lock!(self.senders);
senders.push((tx, cache_size));
}
lock!(self.senders).push((tx, cache_size));
rx
}
@ -171,13 +172,6 @@ impl ClipboardCache {
.map(|(item, _)| item)
}
/// Inserts an entry with `ref_count` initial references,
/// or increments the `ref_count` by 1 if it already exists.
fn insert_or_inc_ref(&mut self, item: Arc<ClipboardItem>) {
let mut item = self.cache.entry(item.id).or_insert((item, 0));
item.1 += 1;
}
/// Removes the entry with key `id`.
/// This ignores references.
fn remove(&mut self, id: usize) -> Option<Arc<ClipboardItem>> {