From 14a68195833bfb73033987b6333c64fbd7b9888a Mon Sep 17 00:00:00 2001
From: June McEnroe <june@causal.agency>
Date: Sun, 28 Jan 2018 12:27:34 -0500
Subject: Add init function to fb interface

---
 bin/color.c |  3 +++
 bin/fb.c    | 71 +++++++++++++++++++++++++++++++------------------------------
 2 files changed, 39 insertions(+), 35 deletions(-)

(limited to 'bin')

diff --git a/bin/color.c b/bin/color.c
index 3e6c3b5a..75118226 100644
--- a/bin/color.c
+++ b/bin/color.c
@@ -2,6 +2,9 @@
 
 static uint32_t color;
 
+void init() {
+}
+
 void draw(uint32_t *buf, uint32_t xres, uint32_t yres) {
     for (uint32_t i = 0; i < xres * yres; ++i) {
         buf[i] = color;
diff --git a/bin/fb.c b/bin/fb.c
index 985ba3b4..fdc14ef5 100644
--- a/bin/fb.c
+++ b/bin/fb.c
@@ -11,24 +11,25 @@
 #include <termios.h>
 #include <unistd.h>
 
+extern void init(int argc, char *argv[]);
 extern void draw(uint32_t *buf, uint32_t xres, uint32_t yres);
-extern void input(char c);
+extern void input(char in);
 
-static uint32_t size;
+static size_t bufLen;
 static uint32_t *buf;
 static uint32_t *saveBuf;
 
+static struct termios saveTerm;
+
 static void restoreBuf(void) {
-    memcpy(buf, saveBuf, size);
+    memcpy(buf, saveBuf, bufLen);
 }
 
-static struct termios saveTerm;
-
 static void restoreTerm(void) {
     tcsetattr(STDERR_FILENO, TCSADRAIN, &saveTerm);
 }
 
-int main() {
+int main(int argc, char *argv[]) {
     int error;
 
     char *path = getenv("FRAMEBUFFER");
@@ -37,33 +38,30 @@ int main() {
     int fd = open(path, O_RDWR);
     if (fd < 0) err(EX_OSFILE, "%s", path);
 
-    struct fb_fix_screeninfo fix;
-    error = ioctl(fd, FBIOGET_FSCREENINFO, &fix);
+    struct fb_fix_screeninfo fixInfo;
+    error = ioctl(fd, FBIOGET_FSCREENINFO, &fixInfo);
     if (error) err(EX_IOERR, "%s", path);
 
-    struct fb_var_screeninfo var;
-    error = ioctl(fd, FBIOGET_VSCREENINFO, &var);
+    struct fb_var_screeninfo varInfo;
+    error = ioctl(fd, FBIOGET_VSCREENINFO, &varInfo);
     if (error) err(EX_IOERR, "%s", path);
 
-    assert(!var.xoffset);
-    assert(!var.yoffset);
-    assert(var.bits_per_pixel == 32);
-    assert(!var.grayscale);
-    assert(var.red.offset == 16);
-    assert(var.red.length == 8);
-    assert(var.green.offset == 8);
-    assert(var.green.length == 8);
-    assert(var.blue.offset == 0);
-    assert(var.blue.length == 8);
-    assert(fix.line_length == var.xres * 4);
-
-    size = var.xres * var.yres * 4;
-    buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-    if (buf == MAP_FAILED) err(EX_OSERR, "%s", path);
-
-    saveBuf = malloc(size);
-    if (!saveBuf) err(EX_OSERR, "malloc");
-    memcpy(saveBuf, buf, size);
+    assert(!varInfo.xoffset);
+    assert(!varInfo.yoffset);
+    assert(varInfo.bits_per_pixel == 32);
+    assert(!varInfo.grayscale);
+    assert(varInfo.red.offset == 16);
+    assert(varInfo.green.offset == 8);
+    assert(varInfo.blue.offset == 0);
+    assert(fixInfo.line_length == varInfo.xres * 4);
+
+    bufLen = varInfo.xres * varInfo.yres * 4;
+    buf = mmap(NULL, bufLen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+    if (buf == MAP_FAILED) err(EX_IOERR, "%s", path);
+
+    saveBuf = malloc(bufLen);
+    if (!saveBuf) err(EX_OSERR, "malloc(%zu)", bufLen);
+    memcpy(saveBuf, buf, bufLen);
     atexit(restoreBuf);
 
     error = tcgetattr(STDERR_FILENO, &saveTerm);
@@ -75,14 +73,17 @@ int main() {
     error = tcsetattr(STDERR_FILENO, TCSADRAIN, &raw);
     if (error) err(EX_IOERR, "tcsetattr");
 
-    for (;;) {
-        draw(buf, var.xres, var.yres);
+    init(argc, argv);
+
+    for(;;) {
+        draw(buf, varInfo.xres, varInfo.yres);
 
-        char c;
-        ssize_t len = read(STDIN_FILENO, &c, 1);
+        char in;
+        ssize_t len = read(STDIN_FILENO, &in, 1);
         if (len < 0) err(EX_IOERR, "read");
-        if (!len || c == CTRL('C')) return EX_OK;
+        if (!len) return EX_DATAERR;
+        if (in == CTRL('C')) return EX_USAGE;
 
-        input(c);
+        input(in);
     }
 }
-- 
cgit 1.4.1

69d03bce3c2bef0&amp;follow=1'>Use braces in Makefile</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-07-27 17:54:01 -0400'>2019-07-27</span></td><td><a href='/torus/commit/torus.h?id=00943ca2639f038773229340af58ab8e1ff1bf36&amp;follow=1'>Make copyright headers consistent</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-07-08 00:05:32 -0400'>2019-07-08</span></td><td><a href='/torus/commit/index.html?id=2dd2154181e59a950f4a47defe592a092b2789fd&amp;follow=1'>Remove NetHack from index</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-04-08 17:33:52 -0400'>2019-04-08</span></td><td><a href='/torus/commit/Makefile?id=f738b1cf12b2000fdb6796cb7a7d538d29f2496c&amp;follow=1'>Don't install rc.kfcgi either</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-03-28 15:49:06 -0400'>2019-03-28</span></td><td><a href='/torus/commit/rc.kfcgi?id=10ea382f16051b9a6cccb119e54934e97f1f1d05&amp;follow=1'>Remove rc.kfcgi</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-16 15:42:22 -0500'>2019-01-16</span></td><td><a href='/torus/commit/index.html?id=9a27e1fb708e5c31aa7d3dc8a57f07251bc3792a&amp;follow=1'>Reformat index.html with explore link</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-16 15:39:33 -0500'>2019-01-16</span></td><td><a href='/torus/commit/explore.html?id=fa43bd428fdc99179005ecef0c4a099d4b95884c&amp;follow=1'>Return false from keydown handler in explore</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-08 15:44:32 -0500'>2019-01-08</span></td><td><a href='/torus/commit/client.c?id=ff29f89d37025e8fe332043abadec9574fa77652&amp;follow=1'>Draw lines to tile edge</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-08 00:30:21 -0500'>2019-01-08</span></td><td><a href='/torus/commit/torus.h?id=92ee655e2e54a3f5ef4a088c6156998f381a6acf&amp;follow=1'>Factor out default paths</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-08 00:24:44 -0500'>2019-01-08</span></td><td><a href='/torus/commit/torus.1?id=45324860fdd496c0a5ff89233ee9a16ce53c3c89&amp;follow=1'>Remove incorrect default coordinates</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-07 01:34:59 -0500'>2019-01-07</span></td><td><a href='/torus/commit/explore.html?id=f74c6a8eeda788788694bcd5de4369a3956567b4&amp;follow=1'>Set img src when explore fragment changes</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-07 01:11:20 -0500'>2019-01-07</span></td><td><a href='/torus/commit/png.h?id=b9551af817e9a2ba90289d9c7de0e4a34f92287d&amp;follow=1'>Compress PNG data in image</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-07 00:33:52 -0500'>2019-01-07</span></td><td><a href='/torus/commit/image.c?id=5e1eef754cb3a09f083577d3e934af583a56337d&amp;follow=1'>madvise MADV_NOCORE in image</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-07 00:22:34 -0500'>2019-01-07</span></td><td><a href='/torus/commit/explore.html?id=9786dcdd3ba8f0dc1ac7ab510ab32bc599bd936b&amp;follow=1'>Add license notice to explore JavaScript</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-07 00:20:23 -0500'>2019-01-07</span></td><td><a href='/torus/commit/explore.html?id=784a3f4a07f4af7c2b586df66f9af91e3cb5b95b&amp;follow=1'>Add Q/Home binding in explore</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-07 00:20:07 -0500'>2019-01-07</span></td><td><a href='/torus/commit/explore.html?id=ef9406f079bf2ef9034ed07654dd9c9076c3074b&amp;follow=1'>Fix explore image URL</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-07 00:15:56 -0500'>2019-01-07</span></td><td><a href='/torus/commit/explore.html?id=f4a050a8e39d3028d31ff57575bb4af32705f2fb&amp;follow=1'>Add HOME button, ssh link, AGPL notice to explore</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-06 22:02:01 -0500'>2019-01-06</span></td><td><a href='/torus/commit/explore.html?id=51c7aec72fb00785b05de05863771e57cd39cf82&amp;follow=1'>Add meta viewport to explore</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-06 19:53:00 -0500'>2019-01-06</span></td><td><a href='/torus/commit/explore.html?id=aae19de38fc6b57a6355293aa9de8f8d8e233d27&amp;follow=1'>Set background and max-width in explore</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-06 19:40:04 -0500'>2019-01-06</span></td><td><a href='/torus/commit/image.c?id=7654154a6a023407adb4af05b906f90226625ad2&amp;follow=1'>Swallow writes after the connection is closed</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-06 19:18:20 -0500'>2019-01-06</span></td><td><a href='/torus/commit/image.c?id=2b343d76c514087054bbf4c27e731ee99ffc21f9&amp;follow=1'>Handle KCGI_HUP</a></td><td>June McEnroe</td></tr>
<tr><td><span title='2019-01-06 17:36:05 -0500'>2019-01-06</span></td><td><a href='/torus/commit/Makefile?id=03ad742efd747bf980254a1e34b731d8f7b5370a&amp;follow=1'>Install html files</a></td><td>June McEnroe