S031: native drag and drop and file clipboard on X11
XDND v5 source and target on an own x11rb connection (proxy window via XdndProxy), CLIPBOARD owner and reader. Wayland and X11 share one Backend trait. examples/xdnd_probe drives both directions under Xvfb. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
8fa4429c5b
commit
3034af407a
10 changed files with 944 additions and 63 deletions
|
|
@ -91,6 +91,7 @@ Never modify: `PROMPT.md` (the user's brief), the user's other projects under `~
|
|||
| `cargo run -p tunal -- --gallery --page menu\|dialog --screenshot <png> [--dark] --size 960x640` | open menu + submenu + two toasts, or dialog + indeterminate toast, over a stand-in folder | S005 |
|
||||
| `TUNAL_SETTINGS=<0-5> cargo run -p tunal -- <dir> --screenshot <png> --size 1000x700` (`XDG_CONFIG_HOME=<scratch>`) | settings dialog page over a folder | S013 |
|
||||
| `TUNAL_DRAG=x1,y1,x2,y2[,hold] TUNAL_REDUCED_MOTION=1 cargo run -p tunal -- <dir> --screenshot <png> --size 1200x760` (`XDG_CONFIG_HOME=<scratch>`) | drags between window points and drops (drop menu) or holds (glow + drag image) before the frame | S014 |
|
||||
| `Xvfb :97 &` then `env -u WAYLAND_DISPLAY DISPLAY=:97 target/debug/tunal <dir> &` and `DISPLAY=:97 target/debug/examples/xdnd_probe x1 y1 x2 y2` (or `in x y <uri>`) | X11 native DnD: fake XTEST drag out of the window onto a probe target, or a probe source dropping into the app; Xvfb only | S031 |
|
||||
| `TUNAL_APPEARANCE=1 XDG_CONFIG_HOME=<scratch> cargo run -p tunal -- <dir> --screenshot <png> --size 1000x760` | folder appearance dialog for `<dir>` (theme icons looked up at once); `cargo run -p tunal-dirmeta --example roundtrip -- <dir>` sets `Icon` in `<dir>/.directory` and prints the old values | S015 |
|
||||
| `TUNAL_PLAY=<audio file> XDG_CONFIG_HOME=<scratch> cargo run -p tunal -- <dir> --screenshot <png> [--dark] --size 1200x760` | player bar open on that file, a third in (tags, cover, waveform read at once; no audio output) | S023 |
|
||||
| `TUNAL_TAGS=editor\|lookup\|mapping\|apply:<file>:<file> TUNAL_TAGS_SYNC=1 TUNAL_MUSICBRAINZ=<mock url> XDG_CONFIG_HOME=<scratch> cargo run -p tunal -- <dir> --screenshot <png> --size 1000x760` | tag editor / lookup / mapping over a folder; `apply` also writes the tags (scratch copies only); mock = a local server for both services | S021 |
|
||||
|
|
|
|||
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -7013,6 +7013,7 @@ dependencies = [
|
|||
"tunal-thumbs",
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
"x11rb",
|
||||
"zbus",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@ The same as S030 on X11 with XDND and the CLIPBOARD selection via `x11rb` (pure
|
|||
crates/tunal/src/native/{mod,wayland}.rs; docs/DECISIONS.md (S030 entry).
|
||||
|
||||
## Steps
|
||||
- [ ] Window id from raw-window-handle; own `x11rb` connection; XdndAware on our window.
|
||||
- [ ] Drag out: XDND source (XdndEnter/Position/Drop/Finished, selection `XdndSelection` with uri-list).
|
||||
- [ ] Drag in: XDND target mapped to the same hit test and drop menu.
|
||||
- [ ] Clipboard: own CLIPBOARD for file copy/cut, read other apps' uri-list on paste.
|
||||
- [ ] PROGRESS entry, INDEX line, commit `S031: …`
|
||||
- [x] Window id from raw-window-handle; own `x11rb` connection; XdndAware on our window.
|
||||
- [x] Drag out: XDND source (XdndEnter/Position/Drop/Finished, selection `XdndSelection` with uri-list).
|
||||
- [x] Drag in: XDND target mapped to the same hit test and drop menu.
|
||||
- [x] Clipboard: own CLIPBOARD for file copy/cut, read other apps' uri-list on paste.
|
||||
- [x] PROGRESS entry, INDEX line, commit `S031: …`
|
||||
|
||||
## End criteria
|
||||
- `cargo build --workspace`, `cargo clippy --workspace --all-targets -- -D warnings` and `cargo fmt --check` pass.
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ Tracker: https://claude.ai/artifact/4X8YCk3cQjUGyuyCqGuU7v
|
|||
## 11 Native drag and drop
|
||||
|
||||
- [x] [S030 — Native drag and drop and file clipboard on Wayland](11%20Native%20drag%20and%20drop/S030-native-dnd-wayland.md) · Opus high
|
||||
- [ ] [S031 — Native drag and drop and file clipboard on X11](11%20Native%20drag%20and%20drop/S031-native-dnd-x11.md) · Opus high
|
||||
- [x] [S031 — Native drag and drop and file clipboard on X11](11%20Native%20drag%20and%20drop/S031-native-dnd-x11.md) · Opus high
|
||||
|
||||
## 12 Testing
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,11 @@ i-slint-core = { version = "=1.19.0", default-features = false }
|
|||
raw-window-handle = "0.6"
|
||||
wayland-backend = { version = "0.3", features = ["client_system"] }
|
||||
wayland-client = "0.31"
|
||||
x11rb = "0.13"
|
||||
|
||||
[dev-dependencies]
|
||||
# examples/xdnd_probe.rs (fake pointer input under Xvfb)
|
||||
x11rb = { version = "0.13", features = ["xtest"] }
|
||||
|
||||
[build-dependencies]
|
||||
slint-build.workspace = true
|
||||
|
|
|
|||
247
crates/tunal/examples/xdnd_probe.rs
Normal file
247
crates/tunal/examples/xdnd_probe.rs
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
//! `DISPLAY=:N cargo run -p tunal --example xdnd_probe -- x1 y1 x2 y2`: an XDND drop target
|
||||
//! window right of `x2 - 40` that accepts `text/uri-list`, then a fake left-button drag
|
||||
//! (XTEST) from (x1, y1) to (x2, y2). Prints the XDND messages it gets and the dropped
|
||||
//! list. For the S031 smoke run under Xvfb (never on a real display).
|
||||
//!
|
||||
//! `xdnd_probe in x y uri`: the other way round, a source dropping `uri` (as
|
||||
//! `text/uri-list`) on the window at (x, y); prints the target's status and finish.
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use x11rb::connection::Connection;
|
||||
use x11rb::protocol::Event;
|
||||
use x11rb::protocol::xproto::{
|
||||
AtomEnum, ClientMessageEvent, ConnectionExt, CreateWindowAux, EventMask, PropMode, WindowClass,
|
||||
};
|
||||
use x11rb::protocol::xtest::ConnectionExt as _;
|
||||
use x11rb::wrapper::ConnectionExt as _;
|
||||
use x11rb::{COPY_DEPTH_FROM_PARENT, CURRENT_TIME};
|
||||
|
||||
fn atom(c: &impl Connection, name: &str) -> u32 {
|
||||
c.intern_atom(false, name.as_bytes())
|
||||
.unwrap()
|
||||
.reply()
|
||||
.unwrap()
|
||||
.atom
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if std::env::args().nth(1).as_deref() == Some("in") {
|
||||
return drag_in();
|
||||
}
|
||||
let n: Vec<i16> = std::env::args()
|
||||
.skip(1)
|
||||
.filter_map(|a| a.parse().ok())
|
||||
.collect();
|
||||
let [x1, y1, x2, y2] = n[..] else {
|
||||
eprintln!("usage: xdnd_probe x1 y1 x2 y2");
|
||||
return;
|
||||
};
|
||||
let (c, screen) = x11rb::connect(None).unwrap();
|
||||
let s = &c.setup().roots[screen];
|
||||
let target = c.generate_id().unwrap();
|
||||
let left = x2 - 40;
|
||||
c.create_window(
|
||||
COPY_DEPTH_FROM_PARENT,
|
||||
target,
|
||||
s.root,
|
||||
left,
|
||||
0,
|
||||
(s.width_in_pixels as i16 - left) as u16,
|
||||
s.height_in_pixels,
|
||||
0,
|
||||
WindowClass::INPUT_OUTPUT,
|
||||
0,
|
||||
&CreateWindowAux::new().background_pixel(s.white_pixel),
|
||||
)
|
||||
.unwrap();
|
||||
let aware = atom(&c, "XdndAware");
|
||||
c.change_property32(PropMode::REPLACE, target, aware, AtomEnum::ATOM, &[5])
|
||||
.unwrap();
|
||||
c.map_window(target).unwrap();
|
||||
c.flush().unwrap();
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
|
||||
let (status, finished, selection, uri_list, prop) = (
|
||||
atom(&c, "XdndStatus"),
|
||||
atom(&c, "XdndFinished"),
|
||||
atom(&c, "XdndSelection"),
|
||||
atom(&c, "text/uri-list"),
|
||||
atom(&c, "PROBE"),
|
||||
);
|
||||
let names = |a: u32| {
|
||||
String::from_utf8_lossy(&c.get_atom_name(a).unwrap().reply().unwrap().name).into_owned()
|
||||
};
|
||||
let motion = |x: i16, y: i16| {
|
||||
c.xtest_fake_input(6, 0, CURRENT_TIME, s.root, x, y, 0)
|
||||
.unwrap();
|
||||
c.flush().unwrap();
|
||||
std::thread::sleep(Duration::from_millis(30));
|
||||
};
|
||||
motion(x1, y1);
|
||||
c.xtest_fake_input(4, 1, CURRENT_TIME, s.root, 0, 0, 0)
|
||||
.unwrap();
|
||||
for step in 1..=30 {
|
||||
motion(x1 + (x2 - x1) * step / 30, y1 + (y2 - y1) * step / 30);
|
||||
}
|
||||
let mut source = 0;
|
||||
let mut released = false;
|
||||
let start = Instant::now();
|
||||
while start.elapsed() < Duration::from_secs(6) {
|
||||
if start.elapsed() > Duration::from_secs(2) && !released {
|
||||
c.xtest_fake_input(5, 1, CURRENT_TIME, s.root, 0, 0, 0)
|
||||
.unwrap();
|
||||
c.flush().unwrap();
|
||||
released = true;
|
||||
}
|
||||
let Some(event) = c.poll_for_event().unwrap() else {
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
continue;
|
||||
};
|
||||
match event {
|
||||
Event::ClientMessage(e) => {
|
||||
let d = e.data.as_data32();
|
||||
let name = names(e.type_);
|
||||
if name != "XdndPosition" {
|
||||
println!("probe: got {name}");
|
||||
}
|
||||
source = d[0];
|
||||
match name.as_str() {
|
||||
"XdndPosition" => {
|
||||
let reply =
|
||||
ClientMessageEvent::new(32, source, status, [target, 1, 0, 0, d[4]]);
|
||||
c.send_event(false, source, EventMask::NO_EVENT, reply)
|
||||
.unwrap();
|
||||
}
|
||||
"XdndDrop" => {
|
||||
c.convert_selection(target, selection, uri_list, prop, d[2])
|
||||
.unwrap();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
c.flush().unwrap();
|
||||
}
|
||||
Event::SelectionNotify(e) => {
|
||||
let v = c
|
||||
.get_property(true, target, e.property, AtomEnum::ANY, 0, 1 << 20)
|
||||
.unwrap()
|
||||
.reply()
|
||||
.unwrap();
|
||||
println!("probe: dropped {:?}", String::from_utf8_lossy(&v.value));
|
||||
let done =
|
||||
ClientMessageEvent::new(32, source, finished, [target, 1, d_copy(&c), 0, 0]);
|
||||
c.send_event(false, source, EventMask::NO_EVENT, done)
|
||||
.unwrap();
|
||||
c.flush().unwrap();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn d_copy(c: &impl Connection) -> u32 {
|
||||
atom(c, "XdndActionCopy")
|
||||
}
|
||||
|
||||
fn drag_in() {
|
||||
let args: Vec<String> = std::env::args().skip(2).collect();
|
||||
let [x, y, uri] = &args[..] else {
|
||||
eprintln!("usage: xdnd_probe in x y uri");
|
||||
return;
|
||||
};
|
||||
let (x, y): (i16, i16) = (x.parse().unwrap(), y.parse().unwrap());
|
||||
let (c, screen) = x11rb::connect(None).unwrap();
|
||||
let s = &c.setup().roots[screen];
|
||||
let source = c.generate_id().unwrap();
|
||||
c.create_window(
|
||||
COPY_DEPTH_FROM_PARENT,
|
||||
source,
|
||||
s.root,
|
||||
-10,
|
||||
-10,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
WindowClass::INPUT_ONLY,
|
||||
0,
|
||||
&CreateWindowAux::new(),
|
||||
)
|
||||
.unwrap();
|
||||
let a = |n: &str| atom(&c, n);
|
||||
let (selection, uri_list) = (a("XdndSelection"), a("text/uri-list"));
|
||||
c.set_selection_owner(source, selection, CURRENT_TIME)
|
||||
.unwrap();
|
||||
c.xtest_fake_input(6, 0, CURRENT_TIME, s.root, x, y, 0)
|
||||
.unwrap();
|
||||
c.flush().unwrap();
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
// The top-level under the pointer, and its proxy.
|
||||
let target = c.query_pointer(s.root).unwrap().reply().unwrap().child;
|
||||
let to = c
|
||||
.get_property(false, target, a("XdndProxy"), AtomEnum::WINDOW, 0, 1)
|
||||
.unwrap()
|
||||
.reply()
|
||||
.unwrap()
|
||||
.value32()
|
||||
.and_then(|mut v| v.next())
|
||||
.unwrap_or(target);
|
||||
let send = |kind: &str, data: [u32; 5]| {
|
||||
let e = ClientMessageEvent::new(32, to, a(kind), data);
|
||||
c.send_event(false, to, EventMask::NO_EVENT, e).unwrap();
|
||||
c.flush().unwrap();
|
||||
};
|
||||
let xy = ((x as u32) << 16) | y as u32;
|
||||
send("XdndEnter", [source, 5 << 24, uri_list, 0, 0]);
|
||||
let copy = a("XdndActionCopy");
|
||||
let start = Instant::now();
|
||||
let mut dropped = false;
|
||||
while start.elapsed() < Duration::from_secs(5) {
|
||||
if !dropped {
|
||||
send("XdndPosition", [source, 0, xy, CURRENT_TIME, copy]);
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
while let Some(event) = c.poll_for_event().unwrap() {
|
||||
match event {
|
||||
Event::ClientMessage(e) => {
|
||||
let d = e.data.as_data32();
|
||||
let name = String::from_utf8_lossy(
|
||||
&c.get_atom_name(e.type_).unwrap().reply().unwrap().name,
|
||||
)
|
||||
.into_owned();
|
||||
println!("probe: got {name} accept={}", d[1] & 1);
|
||||
if name == "XdndStatus" && d[1] & 1 == 1 && !dropped {
|
||||
send("XdndDrop", [source, 0, CURRENT_TIME, 0, 0]);
|
||||
dropped = true;
|
||||
}
|
||||
if name == "XdndFinished" {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Event::SelectionRequest(e) => {
|
||||
c.change_property8(
|
||||
PropMode::REPLACE,
|
||||
e.requestor,
|
||||
e.property,
|
||||
e.target,
|
||||
format!("{uri}\r\n").as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
let n = x11rb::protocol::xproto::SelectionNotifyEvent {
|
||||
response_type: x11rb::protocol::xproto::SELECTION_NOTIFY_EVENT,
|
||||
sequence: 0,
|
||||
time: e.time,
|
||||
requestor: e.requestor,
|
||||
selection: e.selection,
|
||||
target: e.target,
|
||||
property: e.property,
|
||||
};
|
||||
c.send_event(false, e.requestor, EventMask::NO_EVENT, n)
|
||||
.unwrap();
|
||||
c.flush().unwrap();
|
||||
println!("probe: served the list");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
//! Native drag and drop and file clipboard with other apps (D-014). Wayland only for now
|
||||
//! (`wayland.rs`); on X11, or without a window handle (screenshots), `current()` is `None`
|
||||
//! and every caller falls back to in-app behaviour.
|
||||
//! Native drag and drop and file clipboard with other apps (D-014): `wayland.rs` (D-039)
|
||||
//! or `x11.rs` (XDND, CLIPBOARD), both behind `Backend`. Without a window handle
|
||||
//! (screenshots) `current()` is `None` and every caller falls back to in-app behaviour.
|
||||
//!
|
||||
//! API (S031's X11 backend implements the same): `arm` when an in-app drag starts,
|
||||
//! API: `arm` when an in-app drag starts,
|
||||
//! `set_clipboard` on copy/cut, `clipboard_has_files` / `read_clipboard` on paste. Drags
|
||||
//! coming in are fed to Slint's own drop handling (`process_drag_event`), so the DropZones,
|
||||
//! their glow and the drop menu work as for in-app drags.
|
||||
|
||||
mod wayland;
|
||||
mod x11;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
|
@ -24,7 +25,8 @@ use tunal_core::Uri;
|
|||
|
||||
use crate::MainWindow;
|
||||
use crate::dnd::{parse_uri_list, uri_list};
|
||||
use wayland::{Contents, Wayland};
|
||||
use wayland::Wayland;
|
||||
use x11::X11;
|
||||
|
||||
pub mod mime {
|
||||
pub const URI_LIST: &str = "text/uri-list";
|
||||
|
|
@ -60,6 +62,32 @@ pub enum Event {
|
|||
|
||||
pub type Sink = Arc<dyn Fn(Event) + Send + Sync>;
|
||||
|
||||
/// Payload by mime type.
|
||||
pub type Contents = Vec<(&'static str, Vec<u8>)>;
|
||||
|
||||
/// Premultiplied RGBA pixels.
|
||||
pub struct Icon {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub rgba: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Clipboard text and its mime type, read on a backend thread.
|
||||
pub type ReadDone = Box<dyn FnOnce(String, &'static str) + Send>;
|
||||
|
||||
/// One windowing system's side. Positions in `Event`s are logical, relative to the window.
|
||||
trait Backend {
|
||||
/// An in-app drag carrying `contents` started in a window of `size` (logical).
|
||||
fn arm(&self, contents: Contents, size: (f64, f64));
|
||||
fn set_icon(&self, _icon: Icon) {}
|
||||
/// Whether the drop target under a foreign drag takes it.
|
||||
fn accept(&self, yes: bool);
|
||||
fn finish_drop(&self);
|
||||
fn set_selection(&self, contents: Contents);
|
||||
fn selection_has_files(&self) -> bool;
|
||||
fn read_selection(&self, done: ReadDone) -> bool;
|
||||
}
|
||||
|
||||
/// Runs with the files read from the clipboard and whether they were cut.
|
||||
type PasteDone = Box<dyn FnOnce(Vec<Uri>, bool)>;
|
||||
|
||||
|
|
@ -83,7 +111,7 @@ struct Incoming {
|
|||
|
||||
pub struct Native {
|
||||
ui: slint::Weak<MainWindow>,
|
||||
wayland: Wayland,
|
||||
backend: Box<dyn Backend>,
|
||||
incoming: RefCell<Incoming>,
|
||||
paste: RefCell<Option<PasteDone>>,
|
||||
}
|
||||
|
|
@ -115,13 +143,17 @@ pub fn attach_later(ui: &MainWindow) {
|
|||
}
|
||||
});
|
||||
});
|
||||
// X11 (or anything else) leaves this off: S031.
|
||||
let Some(wayland) = Wayland::attach(display, raw_window, sink) else {
|
||||
return;
|
||||
let scale = f64::from(window.window().scale_factor());
|
||||
let backend: Box<dyn Backend> = match Wayland::attach(display, raw_window, sink.clone()) {
|
||||
Some(w) => Box::new(w),
|
||||
None => match X11::attach(raw_window, scale, sink) {
|
||||
Some(x) => Box::new(x),
|
||||
None => return,
|
||||
},
|
||||
};
|
||||
let native = Rc::new(Native {
|
||||
ui: ui.clone(),
|
||||
wayland,
|
||||
backend,
|
||||
incoming: RefCell::new(Incoming::default()),
|
||||
paste: RefCell::new(None),
|
||||
});
|
||||
|
|
@ -180,7 +212,7 @@ impl Native {
|
|||
return;
|
||||
};
|
||||
let size = ui.window().size().to_logical(ui.window().scale_factor());
|
||||
self.wayland.arm(
|
||||
self.backend.arm(
|
||||
contents(uris, None),
|
||||
(f64::from(size.width), f64::from(size.height)),
|
||||
);
|
||||
|
|
@ -189,7 +221,7 @@ impl Native {
|
|||
/// The drag image, shown under the pointer once the drag leaves the window.
|
||||
pub fn set_drag_icon(&self, image: &slint::Image) {
|
||||
if let Some(buffer) = image.to_rgba8_premultiplied() {
|
||||
self.wayland.set_icon(wayland::Icon {
|
||||
self.backend.set_icon(Icon {
|
||||
width: buffer.width(),
|
||||
height: buffer.height(),
|
||||
rgba: buffer.as_bytes().to_vec(),
|
||||
|
|
@ -198,12 +230,12 @@ impl Native {
|
|||
}
|
||||
|
||||
pub fn set_clipboard(&self, uris: &[Uri], cut: bool) {
|
||||
self.wayland.set_selection(contents(uris, Some(cut)));
|
||||
self.backend.set_selection(contents(uris, Some(cut)));
|
||||
}
|
||||
|
||||
/// Another app's files are on the clipboard.
|
||||
pub fn clipboard_has_files(&self) -> bool {
|
||||
self.wayland.selection_has_files()
|
||||
self.backend.selection_has_files()
|
||||
}
|
||||
|
||||
/// Reads another app's files from the clipboard; `done(uris, cut)` runs on the UI thread.
|
||||
|
|
@ -211,13 +243,13 @@ impl Native {
|
|||
pub fn read_clipboard(&self, done: impl FnOnce(Vec<Uri>, bool) + 'static) -> bool {
|
||||
*self.paste.borrow_mut() = Some(Box::new(done));
|
||||
let sink_ui = self.ui.clone();
|
||||
self.wayland.read_selection(move |text, mime| {
|
||||
self.backend.read_selection(Box::new(move |text, mime| {
|
||||
let _ = sink_ui.upgrade_in_event_loop(move |_| {
|
||||
if let Some(native) = current() {
|
||||
native.handle(Event::Clipboard(text, mime));
|
||||
}
|
||||
});
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn handle(&self, event: Event) {
|
||||
|
|
@ -310,7 +342,7 @@ impl Native {
|
|||
};
|
||||
let action = WindowInner::from_pub(ui.window())
|
||||
.process_drag_event(BackendDragEvent::Move { event, allowed });
|
||||
self.wayland
|
||||
self.backend
|
||||
.accept(action.is_some_and(|a| a != DragAction::None));
|
||||
}
|
||||
|
||||
|
|
@ -319,7 +351,7 @@ impl Native {
|
|||
WindowInner::from_pub(ui.window())
|
||||
.process_drag_event(BackendDragEvent::Drop { event, allowed });
|
||||
}
|
||||
self.wayland.finish_drop();
|
||||
self.backend.finish_drop();
|
||||
*self.incoming.borrow_mut() = Incoming::default();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use std::os::fd::{AsFd, OwnedFd};
|
|||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
|
||||
use wayland_client::backend::{Backend, ObjectId};
|
||||
use wayland_client::backend::{Backend as WlBackend, ObjectId};
|
||||
use wayland_client::globals::{GlobalListContents, registry_queue_init};
|
||||
use wayland_client::protocol::wl_buffer::WlBuffer;
|
||||
use wayland_client::protocol::wl_compositor::WlCompositor;
|
||||
|
|
@ -28,7 +28,7 @@ use wayland_client::{
|
|||
Connection, Dispatch, EventQueue, Proxy, QueueHandle, WEnum, event_created_child,
|
||||
};
|
||||
|
||||
use super::{Event, Sink, mime};
|
||||
use super::{Backend, Contents, Event, Icon, ReadDone, Sink, mime};
|
||||
|
||||
/// `BTN_LEFT` from linux/input-event-codes.h.
|
||||
const BTN_LEFT: u32 = 0x110;
|
||||
|
|
@ -59,16 +59,6 @@ struct Shared {
|
|||
sources: HashMap<ObjectId, Contents>,
|
||||
}
|
||||
|
||||
/// Payload by mime type.
|
||||
pub type Contents = Vec<(&'static str, Vec<u8>)>;
|
||||
|
||||
/// Premultiplied RGBA pixels.
|
||||
pub struct Icon {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub rgba: Vec<u8>,
|
||||
}
|
||||
|
||||
struct State {
|
||||
shared: Arc<Mutex<Shared>>,
|
||||
sink: Sink,
|
||||
|
|
@ -101,7 +91,7 @@ impl Wayland {
|
|||
};
|
||||
// SAFETY: Slint's winit backend owns the display and the surface for the window's
|
||||
// lifetime, which outlives the app; we only borrow them.
|
||||
let backend = unsafe { Backend::from_foreign_display(display.display.as_ptr().cast()) };
|
||||
let backend = unsafe { WlBackend::from_foreign_display(display.display.as_ptr().cast()) };
|
||||
let conn = Connection::from_backend(backend);
|
||||
let surface_id =
|
||||
unsafe { ObjectId::from_ptr(WlSurface::interface(), window.surface.as_ptr().cast()) }
|
||||
|
|
@ -139,44 +129,6 @@ impl Wayland {
|
|||
})
|
||||
}
|
||||
|
||||
/// An in-app drag started: if the pointer leaves the window while the button is held,
|
||||
/// the drag becomes a system drag carrying `contents`.
|
||||
pub fn arm(&self, contents: Contents, size: (f64, f64)) {
|
||||
let mut s = self.shared.lock().unwrap();
|
||||
if s.press.is_some() {
|
||||
s.armed = Some((contents, size));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_icon(&self, icon: Icon) {
|
||||
self.shared.lock().unwrap().icon = Some(icon);
|
||||
}
|
||||
|
||||
/// Accept the hovering offer (or not), as the drop target under the pointer decides.
|
||||
pub fn accept(&self, yes: bool) {
|
||||
let s = self.shared.lock().unwrap();
|
||||
if let Some((offer, serial)) = &s.dnd {
|
||||
offer.accept(*serial, yes.then(|| mime::URI_LIST.into()));
|
||||
}
|
||||
drop(s);
|
||||
let _ = self.conn.flush();
|
||||
}
|
||||
|
||||
/// The drop was handled: tell the source and let the offer go.
|
||||
pub fn finish_drop(&self) {
|
||||
let mut s = self.shared.lock().unwrap();
|
||||
s.dropped = false;
|
||||
if let Some((offer, _)) = s.dnd.take() {
|
||||
if s.dnd_action.take().is_some_and(|a| !a.is_empty()) {
|
||||
offer.finish();
|
||||
}
|
||||
s.offers.remove(&offer.id());
|
||||
offer.destroy();
|
||||
}
|
||||
drop(s);
|
||||
let _ = self.conn.flush();
|
||||
}
|
||||
|
||||
/// Reads the hovering offer as `text/uri-list` (on a thread); `Event::DragData` follows.
|
||||
fn read_dnd(shared: &Shared, offer: &WlDataOffer, conn: &Connection, sink: &Sink) {
|
||||
let has = |m: &str| {
|
||||
|
|
@ -195,9 +147,49 @@ impl Wayland {
|
|||
));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Backend for Wayland {
|
||||
/// An in-app drag started: if the pointer leaves the window while the button is held,
|
||||
/// the drag becomes a system drag carrying `contents`.
|
||||
fn arm(&self, contents: Contents, size: (f64, f64)) {
|
||||
let mut s = self.shared.lock().unwrap();
|
||||
if s.press.is_some() {
|
||||
s.armed = Some((contents, size));
|
||||
}
|
||||
}
|
||||
|
||||
fn set_icon(&self, icon: Icon) {
|
||||
self.shared.lock().unwrap().icon = Some(icon);
|
||||
}
|
||||
|
||||
/// Accept the hovering offer (or not), as the drop target under the pointer decides.
|
||||
fn accept(&self, yes: bool) {
|
||||
let s = self.shared.lock().unwrap();
|
||||
if let Some((offer, serial)) = &s.dnd {
|
||||
offer.accept(*serial, yes.then(|| mime::URI_LIST.into()));
|
||||
}
|
||||
drop(s);
|
||||
let _ = self.conn.flush();
|
||||
}
|
||||
|
||||
/// The drop was handled: tell the source and let the offer go.
|
||||
fn finish_drop(&self) {
|
||||
let mut s = self.shared.lock().unwrap();
|
||||
s.dropped = false;
|
||||
if let Some((offer, _)) = s.dnd.take() {
|
||||
if s.dnd_action.take().is_some_and(|a| !a.is_empty()) {
|
||||
offer.finish();
|
||||
}
|
||||
s.offers.remove(&offer.id());
|
||||
offer.destroy();
|
||||
}
|
||||
drop(s);
|
||||
let _ = self.conn.flush();
|
||||
}
|
||||
|
||||
/// The clipboard holds another app's files.
|
||||
pub fn selection_has_files(&self) -> bool {
|
||||
fn selection_has_files(&self) -> bool {
|
||||
let s = self.shared.lock().unwrap();
|
||||
let Some(offer) = &s.selection else {
|
||||
return false;
|
||||
|
|
@ -211,7 +203,7 @@ impl Wayland {
|
|||
}
|
||||
|
||||
/// Puts files on the system clipboard.
|
||||
pub fn set_selection(&self, contents: Contents) {
|
||||
fn set_selection(&self, contents: Contents) {
|
||||
let source = self.manager.create_data_source(&self.qh, ());
|
||||
for (m, _) in &contents {
|
||||
source.offer((*m).into());
|
||||
|
|
@ -226,7 +218,7 @@ impl Wayland {
|
|||
/// Reads files another app put on the clipboard, if it holds any: `done(text, mime)` runs
|
||||
/// on a thread with `x-special/gnome-copied-files` or else `text/uri-list` text.
|
||||
/// Returns false when the clipboard holds no files or is ours.
|
||||
pub fn read_selection(&self, done: impl FnOnce(String, &'static str) + Send + 'static) -> bool {
|
||||
fn read_selection(&self, done: ReadDone) -> bool {
|
||||
let s = self.shared.lock().unwrap();
|
||||
let Some(offer) = &s.selection else {
|
||||
return false;
|
||||
|
|
|
|||
594
crates/tunal/src/native/x11.rs
Normal file
594
crates/tunal/src/native/x11.rs
Normal file
|
|
@ -0,0 +1,594 @@
|
|||
//! X11 drag and drop (XDND v5) and the CLIPBOARD selection over our own `x11rb` connection.
|
||||
//!
|
||||
//! XDND client messages go to the client that created the destination window, which is
|
||||
//! winit's, so a hidden window of ours is set as the app window's `XdndProxy`: sources talk
|
||||
//! to it, and it owns our selections. As a source, winit keeps the pointer grab, so a thread
|
||||
//! polls the pointer while an in-app drag is held and runs the protocol once it leaves the
|
||||
//! window.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::mpsc::{Sender, channel};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use raw_window_handle::RawWindowHandle;
|
||||
use x11rb::connection::Connection;
|
||||
use x11rb::protocol::Event as XEvent;
|
||||
use x11rb::protocol::xproto::{
|
||||
Atom, AtomEnum, ClientMessageEvent, ConnectionExt, CreateWindowAux, EventMask, KeyButMask,
|
||||
PropMode, SelectionNotifyEvent, SelectionRequestEvent, Window, WindowClass,
|
||||
};
|
||||
use x11rb::rust_connection::RustConnection;
|
||||
use x11rb::wrapper::ConnectionExt as _;
|
||||
use x11rb::{CURRENT_TIME, NONE};
|
||||
|
||||
use super::{Backend, Contents, Event, ReadDone, Sink, mime};
|
||||
|
||||
x11rb::atom_manager! {
|
||||
Atoms: AtomsCookie {
|
||||
XdndAware,
|
||||
XdndProxy,
|
||||
XdndEnter,
|
||||
XdndPosition,
|
||||
XdndStatus,
|
||||
XdndLeave,
|
||||
XdndDrop,
|
||||
XdndFinished,
|
||||
XdndSelection,
|
||||
XdndTypeList,
|
||||
XdndActionCopy,
|
||||
CLIPBOARD,
|
||||
TARGETS,
|
||||
TUNAL_DND,
|
||||
TUNAL_CLIP,
|
||||
URI_LIST: b"text/uri-list",
|
||||
GNOME_COPIED: b"x-special/gnome-copied-files",
|
||||
INTERNAL: b"application/x-tunal-uris",
|
||||
}
|
||||
}
|
||||
|
||||
const XDND_VERSION: u32 = 5;
|
||||
|
||||
/// A drag from another app over our window.
|
||||
struct Incoming {
|
||||
source: Window,
|
||||
types: Vec<Atom>,
|
||||
/// The drop target under the pointer takes it (answered on every position).
|
||||
accept: bool,
|
||||
requested: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Shared {
|
||||
/// An in-app drag that may leave the window: payload and window size (physical).
|
||||
armed: Option<(Contents, (f64, f64))>,
|
||||
/// What our selections hand out, by selection atom.
|
||||
owned: HashMap<Atom, Vec<(Atom, Vec<u8>)>>,
|
||||
incoming: Option<Incoming>,
|
||||
/// The target of our drag out accepted it, and finished.
|
||||
status: bool,
|
||||
finished: Option<Sender<()>>,
|
||||
/// Waiting for CLIPBOARD conversions: its TARGETS, or its data.
|
||||
targets: Option<Sender<Vec<Atom>>>,
|
||||
read: Option<(ReadDone, &'static str)>,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
conn: RustConnection,
|
||||
atoms: Atoms,
|
||||
root: Window,
|
||||
/// The app's window (winit's) and our hidden proxy.
|
||||
app: Window,
|
||||
proxy: Window,
|
||||
scale: f64,
|
||||
sink: Sink,
|
||||
shared: Mutex<Shared>,
|
||||
}
|
||||
|
||||
pub struct X11(Arc<Inner>);
|
||||
|
||||
impl X11 {
|
||||
pub fn attach(window: RawWindowHandle, scale: f64, sink: Sink) -> Option<Self> {
|
||||
let app = match window {
|
||||
RawWindowHandle::Xlib(h) => u32::try_from(h.window).ok()?,
|
||||
RawWindowHandle::Xcb(h) => h.window.get(),
|
||||
_ => return None,
|
||||
};
|
||||
let (conn, screen) = x11rb::connect(None).ok()?;
|
||||
let root = conn.setup().roots.get(screen)?.root;
|
||||
let atoms = Atoms::new(&conn).ok()?.reply().ok()?;
|
||||
let proxy = conn.generate_id().ok()?;
|
||||
conn.create_window(
|
||||
0,
|
||||
proxy,
|
||||
root,
|
||||
-10,
|
||||
-10,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
WindowClass::INPUT_ONLY,
|
||||
0,
|
||||
&CreateWindowAux::new().event_mask(EventMask::PROPERTY_CHANGE),
|
||||
)
|
||||
.ok()?;
|
||||
for w in [app, proxy] {
|
||||
conn.change_property32(
|
||||
PropMode::REPLACE,
|
||||
w,
|
||||
atoms.XdndAware,
|
||||
AtomEnum::ATOM,
|
||||
&[XDND_VERSION],
|
||||
)
|
||||
.ok()?;
|
||||
conn.change_property32(
|
||||
PropMode::REPLACE,
|
||||
w,
|
||||
atoms.XdndProxy,
|
||||
AtomEnum::WINDOW,
|
||||
&[proxy],
|
||||
)
|
||||
.ok()?;
|
||||
}
|
||||
conn.flush().ok()?;
|
||||
let inner = Arc::new(Inner {
|
||||
conn,
|
||||
atoms,
|
||||
root,
|
||||
app,
|
||||
proxy,
|
||||
scale,
|
||||
sink,
|
||||
shared: Mutex::new(Shared::default()),
|
||||
});
|
||||
let events = inner.clone();
|
||||
std::thread::Builder::new()
|
||||
.name("tunal-x11".into())
|
||||
.spawn(move || {
|
||||
while let Ok(event) = events.conn.wait_for_event() {
|
||||
events.event(event);
|
||||
}
|
||||
})
|
||||
.ok()?;
|
||||
Some(Self(inner))
|
||||
}
|
||||
}
|
||||
|
||||
impl Backend for X11 {
|
||||
fn arm(&self, contents: Contents, size: (f64, f64)) {
|
||||
let i = &self.0;
|
||||
let size = (size.0 * i.scale, size.1 * i.scale);
|
||||
i.shared.lock().unwrap().armed = Some((contents, size));
|
||||
let inner = i.clone();
|
||||
std::thread::spawn(move || inner.watch_drag());
|
||||
}
|
||||
|
||||
fn accept(&self, yes: bool) {
|
||||
let mut s = self.0.shared.lock().unwrap();
|
||||
if let Some(incoming) = &mut s.incoming {
|
||||
incoming.accept = yes;
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_drop(&self) {
|
||||
let i = &self.0;
|
||||
let incoming = i.shared.lock().unwrap().incoming.take();
|
||||
if let Some(incoming) = incoming {
|
||||
let action = if incoming.accept {
|
||||
i.atoms.XdndActionCopy
|
||||
} else {
|
||||
NONE
|
||||
};
|
||||
i.send(
|
||||
incoming.source,
|
||||
i.atoms.XdndFinished,
|
||||
[i.app, u32::from(incoming.accept), action, 0, 0],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_selection(&self, contents: Contents) {
|
||||
let i = &self.0;
|
||||
i.own(i.atoms.CLIPBOARD, contents);
|
||||
}
|
||||
|
||||
fn selection_has_files(&self) -> bool {
|
||||
let i = &self.0;
|
||||
let a = &i.atoms;
|
||||
let Ok(owner) = i.conn.get_selection_owner(a.CLIPBOARD).map(|c| c.reply()) else {
|
||||
return false;
|
||||
};
|
||||
let owner = owner.map_or(NONE, |r| r.owner);
|
||||
if owner == NONE || owner == i.proxy {
|
||||
return false;
|
||||
}
|
||||
let (tx, rx) = channel();
|
||||
i.shared.lock().unwrap().targets = Some(tx);
|
||||
i.convert(a.CLIPBOARD, a.TARGETS, a.TUNAL_CLIP);
|
||||
rx.recv_timeout(Duration::from_millis(200))
|
||||
.is_ok_and(|t| t.contains(&a.URI_LIST) || t.contains(&a.GNOME_COPIED))
|
||||
}
|
||||
|
||||
fn read_selection(&self, done: ReadDone) -> bool {
|
||||
if !self.selection_has_files() {
|
||||
return false;
|
||||
}
|
||||
let i = &self.0;
|
||||
let a = &i.atoms;
|
||||
// Prefer the list that says copy or cut.
|
||||
let (target, m) = (a.GNOME_COPIED, mime::GNOME_COPIED);
|
||||
i.shared.lock().unwrap().read = Some((done, m));
|
||||
i.convert(a.CLIPBOARD, target, a.TUNAL_CLIP);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Inner {
|
||||
fn send(&self, to: Window, kind: Atom, data: [u32; 5]) {
|
||||
let event = ClientMessageEvent::new(32, to, kind, data);
|
||||
let _ = self.conn.send_event(false, to, EventMask::NO_EVENT, event);
|
||||
let _ = self.conn.flush();
|
||||
}
|
||||
|
||||
fn convert(&self, selection: Atom, target: Atom, property: Atom) {
|
||||
let _ = self
|
||||
.conn
|
||||
.convert_selection(self.proxy, selection, target, property, CURRENT_TIME);
|
||||
let _ = self.conn.flush();
|
||||
}
|
||||
|
||||
fn atom(&self, name: &str) -> Atom {
|
||||
self.conn
|
||||
.intern_atom(false, name.as_bytes())
|
||||
.ok()
|
||||
.and_then(|c| c.reply().ok())
|
||||
.map_or(NONE, |r| r.atom)
|
||||
}
|
||||
|
||||
/// Owns `selection` with `contents`.
|
||||
fn own(&self, selection: Atom, contents: Contents) {
|
||||
let by_atom = contents
|
||||
.into_iter()
|
||||
.map(|(m, bytes)| (self.atom(m), bytes))
|
||||
.collect();
|
||||
self.shared.lock().unwrap().owned.insert(selection, by_atom);
|
||||
let _ = self
|
||||
.conn
|
||||
.set_selection_owner(self.proxy, selection, CURRENT_TIME);
|
||||
let _ = self.conn.flush();
|
||||
}
|
||||
|
||||
/// Root coordinates to the app window's (physical).
|
||||
fn local(&self, x: i16, y: i16) -> Option<(f64, f64)> {
|
||||
let r = self
|
||||
.conn
|
||||
.translate_coordinates(self.root, self.app, x, y)
|
||||
.ok()?
|
||||
.reply()
|
||||
.ok()?;
|
||||
Some((f64::from(r.dst_x), f64::from(r.dst_y)))
|
||||
}
|
||||
|
||||
/// The XDND-aware window under the pointer: (aware window, window to message, version).
|
||||
fn target_at(&self) -> Option<(Window, Window, u32)> {
|
||||
let mut w = self.root;
|
||||
loop {
|
||||
let child = self.conn.query_pointer(w).ok()?.reply().ok()?.child;
|
||||
if child == NONE {
|
||||
return None;
|
||||
}
|
||||
w = child;
|
||||
let aware = self
|
||||
.conn
|
||||
.get_property(false, w, self.atoms.XdndAware, AtomEnum::ATOM, 0, 1)
|
||||
.ok()?
|
||||
.reply()
|
||||
.ok()?;
|
||||
if let Some(version) = aware.value32().and_then(|mut v| v.next()) {
|
||||
let proxy = self
|
||||
.conn
|
||||
.get_property(false, w, self.atoms.XdndProxy, AtomEnum::WINDOW, 0, 1)
|
||||
.ok()
|
||||
.and_then(|c| c.reply().ok())
|
||||
.and_then(|r| r.value32().and_then(|mut v| v.next()))
|
||||
.unwrap_or(w);
|
||||
return Some((w, proxy, version.min(XDND_VERSION)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Follows an armed in-app drag; once the pointer leaves the window with the button
|
||||
/// held, becomes the XDND source until the button is released.
|
||||
fn watch_drag(&self) {
|
||||
let a = &self.atoms;
|
||||
let mut contents = None;
|
||||
// (aware, message window) of the current target.
|
||||
let mut target: Option<(Window, Window)> = None;
|
||||
loop {
|
||||
std::thread::sleep(Duration::from_millis(16));
|
||||
let Some(p) = self
|
||||
.conn
|
||||
.query_pointer(self.root)
|
||||
.ok()
|
||||
.and_then(|c| c.reply().ok())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let held = u16::from(p.mask) & u16::from(KeyButMask::BUTTON1) != 0;
|
||||
if contents.is_none() {
|
||||
let mut s = self.shared.lock().unwrap();
|
||||
let Some((_, (w, h))) = &s.armed else {
|
||||
return;
|
||||
};
|
||||
let (w, h) = (*w, *h);
|
||||
if !held {
|
||||
s.armed = None;
|
||||
return;
|
||||
}
|
||||
drop(s);
|
||||
let Some((x, y)) = self.local(p.root_x, p.root_y) else {
|
||||
return;
|
||||
};
|
||||
if x >= 0.0 && y >= 0.0 && x < w && y < h {
|
||||
continue;
|
||||
}
|
||||
let Some((c, _)) = self.shared.lock().unwrap().armed.take() else {
|
||||
return;
|
||||
};
|
||||
let types: Vec<Atom> = c.iter().map(|(m, _)| self.atom(m)).collect();
|
||||
let _ = self.conn.change_property32(
|
||||
PropMode::REPLACE,
|
||||
self.proxy,
|
||||
a.XdndTypeList,
|
||||
AtomEnum::ATOM,
|
||||
&types,
|
||||
);
|
||||
self.own(a.XdndSelection, c.clone());
|
||||
contents = Some(types);
|
||||
let scale = self.scale;
|
||||
(self.sink)(Event::DragOutStarted {
|
||||
x: x / scale,
|
||||
y: y / scale,
|
||||
});
|
||||
}
|
||||
let types = contents.as_ref().expect("set above");
|
||||
let under = self.target_at().filter(|(w, _, _)| *w != self.proxy);
|
||||
if target.map(|t| t.0) != under.map(|u| u.0) {
|
||||
if let Some((_, to)) = target.take() {
|
||||
self.send(to, a.XdndLeave, [self.proxy, 0, 0, 0, 0]);
|
||||
}
|
||||
if let Some((w, to, version)) = under {
|
||||
let more = u32::from(types.len() > 3);
|
||||
let t = |i: usize| types.get(i).copied().unwrap_or(NONE);
|
||||
self.send(
|
||||
to,
|
||||
a.XdndEnter,
|
||||
[self.proxy, (version << 24) | more, t(0), t(1), t(2)],
|
||||
);
|
||||
eprintln!("tunal: XdndEnter sent to window {w:#x}");
|
||||
self.shared.lock().unwrap().status = false;
|
||||
target = Some((w, to));
|
||||
}
|
||||
}
|
||||
let Some((_, to)) = target else {
|
||||
if !held {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
};
|
||||
if held {
|
||||
let xy = (u32::from(p.root_x as u16) << 16) | u32::from(p.root_y as u16);
|
||||
self.send(
|
||||
to,
|
||||
a.XdndPosition,
|
||||
[self.proxy, 0, xy, CURRENT_TIME, a.XdndActionCopy],
|
||||
);
|
||||
continue;
|
||||
}
|
||||
// Released over a target.
|
||||
let accepted = self.shared.lock().unwrap().status;
|
||||
if !accepted {
|
||||
self.send(to, a.XdndLeave, [self.proxy, 0, 0, 0, 0]);
|
||||
eprintln!("tunal: system drag cancelled");
|
||||
break;
|
||||
}
|
||||
let (tx, rx) = channel();
|
||||
self.shared.lock().unwrap().finished = Some(tx);
|
||||
self.send(to, a.XdndDrop, [self.proxy, 0, CURRENT_TIME, 0, 0]);
|
||||
let done = rx.recv_timeout(Duration::from_secs(5)).is_ok();
|
||||
eprintln!(
|
||||
"tunal: system drag {}",
|
||||
if done {
|
||||
"dropped (copy)"
|
||||
} else {
|
||||
"dropped, no answer"
|
||||
}
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fn event(&self, event: XEvent) {
|
||||
let a = &self.atoms;
|
||||
match event {
|
||||
XEvent::SelectionRequest(e) => self.serve(e),
|
||||
XEvent::SelectionClear(e) => {
|
||||
self.shared.lock().unwrap().owned.remove(&e.selection);
|
||||
}
|
||||
XEvent::SelectionNotify(e) => self.notified(e),
|
||||
XEvent::ClientMessage(e) => {
|
||||
let d = e.data.as_data32();
|
||||
let t = e.type_;
|
||||
if t == a.XdndEnter {
|
||||
let mut types: Vec<Atom> =
|
||||
d[2..].iter().copied().filter(|&x| x != NONE).collect();
|
||||
if d[1] & 1 != 0 {
|
||||
types = self
|
||||
.conn
|
||||
.get_property(false, d[0], a.XdndTypeList, AtomEnum::ATOM, 0, 64)
|
||||
.ok()
|
||||
.and_then(|c| c.reply().ok())
|
||||
.and_then(|r| r.value32().map(Iterator::collect))
|
||||
.unwrap_or_default();
|
||||
}
|
||||
self.shared.lock().unwrap().incoming = Some(Incoming {
|
||||
source: d[0],
|
||||
types,
|
||||
accept: false,
|
||||
requested: false,
|
||||
});
|
||||
} else if t == a.XdndPosition {
|
||||
self.position(d[0], (d[2] >> 16) as i16, (d[2] & 0xffff) as i16, d[3]);
|
||||
} else if t == a.XdndLeave {
|
||||
let had = self.shared.lock().unwrap().incoming.take().is_some();
|
||||
if had {
|
||||
(self.sink)(Event::DragLeave);
|
||||
}
|
||||
} else if t == a.XdndDrop {
|
||||
if self.shared.lock().unwrap().incoming.is_some() {
|
||||
(self.sink)(Event::DragDrop);
|
||||
}
|
||||
} else if t == a.XdndStatus {
|
||||
self.shared.lock().unwrap().status = d[1] & 1 != 0;
|
||||
} else if t == a.XdndFinished
|
||||
&& let Some(tx) = self.shared.lock().unwrap().finished.take()
|
||||
{
|
||||
let _ = tx.send(());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// A foreign drag moved over us: read its list once, report the position, answer.
|
||||
fn position(&self, source: Window, x: i16, y: i16, time: u32) {
|
||||
let a = &self.atoms;
|
||||
let (request, accept) = {
|
||||
let mut s = self.shared.lock().unwrap();
|
||||
let Some(incoming) = s.incoming.as_mut().filter(|i| i.source == source) else {
|
||||
return;
|
||||
};
|
||||
let request = !incoming.requested && incoming.types.contains(&a.URI_LIST);
|
||||
incoming.requested = true;
|
||||
(request, incoming.accept)
|
||||
};
|
||||
if request {
|
||||
let _ = self.conn.convert_selection(
|
||||
self.proxy,
|
||||
a.XdndSelection,
|
||||
a.URI_LIST,
|
||||
a.TUNAL_DND,
|
||||
time,
|
||||
);
|
||||
}
|
||||
if let Some((lx, ly)) = self.local(x, y) {
|
||||
(self.sink)(Event::DragMotion {
|
||||
x: lx / self.scale,
|
||||
y: ly / self.scale,
|
||||
});
|
||||
}
|
||||
let action = if accept { a.XdndActionCopy } else { NONE };
|
||||
// Bit 1: keep sending positions (drop targets differ across the window).
|
||||
self.send(
|
||||
source,
|
||||
a.XdndStatus,
|
||||
[self.app, u32::from(accept) | 2, 0, 0, action],
|
||||
);
|
||||
}
|
||||
|
||||
/// Another app asks for one of our selections.
|
||||
fn serve(&self, e: SelectionRequestEvent) {
|
||||
let a = &self.atoms;
|
||||
let property = if e.property == NONE {
|
||||
e.target
|
||||
} else {
|
||||
e.property
|
||||
};
|
||||
let reply = {
|
||||
let s = self.shared.lock().unwrap();
|
||||
s.owned.get(&e.selection).and_then(|items| {
|
||||
if e.target == a.TARGETS {
|
||||
let mut targets: Vec<Atom> = items.iter().map(|(t, _)| *t).collect();
|
||||
targets.push(a.TARGETS);
|
||||
let _ = self.conn.change_property32(
|
||||
PropMode::REPLACE,
|
||||
e.requestor,
|
||||
property,
|
||||
AtomEnum::ATOM,
|
||||
&targets,
|
||||
);
|
||||
Some(())
|
||||
} else {
|
||||
items
|
||||
.iter()
|
||||
.find(|(t, _)| *t == e.target)
|
||||
.map(|(_, bytes)| {
|
||||
let _ = self.conn.change_property8(
|
||||
PropMode::REPLACE,
|
||||
e.requestor,
|
||||
property,
|
||||
e.target,
|
||||
bytes,
|
||||
);
|
||||
})
|
||||
}
|
||||
})
|
||||
};
|
||||
let notify = SelectionNotifyEvent {
|
||||
response_type: x11rb::protocol::xproto::SELECTION_NOTIFY_EVENT,
|
||||
sequence: 0,
|
||||
time: e.time,
|
||||
requestor: e.requestor,
|
||||
selection: e.selection,
|
||||
target: e.target,
|
||||
property: if reply.is_some() { property } else { NONE },
|
||||
};
|
||||
let _ = self
|
||||
.conn
|
||||
.send_event(false, e.requestor, EventMask::NO_EVENT, notify);
|
||||
let _ = self.conn.flush();
|
||||
}
|
||||
|
||||
/// A conversion we asked for arrived.
|
||||
fn notified(&self, e: SelectionNotifyEvent) {
|
||||
let a = &self.atoms;
|
||||
let value = (e.property != NONE)
|
||||
.then(|| {
|
||||
self.conn
|
||||
.get_property(true, self.proxy, e.property, AtomEnum::ANY, 0, u32::MAX / 4)
|
||||
.ok()?
|
||||
.reply()
|
||||
.ok()
|
||||
})
|
||||
.flatten();
|
||||
if e.selection == a.XdndSelection {
|
||||
if let Some(v) = value {
|
||||
(self.sink)(Event::DragData(
|
||||
String::from_utf8_lossy(&v.value).into_owned(),
|
||||
));
|
||||
}
|
||||
} else if e.selection == a.CLIPBOARD {
|
||||
let mut s = self.shared.lock().unwrap();
|
||||
if e.target == a.TARGETS {
|
||||
if let Some(tx) = s.targets.take() {
|
||||
let targets = value
|
||||
.and_then(|v| v.value32().map(Iterator::collect))
|
||||
.unwrap_or_default();
|
||||
let _ = tx.send(targets);
|
||||
}
|
||||
} else if let Some(v) = value {
|
||||
if let Some((done, m)) = s.read.take() {
|
||||
drop(s);
|
||||
done(String::from_utf8_lossy(&v.value).into_owned(), m);
|
||||
}
|
||||
} else if e.target == a.GNOME_COPIED {
|
||||
// No copied-files list: ask for the plain URI list instead.
|
||||
if let Some((done, _)) = s.read.take() {
|
||||
s.read = Some((done, mime::URI_LIST));
|
||||
drop(s);
|
||||
self.convert(a.CLIPBOARD, a.URI_LIST, a.TUNAL_CLIP);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -244,6 +244,15 @@ One entry per session, newest last, ≤ 15 lines: done, verified how, not done,
|
|||
- Not done: no Wayland run at all (no headless compositor; orchestrator: no window on the live session); attach, drag out/in and clipboard with other apps are deferred to S036.
|
||||
- Later: Move/Link for drag out; modifiers for drags coming in (keyboard focus during a foreign drag).
|
||||
|
||||
## S031 — Native DnD and file clipboard on X11
|
||||
- Done: `native/x11.rs` `X11` (own `x11rb` RustConnection, `tunal-x11` event thread) behind the `Backend` trait now in `native/mod.rs` (Wayland implements it too; `Contents`, `Icon`, `ReadDone` moved there). A hidden InputOnly window of ours is the app window's `XdndProxy` (XDND messages go to the window creator's connection, which is winit's) and owns XdndSelection/CLIPBOARD.
|
||||
- Drag out: winit keeps the pointer grab, so `arm` starts a thread polling `query_pointer`; outside the window with button 1 held it becomes the XDND v5 source (Enter with type list, Position, Leave, Drop, waits for Finished; copy only; no drag icon on X11). Logs `start_drag`, `XdndEnter sent`, the finish.
|
||||
- Drag in: Enter/Position/Leave/Drop on the proxy → the same `Native` events as Wayland (Slint drop targets, glow, menu); the uri-list is converted at the first position; status answers every position with the target's accept.
|
||||
- Clipboard: copy/cut own CLIPBOARD (TARGETS + our mimes); paste asks a foreign owner's TARGETS (200 ms) then `x-special/gnome-copied-files`, falling back to `text/uri-list`.
|
||||
- Tool: `examples/xdnd_probe.rs` (dev-dep x11rb `xtest`), Xvfb only: drag out to a fake target, or `in x y uri` as a fake source.
|
||||
- Verified: build, clippy -D warnings, fmt; Xvfb `:97` with `WAYLAND_DISPLAY` unset: drag of notes.txt out of the window → app logs start_drag + XdndEnter sent + dropped (copy), probe got the uri-list; probe dropping a file on the Music row → status accept after the list was read, XdndFinished accept=1.
|
||||
- Not done: clipboard copy/paste not exercised (no key injection set up); no drag icon on X11; real apps in S036.
|
||||
|
||||
## Later
|
||||
- (orchestrator) Breadcrumb: a long path is hard-clipped at the bar's left edge with no elision mark, and the root crumb elides at 960 px — for S034.
|
||||
-
|
||||
|
|
|
|||
Loading…
Reference in a new issue