libstd/xlib

Bindings to libX11.so.6: display, simple windows and their events, with helpers to open and close a window and to receive the close button. Linux only: X11 does not exist on Windows, and including it there stops the build with an error. For a window that works on both systems use libstd/window, which uses this module on Linux.

#include <libstd/xlib>

Links the shared library libX11.so (FFI).

Example

#include <libstd/stdio>
#include <libstd/xlib>

display, window = xlib_open_window("TryC events", 400, 300)
XSelectInput(display, window, KeyPressMask | ButtonPressMask | StructureNotifyMask)
wm_delete = xlib_catch_close(display, window)

ev = new XEvent()
running = 1
while running == 1:
    XNextEvent(display, ev)              // waits for the next event
    if ev.type == KeyPress:
        key = xevent_key(ev)
        if XLookupKeysym(key, 0) == XK_Escape:
            running = 0
    elif ev.type == ButtonPress:
        button = xevent_button(ev)
        print("click ", button.button, " at ", button.x, ", ", button.y, "\n")
    elif ev.type == ConfigureNotify:
        configure = xevent_configure(ev)
        print("size ", configure.width, " x ", configure.height, "\n")
    elif xlib_is_close_request(ev, wm_delete) == 1:
        running = 0                      // the window's close button

xlib_close_window(display, window)

Events

XSelectInput chooses which events a window receives (the *Mask constants), XNextEvent takes the first one from the queue and waits if it is empty, XPending says how many are queued without waiting. Client messages need no mask: that is how the window manager asks to close the window.

In C, XEvent is a 192-byte union that each event type reads through its own struct. A TryC cast between classes is checked at run time, so a union cannot be written that way. Here XEvent has only the fields common to every event (those of XAnyEvent). After looking at ev.type, xevent_key, xevent_button, ... copy the event into a class with the layout of that event's C struct. The offsets and sizes are those of Xlib.h on LP64 (x86_64 and aarch64), checked with offsetof/sizeof.

Constants

NameValueMeaning
KeyPressMask1
KeyReleaseMask2
ButtonPressMask4
ButtonReleaseMask8
EnterWindowMask16
LeaveWindowMask32
PointerMotionMask64
ExposureMask327681 << 15
StructureNotifyMask1310721 << 17
FocusChangeMask20971521 << 21
KeyPress2
KeyRelease3
ButtonPress4
ButtonRelease5
MotionNotify6
EnterNotify7
LeaveNotify8
FocusIn9
FocusOut10
Expose12
DestroyNotify17
UnmapNotify18
MapNotify19
ConfigureNotify22
ClientMessage33
ShiftMask1
LockMask2
ControlMask4
Mod1Mask8di solito Alt
Button11
Button22
Button33
Button44
Button55
XK_space32
XK_Return652930xff0d
XK_Escape653070xff1b
XK_Left653610xff51
XK_Up653620xff52
XK_Right653630xff53
XK_Down653640xff54

Classes

class
XEvent

What XNextEvent fills: the fields common to every event, then the rest of the 192-byte union. Read type, then convert with the xevent_* function of that type.

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    window long
    _rest0 long
    _rest1 long
    _rest2 long
    _rest3 long
    _rest4 long
    _rest5 long
    _rest6 long
    _rest7 long
    _rest8 long
    _rest9 long
    _rest10 long
    _rest11 long
    _rest12 long
    _rest13 long
    _rest14 long
    _rest15 long
    _rest16 long
    _rest17 long
    _rest18 long
class
XKeyEvent

KeyPress, KeyRelease (xevent_key).

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    window long
    root long
    subwindow long
    time long
    x int
    y int
    x_root int
    y_root int
    state int
    keycode int
    same_screen int
    _pad2 int
class
XButtonEvent

ButtonPress, ButtonRelease (xevent_button); buttons 4 and 5 are the wheel.

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    window long
    root long
    subwindow long
    time long
    x int
    y int
    x_root int
    y_root int
    state int
    button int
    same_screen int
    _pad2 int
class
XMotionEvent

MotionNotify (xevent_motion).

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    window long
    root long
    subwindow long
    time long
    x int
    y int
    x_root int
    y_root int
    state int
    is_hint char
    _pad2 char
    _pad3 char
    _pad4 char
    same_screen int
    _pad5 int
class
XCrossingEvent

EnterNotify, LeaveNotify (xevent_crossing).

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    window long
    root long
    subwindow long
    time long
    x int
    y int
    x_root int
    y_root int
    mode int
    detail int
    same_screen int
    focus int
    state int
    _pad2 int
class
XFocusChangeEvent

FocusIn, FocusOut (xevent_focus).

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    window long
    mode int
    detail int
class
XExposeEvent

Expose (xevent_expose): count is how many Expose events still follow; redraw at 0.

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    window long
    x int
    y int
    width int
    height int
    count int
    _pad2 int
class
XDestroyWindowEvent

DestroyNotify (xevent_destroy): event is the window that selected the event, window the destroyed one.

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    event long
    window long
class
XUnmapEvent

UnmapNotify (xevent_unmap).

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    event long
    window long
    from_configure int
    _pad2 int
class
XMapEvent

MapNotify (xevent_map).

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    event long
    window long
    override_redirect int
    _pad2 int
class
XConfigureEvent

ConfigureNotify (xevent_configure): new position and size.

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    event long
    window long
    x int
    y int
    width int
    height int
    border_width int
    _pad2 int
    above long
    override_redirect int
    _pad3 int
class
XClientMessageEvent

ClientMessage (xevent_client_message). The C data union is given in its format 32 form, five longs.

    type int
    _pad0 int
    serial long
    send_event int
    _pad1 int
    display long
    window long
    message_type long
    format int
    _pad2 int
    data_l0 long
    data_l1 long
    data_l2 long
    data_l3 long
    data_l4 long
class
XAtomList

An array of one Atom, for XSetWMProtocols.

    atom0 long

Functions

extern def XOpenDisplay(display_name: str) -> long

Connects to the X server (0 as str for $DISPLAY).

extern def XCloseDisplay(display: long) -> int

Closes the connection.

extern def XDefaultScreen(display: long) -> int

Default screen number.

extern def XRootWindow(display: long, screen_number: int) -> long

Root window of a screen.

extern def XDefaultRootWindow(display: long) -> long

Root window of the default screen.

extern def XBlackPixel(display: long, screen_number: int) -> long

Black pixel value.

extern def XWhitePixel(display: long, screen_number: int) -> long

White pixel value.

extern def XCreateSimpleWindow(display: long, parent: long, x: int, y: int, width: int, height: int, border_width: int, border: long, background: long) -> long

Creates a window (nine arguments: the stack-passed ones work as in C).

extern def XStoreName(display: long, w: long, window_name: str) -> int

Sets the window title.

extern def XMapWindow(display: long, w: long) -> int

Shows a window.

extern def XUnmapWindow(display: long, w: long) -> int

Hides a window.

extern def XDestroyWindow(display: long, w: long) -> int

Destroys a window.

extern def XFlush(display: long) -> int

Flushes the request queue.

extern def XSync(display: long, discard: int) -> int

Flushes and waits for the server.

extern def XSelectInput(display: long, w: long, event_mask: long) -> int

Chooses the events a window receives: an OR of the *Mask constants.

extern def XPending(display: long) -> int

How many events are queued, without waiting.

extern def XNextEvent(display: long, event: XEvent) -> int

Takes the next event from the queue into an XEvent, waiting if there is none.

extern def XInternAtom(display: long, atom_name: str, only_if_exists: int) -> long

The atom for a name, e.g. WM_DELETE_WINDOW.

extern def XSetWMProtocols(display: long, w: long, protocols: XAtomList, count: int) -> int

Tells the window manager which protocols the window takes part in (see xlib_catch_close).

extern def XLookupKeysym(key_event: XKeyEvent, index: int) -> int

The keysym of a key event (compare with the XK_* constants; letters and digits are their ASCII code). Declared as int: a keysym fits in 29 bits.

def xlib_open_window(title: str, width: int, height: int) -> (long, long)

Opens the display, creates a simple window of the given size with a title, maps it and returns (display, window). To receive events call XSelectInput, and xlib_catch_close for the close button.

def xlib_close_window(display: long, window: long) -> None

Destroys the window and closes the display.

def xlib_catch_close(display: long, window: long) -> long

Asks the window manager to send a WM_DELETE_WINDOW client message when the user presses the window's close button. Without it the window manager closes the connection to the X server, and the program exits without running its own code. Returns the atom to pass to xlib_is_close_request.

def xlib_is_close_request(ev: XEvent, wm_delete: long) -> int

1 if the event is the close button pressed (the WM_DELETE_WINDOW client message), 0 otherwise.

def xevent_copy(ev: ref, dst: ref, size: int) -> None

Copies the first size bytes of an event into another class: in C it would be reading another member of the union.

def xevent_key(ev: XEvent) -> XKeyEvent

The event as an XKeyEvent.

def xevent_button(ev: XEvent) -> XButtonEvent

The event as an XButtonEvent.

def xevent_motion(ev: XEvent) -> XMotionEvent

The event as an XMotionEvent.

def xevent_crossing(ev: XEvent) -> XCrossingEvent

The event as an XCrossingEvent.

def xevent_focus(ev: XEvent) -> XFocusChangeEvent

The event as an XFocusChangeEvent.

def xevent_expose(ev: XEvent) -> XExposeEvent

The event as an XExposeEvent.

def xevent_destroy(ev: XEvent) -> XDestroyWindowEvent

The event as an XDestroyWindowEvent.

def xevent_unmap(ev: XEvent) -> XUnmapEvent

The event as an XUnmapEvent.

def xevent_map(ev: XEvent) -> XMapEvent

The event as an XMapEvent.

def xevent_configure(ev: XEvent) -> XConfigureEvent

The event as an XConfigureEvent.

def xevent_client_message(ev: XEvent) -> XClientMessageEvent

The event as an XClientMessageEvent.