Skip to content

Commit

Permalink
Merge pull request #130 from JakeStanger/refactor/wayland-0.30
Browse files Browse the repository at this point in the history
Update Wayland libraries
  • Loading branch information
JakeStanger authored May 5, 2023
2 parents cf32870 + e1abadc commit 528a8d6
Show file tree
Hide file tree
Showing 29 changed files with 1,834 additions and 1,313 deletions.
113 changes: 65 additions & 48 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ indexmap = "1.9.1"
dirs = "5.0.0"
walkdir = "2.3.2"
notify = { version = "5.0.0", default-features = false }
wayland-client = "0.29.5"
wayland-protocols = { version = "0.29.5", features = ["unstable_protocols", "client"] }
smithay-client-toolkit = { version = "0.16.0", default-features = false, features = ["calloop"] }
wayland-client = "0.30.0"
wayland-protocols = { version = "0.30.0", features = ["unstable", "client"] }
wayland-protocols-wlr = { version = "0.1.0", features = ["client"] }
smithay-client-toolkit = { version = "0.17.0", default-features = false, features = ["calloop"] }
universal-config = { version = "0.4.0", default_features = false }

lazy_static = "1.4.0"
Expand All @@ -75,7 +76,7 @@ cfg-if = "1.0.0"
reqwest = { version = "0.11.14", optional = true }

# clipboard
nix = { version = "0.26.2", optional = true }
nix = { version = "0.26.2", optional = true, features = ["event"] }

# clock
chrono = { version = "0.4.19", optional = true }
Expand Down
40 changes: 17 additions & 23 deletions src/clients/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()));
Expand All @@ -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);

Expand All @@ -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");
Expand All @@ -83,29 +94,19 @@ 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 {
try_send!(tx, ClipboardEvent::Add(item.clone()));
}
}

{
let mut senders = lock!(self.senders);
senders.push((tx, cache_size));
}
lock!(self.senders).push((tx, cache_size));

rx
}
Expand Down Expand Up @@ -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>> {
Expand Down
3 changes: 2 additions & 1 deletion src/clients/music/mpris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use color_eyre::Result;
use lazy_static::lazy_static;
use mpris::{DBusError, Event, Metadata, PlaybackStatus, Player, PlayerFinder};
use std::collections::HashSet;
use std::string;
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;
Expand Down Expand Up @@ -259,7 +260,7 @@ impl From<Metadata> for Track {
.and_then(mpris::MetadataValue::as_str_array)
.and_then(|arr| arr.first().map(|val| (*val).to_string())),
track: value.track_number().map(|track| track as u64),
cover_path: value.art_url().map(|s| s.to_string()),
cover_path: value.art_url().map(string::ToString::to_string),
}
}
}
Expand Down
Loading

0 comments on commit 528a8d6

Please sign in to comment.