fwin

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

commit 6e7471233023f452e09d8e7aa739aed7c559b6cf
parent 75b864603ff7f5d2d785cd43375735c686c79df4
Author: kocotian <kocotian@kocotian.pl>
Date:   Wed, 13 Jan 2021 18:12:16 +0100

WORKS! time and welcome message. bloat currently ;(

Diffstat:
Mfwin.c | 159+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Atemplefont.h | 130+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 285 insertions(+), 4 deletions(-)

diff --git a/fwin.c b/fwin.c @@ -2,22 +2,42 @@ #include <linux/fb.h> #include <sys/ioctl.h> +#include <sys/mman.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> +#include <time.h> #include <unistd.h> +#include "templefont.h" + #define DEFAULT_FRAMEBUFFER "/dev/fb0" +#define GETA(x) (x >> 24) +#define GETR(x) ((x >> 16) & 0x000000FF) +#define GETG(x) ((x >> 8) & 0x000000FF) +#define GETB(x) (x & 0x000000FF) + +#define GETX(x) (x >> 32) +#define GETY(x) (x & 0x0000FFFF) +#define XY(x,y) (((int64_t)x << 32) + (int64_t)y) + +typedef int64_t ColorARGB; +typedef int64_t PositionXY; + 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); +int64_t getres(int fbdesc); +int64_t getvres(int fbdesc); +void fbfill(int fbdesc, ColorARGB col); +void fbdrawpixel(int fbdesc, PositionXY xy, ColorARGB col); + int fbopen(const char *fbname) { @@ -92,15 +112,146 @@ getres(int fbdesc) return ((int64_t)info.xres << 32) + (int32_t)info.yres; } +int64_t /* first 32 bits are x, second 32 bits are y */ +getvres(int fbdesc) +{ + struct fb_var_screeninfo info; + + fbgetinfo(fbdesc, &info); + return ((int64_t)info.xres_virtual << 32) + (int32_t)info.yres_virtual; +} + +void +fbfill(int fbdesc, ColorARGB col) +{ + u_int8_t *memory; + int64_t vres, len, leni; + vres = getvres(fbdesc); + len = GETX(vres) * GETY(vres) * 4; + memory = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fbdesc, 0); + leni = -1; + while (++leni < len) { + switch (leni % 4) { + case 0: memory[leni] = GETB(col); break; + case 1: memory[leni] = GETG(col); break; + case 2: memory[leni] = GETR(col); break; + case 3: memory[leni] = GETA(col); break; + } + } + munmap(memory, len); +} + +void +fbdrawpixel(int fbdesc, PositionXY xy, ColorARGB col) +{ + int64_t vres; + char *memory; + int64_t coordpoint, len; + vres = getvres(fbdesc); + len = GETX(vres) * GETY(vres) * 4; + memory = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fbdesc, 0); + coordpoint = (((GETX(vres) * GETY(xy)) + (GETX(xy) % GETX(vres))) * 4) - 1; + memory[++coordpoint] = GETB(col); + memory[++coordpoint] = GETG(col); + memory[++coordpoint] = GETR(col); + memory[++coordpoint] = GETA(col); + munmap(memory, len); +} + +void +fbdrawrect(int fbdesc, PositionXY xy, PositionXY wh, char filled, ColorARGB col) +{ + char *memory; + int64_t vres, coordpoint, len; + int32_t witer, hiter; + vres = getvres(fbdesc); + len = GETX(vres) * GETY(vres) * 4; + memory = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fbdesc, 0); + hiter = GETY(wh) + 1; + witer = GETX(wh) + 1; + while (--witer > -1) { + while (--hiter > -1) { + coordpoint = (((GETX(vres) * (GETY(xy) + hiter)) + ((GETX(xy) + witer) % GETX(vres))) * 4) - 1; + memory[++coordpoint] = GETB(col); + memory[++coordpoint] = GETG(col); + memory[++coordpoint] = GETR(col); + memory[++coordpoint] = GETA(col); + } + hiter = GETY(wh); + } + munmap(memory, len); +} + +void +fbdrawchar(int fbdesc, PositionXY xy, char ch, ColorARGB fg, ColorARGB bg) +{ + char *memory; + int64_t vres, coordpoint, len; + int32_t x, y, lx, ly, iter; + vres = getvres(fbdesc); + len = GETX(vres) * GETY(vres) * 4; + memory = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fbdesc, 0); + x = GETX(xy) - 1; + y = GETY(xy) - 1; + lx = GETX(xy) + 8; + ly = GETY(xy) + 8; + iter = 0; + while (++y < ly) { + while (++x < lx) { + coordpoint = (((GETX(vres) * (y)) + ((x) % GETX(vres))) * 4) - 1; + if ((sys_font_std[ch] >> iter) & 1) { + memory[++coordpoint] = GETB(fg); + memory[++coordpoint] = GETG(fg); + memory[++coordpoint] = GETR(fg); + memory[++coordpoint] = GETA(fg); + } else { + memory[++coordpoint] = GETB(bg); + memory[++coordpoint] = GETG(bg); + memory[++coordpoint] = GETR(bg); + memory[++coordpoint] = GETA(bg); + } + ++iter; + } + x = GETX(xy) - 1; + } + munmap(memory, len); +} + +void +fbprintf(int fbdesc, PositionXY xy, char *ch, ColorARGB fg, ColorARGB bg) +{ + int32_t x, y; + x = GETX(xy); + y = GETY(xy); + while (*ch) { + fbdrawchar(fbdesc, XY(x, y), *(ch++), fg, bg); + x += 8; + } +} + int main(void) { int fbdesc; - int64_t resolution; + int64_t res, vres; + char t[9]; + + time_t rawtime; + struct tm *timeinfo; if ((fbdesc = fbopen(DEFAULT_FRAMEBUFFER)) < 0) exit(fbdesc); - resolution = getres(fbdesc); - printf("Resolution: %ldx%ld\n", resolution >> 32, resolution & (((int64_t)1 << 32) - 1)); + res = getres(fbdesc); + vres = getvres(fbdesc); + fbfill(fbdesc, 0x070707); + fbdrawrect(fbdesc, 0, XY(GETX(res), 24), 1, 0x222222); + fbprintf(fbdesc, XY(80, 8), "| Welcome to the Framebuffer Window System alpha!", 0xeeeeee, 0x222222); + while (1) { + time(&rawtime); + timeinfo = localtime(&rawtime); + snprintf(t, 9, "%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); + fbprintf(fbdesc, XY(8, 8), t, 0xeeeeee, 0x222222); + sleep(1); + } fbclose(fbdesc); } diff --git a/templefont.h b/templefont.h @@ -0,0 +1,130 @@ +u_int64_t sys_font_std[256] = { + 0x0000000000000000,0x0000000000000000, + 0x000000FF00000000,0x000000FF00FF0000,//horizontal lines + 0x1818181818181818,0x6C6C6C6C6C6C6C6C,//vertical lines + 0x181818F800000000,0x6C6C6CEC0CFC0000,//bottom-right + 0x1818181F00000000,0x6C6C6C6F607F0000,//bottom-left + 0x000000F818181818,0x000000FC0CEC6C6C,//top-right + 0x0000001F18181818,0x0000007F606F6C6C,//top-left + 0x0000000000000000,0x0000000000000000, + 0x0000000000000000,0x0000000000000000, + 0x0000000000000000,0x0000000000000000, + 0x0000000000000000,0x0000000000000000, + 0x0000000000000000,0x0000000000000000, + 0x0000000000000000,0x0000000000000000, + 0x0000000000000000,0x0000000000000000, + 0x0000000000000000,0x0000000000000000, + 0x0000000000000000,0x0008000000000000,// + 0x0000000000000000,0x00180018183C3C18,// ! + 0x0000000000363636,0x006C6CFE6CFE6C6C,//"# + 0x00187ED07C16FC30,0x0060660C18306606,//$% + 0x00DC66B61C36361C,0x0000000000181818,//&' + 0x0030180C0C0C1830,0x000C18303030180C,//() + 0x0000187E3C7E1800,0x000018187E181800,//*+ + 0x0C18180000000000,0x000000007E000000,//,- + 0x0018180000000000,0x0000060C18306000,//./ + 0x003C666E7E76663C,0x007E181818181C18,//01 + 0x007E0C183060663C,0x003C66603860663C,//23 + 0x0030307E363C3830,0x003C6660603E067E,//45 + 0x003C66663E060C38,0x000C0C0C1830607E,//67 + 0x003C66663C66663C,0x001C30607C66663C,//89 + 0x0018180018180000,0x0C18180018180000,//:; + 0x0030180C060C1830,0x0000007E007E0000,//<= + 0x000C18306030180C,0x001800181830663C,//>? + 0x003C06765676663C,0x006666667E66663C,//@A + 0x003E66663E66663E,0x003C66060606663C,//BC + 0x001E36666666361E,0x007E06063E06067E,//DE + 0x000606063E06067E,0x003C66667606663C,//FG + 0x006666667E666666,0x007E18181818187E,//HI + 0x001C36303030307C,0x0066361E0E1E3666,//JK + 0x007E060606060606,0x00C6C6D6D6FEEEC6,//LM + 0x006666767E6E6666,0x003C66666666663C,//NO + 0x000606063E66663E,0x006C36566666663C,//PQ + 0x006666363E66663E,0x003C66603C06663C,//RS + 0x001818181818187E,0x003C666666666666,//TU + 0x00183C6666666666,0x00C6EEFED6D6C6C6,//VW + 0x0066663C183C6666,0x001818183C666666,//XY + 0x007E060C1830607E,0x003E06060606063E,//Z[ + 0x00006030180C0600,0x007C60606060607C,//\] + 0x000000000000663C,0xFFFF000000000000,//^_ + 0x000000000030180C,0x007C667C603C0000,//`a + 0x003E6666663E0606,0x003C6606663C0000,//bc + 0x007C6666667C6060,0x003C067E663C0000,//de + 0x000C0C0C3E0C0C38,0x3C607C66667C0000,//fg + 0x00666666663E0606,0x003C1818181C0018,//hi + 0x0E181818181C0018,0x0066361E36660606,//jk + 0x003C18181818181C,0x00C6D6D6FE6C0000,//lm + 0x00666666663E0000,0x003C6666663C0000,//no + 0x06063E66663E0000,0xE0607C66667C0000,//pq + 0x000606066E360000,0x003E603C067C0000,//rs + 0x00380C0C0C3E0C0C,0x007C666666660000,//tu + 0x00183C6666660000,0x006CFED6D6C60000,//vw + 0x00663C183C660000,0x3C607C6666660000,//xy + 0x007E0C18307E0000,0x003018180E181830,//z{ + 0x0018181818181818,0x000C18187018180C,//|} + 0x000000000062D68C,0xFFFFFFFFFFFFFFFF,//~ + 0x1E30181E3303331E,0x007E333333003300, + 0x001E033F331E0038,0x00FC667C603CC37E, + 0x007E333E301E0033,0x007E333E301E0007, + 0x007E333E301E0C0C,0x3C603E03033E0000, + 0x003C067E663CC37E,0x001E033F331E0033, + 0x001E033F331E0007,0x001E0C0C0C0E0033, + 0x003C1818181C633E,0x001E0C0C0C0E0007, + 0x00333F33331E0C33,0x00333F331E000C0C, + 0x003F061E063F0038,0x00FE33FE30FE0000, + 0x007333337F33367C,0x001E33331E00331E, + 0x001E33331E003300,0x001E33331E000700, + 0x007E33333300331E,0x007E333333000700, + 0x1F303F3333003300,0x001C3E63633E1C63, + 0x001E333333330033,0x18187E03037E1818, + 0x003F67060F26361C,0x000C3F0C3F1E3333, + 0x70337B332F1B1B0F,0x0E1B18187E18D870, + 0x007E333E301E0038,0x001E0C0C0C0E001C, + 0x001E33331E003800,0x007E333333003800, + 0x003333331F001F00,0x00333B3F3733003F, + 0x00007E007C36363C,0x00007E003C66663C, + 0x001E3303060C000C,0x000003033F000000, + 0x000030303F000000,0xF81973C67C1B3363, + 0xC0F9F3E6CF1B3363,0x183C3C1818001800, + 0x0000CC663366CC00,0x00003366CC663300, + 0x1144114411441144,0x55AA55AA55AA55AA, + 0xEEBBEEBBEEBBEEBB,0x1818181818181818, + 0x1818181F18181818,0x1818181F181F1818, + 0x6C6C6C6F6C6C6C6C,0x6C6C6C7F00000000, + 0x1818181F181F0000,0x6C6C6C6F606F6C6C, + 0x6C6C6C6C6C6C6C6C,0x6C6C6C6F607F0000, + 0x0000007F606F6C6C,0x0000007F6C6C6C6C, + 0x0000001F181F1818,0x1818181F00000000, + 0x000000F818181818,0x000000FF18181818, + 0x181818FF00000000,0x181818F818181818, + 0x000000FF00000000,0x181818FF18181818, + 0x181818F818F81818,0x6C6C6CEC6C6C6C6C, + 0x000000FC0CEC6C6C,0x6C6C6CEC0CFC0000, + 0x000000FF00EF6C6C,0x6C6C6CEF00FF0000, + 0x6C6C6CEC0CEC6C6C,0x000000FF00FF0000, + 0x6C6C6CEF00EF6C6C,0x000000FF00FF1818, + 0x000000FF6C6C6C6C,0x181818FF00FF0000, + 0x6C6C6CFF00000000,0x000000FC6C6C6C6C, + 0x000000F818F81818,0x181818F818F80000, + 0x6C6C6CFC00000000,0x6C6C6CEF6C6C6C6C, + 0x181818FF00FF1818,0x0000001F18181818, + 0x181818F800000000,0xFFFFFFFFFFFFFFFF, + 0xFFFFFFFF00000000,0x0F0F0F0F0F0F0F0F, + 0xF0F0F0F0F0F0F0F0,0x00000000FFFFFFFF, + 0x006E3B133B6E0000,0x03031F331F331E00, + 0x0003030303637F00,0x0036363636367F00, + 0x007F660C180C667F,0x001E3333337E0000, + 0x03063E6666666600,0x00181818183B6E00, + 0x3F0C1E33331E0C3F,0x001C36637F63361C, + 0x007736366363361C,0x001E33333E180C38, + 0x00007EDBDB7E0000,0x03067EDBDB7E3060, + 0x003C06033F03063C,0x003333333333331E, + 0x00003F003F003F00,0x003F000C0C3F0C0C, + 0x003F00060C180C06,0x003F00180C060C18, + 0x1818181818D8D870,0x0E1B1B1818181818, + 0x000C0C003F000C0C,0x0000394E00394E00, + 0x000000001C36361C,0x0000001818000000, + 0x0000001800000000,0x383C3637303030F0, + 0x000000363636361E,0x0000003E061C301E, + 0x00003C3C3C3C0000,0xFFFFFFFFFFFFFFFF, +};