Skip to content

X11 drag-out ends as Cancelled the moment it starts: the pointer grab is on a window that is never mapped #3285

Backlog

Description

@Orkunnnn

What happens

On X11, an El::drag_out element starts its drag and on_drag_end reports DragOutcome::Cancelled in the same millisecond, while the button is still held. No drag session runs: nothing follows the pointer, no target hears XdndEnter, and the later release does nothing.

Reproduction

On dev at 308baad, default features, Linux. An example saved as crates/rune/examples/x11_drag_out_cancel.rs:

//! On X11: press on the white square, move 50 px with the button held, hold it 1.5 s, release.
//! Prints when the drag starts and ends, in milliseconds since the Unix epoch.
use std::time::{SystemTime, UNIX_EPOCH};

use rune::{AppSpec, Color, Cx, DragItem, El, el};

fn ms() -> u128 {
    SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis()
}

fn view(_: &(), _: &mut Cx<()>) -> El {
    el().w_full().h_full().child(
        el().absolute(20.0, 20.0)
            .size(120.0, 120.0)
            .fill(Color::WHITE)
            .drag_out(|_: &()| Some(DragItem::new().text("hello")))
            .on_drag_start(|_: &mut (), _| eprintln!("{} drag started", ms()))
            .on_drag_end(|_: &mut (), outcome, _| eprintln!("{} drag ended: {outcome:?}", ms())),
    )
}

fn main() {
    rune::run(AppSpec::new("x11 drag out", (320.0, 240.0), || (), view));
}

A driver that presses, moves and releases through XTEST (drive.py):

# XTEST: press at (80, 80), move to (130, 80) in 4 px steps, hold 1.5 s, release.
import ctypes, ctypes.util, os, time
x = ctypes.cdll.LoadLibrary(ctypes.util.find_library("X11"))
xt = ctypes.cdll.LoadLibrary(ctypes.util.find_library("Xtst"))
x.XOpenDisplay.restype = ctypes.c_void_p
x.XOpenDisplay.argtypes = [ctypes.c_char_p]
x.XFlush.argtypes = [ctypes.c_void_p]
xt.XTestFakeButtonEvent.argtypes = [ctypes.c_void_p, ctypes.c_uint, ctypes.c_int, ctypes.c_ulong]
xt.XTestFakeMotionEvent.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_ulong]
d = x.XOpenDisplay(os.environ["DISPLAY"].encode())
ms = lambda: int(time.time() * 1000)
def move(px, py):
    xt.XTestFakeMotionEvent(d, -1, px, py, 0); x.XFlush(d)
move(80, 80); time.sleep(0.3)
xt.XTestFakeButtonEvent(d, 1, 1, 0); x.XFlush(d); print(ms(), "button pressed", flush=True)
for px in range(84, 134, 4):
    time.sleep(0.02); move(px, 80)
print(ms(), "moved 50 px, holding", flush=True)
time.sleep(1.5)
xt.XTestFakeButtonEvent(d, 1, 0, 0); x.XFlush(d); print(ms(), "button released", flush=True)
time.sleep(0.5)

Both run on an X display of their own (Xwayland of a headless gamescope), with no Wayland display so winit picks X11:

cargo build -p rune --example x11_drag_out_cancel
env -u DISPLAY -u WAYLAND_DISPLAY gamescope --backend headless --force-windows-fullscreen \
  -W 320 -H 240 -w 320 -h 240 -- sh -c \
  'ENABLE_GAMESCOPE_WSI=0 target/debug/examples/x11_drag_out_cancel & sleep 4; python3 drive.py; kill $!'

Output:

1796211693270 button pressed
1796211693335 drag started
1796211693335 drag ended: Cancelled
1796211693531 moved 50 px, holding
1796211695031 button released

The drag ends 0 ms after it starts and 1.7 s before the release.

Why

setup_session (crates/rune/src/platform/x11/drag_source/x11_ops.rs) creates the XDND source window (override-redirect, 1×1 at -100,-100; its comment calls it a "dedicated unmapped source window") and never maps it. It then grabs the pointer on that window. The X protocol refuses a pointer grab whose grab window is not viewable, so the reply is NotViewable. The status check that follows destroys the window and returns None, and begin_drag (wire.rs) reports DragOutcome::Cancelled at once.

Two checks on the same display support this:

  • With Xlib, XGrabPointer on an override-redirect 1×1 window at -100,-100 returns NotViewable while the window is unmapped and Success once it is mapped.
  • With conn.map_window(source_window).ok()?; added locally after the create_window call, the same run gives a drag that lasts until the release:
1796211723414 button pressed
1796211723518 drag started
1796211723675 moved 50 px, holding
1796211725175 button released
1796211725176 drag ended: Cancelled

Cancelled there is the release over a window that takes no drops. I did not exercise the full drag (enter, position, drop on a real target) past this point.

The unit tests in drag_source/tests.rs drive the state machine without an X server, so they do not reach the grab.

Expected

The drag runs until the release (or Escape), and on_drag_end reports what the drop did.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions