Files
fb/fb.c

46 lines
1.2 KiB
C
Raw Normal View History

2026-03-14 11:53:08 +11:00
#include "fb.h"
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
Framebuffer newFramebuffer(const char* device) {
Framebuffer fb;
// Open the framebuffer file descriptor
fb.fd = open(device, O_RDWR);
assert(fb.fd >= 0 && "Invalid framebuffer device, please ensure your user is in the 'video' group.");
// Get information about framebuffer
ioctl(fb.fd, FBIOGET_VSCREENINFO, &fb.vinfo);
// Get size of screen for writing pixels in bounds
fb.screensize = fb.vinfo.xres * fb.vinfo.yres * fb.vinfo.bits_per_pixel / 8;
fb.data = mmap(0, fb.screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fb.fd, 0);
return fb;
}
void writeColourToFramebuffer(Framebuffer* fb, Pixel pixel, Colour colour) {
int offset = (pixel.y * fb->vinfo.xres + pixel.x) * (fb->vinfo.bits_per_pixel / 8);
fb->data[offset + 0] = colour.red;
fb->data[offset + 1] = colour.green;
fb->data[offset + 2] = colour.blue;
fb->data[offset + 3] = colour.alpha;
munmap(fb->data, fb->screensize);
}
void destroyFramebuffer(Framebuffer* fb) {
close(fb->fd);
// NOTE: Do not free fb->data, this is not memory owned by us.
// Will cause a segfault
}