Writing to vt framebuffer

From: obiwac <obiwac_at_gmail.com>
Date: Sat, 21 May 2022 22:20:00 UTC
Good evening all,

For a certain application I need to be able to draw directly to the
VBE/GOP framebuffer without the help of xorg-server (too cumbersome),
and I had no problems getting this to work with the old syscons driver
a while back:

    ioctl(0, KDENABIO, 0);
    ioctl(0, VT_WAITACTIVE, 0);

    ioctl(0, KDSETMODE, KD_GRAPHICS);
    ioctl(0, _IO('V', 280 - M_VESA_BASE), 0);

    video_adapter_info_t adp;
    ioctl(0, CONS_ADPINFO, &adp);
    ioctl(0, CONS_SETWINORG, 0);

    uint8_t* fb = mmap(NULL, adp.va_window_size, PROT_READ |
PROT_WRITE, MAP_FILE | MAP_SHARED, STDOUT_FILENO, 0);

(Error handling omitted for the sake of brevity.)

No problems up until here, but I'd like to move on to newcons (for
UEFI, and because I've been having some slightly weird behaviour with
it and the i915kms & radeonkms drivers). Problem is I can only find
very little information about vt! By faffing around in the vt source
(src/sys/dev/vt) & Xorg source (especially
hw/xfree86/os-support/bsd/bsd_init.c), I've come up with:

    int fd = open("/dev/ttyv0", O_RDWR | O_NDELAY);
    int vtno = 2; // rolled a dice and it landed on 2

    ioctl(fd, VT_ACTIVATE, vtno);
    ioctl(fd, VT_WAITACTIVE, vtno);

This does... something, at least. If I'm running Xorg, it switches to
a virtual terminal and displays random blocks of colours on screen (at
least I think that's what it's doing; ngl I don't understand much of
all this, mostly distilling what Xorg does... one thing I did gather
from this little promenade through bsd_init.c is that vt is the same
thing as pcvt on OpenBSD, or at the very least is meant to be
compatible with it?)

But further than this, I can't, it would seem, set modes, map
framebuffer memory, or do anything of the sorts. And I'm a bit at wits
end here; I seriously can't understand how Xorg does things after
this. I don't even get how it can possibly work, actually, as the
KD_SETMODE ioctl in vt_core.c doesn't seem like it's even doing
anything at all.

I don't really need to set modes either. Just having the correct mode
set by the loader and being able to simply draw to a framebuffer would
be all that I need. Hopefully someone can enlighten me on vt's
mystique which transcends what the likes of mere mortals such as
myself can comprehend.

I think once (read: if) I wrap my head around all this I'll finally
get vidcontrol working with vt (if that's possible at all) because
that's been bugging me for a while too!