Skip to content

Commit

Permalink
chore: make connection static
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Oct 15, 2023
1 parent a801cf2 commit a794a2c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
40 changes: 25 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::Path;

mod pipewirethread;
use std::sync::OnceLock;

const PORTAL_RESPONSE_SUCCESS: u32 = 0;
const PORTAL_RESPONSE_CANCELLED: u32 = 1;
const PORTAL_RESPONSE_OTHER: u32 = 2;

static SESSION: OnceLock<zbus::Connection> = OnceLock::new();

async fn get_connection() -> zbus::Connection {
if let Some(cnx) = SESSION.get() {
cnx.clone()
} else {
panic!("Cannot get cnx");
}
}

async fn set_connection(connection: Connection) {
SESSION.set(connection).expect("Cannot set a OnceLock");
}

#[derive(zvariant::Type)]
#[zvariant(signature = "(ua{sv})")]
enum PortalResponse<T: zvariant::Type + serde::Serialize> {
Expand Down Expand Up @@ -72,7 +87,8 @@ fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Resul
Ok((watcher, rx))
}

async fn async_watch<P: AsRef<Path>>(path: P, connection: Connection) -> notify::Result<()> {
async fn async_watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
let connection = get_connection().await;
let (mut watcher, mut rx) = async_watcher()?;

let signal_context =
Expand Down Expand Up @@ -127,24 +143,18 @@ async fn main() -> anyhow::Result<()> {
.build()
.await?;

if let Ok(home) = std::env::var("HOME") {
set_connection(conn).await;
tokio::spawn(async {
let Ok(home) = std::env::var("HOME") else {
return;
};
let config_path = std::path::Path::new(home.as_str())
.join(".config")
.join("xdg-desktop-portal-luminous");
if config_path.exists() && config_path.is_dir() {
tokio::spawn(async move {
let Ok(home) = std::env::var("HOME") else {
return;
};
let config_path = std::path::Path::new(home.as_str())
.join(".config")
.join("xdg-desktop-portal-luminous");
if let Err(e) = async_watch(config_path, conn).await {
tracing::info!("Maybe file is not exist, error: {e}");
}
});
if let Err(e) = async_watch(config_path).await {
tracing::info!("Maybe file is not exist, error: {e}");
}
};
});

pending::<()>().await;

Expand Down
6 changes: 1 addition & 5 deletions src/remotedesktop/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@ impl Dispatch<WlSeat, ()> for AppData {
if let Some(virtual_keyboard_manager) = state.virtual_keyboard_manager.as_ref() {
let virtual_keyboard = virtual_keyboard_manager.create_virtual_keyboard(seat, qh, ());
let (file, size) = get_keymap_as_file();
virtual_keyboard.keymap(
wl_keyboard::KeymapFormat::XkbV1.into(),
file.as_fd(),
size,
);
virtual_keyboard.keymap(wl_keyboard::KeymapFormat::XkbV1.into(), file.as_fd(), size);
state.virtual_keyboard = Some(virtual_keyboard);
}
if let Some(virtual_pointer_manager) = state.virtual_pointer_manager.as_ref() {
Expand Down

0 comments on commit a794a2c

Please sign in to comment.