fwin

experimental framebuffer window system for fun
git clone git://git.kocotian.pl/fwin.git
Log | Files | Refs | README | LICENSE

commit 396bffe4ccaee4f34e156cca5a15a7e0b5b8dea0
Author: kocotian <kocotian@kocotian.pl>
Date:   Wed, 13 Jan 2021 13:09:45 +0100

initial, basic framebuffer operations

Diffstat:
AMakefile | 4++++
Afwin.c | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,4 @@ +CC = gcc + +fwin: fwin.c + ${CC} -std=c99 -Wall -Wextra -o $@ $< diff --git a/fwin.c b/fwin.c @@ -0,0 +1,106 @@ +#define _XOPEN_SOURCE 700 + +#include <linux/fb.h> +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <sys/types.h> + +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#define DEFAULT_FRAMEBUFFER "/dev/fb0" + +int fbopen(const char *fbname); +int fbclose(int fbdesc); +int64_t fbres(int fbdesc); +int fbgetinfo(int fbdesc, struct fb_var_screeninfo *info); +int fbsetinfo(int fbdesc, struct fb_var_screeninfo *info); + +int +fbopen(const char *fbname) +{ + return open(fbname, O_RDWR); +} + +int +fbclose(int fbdesc) +{ + return close(fbdesc); +} + +/* From <linux/fb.h>: +struct fb_var_screeninfo { + __u32 xres; -- visible resolution + __u32 yres; + __u32 xres_virtual; -- virtual resolution + __u32 yres_virtual; + __u32 xoffset; -- offset from virtual to visible + __u32 yoffset; -- resolution + + __u32 bits_per_pixel; -- guess what + __u32 grayscale; -- 0 = color, 1 = grayscale, + -- >1 = FOURCC + struct fb_bitfield red; -- bitfield in fb mem if true color, + struct fb_bitfield green; -- else only length is significant + struct fb_bitfield blue; + struct fb_bitfield transp; -- transparency + + __u32 nonstd; -- != 0 Non standard pixel format + + __u32 activate; -- see FB_ACTIVATE_* + + __u32 height; -- height of picture in mm + __u32 width; -- width of picture in mm + + __u32 accel_flags; -- (OBSOLETE) see fb_info.flags + + -- Timing: All values in pixclocks, except pixclock (of course) + __u32 pixclock; -- pixel clock in ps (pico seconds) + __u32 left_margin; -- time from sync to picture + __u32 right_margin; -- time from picture to sync + __u32 upper_margin; -- time from sync to picture + __u32 lower_margin; + __u32 hsync_len; -- length of horizontal sync + __u32 vsync_len; -- length of vertical sync + __u32 sync; -- see FB_SYNC_* + __u32 vmode; -- see FB_VMODE_* + __u32 rotate; -- angle we rotate counter clockwise + __u32 colorspace; -- colorspace for FOURCC-based modes + __u32 reserved[4]; -- Reserved for future compatibility +}; */ + +int +fbgetinfo(int fbdesc, struct fb_var_screeninfo *info) +{ + return ioctl(fbdesc, FBIOGET_VSCREENINFO, info); +} + +int +fbsetinfo(int fbdesc, struct fb_var_screeninfo *info) +{ + return ioctl(fbdesc, FBIOPUT_VSCREENINFO, info); +} + +int64_t /* first 32 bits are x, second 32 bits are y */ +getres(int fbdesc) +{ + struct fb_var_screeninfo info; + + fbgetinfo(fbdesc, &info); + return ((int64_t)info.xres << 32) + (int32_t)info.yres; +} + +int +main(void) +{ + int fbdesc; + int64_t resolution; + + if ((fbdesc = fbopen(DEFAULT_FRAMEBUFFER)) < 0) + exit(fbdesc); + resolution = getres(fbdesc); + printf("Resolution: %ldx%ld\n", resolution >> 32, resolution & (((int64_t)1 << 32) - 1)); + fbclose(fbdesc); +}