1
0
Fork 0
forked from wry/wry

all: refactor to cargo workspace, remove config shared library, remove protocol perms, add dpms cli (#7)

This commit is contained in:
kossLAN 2026-06-06 23:14:53 -04:00
parent 5db14936e7
commit bfc2a525de
616 changed files with 32344 additions and 31026 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,413 @@
use {
super::Wm,
crate::{
criteria::tlm::{TL_CHANGED_CLASS_INST, TL_CHANGED_ROLE},
ifs::wl_surface::x_surface::xwindow::{XInputModel, XwindowData},
tree::ToplevelNode,
utils::{bitflags::BitflagsExt, errorfmt::ErrorFmt},
wire_xcon::GetAtomName,
xcon::{
XconError,
consts::{
ATOM_ATOM, ATOM_STRING, ATOM_WINDOW, ATOM_WM_CLASS, ATOM_WM_NAME,
ATOM_WM_SIZE_HINTS, ATOM_WM_TRANSIENT_FOR, ICCCM_WM_HINT_INPUT,
MWM_HINTS_DECORATIONS_FIELD, MWM_HINTS_FLAGS_FIELD,
},
},
},
bstr::{ByteSlice, ByteVec},
std::rc::Rc,
};
impl Wm {
pub(super) fn compute_input_model(&self, data: &Rc<XwindowData>) {
let has_wm_take_focus = data.info.protocols.contains(&self.atoms.WM_TAKE_FOCUS);
let accepts_input = data.info.icccm_hints.input.get();
let model = match (accepts_input, has_wm_take_focus) {
(false, false) => XInputModel::None,
(true, false) => XInputModel::Passive,
(true, true) => XInputModel::Local,
(false, true) => XInputModel::Global,
};
data.info.input_model.set(model);
}
pub(super) async fn load_window_wm_window_role(&self, data: &Rc<XwindowData>) {
let property_changed = || {
if let Some(window) = data.window.get() {
window.toplevel_data.property_changed(TL_CHANGED_ROLE);
}
};
let mut buf = vec![];
match self
.c
.get_property::<u8>(data.window_id, self.atoms.WM_WINDOW_ROLE, 0, &mut buf)
.await
{
Ok(ty) if ty == ATOM_STRING => {}
Ok(ty) if ty == self.atoms.UTF8_STRING => {}
Ok(ty) => {
self.unexpected_type(data.window_id, "WM_WINDOW_ROLE", ty)
.await;
return;
}
Err(XconError::PropertyUnavailable) => {
data.info.role.borrow_mut().take();
property_changed();
return;
}
Err(e) => {
log::error!(
"Could not retrieve WM_WINDOW_ROLE property: {}",
ErrorFmt(e)
);
return;
}
}
*data.info.role.borrow_mut() = Some(buf.into_string_lossy());
property_changed();
}
pub(super) async fn load_window_wm_class(&self, data: &Rc<XwindowData>) {
let mut buf = vec![];
let property_changed = || {
if let Some(window) = data.window.get() {
let class = data.info.class.borrow();
for handle in window.toplevel_data.manager_handles.lock().values() {
handle.send_app_id(class.as_deref().unwrap_or_default());
handle.send_done();
}
window.toplevel_data.property_changed(TL_CHANGED_CLASS_INST);
}
};
match self
.c
.get_property::<u8>(data.window_id, ATOM_WM_CLASS, 0, &mut buf)
.await
{
Ok(ty) if ty == ATOM_STRING => {}
Ok(ty) if ty == self.atoms.UTF8_STRING => {}
Ok(ty) => {
self.unexpected_type(data.window_id, "WM_CLASS", ty).await;
return;
}
Err(XconError::PropertyUnavailable) => {
data.info.instance.borrow_mut().take();
data.info.class.borrow_mut().take();
property_changed();
return;
}
Err(e) => {
log::error!("Could not retrieve WM_CLASS property: {}", ErrorFmt(e));
return;
}
}
let mut iter = buf.split(|c| *c == 0);
let mut map = || Some(iter.next().unwrap_or(&[]).to_str_lossy().into_owned());
*data.info.instance.borrow_mut() = map();
*data.info.class.borrow_mut() = map();
property_changed();
}
async fn load_window_wm_name2(&self, data: &Rc<XwindowData>, prop: u32, name: &str) {
let mut buf = vec![];
match self
.c
.get_property::<u8>(data.window_id, prop, 0, &mut buf)
.await
{
Ok(ty) if ty == ATOM_STRING && data.info.utf8_title.get() => return,
Ok(ty) if ty == ATOM_STRING => {}
Ok(ty) if ty == self.atoms.COMPOUND_TEXT => return,
Ok(ty) if ty == self.atoms.UTF8_STRING => {
data.info.utf8_title.set(true);
}
Ok(ty) => {
self.unexpected_type(data.window_id, name, ty).await;
return;
}
Err(XconError::PropertyUnavailable) => return,
Err(e) => {
log::error!("Could not retrieve {} property: {}", name, ErrorFmt(e));
return;
}
}
let title = buf.as_bstr().to_string();
if let Some(window) = data.window.get() {
window.toplevel_data.set_title(&title);
window.tl_title_changed();
}
*data.info.title.borrow_mut() = Some(title);
data.title_changed();
}
async fn unexpected_type(&self, window: u32, prop: &str, ty: u32) {
let mut ty_name = "unknown".as_bytes().as_bstr();
let res = self.c.call(&GetAtomName { atom: ty }).await;
if let Ok(res) = &res {
ty_name = res.get().name;
}
log::error!(
"Property {} of window {} has unexpected type {} ({})",
prop,
window,
ty_name,
ty
);
}
pub(super) async fn load_window_wm_name(&self, data: &Rc<XwindowData>) {
self.load_window_wm_name2(data, ATOM_WM_NAME, "WM_NAME")
.await;
}
pub(super) async fn load_window_net_wm_name(&self, data: &Rc<XwindowData>) {
self.load_window_wm_name2(data, self.atoms._NET_WM_NAME, "_NET_WM_NAME")
.await;
}
pub(super) async fn load_window_wm_transient_for(&self, data: &Rc<XwindowData>) {
let mut buf = vec![];
if let Err(e) = self
.c
.get_property::<u32>(data.window_id, ATOM_WM_TRANSIENT_FOR, ATOM_WINDOW, &mut buf)
.await
{
if not_matches!(e, XconError::PropertyUnavailable) {
log::error!(
"Could not retrieve WM_TRANSIENT_FOR property: {}",
ErrorFmt(e)
);
}
}
if let Some(old) = data.parent.take() {
old.children.remove(&data.window_id);
}
if let Some(w) = buf.first()
&& let Some(w) = self.windows.get(w)
{
if data.is_ancestor_of(w.clone()) {
log::error!("Cannot set WM_TRANSIENT_FOR because it would create a cycle");
return;
}
w.children.set(data.window_id, data.clone());
data.parent.set(Some(w.clone()));
}
}
pub(super) async fn load_window_wm_protocols(&self, data: &Rc<XwindowData>) {
let mut buf = vec![];
if let Err(e) = self
.c
.get_property::<u32>(data.window_id, self.atoms.WM_PROTOCOLS, ATOM_ATOM, &mut buf)
.await
{
if not_matches!(e, XconError::PropertyUnavailable) {
log::error!("Could not retrieve WM_PROTOCOLS property: {}", ErrorFmt(e));
}
return;
}
data.info.protocols.clear();
data.info
.protocols
.lock()
.extend(buf.iter().copied().map(|v| (v, ())));
self.compute_input_model(data);
}
pub(super) async fn load_window_wm_hints(&self, data: &Rc<XwindowData>) {
let mut buf = vec![];
if let Err(e) = self
.c
.get_property::<u32>(data.window_id, self.atoms.WM_HINTS, 0, &mut buf)
.await
{
if not_matches!(e, XconError::PropertyUnavailable) {
log::error!("Could not retrieve WM_HINTS property: {}", ErrorFmt(e));
}
data.info.icccm_hints.input.set(true);
self.compute_input_model(data);
return;
}
let mut values = [0; 9];
let len = values.len().min(buf.len());
values[..len].copy_from_slice(&buf[..len]);
data.info.icccm_hints.flags.set(values[0] as i32);
data.info.icccm_hints.input.set(values[1] != 0);
data.info.icccm_hints.initial_state.set(values[2] as i32);
data.info.icccm_hints.icon_pixmap.set(values[3]);
data.info.icccm_hints.icon_window.set(values[4]);
data.info.icccm_hints.icon_x.set(values[5] as i32);
data.info.icccm_hints.icon_y.set(values[6] as i32);
data.info.icccm_hints.icon_mask.set(values[7]);
data.info.icccm_hints.window_group.set(values[8]);
if data
.info
.icccm_hints
.flags
.get()
.not_contains(ICCCM_WM_HINT_INPUT)
{
data.info.icccm_hints.input.set(true);
}
self.compute_input_model(data);
}
pub(super) async fn load_window_wm_normal_hints(&self, data: &Rc<XwindowData>) {
let mut buf = vec![];
if let Err(e) = self
.c
.get_property::<u32>(
data.window_id,
self.atoms.WM_NORMAL_HINTS,
ATOM_WM_SIZE_HINTS,
&mut buf,
)
.await
{
if not_matches!(e, XconError::PropertyUnavailable) {
log::error!(
"Could not retrieve WM_NORMAL_HINTS property: {}",
ErrorFmt(e)
);
}
return;
}
let mut values = [0; 18];
let len = values.len().min(buf.len());
values[..len].copy_from_slice(&buf[..len]);
data.info.normal_hints.flags.set(values[0]);
data.info.normal_hints.x.set(values[1] as i32);
data.info.normal_hints.y.set(values[2] as i32);
data.info.normal_hints.width.set(values[3] as i32);
data.info.normal_hints.height.set(values[4] as i32);
data.info.normal_hints.min_width.set(values[5] as i32);
data.info.normal_hints.min_height.set(values[6] as i32);
data.info.normal_hints.max_width.set(values[7] as i32);
data.info.normal_hints.max_height.set(values[8] as i32);
data.info.normal_hints.width_inc.set(values[9] as i32);
data.info.normal_hints.height_inc.set(values[10] as i32);
data.info.normal_hints.min_aspect_num.set(values[11] as i32);
data.info.normal_hints.min_aspect_den.set(values[12] as i32);
data.info.normal_hints.max_aspect_num.set(values[13] as i32);
data.info.normal_hints.max_aspect_den.set(values[14] as i32);
data.info.normal_hints.base_width.set(values[15] as i32);
data.info.normal_hints.base_height.set(values[16] as i32);
data.info.normal_hints.win_gravity.set(values[17]);
self.update_wants_floating(data);
}
pub(super) async fn load_window_motif_wm_hints(&self, data: &Rc<XwindowData>) {
let mut buf = vec![];
if let Err(e) = self
.c
.get_property::<u32>(data.window_id, self.atoms._MOTIF_WM_HINTS, 0, &mut buf)
.await
{
if not_matches!(e, XconError::PropertyUnavailable) {
log::error!(
"Could not retrieve _MOTIF_WM_HINTS property: {}",
ErrorFmt(e)
);
}
return;
}
let mut values = [0; 5];
let len = values.len().min(buf.len());
values[..len].copy_from_slice(&buf[..len]);
data.info
.motif_hints
.flags
.set(values[MWM_HINTS_FLAGS_FIELD]);
data.info
.motif_hints
.decorations
.set(values[MWM_HINTS_DECORATIONS_FIELD]);
}
pub(super) async fn load_window_net_startup_id(&self, data: &Rc<XwindowData>) {
let mut buf = vec![];
match self
.c
.get_property::<u8>(data.window_id, self.atoms._NET_STARTUP_ID, 0, &mut buf)
.await
{
Ok(ty) if ty == ATOM_STRING => {}
Ok(ty) if ty == self.atoms.UTF8_STRING => {}
Ok(ty) => {
self.unexpected_type(data.window_id, "_NET_STARTUP_ID", ty)
.await;
return;
}
Err(XconError::PropertyUnavailable) => return,
Err(e) => {
log::error!(
"Could not retrieve _NET_STARTUP_ID property: {}",
ErrorFmt(e)
);
return;
}
}
*data.info.startup_id.borrow_mut() = Some(buf.into());
}
pub(super) async fn load_window_net_wm_state(&self, data: &Rc<XwindowData>) {
data.info.fullscreen.set(false);
let mut buf = vec![];
if let Err(e) = self
.c
.get_property::<u32>(data.window_id, self.atoms._NET_WM_STATE, 0, &mut buf)
.await
{
if not_matches!(e, XconError::PropertyUnavailable) {
log::error!("Could not retrieve _NET_WM_STATE property: {}", ErrorFmt(e));
}
return;
}
for prop in buf {
if prop == self.atoms._NET_WM_STATE_MODAL {
data.info.modal.set(true);
self.update_wants_floating(data);
} else if prop == self.atoms._NET_WM_STATE_FULLSCREEN {
data.info.fullscreen.set(true);
} else if prop == self.atoms._NET_WM_STATE_MAXIMIZED_VERT {
data.info.maximized_vert.set(true);
} else if prop == self.atoms._NET_WM_STATE_MAXIMIZED_HORZ {
data.info.maximized_horz.set(true);
} else if prop == self.atoms._NET_WM_STATE_HIDDEN {
data.info.minimized.set(true);
}
}
}
pub(super) async fn load_window_net_wm_window_type(&self, data: &Rc<XwindowData>) {
let mut buf = vec![];
if let Err(e) = self
.c
.get_property::<u32>(
data.window_id,
self.atoms._NET_WM_WINDOW_TYPE,
ATOM_ATOM,
&mut buf,
)
.await
{
if not_matches!(e, XconError::PropertyUnavailable) {
log::error!(
"Could not retrieve _NET_WM_WINDOW_TYPE property: {}",
ErrorFmt(e)
);
}
return;
}
data.info
.never_focus
.set(buf.iter().any(|t| self.never_focus.contains(t)));
data.info.window_types.clear();
data.info
.window_types
.lock()
.extend(buf.iter().copied().map(|v| (v, ())));
self.update_wants_floating(data);
}
}

View file

@ -0,0 +1,527 @@
use {
super::*,
super::transfer::{WaylandToXTransfer, XToWaylandTransfer},
crate::wire_xcon::GetAtomName,
};
pub(super) struct EnhancedOffer {
offer: Rc<XDataOffer>,
mime_types: RefCell<Vec<u32>>,
active: Cell<bool>,
}
#[derive(Default)]
pub(super) struct SelectionData<T: XTransfer> {
pub(super) sources: CopyHashMap<SeatId, Rc<XDataSource>>,
pub(super) offers: CopyHashMap<SeatId, Rc<EnhancedOffer>>,
pub(super) active_offer: CloneCell<Option<Rc<EnhancedOffer>>>,
pub(super) win: Cell<u32>,
pub(super) selection: Cell<u32>,
pub(super) pending_transfers: RefCell<Vec<PendingTransfer>>,
pub(super) _phantom: PhantomData<T>,
}
impl<T: XTransfer> SelectionData<T> {
pub(super) fn destroy(&self) {
for offer in self.offers.lock().drain_values() {
destroy_data_offer::<T>(&offer.offer);
}
self.active_offer.take();
self.destroy_sources();
}
fn destroy_sources(&self) {
for source in self.sources.lock().drain_values() {
destroy_data_source::<T>(&source);
}
}
pub(super) fn seat_removed(&self, id: SeatId) {
if let Some(offer) = self.active_offer.get()
&& offer.offer.get_seat().id() == id
{
self.active_offer.take();
}
self.offers.remove(&id);
self.sources.remove(&id);
}
}
pub(super) struct PendingTransfer {
mime_type: u32,
fd: Rc<OwnedFd>,
}
const TEXT_PLAIN_UTF_8: &str = "text/plain;charset=utf-8";
const TEXT_PLAIN: &str = "text/plain";
impl Wm {
pub(super) async fn dd_add_offer_mime_type<T: XTransfer>(
&mut self,
sd: &SelectionData<T>,
seat: SeatId,
offer: DataOfferId,
mt: String,
) {
let enhanced = match sd.offers.get(&seat) {
Some(r) if r.offer.offer_id != offer => {
return;
}
None => {
return;
}
Some(r) => r,
};
let mt = match self.mime_type_to_atom(mt).await {
Ok(mt) => mt,
Err(e) => {
log::error!("Could not get mime type atom: {}", ErrorFmt(e));
return;
}
};
enhanced.mime_types.borrow_mut().push(mt);
}
pub(super) async fn dd_set_offer<T: XTransfer>(
&mut self,
sd: &SelectionData<T>,
seat: SeatId,
offer: Rc<XDataOffer>,
) {
let mut mime_types = vec![];
if let Some(offer) = sd.offers.remove(&seat) {
destroy_data_offer::<T>(&offer.offer);
mime_types = mem::take(offer.mime_types.borrow_mut().deref_mut());
}
sd.offers.set(
seat,
Rc::new(EnhancedOffer {
offer,
mime_types: RefCell::new(mime_types),
active: Cell::new(false),
}),
);
}
pub(super) async fn dd_set_selection<T: XTransfer>(
&mut self,
sd: &SelectionData<T>,
seat: SeatId,
offer: Option<Rc<XDataOffer>>,
) {
let offer = match offer {
None => {
if let Some(offer) = sd.offers.remove(&seat) {
destroy_data_offer::<T>(&offer.offer);
if offer.active.get() {
sd.active_offer.take();
}
}
return;
}
Some(offer) => offer,
};
let enhanced = match sd.offers.get(&seat) {
None => {
destroy_data_offer::<T>(&offer);
return;
}
Some(e) => e,
};
if !rc_eq(&enhanced.offer, &offer) {
destroy_data_offer::<T>(&offer);
return;
}
if !enhanced.active.replace(true)
&& let Some(old) = sd.active_offer.set(Some(enhanced))
{
old.active.set(false);
}
let so = SetSelectionOwner {
owner: sd.win.get(),
selection: sd.selection.get(),
time: 0,
};
if let Err(err) = self.c.call(&so).await {
log::error!("Could not set primary selection owner: {}", ErrorFmt(err));
}
}
async fn get_atom_name(&mut self, atom: u32) -> Result<String, XconError> {
if let Some(name) = self.atom_name_cache.get(&atom) {
return Ok(name.clone());
}
let gan = GetAtomName { atom };
match self.c.call(&gan).await {
Ok(name) => {
let name = name.get().name.to_string();
self.atom_name_cache.insert(atom, name.clone());
Ok(name)
}
Err(e) => Err(e),
}
}
async fn get_atom(&mut self, name: String) -> Result<u32, XconError> {
if let Some(atom) = self.atom_cache.get(&name) {
return Ok(*atom);
}
let ia = InternAtom {
only_if_exists: 0,
name: name.as_bytes().as_bstr(),
};
match self.c.call(&ia).await {
Ok(id) => {
let atom = id.get().atom;
self.atom_cache.insert(name, atom);
Ok(atom)
}
Err(e) => Err(e),
}
}
async fn mime_type_to_atom(&mut self, mime_type: String) -> Result<u32, XconError> {
match mime_type.as_str() {
TEXT_PLAIN_UTF_8 => Ok(self.atoms.UTF8_STRING),
TEXT_PLAIN => Ok(ATOM_STRING),
_ => self.get_atom(mime_type).await,
}
}
async fn atom_to_mime_type(&mut self, atom: u32) -> Result<String, XconError> {
if atom == self.atoms.UTF8_STRING {
Ok(TEXT_PLAIN_UTF_8.to_string())
} else if atom == ATOM_STRING {
Ok(TEXT_PLAIN.to_string())
} else {
self.get_atom_name(atom).await
}
}
pub(super) async fn dd_send_source<T: XTransfer>(
&mut self,
sd: &SelectionData<T>,
seat: SeatId,
src: DataSourceId,
mime_type: String,
fd: Rc<OwnedFd>,
) {
let actual_src = match sd.sources.get(&seat) {
None => return,
Some(src) => src,
};
if actual_src.source_data().id != src {
return;
}
let mime_type = match self.mime_type_to_atom(mime_type).await {
Ok(mt) => mt,
Err(e) => {
log::error!("Could not intern mime type: {}", ErrorFmt(e));
return;
}
};
let cs = ConvertSelection {
requestor: sd.win.get(),
selection: sd.selection.get(),
target: mime_type,
property: self.atoms._WL_SELECTION,
time: 0,
};
if let Err(e) = self.c.call(&cs).await {
log::error!(
"Could not perform convert selection request: {}",
ErrorFmt(e)
);
return;
}
sd.pending_transfers
.borrow_mut()
.push(PendingTransfer { mime_type, fd });
}
pub(super) fn dd_cancel_source<T: XTransfer>(
&mut self,
sd: &SelectionData<T>,
seat: SeatId,
source: DataSourceId,
) {
if let Some(cur) = sd.sources.get(&seat)
&& cur.source_data().id == source
{
sd.sources.remove(&seat);
destroy_data_source::<T>(&cur);
}
}
pub(super) async fn handle_xfixes_event(
&mut self,
event: &Event,
) -> Result<(), XWaylandError> {
match event.code() {
XfixesSelectionNotify::OPCODE => self.handle_xfixes_selection_notify(event).await,
_ => Ok(()),
}
}
async fn handle_xfixes_selection_notify(&mut self, event: &Event) -> Result<(), XWaylandError> {
let event: XfixesSelectionNotify = event.parse()?;
let shared = self.shared.clone();
if event.selection == self.atoms.PRIMARY {
self.handle_xfixes_selection_notify_(&shared.primary_selection, &event)
.await
} else if event.selection == self.atoms.CLIPBOARD {
self.handle_xfixes_selection_notify_(&shared.data, &event)
.await
} else {
Ok(())
}
}
async fn handle_xfixes_selection_notify_<T: XTransfer>(
&mut self,
sd: &SelectionData<T>,
event: &XfixesSelectionNotify,
) -> Result<(), XWaylandError> {
if event.owner == sd.win.get() {
return Ok(());
}
sd.destroy_sources();
let cs = ConvertSelection {
requestor: sd.win.get(),
selection: sd.selection.get(),
target: self.atoms.TARGETS,
property: self.atoms._WL_SELECTION,
time: event.timestamp,
};
if let Err(e) = self.c.call(&cs).await {
log::error!("Could not convert selection: {}", ErrorFmt(e));
}
Ok(())
}
pub(super) async fn handle_selection_request(
&mut self,
event: &Event,
) -> Result<(), XWaylandError> {
let event: SelectionRequest = event.parse()?;
let shared = self.shared.clone();
if event.selection == self.atoms.PRIMARY {
self.handle_selection_request_(&shared.primary_selection, &event)
.await
} else if event.selection == self.atoms.CLIPBOARD {
self.handle_selection_request_(&shared.data, &event).await
} else {
log::warn!("Unknown selection request");
Ok(())
}
}
async fn handle_selection_request_<T: XTransfer>(
&mut self,
sd: &SelectionData<T>,
event: &SelectionRequest,
) -> Result<(), XWaylandError> {
let mut success = Some(false);
if let Some(offer) = sd.active_offer.get() {
let mt = offer.mime_types.borrow_mut();
if event.target == self.atoms.TARGETS {
let cp = ChangeProperty {
mode: PROP_MODE_REPLACE,
window: event.requestor,
property: event.property,
ty: ATOM_ATOM,
format: 32,
data: uapi::as_bytes(&mt[..]),
};
match self.c.call(&cp).await {
Ok(_) => success = Some(true),
Err(e) => {
log::error!("Could not set selection property: {}", ErrorFmt(e));
}
}
} else {
'convert: {
let present = mt.contains(&event.target);
drop(mt);
let mt = match self.atom_to_mime_type(event.target).await {
Ok(mt) => mt,
Err(e) => {
log::error!("Could not get mime type name: {}", ErrorFmt(e));
break 'convert;
}
};
if !present {
log::error!("Peer requested unavailable target {}", mt);
break 'convert;
}
let Pipe {
read: rx,
write: tx,
} = match pipe() {
Ok(p) => p,
Err(e) => {
log::error!("Could not create pipe: {}", e);
break 'convert;
}
};
success = None;
receive_data_offer::<T>(&offer.offer, &mt, Rc::new(tx));
let id = self.transfer_ids.fetch_add(1);
let wtx = WaylandToXTransfer {
id,
fd: Rc::new(rx),
ring: self.state.ring.clone(),
c: self.c.clone(),
window: event.requestor,
time: event.time,
property: event.property,
ty: event.target,
selection: sd.selection.get(),
shared: self.shared.clone(),
};
self.shared
.transfers
.set(id, self.state.eng.spawn("wayland to X transfer", wtx.run()));
}
}
}
if let Some(success) = success {
let target = match success {
true => event.target,
false => ATOM_NONE,
};
let sn = SelectionNotify {
time: event.time,
requestor: event.requestor,
selection: sd.selection.get(),
target,
property: event.property,
};
if let Err(e) = self.c.send_event(false, event.requestor, 0, &sn).await {
log::error!("Could not send event: {}", ErrorFmt(e));
}
}
Ok(())
}
pub(super) async fn handle_selection_notify(
&mut self,
event: &Event,
) -> Result<(), XWaylandError> {
let event: SelectionNotify = event.parse()?;
if event.property != self.atoms._WL_SELECTION {
return Ok(());
}
let shared = self.shared.clone();
if event.selection == self.atoms.PRIMARY {
self.handle_selection_notify_(&shared.primary_selection, &event)
.await
} else if event.selection == self.atoms.CLIPBOARD {
self.handle_selection_notify_(&shared.data, &event).await
} else {
Ok(())
}
}
async fn handle_selection_notify_<T: XTransfer>(
&mut self,
sd: &SelectionData<T>,
event: &SelectionNotify,
) -> Result<(), XWaylandError> {
if event.property != self.atoms._WL_SELECTION {
return Ok(());
}
if event.target == ATOM_NONE {
return Ok(());
}
if event.target == self.atoms.TARGETS {
let targets = self.get_selection_mime_types(sd.win.get()).await?;
for dev in self.shared.devices.lock().values() {
let seat = T::get_device_seat(dev);
if !seat.may_modify_primary_selection(&self.client, None) {
continue;
}
let source = Rc::new(XDataSource {
state: self.state.clone(),
device: dev.clone(),
data: SourceData::new(&self.client),
location: T::LOCATION,
});
for target in &targets {
add_data_source_mime_type::<T>(&source, target);
}
let res = match source.location {
TransferLocation::Clipboard => seat.set_selection(Some(source.clone())),
TransferLocation::PrimarySelection => {
seat.set_primary_selection(Some(source.clone()))
}
};
if let Err(e) = res {
log::error!("Could not set selection: {}", ErrorFmt(e));
return Ok(());
}
sd.sources.set(seat.id(), source);
}
} else {
let mut transfers = sd.pending_transfers.borrow_mut();
let transfers = transfers.drain(..);
let mut data = vec![];
let gp = self
.c
.get_property(
sd.win.get(),
self.atoms._WL_SELECTION,
event.target,
&mut data,
)
.await;
if let Err(e) = gp {
log::error!("Could not get converted property: {}", e);
return Ok(());
}
let mut data = Buf::from_slice(&data);
for transfer in transfers {
if event.target != transfer.mime_type {
log::error!("Conversion yielded an incompatible mime type");
continue;
}
let id = self.transfer_ids.fetch_add(1);
let transfer = XToWaylandTransfer {
id,
data: data.clone(),
fd: transfer.fd,
state: self.state.clone(),
shared: self.shared.clone(),
};
self.shared.transfers.set(
id,
self.state
.eng
.spawn("X to wayland transfer", transfer.run()),
);
}
}
Ok(())
}
async fn get_selection_mime_types(
&mut self,
window: u32,
) -> Result<Vec<String>, XWaylandError> {
let mut buf = vec![];
self.c
.get_property3::<u32>(window, self.atoms._WL_SELECTION, ATOM_ATOM, true, &mut buf)
.await?;
let mut res = vec![];
for atom in buf {
let name = match self.atom_to_mime_type(atom).await {
Ok(n) => n,
Err(e) => {
log::error!("Could not get atom name: {}", ErrorFmt(e));
continue;
}
};
res.push(name);
}
Ok(res)
}
}

View file

@ -0,0 +1,109 @@
use {
super::XwmShared,
crate::{
io_uring::{IoUring, IoUringError},
state::State,
utils::{buf::Buf, errorfmt::ErrorFmt, oserror::OsError},
wire_xcon::{ChangeProperty, SelectionNotify},
xcon::{
Xcon,
consts::{ATOM_NONE, PROP_MODE_APPEND},
},
},
std::{rc::Rc, time::Duration},
uapi::{OwnedFd, c},
};
pub(super) struct XToWaylandTransfer {
pub(super) id: u64,
pub(super) data: Buf,
pub(super) fd: Rc<OwnedFd>,
pub(super) state: Rc<State>,
pub(super) shared: Rc<XwmShared>,
}
impl XToWaylandTransfer {
pub(super) async fn run(mut self) {
let timeout = self.state.now() + Duration::from_millis(5000);
let mut pos = 0;
while pos < self.data.len() {
let res = self
.state
.ring
.write(&self.fd, self.data.slice(pos..), Some(timeout));
match res.await {
Ok(n) => pos += n,
Err(IoUringError::OsError(OsError(c::ECANCELED))) => {
log::error!("Transfer timed out");
break;
}
Err(e) => {
log::error!("Could not write to wayland client: {}", ErrorFmt(e));
break;
}
}
}
self.shared.transfers.remove(&self.id);
}
}
pub(super) struct WaylandToXTransfer {
pub(super) id: u64,
pub(super) fd: Rc<OwnedFd>,
pub(super) ring: Rc<IoUring>,
pub(super) c: Rc<Xcon>,
pub(super) window: u32,
pub(super) time: u32,
pub(super) property: u32,
pub(super) ty: u32,
pub(super) selection: u32,
pub(super) shared: Rc<XwmShared>,
}
impl WaylandToXTransfer {
pub(super) async fn run(self) {
let mut success = false;
let mut buf = Buf::new(1024);
loop {
match self.ring.read(&self.fd, buf.clone()).await {
Ok(0) => {
success = true;
break;
}
Ok(n) => {
let cp = ChangeProperty {
mode: PROP_MODE_APPEND,
window: self.window,
property: self.property,
ty: self.ty,
format: 8,
data: &buf[..n],
};
if let Err(e) = self.c.call(&cp).await {
log::error!("Could not append data to property: {}", ErrorFmt(e));
break;
}
}
Err(e) => {
log::error!("Could not read from wayland client: {}", ErrorFmt(e));
break;
}
}
}
let target = match success {
true => self.ty,
false => ATOM_NONE,
};
let sn = SelectionNotify {
time: self.time,
requestor: self.window,
selection: self.selection,
target,
property: self.property,
};
if let Err(e) = self.c.send_event(false, self.window, 0, &sn).await {
log::error!("Could not send event: {}", ErrorFmt(e));
}
self.shared.transfers.remove(&self.id);
}
}