Skip to content

Commit

Permalink
Update wayland stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
A6GibKm committed Jun 9, 2022
1 parent 0deb5a5 commit f9cff40
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 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 = {version = "0.30.0-beta.3", optional = true}
wayland-protocols = {version = "0.30.0-beta.3", optional = true, features = ["unstable", "client", "staging"]}
wayland-backend = {version = "0.1.0-beta.3", optional = true, features = ["client_system"]}
wayland-client = {version = "0.30.0-beta.4", optional = true}
wayland-protocols = {version = "0.30.0-beta.4", optional = true, features = ["unstable", "client", "staging"]}
wayland-backend = {version = "0.1.0-beta.4", 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
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
95 changes: 74 additions & 21 deletions src/activation_token/wayland.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
use wayland_client::{protocol::wl_surface::WlSurface, Proxy, QueueHandle};
use wayland_client::{
protocol::{wl_registry, wl_surface::WlSurface},
Proxy, QueueHandle,
};
use wayland_protocols::xdg::activation::v1::client::{
xdg_activation_token_v1::{Event, XdgActivationTokenV1},
xdg_activation_v1::XdgActivationV1,
};

#[derive(Debug, Default)]
pub struct WaylandActivationToken {
pub token: String,
// We use a option to transform Self into Gtk4ActivationToken.
pub(super) inner: Option<XdgActivationTokenV1>,
pub(crate) token: String,
wl_activation: Option<XdgActivationV1>,
wl_token: Option<XdgActivationTokenV1>,
}

// Is this ok?
impl Drop for WaylandActivationToken {
fn drop(&mut self) {
if let Some(wl_token) = self.inner.take() {
if let Some(wl_token) = self.wl_token.take() {
wl_token.destroy();
}

if let Some(wl_activation) = self.wl_activation.take() {
wl_activation.destroy();
}
}
}

impl WaylandActivationToken {
// Can be changed to display.
pub fn from_surface(surface: &WlSurface) -> Result<Self, Box<dyn std::error::Error>> {
let cnx = wayland_client::Connection::connect_to_env().unwrap();
let wl_activation = XdgActivationV1::from_id(&cnx, surface.id()).unwrap();
let mut queue = cnx.new_event_queue();
let queue_handle = queue.handle();
let inner = wl_activation
.get_activation_token(&queue_handle, ())
.unwrap();
let mut exported_token = ExportedActivationToken::default();
queue.blocking_dispatch(&mut exported_token).unwrap();

Ok(Self {
token: exported_token.0,
inner: Some(inner),
})
let backend = surface.backend().upgrade().unwrap();
let cnx = wayland_client::Connection::from_backend(backend);
let display = cnx.display();
let mut event_queue = cnx.new_event_queue();
let qhandle = event_queue.handle();
display.get_registry(&qhandle, ())?;

let mut state = WaylandActivationToken::default();
event_queue.sync_roundtrip(&mut state).unwrap();

let wl_activation = state.wl_activation.take().unwrap();
let wl_token = wl_activation.get_activation_token(&qhandle, ())?;
// Maybe we can destroy this after getting the token as str?
state.wl_token = Some(wl_token);
// Maybe we can destroy this?
state.wl_activation = Some(wl_activation);

event_queue.sync_roundtrip(&mut state).unwrap();

Ok(state)
}
}

#[derive(Default)]
struct ExportedActivationToken(String);
impl wayland_client::Dispatch<XdgActivationTokenV1, ()> for ExportedActivationToken {
impl wayland_client::Dispatch<XdgActivationTokenV1, ()> for WaylandActivationToken {
fn event(
&mut self,
_proxy: &XdgActivationTokenV1,
Expand All @@ -51,9 +66,47 @@ impl wayland_client::Dispatch<XdgActivationTokenV1, ()> for ExportedActivationTo
) {
match event {
Event::Done { token } => {
self.0 = token;
self.token = token;
}
_ => unreachable!(),
}
}
}

impl wayland_client::Dispatch<wl_registry::WlRegistry, ()> for WaylandActivationToken {
fn event(
&mut self,
registry: &wl_registry::WlRegistry,
event: wl_registry::Event,
_: &(),
_: &wayland_client::Connection,
qhandle: &QueueHandle<Self>,
) {
if let wl_registry::Event::Global {
name,
interface,
version,
} = event
{
println!("[{}] {} (v{})", name, interface, version);
if &interface == "xdg_activation_v1" {
let activation = registry
.bind::<XdgActivationV1, (), Self>(name, 1, qhandle, ())
.unwrap();
self.wl_activation = Some(activation);
}
}
}
}

impl wayland_client::Dispatch<XdgActivationV1, ()> for WaylandActivationToken {
fn event(
&mut self,
_activation: &XdgActivationV1,
_event: wayland_protocols::xdg::activation::v1::client::xdg_activation_v1::Event,
_: &(),
_: &wayland_client::Connection,
_qhandle: &QueueHandle<Self>,
) {
}
}

0 comments on commit f9cff40

Please sign in to comment.