Skip to content

Commit

Permalink
wayland-backend: Add getter for global name
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Jun 8, 2024
1 parent 7669047 commit 84bfcc0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
22 changes: 21 additions & 1 deletion wayland-backend/src/rs/server_impl/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{

use super::{
client::ClientStore, registry::Registry, ClientData, ClientId, Credentials, GlobalHandler,
InnerClientId, InnerGlobalId, InnerObjectId, ObjectData, ObjectId,
GlobalId, InnerClientId, InnerGlobalId, InnerObjectId, ObjectData, ObjectId,
};

pub(crate) type PendingDestructor<D> = (Arc<dyn ObjectData<D>>, InnerClientId, InnerObjectId);
Expand Down Expand Up @@ -252,6 +252,10 @@ impl InnerHandle {
self.state.lock().unwrap().global_info(id)
}

pub fn global_name(&self, global: InnerGlobalId, client: InnerClientId) -> Option<u32> {
self.state.lock().unwrap().global_name(global, client)
}

Check warning on line 257 in wayland-backend/src/rs/server_impl/handle.rs

View check run for this annotation

Codecov / codecov/patch

wayland-backend/src/rs/server_impl/handle.rs#L255-L257

Added lines #L255 - L257 were not covered by tests

pub fn get_global_handler<D: 'static>(
&self,
id: InnerGlobalId,
Expand Down Expand Up @@ -298,6 +302,7 @@ pub(crate) trait ErasedState: downcast_rs::Downcast {
fn post_error(&mut self, object_id: InnerObjectId, error_code: u32, message: CString);
fn kill_client(&mut self, client_id: InnerClientId, reason: DisconnectReason);
fn global_info(&self, id: InnerGlobalId) -> Result<GlobalInfo, InvalidId>;
fn global_name(&self, global: InnerGlobalId, client: InnerClientId) -> Option<u32>;
fn flush(&mut self, client: Option<ClientId>) -> std::io::Result<()>;
}

Expand Down Expand Up @@ -440,6 +445,21 @@ impl<D> ErasedState for State<D> {
self.registry.get_info(id)
}

fn global_name(&self, global_id: InnerGlobalId, client_id: InnerClientId) -> Option<u32> {
let client = self.clients.get_client(client_id.clone()).ok()?;
let handler = self.registry.get_handler(global_id.clone()).ok()?;
let name = global_id.id;

let can_view =
handler.can_view(ClientId { id: client_id }, &client.data, GlobalId { id: global_id });

if can_view {
Some(name)

Check warning on line 457 in wayland-backend/src/rs/server_impl/handle.rs

View check run for this annotation

Codecov / codecov/patch

wayland-backend/src/rs/server_impl/handle.rs#L448-L457

Added lines #L448 - L457 were not covered by tests
} else {
None

Check warning on line 459 in wayland-backend/src/rs/server_impl/handle.rs

View check run for this annotation

Codecov / codecov/patch

wayland-backend/src/rs/server_impl/handle.rs#L459

Added line #L459 was not covered by tests
}
}

Check warning on line 461 in wayland-backend/src/rs/server_impl/handle.rs

View check run for this annotation

Codecov / codecov/patch

wayland-backend/src/rs/server_impl/handle.rs#L461

Added line #L461 was not covered by tests

fn flush(&mut self, client: Option<ClientId>) -> std::io::Result<()> {
self.flush(client)
}
Expand Down
9 changes: 9 additions & 0 deletions wayland-backend/src/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,15 @@ impl Handle {
self.handle.global_info(id.id)
}

/// Get the name of the global.
///
/// - `client` Client for which to look up the global.
#[doc(alias = "wl_global_get_name")]
#[inline]
pub fn global_name(&self, global: GlobalId, client: ClientId) -> Option<u32> {
self.handle.global_name(global.id, client.id)
}

Check warning on line 491 in wayland-backend/src/server_api.rs

View check run for this annotation

Codecov / codecov/patch

wayland-backend/src/server_api.rs#L489-L491

Added lines #L489 - L491 were not covered by tests

/// Returns the handler which manages the visibility and notifies when a client has bound the global.
#[inline]
pub fn get_global_handler<D: 'static>(
Expand Down
25 changes: 25 additions & 0 deletions wayland-backend/src/sys/server_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,10 @@ impl InnerHandle {
self.state.lock().unwrap().global_info(id)
}

pub fn global_name(&self, global: InnerGlobalId, client: InnerClientId) -> Option<u32> {
self.state.lock().unwrap().global_name(global, client)
}

/// Returns the handler which manages the visibility and notifies when a client has bound the global.
pub fn get_global_handler<D: 'static>(
&self,
Expand Down Expand Up @@ -852,6 +856,7 @@ pub(crate) trait ErasedState: downcast_rs::Downcast {
fn post_error(&mut self, object_id: InnerObjectId, error_code: u32, message: CString);
fn kill_client(&mut self, client_id: InnerClientId, reason: DisconnectReason);
fn global_info(&self, id: InnerGlobalId) -> Result<GlobalInfo, InvalidId>;
fn global_name(&self, global: InnerGlobalId, client: InnerClientId) -> Option<u32>;
fn is_known_global(&self, global_ptr: *const wl_global) -> bool;
fn flush(&mut self, client: Option<ClientId>) -> std::io::Result<()>;
fn display_ptr(&self) -> *mut wl_display;
Expand Down Expand Up @@ -1225,6 +1230,26 @@ impl<D: 'static> ErasedState for State<D> {
})
}

fn global_name(&self, global: InnerGlobalId, client: InnerClientId) -> Option<u32> {
if !global.alive.load(Ordering::Acquire) {
return None;
}

if !client.alive.load(Ordering::Acquire) {
return None;
}

let name = unsafe {
ffi_dispatch!(wayland_server_handle(), wl_global_get_name, global.ptr, client.ptr)
};

if name == 0 {
None
} else {
Some(name)
}
}

fn is_known_global(&self, global_ptr: *const wl_global) -> bool {
self.known_globals.iter().any(|ginfo| (ginfo.ptr as *const wl_global) == global_ptr)
}
Expand Down
1 change: 1 addition & 0 deletions wayland-sys/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ external_library!(WaylandServer, "wayland-server",
// wl_global
fn wl_global_remove(*mut wl_global) -> (),
fn wl_global_destroy(*mut wl_global) -> (),
fn wl_global_get_name(*mut wl_global, *mut wl_client) -> u32,
fn wl_global_get_user_data(*const wl_global) -> *mut c_void,
// wl_resource
fn wl_resource_post_event_array(*mut wl_resource, u32, *mut wl_argument) -> (),
Expand Down

0 comments on commit 84bfcc0

Please sign in to comment.