11 #define MIN(a, b) ((a) < (b) ? (a) : (b))
12 #define MAX(a, b) ((a) > (b) ? (a) : (b))
13 #define NLEVELS (1 << 8)
17 static struct fb_var_screeninfo vinfo
;
18 static struct fb_fix_screeninfo finfo
;
20 static int nr
, ng
, nb
;
21 static int rl
, rr
, gl
, gr
, bl
, br
; /* fb_color() shifts */
23 static int fb_len(void)
25 return finfo
.line_length
* vinfo
.yres_virtual
;
28 static void fb_cmap_save(int save
)
30 static unsigned short red
[NLEVELS
], green
[NLEVELS
], blue
[NLEVELS
];
32 if (finfo
.visual
== FB_VISUAL_TRUECOLOR
)
35 cmap
.len
= MAX(nr
, MAX(ng
, nb
));
40 ioctl(fd
, save
? FBIOGETCMAP
: FBIOPUTCMAP
, &cmap
);
45 unsigned short red
[NLEVELS
], green
[NLEVELS
], blue
[NLEVELS
];
48 if (finfo
.visual
== FB_VISUAL_TRUECOLOR
)
51 for (i
= 0; i
< nr
; i
++)
52 red
[i
] = (65535 / (nr
- 1)) * i
;
53 for (i
= 0; i
< ng
; i
++)
54 green
[i
] = (65535 / (ng
- 1)) * i
;
55 for (i
= 0; i
< nb
; i
++)
56 blue
[i
] = (65535 / (nb
- 1)) * i
;
59 cmap
.len
= MAX(nr
, MAX(ng
, nb
));
65 ioctl(fd
, FBIOPUTCMAP
, &cmap
);
68 unsigned fb_mode(void)
70 return (bpp
<< 16) | (vinfo
.red
.length
<< 8) |
71 (vinfo
.green
.length
<< 4) | (vinfo
.blue
.length
);
74 static void init_colors(void)
76 nr
= 1 << vinfo
.red
.length
;
77 ng
= 1 << vinfo
.blue
.length
;
78 nb
= 1 << vinfo
.green
.length
;
79 rr
= 8 - vinfo
.red
.length
;
80 rl
= vinfo
.red
.offset
;
81 gr
= 8 - vinfo
.green
.length
;
82 gl
= vinfo
.green
.offset
;
83 br
= 8 - vinfo
.blue
.length
;
84 bl
= vinfo
.blue
.offset
;
89 fd
= open(FBDEV_PATH
, O_RDWR
);
92 if (ioctl(fd
, FBIOGET_VSCREENINFO
, &vinfo
) == -1)
94 if (ioctl(fd
, FBIOGET_FSCREENINFO
, &finfo
) == -1)
96 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
97 bpp
= (vinfo
.bits_per_pixel
+ 7) >> 3;
98 fb
= mmap(NULL
, fb_len(), PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
114 munmap(fb
, fb_len());
130 return fb
+ (r
+ vinfo
.yoffset
) * finfo
.line_length
;
133 void fb_set(int r
, int c
, void *mem
, int len
)
135 memcpy(fb_mem(r
) + (c
+ vinfo
.xoffset
) * bpp
, mem
, len
* bpp
);
138 unsigned fb_val(int r
, int g
, int b
)
140 return ((r
>> rr
) << rl
) | ((g
>> gr
) << gl
) | ((b
>> br
) << bl
);