fwin

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

commit a66eaaa9b45f8661ffb847f60b4da8d9e71c96ad
parent 93a599d4d48c384540812daf0f41e0aea7a13e80
Author: kocotian <kocotian@kocotian.pl>
Date:   Thu, 14 Jan 2021 22:10:47 +0100

basic user input

Diffstat:
MCHANGELOG | 5+++++
Mfwin.c | 48+++++++++++++++++++++++++++++++++++++-----------
Agetch.h | 39+++++++++++++++++++++++++++++++++++++++
3 files changed, 81 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,4 +1,9 @@ ================ +- v0.2 (unrel.): + +* some basic user input + +================ - v0.1: * HUGE progress diff --git a/fwin.c b/fwin.c @@ -5,6 +5,7 @@ #include <time.h> #include "fwin.h" +#include "getch.h" int main(void) @@ -13,7 +14,7 @@ main(void) int64_t res, vres, len; char *mappoint; - FbDrawable framebuf, terminal, tux; + FbDrawable screen, framebuf, terminal, tux; if ((fbdesc = fbopen(DEFAULT_FRAMEBUFFER)) < 0) exit(fbdesc); @@ -25,8 +26,14 @@ main(void) if ((mappoint = fbmap(fbdesc, len)) == MAP_FAILED) exit((int64_t)mappoint); - framebuf.fbdesc = fbdesc; - framebuf.mappoint = mappoint; + screen.fbdesc = fbdesc; + screen.mappoint = mappoint; + screen.len = len; + screen.res[0] = getres(fbdesc); + screen.res[1] = getvres(fbdesc); + + framebuf.fbdesc = -1; + framebuf.mappoint = malloc(len); framebuf.len = len; framebuf.res[0] = getres(fbdesc); framebuf.res[1] = getvres(fbdesc); @@ -44,18 +51,37 @@ main(void) close(tux.fbdesc); tux.res[0] = tux.res[1] = XY(128, 128); - fbfill(&framebuf, 0x000000); - fbdrawrect(&framebuf, 0, XY(GETX(res), 24), 1, 0x222222); - fbdrawrect(&framebuf, XY(0, 24), XY(GETX(res), (GETY(res) - 25)), 1, 0x1d2021); - fbputs(&framebuf, XY(8, 8), "Welcome to the Framebuffer Window System alpha!", 0xeeeeee, 0x222222); + int sel = 0; + PositionXY xy[] = { + XY(59, 91), + XY(285, 181), + }; + + while (1) { + fbfill(&framebuf, 0x1d2021); + fbdrawrect(&framebuf, 0, XY(GETX(res), 24), 1, 0xeeeeee); + fbputs(&framebuf, XY(8, 8), "Welcome to the Framebuffer Window System alpha!", 0x222222, 0xeeeeee); - fbdrawrect(&terminal, XY(0, 0), XY(514, 130), 0, 0xd79921); - fbputs(&terminal, XY(9, 9), "simple window", 0xebdbb2, 0x000000); + fbdrawrect(&terminal, XY(0, 0), XY(514, 130), 0, 0xd79921); + fbputs(&terminal, XY(9, 9), "simple window", 0xebdbb2, 0x000000); - drwcpyat(&framebuf, &terminal, XY(185, 281)); - drwcpyat(&framebuf, &tux, XY(59, 91)); + drwcpyat(&framebuf, &terminal, xy[0]); + drwcpyat(&framebuf, &tux, xy[1]); + drwcpyraw(&screen, &framebuf); + switch (getch(0)) { + case 'h': xy[sel] = XY(GETX(xy[sel]) - 1, GETY(xy[sel])); break; + case 'l': xy[sel] = XY(GETX(xy[sel]) + 1, GETY(xy[sel])); break; + case 'k': xy[sel] = XY(GETX(xy[sel]), GETY(xy[sel]) - 1); break; + case 'j': xy[sel] = XY(GETX(xy[sel]), GETY(xy[sel]) + 1); break; + case 'J': sel = sel ? 0 : 1; break; + case 'K': sel = sel ? 0 : 1; break; + case 'q': goto done; break; + } + } + done: free(terminal.mappoint); + free(framebuf.mappoint); fbunmap(&tux.mappoint, tux.len); fbunmap(&mappoint, len); diff --git a/getch.h b/getch.h @@ -0,0 +1,39 @@ +/* UNLICENSED */ + +#include <sys/ioctl.h> +#include <stdio.h> +#include <termios.h> +#include <unistd.h> + +static struct termios old, current; + +/* Initialize new terminal i/o settings */ +void +initTermios(int echo) +{ + tcgetattr(0, &old); /* grab old terminal i/o settings */ + current = old; /* make new settings same as old settings */ + current.c_lflag &= ~ICANON; /* disable buffered i/o */ + if(echo) + current.c_lflag |= ECHO; /* set echo mode */ + else + current.c_lflag &= ~ECHO; /* set no echo mode */ + tcsetattr(0, TCSANOW, &current); /* use these new terminal i/o settings now */ +} + +/* Restore old terminal i/o settings */ +void +resetTermios(void) +{ + tcsetattr(0, TCSANOW, &old); +} + +char +getch(char echo) +{ + char ch; + initTermios(echo); + ch = getchar(); + resetTermios(); + return ch; +}