summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-01-28 12:27:34 -0500
committerJune McEnroe <june@causal.agency>2018-01-28 12:27:34 -0500
commit14a68195833bfb73033987b6333c64fbd7b9888a (patch)
tree50e115c2f7566ff3c702fa8cc1e8cef155c5a36a
parentAdd color.c (diff)
downloadsrc-14a68195833bfb73033987b6333c64fbd7b9888a.tar.gz
src-14a68195833bfb73033987b6333c64fbd7b9888a.zip
Add init function to fb interface
-rw-r--r--bin/color.c3
-rw-r--r--bin/fb.c71
2 files changed, 39 insertions, 35 deletions
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);
     }
 }