summary refs log tree commit diff
path: root/bin/fb.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--bin/fb.c71
1 files changed, 36 insertions, 35 deletions
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);
     }
 }