1

I'm using the x11cb library, used to interact with the x11 server using Rust.

use std::error::Error;
use x11rb::connection::Connection;
use x11rb::protocol::xproto::*;
use x11rb::COPY_DEPTH_FROM_PARENT;

fn main() -> Result<(), Box<dyn Error>> {
    // Open the connection to the X server. Use the DISPLAY environment variable.
    let (conn, screen_num) = x11rb::connect(None)?;

    // Get the screen #screen_num
    let screen = &conn.setup().roots[screen_num];

    // Ask for our window's Id
    let win = conn.generate_id()?;

    // Create the window
    conn.create_window(
        COPY_DEPTH_FROM_PARENT,    // depth (same as root)
        win,                       // window Id
        screen.root,               // parent window
        200,                       // x
        200,                       // y
        150,                       // width
        150,                       // height
        10,                        // border width
        WindowClass::INPUT_OUTPUT, // class
        screen.root_visual,        // visual
        &Default::default(),
    )?; // masks, not used yet

    // Map the window on the screen
    conn.map_window(win)?;

    // Make sure commands are sent before the sleep, so window is shown
    conn.flush()?;

    std::thread::sleep(std::time::Duration::from_secs(10));

    Ok(())
}

The window resizes if I change the width and height parameters. However, the window doesn't change positions if I modify the x and y parameters.

What could be the reason?

Rust Playground

Note: I'm using Ubuntu 22.10 with X11 and NVIDIA propetary drivers.

Update:

For some reason, this successfully repositions the window (but don't know why modifying x and y in create_window doesn't):

let values = ConfigureWindowAux::default().x(500).y(200);                                   
conn.configure_window(win, &values)?;
alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • This might be the reason: https://stackoverflow.com/a/50080357/54937 – Sedat Kapanoglu Feb 20 '23 at 05:44
  • Does this answer your question? [Xlib: window is created in wrong position](https://stackoverflow.com/questions/12820233/xlib-window-is-created-in-wrong-position) – n. m. could be an AI Feb 20 '23 at 06:10
  • 1
    What is the problem exactly? The coordinates in the the CreateWindow request are just a hint to the window manager. Window managers will ignore them as they see fit. Window managers typically honour the position in the ConfigureWindow request, but there is no guarantee (for example a tiling WM will not break the tiling, program-provided coordinates be damned.) Clients should use ConfigureWindow if specific placement on the screen is desired, but they should be prepared to deal with any placement. If you absolutely need exact placement, you have to create an override-redirect window. – n. m. could be an AI Feb 20 '23 at 08:32
  • 1
    @alexchenco If the `XConfigureWindow` call happens **before** `XMapWindow`, then it is the same as specifying `x` and `y` in `XCreateWindow`: the window manager will change the position most of the time because, since coordinates are required at window creation, we provide them but, the window manager assumes that we don't care so much. On the other hand, if the `XConfigureWindow` call happens **after** `XMapWindow`, then the window manager understands that you **really** want to place the window by yourself and lets you do it (if possible, see the comment about *tiling*). – prog-fh Feb 20 '23 at 08:46

1 Answers1

0

I had to use .override_redirect:

let values = CreateWindowAux::default()
   .background_pixel(x11rb::NONE)
   .border_pixel(x11rb::NONE)
   .override_redirect(1u32)
   .event_mask(EventMask::EXPOSURE | EventMask::STRUCTURE_NOTIFY); // Create the window

conn.create_window(
    COPY_DEPTH_FROM_PARENT,    // depth (same as root)
    win,                       // window Id
    screen.root,               // parent window
    200,                       // x
    200,                       // y
    150,                       // width
    150,                       // height
    10,                        // border width
    WindowClass::INPUT_OUTPUT, // class
    screen.root_visual,        // visual
    &values,
)?; // masks, not used yet
alexchenco
  • 53,565
  • 76
  • 241
  • 413