412 lines
15 KiB
Rust
412 lines
15 KiB
Rust
//! `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.
|
||
//!
|
||
//! `xdnd_probe click x y [count] [gap_ms] [hover_ms]`: moves the pointer to (x, y), rests
|
||
//! there `hover_ms` (default 0), then clicks `count` times (default 1) `gap_ms` apart
|
||
//! (default 400), printing the seconds since the epoch of each click. `button` (default 1,
|
||
//! 2 is the middle button) as a sixth number.
|
||
//!
|
||
//! `xdnd_probe key ctrl+shift+t f3 alt+2 …`: presses each chord in turn (XTEST), 300 ms
|
||
//! apart; letters, digits, `f1`–`f12` and the modifiers `ctrl`, `shift`, `alt`.
|
||
//!
|
||
//! `xdnd_probe press x1 y1 x2 y2 [hold_ms]`: a plain left-button drag (no drop target) from
|
||
//! (x1, y1) to (x2, y2), held `hold_ms` (default 0) before the release; a selection box.
|
||
|
||
use std::time::{Duration, Instant};
|
||
|
||
use x11rb::connection::Connection;
|
||
use x11rb::protocol::Event;
|
||
use x11rb::protocol::xproto::{
|
||
AtomEnum, ClientMessageEvent, ConnectionExt, CreateWindowAux, EventMask, InputFocus, 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();
|
||
}
|
||
if std::env::args().nth(1).as_deref() == Some("click") {
|
||
return click();
|
||
}
|
||
if std::env::args().nth(1).as_deref() == Some("key") {
|
||
return keys();
|
||
}
|
||
if std::env::args().nth(1).as_deref() == Some("press") {
|
||
return press();
|
||
}
|
||
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 click() {
|
||
let n: Vec<u64> = std::env::args()
|
||
.skip(2)
|
||
.filter_map(|a| a.parse().ok())
|
||
.collect();
|
||
let (x, y, count, gap, hover, button) = match n[..] {
|
||
[x, y] => (x, y, 1, 400, 0, 1),
|
||
[x, y, c] => (x, y, c, 400, 0, 1),
|
||
[x, y, c, g] => (x, y, c, g, 0, 1),
|
||
[x, y, c, g, h] => (x, y, c, g, h, 1),
|
||
[x, y, c, g, h, b] => (x, y, c, g, h, b as u8),
|
||
_ => {
|
||
eprintln!("usage: xdnd_probe click x y [count] [gap_ms] [hover_ms] [button]");
|
||
return;
|
||
}
|
||
};
|
||
let (c, screen) = x11rb::connect(None).unwrap();
|
||
let root = c.setup().roots[screen].root;
|
||
// Arrive from a few pixels away, as a hand would: the window sees a move onto (x, y).
|
||
for dx in [4, 0] {
|
||
c.xtest_fake_input(6, 0, CURRENT_TIME, root, x as i16 - dx, y as i16, 0)
|
||
.unwrap();
|
||
c.flush().unwrap();
|
||
std::thread::sleep(Duration::from_millis(20));
|
||
}
|
||
std::thread::sleep(Duration::from_millis(hover.max(30)));
|
||
for i in 0..count {
|
||
if i > 0 {
|
||
std::thread::sleep(Duration::from_millis(gap));
|
||
}
|
||
c.xtest_fake_input(4, button, CURRENT_TIME, root, 0, 0, 0)
|
||
.unwrap();
|
||
c.get_input_focus().unwrap().reply().unwrap();
|
||
std::thread::sleep(Duration::from_millis(40));
|
||
c.xtest_fake_input(5, button, CURRENT_TIME, root, 0, 0, 0)
|
||
.unwrap();
|
||
// A round trip: the server has taken the release before the next press (or exit).
|
||
c.get_input_focus().unwrap().reply().unwrap();
|
||
let now = std::time::SystemTime::now()
|
||
.duration_since(std::time::UNIX_EPOCH)
|
||
.unwrap_or_default();
|
||
println!("probe: click {} at {:.3}", i + 1, now.as_secs_f64());
|
||
}
|
||
}
|
||
|
||
/// The keysym of one key name of a chord.
|
||
fn keysym(name: &str) -> Option<u32> {
|
||
Some(match name {
|
||
"ctrl" => 0xffe3,
|
||
"shift" => 0xffe1,
|
||
"alt" => 0xffe9,
|
||
_ if name.len() == 1 => name.chars().next()? as u32,
|
||
_ => {
|
||
0xffbe
|
||
+ name
|
||
.strip_prefix('f')?
|
||
.parse::<u32>()
|
||
.ok()?
|
||
.checked_sub(1)?
|
||
}
|
||
})
|
||
}
|
||
|
||
fn keys() {
|
||
let (c, screen) = x11rb::connect(None).unwrap();
|
||
let root = c.setup().roots[screen].root;
|
||
let (min, max) = (c.setup().min_keycode, c.setup().max_keycode);
|
||
let map = c
|
||
.get_keyboard_mapping(min, max - min + 1)
|
||
.unwrap()
|
||
.reply()
|
||
.unwrap();
|
||
let per = map.keysyms_per_keycode as usize;
|
||
// No window manager under Xvfb: give the keyboard to the window under the pointer.
|
||
let under = c.query_pointer(root).unwrap().reply().unwrap().child;
|
||
if under != 0 {
|
||
c.set_input_focus(InputFocus::PARENT, under, CURRENT_TIME)
|
||
.unwrap();
|
||
}
|
||
let code = |sym: u32| {
|
||
map.keysyms
|
||
.chunks(per)
|
||
.position(|syms| syms.contains(&sym))
|
||
.map(|i| min + i as u8)
|
||
};
|
||
for chord in std::env::args().skip(2) {
|
||
let lower = chord.to_ascii_lowercase();
|
||
let Some(codes) = lower
|
||
.split('+')
|
||
.map(|k| keysym(k).and_then(code))
|
||
.collect::<Option<Vec<u8>>>()
|
||
else {
|
||
eprintln!("probe: unknown key in {chord}");
|
||
continue;
|
||
};
|
||
for &k in &codes {
|
||
c.xtest_fake_input(2, k, CURRENT_TIME, root, 0, 0, 0)
|
||
.unwrap();
|
||
}
|
||
c.get_input_focus().unwrap().reply().unwrap();
|
||
std::thread::sleep(Duration::from_millis(40));
|
||
for &k in codes.iter().rev() {
|
||
c.xtest_fake_input(3, k, CURRENT_TIME, root, 0, 0, 0)
|
||
.unwrap();
|
||
}
|
||
c.get_input_focus().unwrap().reply().unwrap();
|
||
println!("probe: key {chord}");
|
||
std::thread::sleep(Duration::from_millis(300));
|
||
}
|
||
}
|
||
|
||
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");
|
||
}
|
||
_ => {}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
fn press() {
|
||
let n: Vec<i16> = std::env::args()
|
||
.skip(2)
|
||
.filter_map(|a| a.parse().ok())
|
||
.collect();
|
||
let (x1, y1, x2, y2, hold) = match n[..] {
|
||
[a, b, c, d] => (a, b, c, d, 0),
|
||
[a, b, c, d, h] => (a, b, c, d, h),
|
||
_ => {
|
||
eprintln!("usage: xdnd_probe press x1 y1 x2 y2 [hold_ms]");
|
||
return;
|
||
}
|
||
};
|
||
let (c, screen) = x11rb::connect(None).unwrap();
|
||
let root = c.setup().roots[screen].root;
|
||
let motion = |x: i16, y: i16| {
|
||
c.xtest_fake_input(6, 0, CURRENT_TIME, root, x, y, 0)
|
||
.unwrap();
|
||
c.get_input_focus().unwrap().reply().unwrap();
|
||
std::thread::sleep(Duration::from_millis(20));
|
||
};
|
||
motion(x1, y1);
|
||
c.xtest_fake_input(4, 1, CURRENT_TIME, root, 0, 0, 0)
|
||
.unwrap();
|
||
for step in 1..=20 {
|
||
motion(x1 + (x2 - x1) * step / 20, y1 + (y2 - y1) * step / 20);
|
||
}
|
||
std::thread::sleep(Duration::from_millis(hold.max(0) as u64));
|
||
c.xtest_fake_input(5, 1, CURRENT_TIME, root, 0, 0, 0)
|
||
.unwrap();
|
||
c.get_input_focus().unwrap().reply().unwrap();
|
||
}
|