Support XDG Activation
Test XDG Activation
This commit is contained in:
parent
b34b08f004
commit
180efb0ba9
10 changed files with 413 additions and 27 deletions
|
|
@ -1,4 +1,5 @@
|
|||
mod data_device;
|
||||
pub mod xdg_activation;
|
||||
|
||||
use crate::server::{ObjectEvent, ObjectKey};
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
|
@ -46,6 +47,7 @@ use wayland_protocols::{
|
|||
viewporter::client::{wp_viewport::WpViewport, wp_viewporter::WpViewporter},
|
||||
},
|
||||
xdg::{
|
||||
activation::v1::client::xdg_activation_v1::XdgActivationV1,
|
||||
shell::client::{
|
||||
xdg_popup::XdgPopup, xdg_positioner::XdgPositioner, xdg_surface::XdgSurface,
|
||||
xdg_toplevel::XdgToplevel, xdg_wm_base::XdgWmBase,
|
||||
|
|
@ -69,6 +71,7 @@ pub struct Globals {
|
|||
smithay_client_toolkit::data_device_manager::WritePipe,
|
||||
)>,
|
||||
pub cancelled: bool,
|
||||
pub pending_activations: Vec<(xcb::x::Window, String)>,
|
||||
}
|
||||
|
||||
pub type ClientQueueHandle = QueueHandle<Globals>;
|
||||
|
|
@ -136,6 +139,7 @@ delegate_noop!(Globals: WpViewport);
|
|||
delegate_noop!(Globals: ZxdgOutputManagerV1);
|
||||
delegate_noop!(Globals: ZwpPointerConstraintsV1);
|
||||
delegate_noop!(Globals: ZwpTabletManagerV2);
|
||||
delegate_noop!(Globals: XdgActivationV1);
|
||||
|
||||
impl Dispatch<WlRegistry, GlobalListContents> for Globals {
|
||||
fn event(
|
||||
|
|
|
|||
42
src/clientside/xdg_activation.rs
Normal file
42
src/clientside/xdg_activation.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use smithay_client_toolkit::{
|
||||
activation::{ActivationHandler, RequestData, RequestDataExt},
|
||||
delegate_activation,
|
||||
};
|
||||
use xcb::x;
|
||||
|
||||
use crate::clientside::Globals;
|
||||
|
||||
delegate_activation!(Globals, ActivationData);
|
||||
|
||||
pub struct ActivationData {
|
||||
window: x::Window,
|
||||
data: RequestData,
|
||||
}
|
||||
|
||||
impl ActivationData {
|
||||
pub fn new(window: x::Window, data: RequestData) -> Self {
|
||||
Self { window, data }
|
||||
}
|
||||
}
|
||||
|
||||
impl RequestDataExt for ActivationData {
|
||||
fn app_id(&self) -> Option<&str> {
|
||||
self.data.app_id()
|
||||
}
|
||||
|
||||
fn seat_and_serial(&self) -> Option<(&wayland_client::protocol::wl_seat::WlSeat, u32)> {
|
||||
self.data.seat_and_serial()
|
||||
}
|
||||
|
||||
fn surface(&self) -> Option<&wayland_client::protocol::wl_surface::WlSurface> {
|
||||
self.data.surface()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActivationHandler for Globals {
|
||||
type RequestData = ActivationData;
|
||||
|
||||
fn new_token(&mut self, token: String, data: &Self::RequestData) {
|
||||
self.pending_activations.push((data.window, token));
|
||||
}
|
||||
}
|
||||
|
|
@ -445,10 +445,18 @@ impl<C: XConnection> Dispatch<WlSeat, ObjectKey> for ServerState<C> {
|
|||
state
|
||||
.objects
|
||||
.insert_from_other_objects([*key], |[seat_obj], key| {
|
||||
let Seat { client, .. }: &Seat = seat_obj.try_into().unwrap();
|
||||
let client = client.get_keyboard(&state.qh, key);
|
||||
let Seat {
|
||||
client: client_seat,
|
||||
..
|
||||
}: &Seat = seat_obj.try_into().unwrap();
|
||||
let client = client_seat.get_keyboard(&state.qh, key);
|
||||
let server = data_init.init(id, key);
|
||||
Keyboard { client, server }.into()
|
||||
Keyboard {
|
||||
client,
|
||||
server,
|
||||
seat: client_seat.clone(),
|
||||
}
|
||||
.into()
|
||||
});
|
||||
}
|
||||
Request::<WlSeat>::GetTouch { id } => {
|
||||
|
|
|
|||
|
|
@ -540,7 +540,12 @@ impl HandleEvent for Pointer {
|
|||
}
|
||||
}
|
||||
|
||||
pub type Keyboard = GenericObject<WlKeyboard, client::wl_keyboard::WlKeyboard>;
|
||||
pub struct Keyboard {
|
||||
pub server: WlKeyboard,
|
||||
pub client: client::wl_keyboard::WlKeyboard,
|
||||
pub seat: client::wl_seat::WlSeat,
|
||||
}
|
||||
|
||||
impl HandleEvent for Keyboard {
|
||||
type Event = client::wl_keyboard::Event;
|
||||
|
||||
|
|
@ -557,7 +562,14 @@ impl HandleEvent for Keyboard {
|
|||
.get(key)
|
||||
.map(<_ as AsRef<SurfaceData>>::as_ref)
|
||||
}) {
|
||||
state.last_kb_serial = Some(serial);
|
||||
state.last_kb_serial = Some((
|
||||
state
|
||||
.last_kb_serial
|
||||
.take()
|
||||
.and_then(|(seat, _)| (seat == self.seat).then_some(seat))
|
||||
.unwrap_or_else(|| self.seat.clone()),
|
||||
serial,
|
||||
));
|
||||
let output_name = data.get_output_name(state);
|
||||
state.to_focus = Some(FocusData {
|
||||
window: data.window.unwrap(),
|
||||
|
|
@ -584,6 +596,22 @@ impl HandleEvent for Keyboard {
|
|||
self.server.leave(serial, &data.server);
|
||||
}
|
||||
}
|
||||
client::wl_keyboard::Event::Key {
|
||||
serial,
|
||||
time,
|
||||
key,
|
||||
state: key_state,
|
||||
} => {
|
||||
state.last_kb_serial = Some((
|
||||
state
|
||||
.last_kb_serial
|
||||
.take()
|
||||
.and_then(|(seat, _)| (seat == self.seat).then_some(seat))
|
||||
.unwrap_or_else(|| self.seat.clone()),
|
||||
serial,
|
||||
));
|
||||
self.server.key(serial, time, key, convert_wenum(key_state));
|
||||
}
|
||||
_ => simple_event_shunt! {
|
||||
self.server, event: client::wl_keyboard::Event => [
|
||||
Keymap {
|
||||
|
|
@ -591,12 +619,6 @@ impl HandleEvent for Keyboard {
|
|||
|fd| fd.as_fd(),
|
||||
size
|
||||
},
|
||||
Key {
|
||||
serial,
|
||||
time,
|
||||
key,
|
||||
|state| convert_wenum(state)
|
||||
},
|
||||
Modifiers {
|
||||
serial,
|
||||
mods_depressed,
|
||||
|
|
|
|||
|
|
@ -11,11 +11,12 @@ use crate::{X11Selection, XConnection};
|
|||
use log::{debug, warn};
|
||||
use rustix::event::{poll, PollFd, PollFlags};
|
||||
use slotmap::{new_key_type, HopSlotMap, SparseSecondaryMap};
|
||||
use smithay_client_toolkit::activation::ActivationState;
|
||||
use smithay_client_toolkit::data_device_manager::{
|
||||
data_device::DataDevice, data_offer::SelectionOffer, data_source::CopyPasteSource,
|
||||
DataDeviceManagerState,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::io::Read;
|
||||
use std::os::fd::{AsFd, BorrowedFd};
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
|
@ -43,10 +44,11 @@ use wayland_protocols::{
|
|||
xwayland_shell_v1::XwaylandShellV1, xwayland_surface_v1::XwaylandSurfaceV1,
|
||||
},
|
||||
};
|
||||
use wayland_server::protocol::wl_seat::WlSeat;
|
||||
use wayland_server::{
|
||||
protocol::{
|
||||
wl_callback::WlCallback, wl_compositor::WlCompositor, wl_output::WlOutput, wl_seat::WlSeat,
|
||||
wl_shm::WlShm, wl_surface::WlSurface,
|
||||
wl_callback::WlCallback, wl_compositor::WlCompositor, wl_output::WlOutput, wl_shm::WlShm,
|
||||
wl_surface::WlSurface,
|
||||
},
|
||||
Client, DisplayHandle, Resource, WEnum,
|
||||
};
|
||||
|
|
@ -102,6 +104,7 @@ struct WindowData {
|
|||
attrs: WindowAttributes,
|
||||
output_offset: WindowOutputOffset,
|
||||
output_key: Option<ObjectKey>,
|
||||
activation_token: Option<String>,
|
||||
}
|
||||
|
||||
impl WindowData {
|
||||
|
|
@ -110,6 +113,7 @@ impl WindowData {
|
|||
override_redirect: bool,
|
||||
dims: WindowDims,
|
||||
parent: Option<x::Window>,
|
||||
activation_token: Option<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
window,
|
||||
|
|
@ -124,6 +128,7 @@ impl WindowData {
|
|||
},
|
||||
output_offset: WindowOutputOffset::default(),
|
||||
output_key: None,
|
||||
activation_token,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -488,6 +493,7 @@ pub struct ServerState<C: XConnection> {
|
|||
associated_windows: SparseSecondaryMap<ObjectKey, x::Window>,
|
||||
output_keys: SparseSecondaryMap<ObjectKey, ()>,
|
||||
windows: HashMap<x::Window, WindowData>,
|
||||
pids: HashSet<u32>,
|
||||
|
||||
qh: ClientQueueHandle,
|
||||
client: Option<Client>,
|
||||
|
|
@ -499,7 +505,8 @@ pub struct ServerState<C: XConnection> {
|
|||
|
||||
xdg_wm_base: XdgWmBase,
|
||||
clipboard_data: Option<ClipboardData<C::X11Selection>>,
|
||||
last_kb_serial: Option<u32>,
|
||||
last_kb_serial: Option<(client::wl_seat::WlSeat, u32)>,
|
||||
activation_state: Option<ActivationState>,
|
||||
global_output_offset: GlobalOutputOffset,
|
||||
global_offset_updated: bool,
|
||||
}
|
||||
|
|
@ -529,6 +536,12 @@ impl<C: XConnection> ServerState<C> {
|
|||
source: None::<CopyPasteData<C::X11Selection>>,
|
||||
});
|
||||
|
||||
let activation_state = ActivationState::bind(&clientside.global_list, &qh)
|
||||
.inspect_err(|e| {
|
||||
warn!("Could not bind xdg activation ({e:?}). Windows might not recive focus depending on compositor focus stealing policy.")
|
||||
})
|
||||
.ok();
|
||||
|
||||
dh.create_global::<Self, XwaylandShellV1, _>(1, ());
|
||||
clientside
|
||||
.global_list
|
||||
|
|
@ -537,6 +550,7 @@ impl<C: XConnection> ServerState<C> {
|
|||
|
||||
Self {
|
||||
windows: HashMap::new(),
|
||||
pids: HashSet::new(),
|
||||
clientside,
|
||||
client: None,
|
||||
qh,
|
||||
|
|
@ -552,6 +566,7 @@ impl<C: XConnection> ServerState<C> {
|
|||
xdg_wm_base,
|
||||
clipboard_data,
|
||||
last_kb_serial: None,
|
||||
activation_state,
|
||||
global_output_offset: GlobalOutputOffset {
|
||||
x: GlobalOutputOffsetDimension {
|
||||
owner: None,
|
||||
|
|
@ -593,10 +608,23 @@ impl<C: XConnection> ServerState<C> {
|
|||
override_redirect: bool,
|
||||
dims: WindowDims,
|
||||
parent: Option<x::Window>,
|
||||
pid: Option<u32>,
|
||||
) {
|
||||
let activation_token = pid
|
||||
.filter(|pid| self.pids.insert(*pid))
|
||||
.and_then(|pid| std::fs::read(format!("/proc/{pid}/environ")).ok())
|
||||
.and_then(|environ| {
|
||||
environ
|
||||
.split(|byte| *byte == 0)
|
||||
.find_map(|line| line.strip_prefix(b"XDG_ACTIVATION_TOKEN="))
|
||||
.and_then(|token| String::from_utf8(token.to_vec()).ok())
|
||||
});
|
||||
if activation_token.is_none() {
|
||||
self.activate_window(window);
|
||||
}
|
||||
self.windows.insert(
|
||||
window,
|
||||
WindowData::new(window, override_redirect, dims, parent),
|
||||
WindowData::new(window, override_redirect, dims, parent, activation_token),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -823,6 +851,41 @@ impl<C: XConnection> ServerState<C> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn activate_window(&mut self, window: x::Window) {
|
||||
let Some(activation_state) = self.activation_state.as_ref() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(last_focused_toplevel) = self.last_focused_toplevel else {
|
||||
warn!("No last focused toplevel, cannot focus window {window:?}");
|
||||
return;
|
||||
};
|
||||
let Some(win) = self.windows.get(&last_focused_toplevel) else {
|
||||
warn!("Unknown last focused toplevel, cannot focus window {window:?}");
|
||||
return;
|
||||
};
|
||||
let Some(key) = win.surface_key else {
|
||||
warn!("Last focused toplevel has no surface, cannot focus window {window:?}");
|
||||
return;
|
||||
};
|
||||
let Some(object) = self.objects.get_mut(key) else {
|
||||
warn!("Last focused toplevel has stale reference, cannot focus window {window:?}");
|
||||
return;
|
||||
};
|
||||
let surface: &mut SurfaceData = object.as_mut();
|
||||
activation_state.request_token_with_data(
|
||||
&self.qh,
|
||||
xdg_activation::ActivationData::new(
|
||||
window,
|
||||
smithay_client_toolkit::activation::RequestData {
|
||||
app_id: win.attrs.class.clone(),
|
||||
seat_and_serial: self.last_kb_serial.clone(),
|
||||
surface: Some(surface.client.clone()),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn destroy_window(&mut self, window: x::Window) {
|
||||
let _ = self.windows.remove(&window);
|
||||
}
|
||||
|
|
@ -839,7 +902,12 @@ impl<C: XConnection> ServerState<C> {
|
|||
let CopyPasteData::X11 { inner, .. } = d.source.insert(data) else {
|
||||
unreachable!();
|
||||
};
|
||||
if let Some(serial) = self.last_kb_serial.as_ref().copied() {
|
||||
if let Some(serial) = self
|
||||
.last_kb_serial
|
||||
.as_ref()
|
||||
.map(|(_seat, serial)| serial)
|
||||
.copied()
|
||||
{
|
||||
inner.set_selection(d.device.as_ref().unwrap(), serial);
|
||||
}
|
||||
}
|
||||
|
|
@ -920,6 +988,7 @@ impl<C: XConnection> ServerState<C> {
|
|||
}
|
||||
|
||||
self.handle_clipboard_events();
|
||||
self.handle_activations();
|
||||
self.clientside
|
||||
.queue
|
||||
.flush()
|
||||
|
|
@ -967,6 +1036,24 @@ impl<C: XConnection> ServerState<C> {
|
|||
}
|
||||
}
|
||||
|
||||
fn handle_activations(&mut self) {
|
||||
let Some(activation_state) = self.activation_state.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let globals = &mut self.clientside.globals;
|
||||
|
||||
globals.pending_activations.retain(|(window, token)| {
|
||||
if let Some(window) = self.windows.get(window) {
|
||||
if let Some(key) = window.surface_key {
|
||||
let surface: &SurfaceData = self.objects[key].as_ref();
|
||||
activation_state.activate::<Self>(&surface.client, token.clone());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
});
|
||||
}
|
||||
|
||||
fn calc_global_output_offset(&mut self) {
|
||||
for (key, _) in &self.output_keys {
|
||||
let Some(object) = &self.objects.get(key) else {
|
||||
|
|
@ -1134,6 +1221,14 @@ impl<C: XConnection> ServerState<C> {
|
|||
toplevel.set_fullscreen(None);
|
||||
}
|
||||
|
||||
let surface: &SurfaceData = self.objects[surface_key].as_ref();
|
||||
if let (Some(activation_state), Some(token)) = (
|
||||
self.activation_state.as_ref(),
|
||||
window.activation_token.clone(),
|
||||
) {
|
||||
activation_state.activate::<Self>(&surface.client, token);
|
||||
}
|
||||
|
||||
ToplevelData {
|
||||
xdg: XdgSurfaceData {
|
||||
surface: xdg,
|
||||
|
|
|
|||
|
|
@ -515,7 +515,7 @@ impl TestFixture {
|
|||
let dims = data.dims;
|
||||
self.register_window(window, data);
|
||||
self.satellite
|
||||
.new_window(window, override_redirect, dims, parent);
|
||||
.new_window(window, override_redirect, dims, parent, None);
|
||||
}
|
||||
|
||||
fn map_window(
|
||||
|
|
@ -1118,6 +1118,7 @@ fn window_group_properties() {
|
|||
..Default::default()
|
||||
},
|
||||
None,
|
||||
None,
|
||||
);
|
||||
f.satellite
|
||||
.set_win_title(prop_win, WmName::WmName("window".into()));
|
||||
|
|
@ -1137,7 +1138,7 @@ fn window_group_properties() {
|
|||
let (_, surface) = comp.create_surface();
|
||||
let dims = data.dims;
|
||||
f.register_window(win, data);
|
||||
f.satellite.new_window(win, false, dims, None);
|
||||
f.satellite.new_window(win, false, dims, None, None);
|
||||
f.satellite.set_win_hints(
|
||||
win,
|
||||
super::WmHints {
|
||||
|
|
|
|||
|
|
@ -121,6 +121,7 @@ impl XState {
|
|||
xcb::Extension::Composite,
|
||||
xcb::Extension::RandR,
|
||||
xcb::Extension::XFixes,
|
||||
xcb::Extension::Res,
|
||||
],
|
||||
&[],
|
||||
)
|
||||
|
|
@ -301,7 +302,13 @@ impl XState {
|
|||
} else {
|
||||
Some(parent)
|
||||
};
|
||||
server_state.new_window(e.window(), e.override_redirect(), (&e).into(), parent);
|
||||
server_state.new_window(
|
||||
e.window(),
|
||||
e.override_redirect(),
|
||||
(&e).into(),
|
||||
parent,
|
||||
self.get_pid(e.window()),
|
||||
);
|
||||
}
|
||||
xcb::Event::X(x::Event::ReparentNotify(e)) => {
|
||||
debug!("reparent event: {e:?}");
|
||||
|
|
@ -313,6 +320,7 @@ impl XState {
|
|||
attrs.override_redirect,
|
||||
attrs.dims,
|
||||
None,
|
||||
self.get_pid(e.window()),
|
||||
);
|
||||
self.handle_window_attributes(server_state, e.window(), attrs);
|
||||
} else {
|
||||
|
|
@ -447,6 +455,9 @@ impl XState {
|
|||
}
|
||||
}
|
||||
}
|
||||
x if x == self.atoms.active_win => {
|
||||
server_state.activate_window(e.window());
|
||||
}
|
||||
t => warn!("unrecognized message: {t:?}"),
|
||||
},
|
||||
xcb::Event::X(x::Event::MappingNotify(_)) => {}
|
||||
|
|
@ -649,6 +660,24 @@ impl XState {
|
|||
}
|
||||
}
|
||||
|
||||
fn get_pid(&self, window: x::Window) -> Option<u32> {
|
||||
let Some(pid) = self
|
||||
.connection
|
||||
.wait_for_reply(self.connection.send_request(&xcb::res::QueryClientIds {
|
||||
specs: &[xcb::res::ClientIdSpec {
|
||||
client: window.resource_id(),
|
||||
mask: xcb::res::ClientIdMask::LOCAL_CLIENT_PID,
|
||||
}],
|
||||
}))
|
||||
.ok()
|
||||
.and_then(|reply| Some(*reply.ids().next()?.value().first()?))
|
||||
else {
|
||||
warn!("Failed to get pid of window: {window:?}");
|
||||
return None;
|
||||
};
|
||||
Some(pid)
|
||||
}
|
||||
|
||||
fn handle_property_change(
|
||||
&mut self,
|
||||
event: x::PropertyNotifyEvent,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue