Skip to content

std.x11

std.x11 is the first real host-window experiment in cnegative.

This page is more advanced than the earlier stdlib pages. If you are just learning the language, you can skip it for now.

What this module is for

std.x11 exists to prove that cnegative can talk to a real host window system.

Right now it is intentionally tiny:

  • Linux only
  • X11 only
  • real native window open/pump/close
  • no drawing API yet
  • no input layer yet
  • no Wayland, macOS, or Windows support

Import

cneg
import std.x11 as x11;

Current API

  • x11.open_window(str, int, int) -> result int
  • x11.pump(int) -> result bool
  • x11.close(int) -> result bool

The returned int is a raw native window handle owned by the runtime helper. Treat it as an opaque handle even though the current type is just int.

What each call means

x11.open_window(title, width, height)

Creates a real X11 window.

  • ok means the window opened
  • err means setup failed

x11.pump(handle)

Processes pending X11 events.

  • ok true means keep running
  • ok false means the window was closed
  • err means the runtime helper failed

x11.close(handle)

Destroys the native window for that handle.

Minimal example

cneg
import std.time as time;
import std.x11 as x11;

fn:int main() {
    let opened:result int = x11.open_window("cnegative x11", 640, 360);
    if opened.ok == false {
        return 1;
    }

    if opened.ok {
        let handle:int = opened.value;
        let mut frames:int = 0;
        let mut running:bool = true;

        while running && frames < 180 {
            let pumped:result bool = x11.pump(handle);
            if pumped.ok == false {
                let closed_err:result bool = x11.close(handle);
                if closed_err.ok == false {
                    return 2;
                }
                return 3;
            }

            if pumped.ok && pumped.value == false {
                running = false;
            }

            if running {
                time.sleep_ms(16);
                frames = frames + 1;
            }
        }

        let closed:result bool = x11.close(handle);
        if closed.ok == false {
            return 4;
        }
    }

    return 0;
}

Current behavior

  • This opens a real native window on Linux/X11.
  • The example loop sleeps for about three seconds at roughly 60 FPS.
  • The backend links -lX11 automatically when std.x11 appears in typed IR.

experimental and Linux-only

This is not a portable window API yet. It is a deliberately narrow host experiment.

why it exists

std.x11 is here so cnegative can stress-test real OS interaction before a broader FFI or graphics story lands.

What is not here yet

  • drawing/present APIs
  • keyboard or mouse input helpers
  • multiple windows
  • opaque window types
  • Wayland support
  • cross-platform window abstraction

Those are future steps. Right now std.x11 is only enough to open, pump, and close a real window.

cnegative docs track the current v0.5.2 compiler surface.