rust-windowing / rust-windowing/winit

Enabling override-redirect causes winit to receive no keyboard events

Open
#1,997 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

DS - x11
Dominant language
Rust
Stars
6.2k
Forks
1.3k
Avg merge
2d 19h
Merged PRs (30d)
9

Description

I've been developing an application launcher for my personal use and in order to get it to pop out and have no decorations (eg. see screenshots of Rofi) I've enabled override-redirect using the handy function using .with_override_redirect(true) in WindowBuilder.

Now, from my testing, with override-redirect enabled, I've had my window pick up mouse events just fine, but it didn't receive any keyboard events (I'm using Piston with the glutin backend, which seems to utilize winit, but I've also tested it with conrod's examples, which also use winit). The solution I found by looking at other application launchers was to use XGrabKeyboard, which causes my winit window to basically capture the keyboard until the program exits.

For reference, here's the code I use to get those keyboard events:

// I believe this is actually using glutin, but window_ref can be treated as a
// winit::window::Window and it should still work…
unsafe {
    let xconn = window_ref.xlib_xconnection().unwrap();
    while ((*xconn).xlib.XGrabKeyboard)(
        window_ref.xlib_display().unwrap() as *mut x11::ffi::_XDisplay,
        window_ref.xlib_window().unwrap(),
        x11::ffi::True,
        x11::ffi::GrabModeAsync,
        x11::ffi::GrabModeAsync,
        x11::ffi::CurrentTime,
    ) != 0
    {
        // When you start the program from the window manager directly,
        // XGrabKeyboard fails. So you have to sleep, and do it again.
        std::thread::sleep(Duration::from_millis(10));
    }
}

This raises a couple of points:

  1. For my use case, this is fine, but I'd wonder if there's a way to do it without "eating" the keyboard and preventing other applications from not being able to receive keyboard input (FWIW, the mouse isn't grabbed by my window). I don't know too much about X11's APIs, so I'm not too sure how I'd go about doing that.

  2. Using XGrabKeyboard feels a bit hacky. I've seen somewhere in the issues in the repo someone mentioning that using the X11 API was a bit of an undocumented thing (to be honest, I'm still not too certain as to how the xlib_xconnection can be called on the Window) to do, so it would be nicer to have something within winit that would solve the keyboard events not being picked up. There's issue #647, but that doesn't address what I mentioned in point 1 (if that's even possible), and it's not been implemented yet (although I wouldn't mind trying to implement it myself).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with WindowBuilder's .with_override_redirect(true) path and review issue #647 for related keyboard-input behavior. Compare the reported XGrabKeyboard workaround with winit's existing X11 entry points, including xlib_xconnection, and clarify whether the desired behavior can avoid preventing other applications from receiving keyboard input. Done means an agreed and documented or implemented approach for keyboard events with override-redirect windows.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
desktop, operating-systems
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.