Skip to content

Commit

Permalink
Update wayland stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
A6GibKm committed Jul 8, 2022
1 parent f6ca72b commit 862eb94
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 88 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ futures = "0.3"
tracing = {version = "0.1", optional = true}
libc = {version = "0.2.94", optional = true}
raw-window-handle = {version = "0.4", optional = true}
wayland-client = {git = "https://github.com/Smithay/wayland-rs", branch = "vberger/backend-from_foreign", optional = true}
wayland-protocols = {git = "https://github.com/Smithay/wayland-rs", branch = "vberger/backend-from_foreign", optional = true, features = ["unstable", "client", "staging"]}
wayland-backend = {git = "https://github.com/Smithay/wayland-rs", branch = "vberger/backend-from_foreign", optional = true, features = ["client_system"]}
wayland-client = {version = "0.30.0-beta.7", optional = true}
wayland-protocols = {version = "0.30.0-beta.7", optional = true, features = ["unstable", "client", "staging"]}
wayland-backend = {version = "0.1.0-beta.7", optional = true, features = ["client_system"]}
async-std = {version = "1.11", optional = true}
tokio = {version = "1.17", features = ["fs", "io-util"], optional = true, default-features = false}

Expand Down
28 changes: 11 additions & 17 deletions ashpd-demo/Cargo.lock

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

4 changes: 2 additions & 2 deletions ashpd-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ gtk = {package = "gtk4", version = "0.4"}
adw = {version = "0.1", package = "libadwaita"}
serde = {version = "1.0", features = ["derive"]}
chrono = {version = "0.4", default-features = false, features = ["clock"]}
gdk4wayland = {package = "gdk4-wayland"}

[dependencies.shumate]
package = "libshumate"
version = "0.1.0-alpha.4"

[dependencies.ashpd]
git = "https://github.com/A6GibKm/ashpd"
branch = "wayland2"
path = "../"
features = ["gtk4", "wayland", "pipewire", "tracing"]
4 changes: 1 addition & 3 deletions ashpd-demo/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ impl Application {
}));
})
);
let is_sandboxed = futures::executor::block_on(async {
ashpd::is_sandboxed().await
});
let is_sandboxed = futures::executor::block_on(async { ashpd::is_sandboxed().await });
// The restart app requires the Flatpak portal
gtk_macros::get_action!(self, @restart).set_enabled(is_sandboxed);

Expand Down
68 changes: 48 additions & 20 deletions ashpd-demo/src/portals/desktop/open_uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,54 @@ impl OpenUriPage {
}

async fn open_uri(&self) {
let imp = self.imp();
let writable = imp.writeable_switch.is_active();
let ask = imp.ask_switch.is_active();
let root = self.native().unwrap();
let identifier = WindowIdentifier::from_native(&root).await;
let uri = imp.uri_entry.text();
let activation_token = if imp.activation_token_switch.is_active() {
Some(ashpd::ActivationToken::from_native(&root).unwrap())
} else {
None
};
match open_uri::open_uri(&identifier, &uri, writable, ask, activation_token).await {
Ok(_) => {
self.send_notification(
"Open URI request was successful",
NotificationKind::Success,
);
}
Err(_err) => {
self.send_notification("Request to open URI failed", NotificationKind::Error);
unsafe {
let imp = self.imp();
let writable = imp.writeable_switch.is_active();
let ask = imp.ask_switch.is_active();
let root = self.native().unwrap();
use gtk::glib::translate::ToGlibPtr;

let surface = root
.surface()
.downcast::<gdk4wayland::WaylandSurface>()
.unwrap();
let display = root
.display()
.downcast::<gdk4wayland::WaylandDisplay>()
.unwrap();

let surface_ptr =
gdk4wayland::ffi::gdk_wayland_surface_get_wl_surface(surface.to_glib_none().0);
let display_ptr =
gdk4wayland::ffi::gdk_wayland_display_get_wl_display(display.to_glib_none().0);

let identifier =
WindowIdentifier::from_wayland_raw(surface_ptr as *mut _, display_ptr as *mut _)
.await;
let app_id = String::from(crate::config::APP_ID);
let token =
ashpd::ActivationToken::from_wayland_raw(app_id, surface_ptr as *mut _, display_ptr as *mut _)
.await.unwrap();

tracing::debug!("Handle {identifier}");
tracing::debug!("Activation Token {}", token.as_str());

let uri = imp.uri_entry.text();
let activation_token = if imp.activation_token_switch.is_active() {
Some(token)
} else {
None
};
match open_uri::open_uri(&identifier, &uri, writable, ask, activation_token).await {
Ok(_) => {
self.send_notification(
"Open URI request was successful",
NotificationKind::Success,
);
}
Err(_err) => {
self.send_notification("Request to open URI failed", NotificationKind::Error);
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions ashpd-demo/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ mod imp {
if config::PROFILE == "Devel" {
obj.add_css_class("devel");
}
let is_sandboxed = futures::executor::block_on(async {
ashpd::is_sandboxed().await
});
let is_sandboxed = futures::executor::block_on(async { ashpd::is_sandboxed().await });
// Add pages based on whether the app is sandboxed
if is_sandboxed {
self.sidebar
Expand Down
4 changes: 2 additions & 2 deletions src/activation_token/gtk3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use gtk3::prelude::*;
use gtk3::{gdk, glib};
use wayland_client::{
backend::ObjectId,
protocol::{__interfaces::WL_SURFACE_INTERFACE, wl_surface::WlSurface},
protocol::{wl_surface::WlSurface},
Proxy,
};
use wayland_protocols::xdg::activation::v1::client::xdg_activation_token_v1::XdgActivationTokenV1;
Expand All @@ -30,7 +30,7 @@ impl Gtk3ActivationToken {
wayland_window.to_glib_none().0,
);

ObjectId::from_ptr(&WL_SURFACE_INTERFACE, ptr as *mut _).unwrap()
ObjectId::from_ptr(&WlSurface::interface(), ptr as *mut _).unwrap()
};
let wl_surface = WlSurface::from_id(&cnx, surface_id).unwrap();
let wl = WaylandActivationToken::from_surface(&wl_surface).ok()?;
Expand Down
8 changes: 3 additions & 5 deletions src/activation_token/gtk4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ pub struct Gtk4ActivationToken {
impl Gtk4ActivationToken {
pub fn from_native<N: glib::IsA<gdk::Display>>(native: &N) -> Option<Self> {
match native.backend() {
gdk::Backend::Wayland => native
.startup_notification_id()
.map(|token| Self {
token: token.to_string(),
}),
gdk::Backend::Wayland => native.startup_notification_id().map(|token| Self {
token: token.to_string(),
}),
gdk::Backend::X11 => todo!(),
_ => None,
}
Expand Down
25 changes: 21 additions & 4 deletions src/activation_token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,29 @@ impl Serialize for ActivationToken {

impl ActivationToken {
#[cfg(feature = "wayland")]
pub fn from_surface(
pub async fn from_wayland_surface(
app_id: String,
surface: &wayland_client::protocol::wl_surface::WlSurface,
) -> Result<Self, Box<dyn std::error::Error>> {
let token = WaylandActivationToken::from_surface(surface)?;
) -> Option<Self> {
let token = WaylandActivationToken::from_surface(app_id, surface).await?;

Ok(Self::Wayland(token))
Some(Self::Wayland(token))
}

#[cfg(feature = "wayland")]
/// Create an instance of [`ActivationToken`] from a Wayland surface.
///
/// ## Safety
///
/// The surface and display have to be valid Wayland pointers.
pub async unsafe fn from_wayland_raw(
app_id: String,
surface_ptr: *mut std::ffi::c_void,
display_ptr: *mut std::ffi::c_void,
) -> Option<Self> {
let token = WaylandActivationToken::from_raw(app_id, surface_ptr, display_ptr).await?;

Some(Self::Wayland(token))
}

#[cfg(feature = "gtk4")]
Expand Down
Loading

0 comments on commit 862eb94

Please sign in to comment.