[ANN] ocaml-wayland (pure OCaml wayland protocol library)

I’ve just released ocaml-wayland, a pure OCaml Wayland implementation. You can get it with:

opam install wayland

Wayland is a communications system intended for use between processes on a single computer. It is mainly used by graphical applications (clients) to talk to display servers, but nothing about it is specific to graphics and it could be used for other things.

A Wayland protocol is defined by writing a schema file. Typed bindings can then be created automatically from that for various languages. Wayland’s main interesting feature is that you can pass file descriptors as message arguments. For example, to send a frame of video you can get a file descriptor to some shared memory, draw the image to it, and then pass the FD to the server for rendering, with no need to copy the data.

The ocaml-wayland library can be used to write applications, display servers, or proxies. The example in the repository opens a window showing some scrolling squares. Clicking reverses the direction and pressing keys changes the colours.

I used the library to write wayland-virtwl-proxy, which allows applications running in VMs to open windows directly on the host desktop, as described in my recent blog post “Qubes-lite with KVM and Wayland”.

Note that, unlike the older X11 protocol, Wayland doesn’t provide any graphics primitives (such as drawing rectangles or text). Instead, applications render the window contents however they please and then simply share the buffer with the compositor. So if you want to write a normal application using it, you’ll need to find or write a separate GUI toolkit too.

38 Likes

ocaml-wayland has been very stable over the last few months and so I’ve now released version 1.0. The main changes are improved error handling and diagnostics.

I’ve been using this to write an Xwayland adaptor, which acts as an X11 window manager to Xwayland, converting between the two protocols. This allows running X11 apps in VMs and having them appear alongside other application windows on the host. It can also be used to fix other problems, such as support for HiDPI screens and Sway’s buggy clipboard support:

Isolating Xwayland in a VM - Thomas Leonard's blog

19 Likes

That is quite impressive! Can’t wait to experiment with an OCaml wayland client :wink:

Do you think your approach dealing with HiDPI works only because you’re using a proxy? There have been a few patchsets trying to add HiDPI support to Xwayland, but none got approved by the community.

If you have some ideas on the subject, I think your input would be quite valuable there.

1 Like

Can’t wait to experiment with an OCaml wayland client

There’s a minimal example client here, which might be useful as a starting point: ocaml-wayland/example/test.ml at master · talex5/ocaml-wayland · GitHub

Do you think your approach dealing with HiDPI works only because you’re using a proxy? There have been a few patchsets trying to add HiDPI support to Xwayland, but none got approved by the community.

No, it could be done in the compositor or in Xwayland too. All I’m doing is reversing Wayland’s broken attempts at supporting HiDPI, so that X applications can use their existing support. The PRs seem to be about trying to support multiple monitors with different scale factors, whereas I’m happy to have it working just on one monitor!

Wow… just got through the “Isolating Xwayland…” blogpost. I see you like pain… though at least it’s a hilarious read ;D Great job getting it to work

1 Like