Linux mode setting, from the comfort of OCaml

https://news.ycombinator.com/rss Hits: 17
Summary

Linux provides the KMS (Kernel Mode Setting) API to let applications query and configure display settings. It's used by Wayland compositors and other programs that need to configure the hardware directly. I found the C API a little verbose and hard to follow so I made libdrm-ocaml, which lets us run commands interactively in a REPL. We'll start by discovering what hardware is available and how it's currently configured, then configure a monitor to display a simple bitmap, and then finally render a 3D animation. The post should be a useful introduction to KMS even if you don't know OCaml. Table of Contents Running it yourself If you want to follow along, you'll need to install libdrm-ocaml and an interactive REPL like utop. With Nix, you can set everything up like this: git clone https://github.com/talex5/libdrm-ocaml cd libdrm-ocaml nix develop dune utop You should see a utop # prompt, where you can enter OCaml expressions. Use ;; to tell the REPL you've finished typing and it's time to evaluate, e.g. 1 2 utop # 1+1;; - : int = 2 Alternatively, you can install things using opam (OCaml's package manager): opam install libdrm utop utop Then, at the utop prompt enter #require "libdrm";; (including the leading #). Querying the current state Before changing anything, we'll start by discovering what hardware is available. I'll introduce the API as we go along, but you can check the API reference docs if you want more information. Finding devices To list available graphics devices: 1 2 3 4 5 6 7 8 9 10 utop # Drm.Device.list ();; - : Drm.Device.Info.t list = [{primary_node = Some "/dev/dri/card0"; render_node = Some "/dev/dri/renderD128"; info = PCI {bus = {domain = 0; bus = 1; dev = 0; func = 0}; dev = {vendor_id = 0x1002; device_id = 0x67ff; subvendor_id = 0x1458; subdevice_id = 0x230b; revision_id = 0xff}}}] libdrm scans the /dev/dri/ directory looking for devices. It uses stat to find the device major and minor numbers and uses the virtual /sys filesystem to get inform...

First seen: 2025-11-16 20:59

Last seen: 2025-11-17 12:46