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
| Name | Value | Meaning |
|---|---|---|
KeyPressMask | 1 | |
KeyReleaseMask | 2 | |
ButtonPressMask | 4 | |
ButtonReleaseMask | 8 | |
EnterWindowMask | 16 | |
LeaveWindowMask | 32 | |
PointerMotionMask | 64 | |
ExposureMask | 32768 | 1 << 15 |
StructureNotifyMask | 131072 | 1 << 17 |
FocusChangeMask | 2097152 | 1 << 21 |
KeyPress | 2 | |
KeyRelease | 3 | |
ButtonPress | 4 | |
ButtonRelease | 5 | |
MotionNotify | 6 | |
EnterNotify | 7 | |
LeaveNotify | 8 | |
FocusIn | 9 | |
FocusOut | 10 | |
Expose | 12 | |
DestroyNotify | 17 | |
UnmapNotify | 18 | |
MapNotify | 19 | |
ConfigureNotify | 22 | |
ClientMessage | 33 | |
ShiftMask | 1 | |
LockMask | 2 | |
ControlMask | 4 | |
Mod1Mask | 8 | di solito Alt |
Button1 | 1 | |
Button2 | 2 | |
Button3 | 3 | |
Button4 | 4 | |
Button5 | 5 | |
XK_space | 32 | |
XK_Return | 65293 | 0xff0d |
XK_Escape | 65307 | 0xff1b |
XK_Left | 65361 | 0xff51 |
XK_Up | 65362 | 0xff52 |
XK_Right | 65363 | 0xff53 |
XK_Down | 65364 | 0xff54 |
Classes
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 longKeyPress, 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 intButtonPress, 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 intMotionNotify (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 intEnterNotify, 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 intFocusIn, FocusOut (xevent_focus).
type int
_pad0 int
serial long
send_event int
_pad1 int
display long
window long
mode int
detail intExpose (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 intDestroyNotify (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 longUnmapNotify (xevent_unmap).
type int
_pad0 int
serial long
send_event int
_pad1 int
display long
event long
window long
from_configure int
_pad2 intMapNotify (xevent_map).
type int
_pad0 int
serial long
send_event int
_pad1 int
display long
event long
window long
override_redirect int
_pad2 intConfigureNotify (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 intClientMessage (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 longAn array of one Atom, for XSetWMProtocols.
atom0 longFunctions
Connects to the X server (0 as str for $DISPLAY).
Closes the connection.
Default screen number.
Root window of a screen.
Root window of the default screen.
Black pixel value.
White pixel value.
Creates a window (nine arguments: the stack-passed ones work as in C).
Sets the window title.
Shows a window.
Hides a window.
Destroys a window.
Flushes the request queue.
Flushes and waits for the server.
Chooses the events a window receives: an OR of the *Mask constants.
How many events are queued, without waiting.
Takes the next event from the queue into an XEvent, waiting if there is none.
The atom for a name, e.g. WM_DELETE_WINDOW.
Tells the window manager which protocols the window takes part in (see xlib_catch_close).
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.
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.
Destroys the window and closes the display.
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.
1 if the event is the close button pressed (the WM_DELETE_WINDOW client message), 0 otherwise.
Copies the first size bytes of an event into another class: in C it would be reading another member of the union.
The event as an XKeyEvent.
The event as an XButtonEvent.
The event as an XMotionEvent.
The event as an XCrossingEvent.
The event as an XFocusChangeEvent.
The event as an XExposeEvent.
The event as an XDestroyWindowEvent.
The event as an XUnmapEvent.
The event as an XMapEvent.
The event as an XConfigureEvent.
The event as an XClientMessageEvent.