Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wayland] Implement XdgForeign #1342

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions anvil/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ use smithay::{
xdg_activation::{
XdgActivationHandler, XdgActivationState, XdgActivationToken, XdgActivationTokenData,
},
xdg_foreign::{XdgForeignHandler, XdgForeignState},
},
};

Expand Down Expand Up @@ -147,6 +148,7 @@ pub struct AnvilState<BackendData: Backend + 'static> {
pub xdg_shell_state: XdgShellState,
pub presentation_state: PresentationState,
pub fractional_scale_manager_state: FractionalScaleManagerState,
pub xdg_foreign_state: XdgForeignState,

pub dnd_icon: Option<WlSurface>,

Expand Down Expand Up @@ -513,6 +515,13 @@ impl<BackendData: Backend + 'static> XWaylandKeyboardGrabHandler for AnvilState<
#[cfg(feature = "xwayland")]
delegate_xwayland_keyboard_grab!(@<BackendData: Backend + 'static> AnvilState<BackendData>);

impl<BackendData: Backend> XdgForeignHandler for AnvilState<BackendData> {
fn xdg_foreign_state(&mut self) -> &mut XdgForeignState {
&mut self.xdg_foreign_state
}
}
smithay::delegate_xdg_foreign!(@<BackendData: Backend + 'static> AnvilState<BackendData>);

impl<BackendData: Backend + 'static> AnvilState<BackendData> {
pub fn init(
display: Display<AnvilState<BackendData>>,
Expand Down Expand Up @@ -573,6 +582,7 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
let xdg_shell_state = XdgShellState::new::<Self>(&dh);
let presentation_state = PresentationState::new::<Self>(&dh, clock.id() as u32);
let fractional_scale_manager_state = FractionalScaleManagerState::new::<Self>(&dh);
let xdg_foreign_state = XdgForeignState::new::<Self>(&dh);
TextInputManagerState::new::<Self>(&dh);
InputMethodManagerState::new::<Self, _>(&dh, |_client| true);
VirtualKeyboardManagerState::new::<Self, _>(&dh, |_client| true);
Expand Down Expand Up @@ -667,6 +677,7 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
xdg_shell_state,
presentation_state,
fractional_scale_manager_state,
xdg_foreign_state,
dnd_icon: None,
suppressed_keys: Vec::new(),
cursor_status,
Expand Down
1 change: 1 addition & 0 deletions src/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ pub mod text_input;
pub mod viewporter;
pub mod virtual_keyboard;
pub mod xdg_activation;
pub mod xdg_foreign;
#[cfg(feature = "xwayland")]
pub mod xwayland_keyboard_grab;
234 changes: 234 additions & 0 deletions src/wayland/xdg_foreign/handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
use std::collections::HashSet;

use wayland_protocols::xdg::foreign::zv2::server::{
zxdg_exported_v2::{self, ZxdgExportedV2},
zxdg_exporter_v2::{self, ZxdgExporterV2},
zxdg_imported_v2::{self, ZxdgImportedV2},
zxdg_importer_v2::{self, ZxdgImporterV2},
};
use wayland_server::{backend::ClientId, Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New};

use crate::wayland::{compositor, shell::xdg::XdgToplevelSurfaceData};

use super::{
ExportedState, XdgExportedUserData, XdgForeignHandle, XdgForeignHandler, XdgForeignState,
XdgImportedUserData,
};

//
// Export
//

impl<D> GlobalDispatch<ZxdgExporterV2, (), D> for XdgForeignState
where
D: Dispatch<ZxdgExporterV2, ()>,
{
fn bind(
_state: &mut D,
_handle: &DisplayHandle,
_client: &Client,
resource: New<ZxdgExporterV2>,
_global_data: &(),
data_init: &mut DataInit<'_, D>,
) {
data_init.init(resource, ());
}
}

impl<D> Dispatch<ZxdgExporterV2, (), D> for XdgForeignState
where
D: Dispatch<ZxdgExportedV2, XdgExportedUserData>,
D: XdgForeignHandler,
{
fn request(
state: &mut D,
_client: &Client,
_resource: &ZxdgExporterV2,
request: zxdg_exporter_v2::Request,
_data: &(),
_dhandle: &DisplayHandle,
data_init: &mut DataInit<'_, D>,
) {
match request {
zxdg_exporter_v2::Request::ExportToplevel { id, surface } => {
let handle = XdgForeignHandle::new();
let exported = data_init.init(
id,
XdgExportedUserData {
handle: handle.clone(),
},
);
exported.handle(handle.as_str().to_owned());

state.xdg_foreign_state().exported.insert(
handle,
ExportedState {
exported_surface: surface,
requested_parent: None,
imported_by: HashSet::new(),
},
);
}
zxdg_exporter_v2::Request::Destroy => {}
_ => {}
}
}
}

impl<D: XdgForeignHandler> Dispatch<ZxdgExportedV2, XdgExportedUserData, D> for XdgForeignState {
fn request(
_state: &mut D,
_client: &Client,
_resource: &ZxdgExportedV2,
_request: zxdg_exported_v2::Request,
_data: &XdgExportedUserData,
_dhandle: &DisplayHandle,
_data_init: &mut DataInit<'_, D>,
) {
}

fn destroyed(state: &mut D, _client: ClientId, _resource: &ZxdgExportedV2, data: &XdgExportedUserData) {
// Revoke the previously exported surface.
// This invalidates any relationship the importer may have set up using the xdg_imported created given the handle sent via xdg_exported.handle.
if let Some(mut state) = state.xdg_foreign_state().exported.remove(&data.handle) {
invalidate_all_relationships(&mut state);
}
}
}

//
// Import
//

impl<D> GlobalDispatch<ZxdgImporterV2, (), D> for XdgForeignState
where
D: Dispatch<ZxdgImporterV2, ()>,
{
fn bind(
_state: &mut D,
_handle: &DisplayHandle,
_client: &Client,
resource: New<ZxdgImporterV2>,
_global_data: &(),
data_init: &mut DataInit<'_, D>,
) {
data_init.init(resource, ());
}
}

impl<D: XdgForeignHandler> Dispatch<ZxdgImporterV2, (), D> for XdgForeignState
where
D: Dispatch<ZxdgImportedV2, XdgImportedUserData>,
{
fn request(
state: &mut D,
_client: &Client,
_resource: &ZxdgImporterV2,
request: zxdg_importer_v2::Request,
_data: &(),
_dhandle: &DisplayHandle,
data_init: &mut DataInit<'_, D>,
) {
match request {
zxdg_importer_v2::Request::ImportToplevel { id, handle } => {
let exported = state
.xdg_foreign_state()
.exported
.iter_mut()
.find(|(key, _)| key.as_str() == handle.as_str());

let imported = data_init.init(
id,
XdgImportedUserData {
handle: XdgForeignHandle(handle),
},
);

match exported {
Some((_, state)) => {
state.imported_by.insert(imported);
}
None => {
imported.destroyed();
}
}
}
zxdg_importer_v2::Request::Destroy => {}
_ => {}
}
}
}

impl<D: XdgForeignHandler> Dispatch<ZxdgImportedV2, XdgImportedUserData, D> for XdgForeignState {
fn request(
state: &mut D,
_client: &Client,
resource: &ZxdgImportedV2,
request: zxdg_imported_v2::Request,
data: &XdgImportedUserData,
_dhandle: &DisplayHandle,
_data_init: &mut DataInit<'_, D>,
) {
match request {
zxdg_imported_v2::Request::SetParentOf { surface } => {
if let Some((_, state)) = state
.xdg_foreign_state()
.exported
.iter_mut()
.find(|(key, _)| key.as_str() == data.handle.as_str())
{
compositor::with_states(&state.exported_surface, |states| {
if let Some(data) = states.data_map.get::<XdgToplevelSurfaceData>() {
data.lock().unwrap().parent = Some(surface.clone());
}
});

state.requested_parent = Some((surface, resource.clone()));
}
}
zxdg_imported_v2::Request::Destroy => {}
_ => {}
}
}

fn destroyed(state: &mut D, _client: ClientId, resource: &ZxdgImportedV2, data: &XdgImportedUserData) {
if let Some((_, state)) = state
.xdg_foreign_state()
.exported
.iter_mut()
.find(|(key, _)| key.as_str() == data.handle.as_str())
{
state.imported_by.remove(resource);
invalidate_relationship_for(state, Some(resource));
}
}
}

fn invalidate_all_relationships(state: &mut ExportedState) {
invalidate_relationship_for(state, None);
}

fn invalidate_relationship_for(state: &mut ExportedState, invalidate_for: Option<&ZxdgImportedV2>) {
let Some((requested_parent, requested_by)) = state.requested_parent.as_ref() else {
return;
};

if let Some(invalidate_for) = invalidate_for {
if invalidate_for != requested_by {
return;
}
}

compositor::with_states(&state.exported_surface, |states| {
let Some(data) = states.data_map.get::<XdgToplevelSurfaceData>() else {
return;
};

let data = &mut *data.lock().unwrap();
if data.parent.as_ref() == Some(requested_parent) {
data.parent = None;
}
});

state.requested_parent = None;
}
Loading
Loading