11 #define MIN(a, b) ((a) < (b) ? (a) : (b))
12 #define MAX(a, b) ((a) > (b) ? (a) : (b))
13 #define NLEVELS (1 << 8)
15 static struct fb_var_screeninfo vinfo
; /* linux-specific FB structure */
16 static struct fb_fix_screeninfo finfo
; /* linux-specific FB structure */
17 static int fd
; /* FB device file descriptor */
18 static void *fb
; /* mmap()ed FB memory */
19 static int bpp
; /* bytes per pixel */
20 static int nr
, ng
, nb
; /* color levels */
21 static int rl
, rr
, gl
, gr
, bl
, br
; /* shifts per color */
22 static int xres
, yres
, xoff
, yoff
; /* drawing region */
24 static int fb_len(void)
26 return finfo
.line_length
* vinfo
.yres_virtual
;
29 static void fb_cmap_save(int save
)
31 static unsigned short red
[NLEVELS
], green
[NLEVELS
], blue
[NLEVELS
];
33 if (finfo
.visual
== FB_VISUAL_TRUECOLOR
)
36 cmap
.len
= MAX(nr
, MAX(ng
, nb
));
41 ioctl(fd
, save
? FBIOGETCMAP
: FBIOPUTCMAP
, &cmap
);
46 unsigned short red
[NLEVELS
], green
[NLEVELS
], blue
[NLEVELS
];
49 if (finfo
.visual
== FB_VISUAL_TRUECOLOR
)
52 for (i
= 0; i
< nr
; i
++)
53 red
[i
] = (65535 / (nr
- 1)) * i
;
54 for (i
= 0; i
< ng
; i
++)
55 green
[i
] = (65535 / (ng
- 1)) * i
;
56 for (i
= 0; i
< nb
; i
++)
57 blue
[i
] = (65535 / (nb
- 1)) * i
;
60 cmap
.len
= MAX(nr
, MAX(ng
, nb
));
66 ioctl(fd
, FBIOPUTCMAP
, &cmap
);
69 unsigned fb_mode(void)
71 return ((rl
< gl
) << 22) | ((rl
< bl
) << 21) | ((gl
< bl
) << 20) |
72 (bpp
<< 16) | (vinfo
.red
.length
<< 8) |
73 (vinfo
.green
.length
<< 4) | (vinfo
.blue
.length
);
76 static void init_colors(void)
78 nr
= 1 << vinfo
.red
.length
;
79 ng
= 1 << vinfo
.blue
.length
;
80 nb
= 1 << vinfo
.green
.length
;
81 rr
= 8 - vinfo
.red
.length
;
82 rl
= vinfo
.red
.offset
;
83 gr
= 8 - vinfo
.green
.length
;
84 gl
= vinfo
.green
.offset
;
85 br
= 8 - vinfo
.blue
.length
;
86 bl
= vinfo
.blue
.offset
;
89 int fb_init(char *dev
)
91 char *path
= dev
? dev
: FBDEV
;
92 char *geom
= dev
? strchr(dev
, ':') : NULL
;
95 sscanf(geom
+ 1, "%dx%d%d%d", &xres
, &yres
, &xoff
, &yoff
);
97 fd
= open(path
, O_RDWR
);
100 if (ioctl(fd
, FBIOGET_VSCREENINFO
, &vinfo
) < 0)
102 if (ioctl(fd
, FBIOGET_FSCREENINFO
, &finfo
) < 0)
104 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
105 bpp
= (vinfo
.bits_per_pixel
+ 7) >> 3;
106 fb
= mmap(NULL
, fb_len(), PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
107 if (fb
== MAP_FAILED
)
122 munmap(fb
, fb_len());
128 return yres
? yres
: vinfo
.yres
;
133 return xres
? xres
: vinfo
.xres
;
138 return fb
+ (r
+ vinfo
.yoffset
+ yoff
) * finfo
.line_length
+ (vinfo
.xoffset
+ xoff
) * bpp
;
141 unsigned fb_val(int r
, int g
, int b
)
143 return ((r
>> rr
) << rl
) | ((g
>> gr
) << gl
) | ((b
>> br
) << bl
);