Skip to content

Commit c725ec4

Browse files
Retrieve new toplevels from cosmic-comp
1 parent 24c4359 commit c725ec4

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/background.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22

3-
use std::collections::HashMap;
3+
use std::{collections::HashMap, sync::{Arc, Condvar, Mutex}};
44

5+
use ashpd::enumflags2::BitFlags;
56
use cosmic::{iced::window, widget};
67
use futures::{FutureExt, TryFutureExt};
78
use tokio::sync::mpsc::Sender;
89
use zbus::{fdo, object_server::SignalContext, zvariant};
910

10-
use crate::{app::CosmicPortal, fl, subscription, PortalResponse};
11-
12-
const POP_SHELL_DEST: &str = "com.System76.PopShell";
13-
const POP_SHELL_PATH: &str = "/com.System76.PopShell";
11+
use crate::{app::CosmicPortal, fl, subscription, wayland::WaylandHelper, PortalResponse};
1412

1513
/// Background portal backend
1614
///
1715
/// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.impl.portal.Background.html
1816
pub struct Background {
17+
wayland_helper: WaylandHelper,
1918
tx: Sender<subscription::Event>,
2019
}
2120

2221
impl Background {
23-
pub const fn new(tx: Sender<subscription::Event>) -> Self {
24-
Self { tx }
22+
pub fn new(wayland_helper: WaylandHelper, tx: Sender<subscription::Event>) -> Self {
23+
let toplevel_signal = wayland_helper.toplevel_signal();
24+
let toplevel_handle = std::thread::Builder::new()
25+
.name("background-toplevel-updates".into())
26+
.spawn(move || Background::toplevel_signal(toplevel_signal))
27+
.expect("Spawning toplevels update thread should succeed");
28+
29+
Self {
30+
wayland_helper,
31+
tx,
32+
toplevel_handle,
33+
}
34+
}
35+
36+
fn toplevel_signal(signal: Arc<(Mutex<bool>, Condvar)>) {
37+
let connection = zbus::blocking::Connection::session().unwrap();
38+
39+
loop {
40+
41+
}
2542
}
2643
}
2744

2845
#[zbus::interface(name = "org.freedesktop.impl.portal.Background")]
2946
impl Background {
3047
/// Current status on running apps
31-
async fn get_app_state(
32-
&self,
33-
// #[zbus(connection)] connection: &zbus::Connection,
34-
) -> fdo::Result<HashMap<String, AppStatus>> {
48+
async fn get_app_state(&self) -> fdo::Result<HashMap<String, AppStatus>> {
3549
// TODO: Subscribe to Wayland window open events for running apps
3650
log::warn!("[background] GetAppState is currently unimplemented");
3751
Ok(HashMap::default())
@@ -147,7 +161,7 @@ impl Background {
147161
// }
148162

149163
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, zvariant::Type)]
150-
#[zvariant(signature = "u")]
164+
#[zvariant(signature = "v")]
151165
pub enum AppStatus {
152166
/// No open windows
153167
Background = 0,

src/subscription.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ pub(crate) async fn process_changes(
144144
let connection = zbus::ConnectionBuilder::session()?
145145
.name(DBUS_NAME)?
146146
.serve_at(DBUS_PATH, Access::new(wayland_helper.clone(), tx.clone()))?
147-
.serve_at(DBUS_PATH, Background::new(tx.clone()))?
147+
.serve_at(
148+
DBUS_PATH,
149+
Background::new(wayland_helper.clone(), tx.clone()),
150+
)?
148151
.serve_at(DBUS_PATH, FileChooser::new(tx.clone()))?
149152
.serve_at(
150153
DBUS_PATH,

src/wayland/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct WaylandHelperInner {
8787
output_infos: Mutex<HashMap<wl_output::WlOutput, OutputInfo>>,
8888
output_toplevels: Mutex<HashMap<wl_output::WlOutput, Vec<ZcosmicToplevelHandleV1>>>,
8989
toplevels: Mutex<Vec<(ZcosmicToplevelHandleV1, ToplevelInfo)>>,
90+
toplevel_update: Arc<(Mutex<bool>, Condvar)>,
9091
qh: QueueHandle<AppData>,
9192
screencopy_manager: zcosmic_screencopy_manager_v2::ZcosmicScreencopyManagerV2,
9293
output_source_manager: ZcosmicOutputImageSourceManagerV1,
@@ -167,6 +168,7 @@ impl AppData {
167168
.toplevels()
168169
.filter_map(|(handle, info)| Some((handle.clone(), info?.clone())))
169170
.collect();
171+
self.wayland_helper.toplevel_notify();
170172
}
171173
}
172174

@@ -250,13 +252,15 @@ impl WaylandHelper {
250252
let screencopy_state = ScreencopyState::new(&globals, &qh);
251253
let shm_state = Shm::bind(&globals, &qh).unwrap();
252254
let zwp_dmabuf = globals.bind(&qh, 4..=4, sctk::globals::GlobalData).unwrap();
255+
let toplevel_update = Arc::new((Mutex::new(false), Condvar::new()));
253256
let wayland_helper = WaylandHelper {
254257
inner: Arc::new(WaylandHelperInner {
255258
conn,
256259
outputs: Mutex::new(Vec::new()),
257260
output_infos: Mutex::new(HashMap::new()),
258261
output_toplevels: Mutex::new(HashMap::new()),
259262
toplevels: Mutex::new(Vec::new()),
263+
toplevel_update,
260264
qh: qh.clone(),
261265
screencopy_manager: screencopy_state.screencopy_manager.clone(),
262266
output_source_manager: screencopy_state.output_source_manager.clone().unwrap(),
@@ -304,6 +308,10 @@ impl WaylandHelper {
304308
self.inner.toplevels.lock().unwrap().clone()
305309
}
306310

311+
pub fn toplevel_signal(&self) -> Arc<(Mutex<bool>, Condvar)> {
312+
Arc::clone(&self.inner.toplevel_update)
313+
}
314+
307315
pub fn output_info(&self, output: &wl_output::WlOutput) -> Option<OutputInfo> {
308316
self.inner.output_infos.lock().unwrap().get(output).cloned()
309317
}

0 commit comments

Comments
 (0)