+#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;
+}