initial commit
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 19 Aug 2016 04:01:33 +0000 (07:01 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 19 Aug 2016 04:01:33 +0000 (07:01 +0300)
Makefile [new file with mode: 0644]
src/fbgfx.c [new file with mode: 0644]
src/fbgfx.h [new file with mode: 0644]
src/main.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..7882958
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,12 @@
+src = $(wildcard src/*.c)
+obj = $(src:.c=.o)
+bin = fbgfx
+
+CFLAGS = -pedantic -Wall -g
+
+$(bin): $(obj)
+       $(CC) -o $@ $(obj) $(LDFLAGS)
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin)
diff --git a/src/fbgfx.c b/src/fbgfx.c
new file mode 100644 (file)
index 0000000..4b96eeb
--- /dev/null
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <linux/fb.h>
+#include "fbgfx.h"
+
+static void cleanup(void);
+
+static int fd = -1;
+static void *vmem;
+static int vmem_size;
+static struct fb_fix_screeninfo finfo;
+static struct fb_var_screeninfo vinfo;
+
+static int init(void)
+{
+       if(fd >= 0) return 0;
+
+       if((fd = open("/dev/fb0", O_RDWR)) == -1) {
+               fprintf(stderr, "failed to open framebuffer device\n");
+               return -1;
+       }
+
+       ioctl(fd, FBIOGET_FSCREENINFO, &finfo);
+
+       atexit(cleanup);
+       return 0;
+}
+
+static void cleanup(void)
+{
+       if(vmem) {
+               munmap(vmem, vmem_size);
+       }
+       if(fd != -1) {
+               close(fd);
+       }
+}
+
+void *fbgfx_set_video_mode(int x, int y, int depth)
+{
+       if(init() == -1) {
+               return 0;
+       }
+       return 0;
+}
+
+void *fbgfx_get_video_mode(int *xptr, int *yptr, int *depthptr)
+{
+       if(init() == -1) {
+               return 0;
+       }
+       if(vmem) return vmem;
+
+       ioctl(fd, FBIOGET_VSCREENINFO, &vinfo);
+       vmem_size = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
+
+       if((vmem = mmap(0, vmem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == (void*)-1) {
+               fprintf(stderr, "failed to map video memory\n");
+               vmem = 0;
+               return 0;
+       }
+       *xptr = vinfo.xres;
+       *yptr = vinfo.yres;
+       *depthptr = vinfo.bits_per_pixel;
+       return vmem;
+}
diff --git a/src/fbgfx.h b/src/fbgfx.h
new file mode 100644 (file)
index 0000000..6c18d7f
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef FBGFX_H_
+#define FBGFX_H_
+
+void *fbgfx_set_video_mode(int x, int y, int depth);
+void *fbgfx_get_video_mode(int *xptr, int *yptr, int *depthptr);
+
+
+#endif /* FBGFX_H_ */
diff --git a/src/main.c b/src/main.c
new file mode 100644 (file)
index 0000000..9d73367
--- /dev/null
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "fbgfx.h"
+
+static unsigned char *vmem;
+static int xsz, ysz, depth;
+
+int main(void)
+{
+       if(!(vmem = fbgfx_get_video_mode(&xsz, &ysz, &depth))) {
+               return 1;
+       }
+       printf("current video mode: %dx%d %dbpp\n", xsz, ysz, depth);
+
+       /*memset(vmem, 0xff, xsz * ysz * depth / 8);*/
+
+       return 0;
+}