FLTK custom widget with Cairo under Wayland
16:24 11 Dec 2025

We are maintaining an open source code base for an desktop application on Linux (and BSD). The GUI of this application is built with FLTK (and is quite complex).

In this GUI, there is a custom widget, actually a rotary knob, which draws with Cairo.
The class is derived from Fl_Dial and overrides the draw() function

Within this custom draw function, in the existing (old) code, a Cairo context is retrieved from XLib

    cairo_surface_t* Xsurface =
        cairo_xlib_surface_create(fl_display, fl_window,
                                  fl_visual->visual,
                                  Fl_Window::current()->w() * scale,
                                  Fl_Window::current()->h() * scale);
    cairo_t *cr = cairo_create (Xsurface);

This can obviously not work when running under Wayland, which is now sometimes the case with FLTK-1.4 -- and so we just get a SEGFAULT in that case.

Now the question is: how best to approach the task to port our custom drawing code, so that it runs both under X11 and Wayland? I am aware that Cairo can draw under Wayland and seemingly FLTK itself depends on Cairo. But I could not find any good starting point yet; I'd guess that somehow EGL will be involved, or that there is already a function somewhere in FLTK to get a cairo drawing surface for a given widget (Note in our case it is a widget, not a window).

Of course, one possible solution would be to draw with Cairo "offline" and then retrieve a bitmap and pass that to FLTK. But that seems rather like a workaround.

Has anyone already done something similar? Maybe some starting point into the Documentation? Or a hint where to take a closer look into the FLTK sources? We are generally aware of the basic concepts and I have seen similar solutions for GTK, but that does not help in our case, and a general internet search did not land any really helpful content beyond what we know already....

c++ fltk