4b96eeb535fc192f549bb5fe2078763b3590ee4f
[fbgfx] / src / fbgfx.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6 #include <sys/mman.h>
7 #include <linux/fb.h>
8 #include "fbgfx.h"
9
10 static void cleanup(void);
11
12 static int fd = -1;
13 static void *vmem;
14 static int vmem_size;
15 static struct fb_fix_screeninfo finfo;
16 static struct fb_var_screeninfo vinfo;
17
18 static int init(void)
19 {
20         if(fd >= 0) return 0;
21
22         if((fd = open("/dev/fb0", O_RDWR)) == -1) {
23                 fprintf(stderr, "failed to open framebuffer device\n");
24                 return -1;
25         }
26
27         ioctl(fd, FBIOGET_FSCREENINFO, &finfo);
28
29         atexit(cleanup);
30         return 0;
31 }
32
33 static void cleanup(void)
34 {
35         if(vmem) {
36                 munmap(vmem, vmem_size);
37         }
38         if(fd != -1) {
39                 close(fd);
40         }
41 }
42
43 void *fbgfx_set_video_mode(int x, int y, int depth)
44 {
45         if(init() == -1) {
46                 return 0;
47         }
48         return 0;
49 }
50
51 void *fbgfx_get_video_mode(int *xptr, int *yptr, int *depthptr)
52 {
53         if(init() == -1) {
54                 return 0;
55         }
56         if(vmem) return vmem;
57
58         ioctl(fd, FBIOGET_VSCREENINFO, &vinfo);
59         vmem_size = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
60
61         if((vmem = mmap(0, vmem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == (void*)-1) {
62                 fprintf(stderr, "failed to map video memory\n");
63                 vmem = 0;
64                 return 0;
65         }
66         *xptr = vinfo.xres;
67         *yptr = vinfo.yres;
68         *depthptr = vinfo.bits_per_pixel;
69         return vmem;
70 }