13 #define FBDEV_PATH "/dev/fb0"
14 #define MAXWIDTH (1 << 12)
15 #define BPP sizeof(fbval_t)
16 #define NLEVELS (1 << 8)
19 static unsigned char *fb
;
20 static struct fb_var_screeninfo vinfo
;
21 static struct fb_fix_screeninfo finfo
;
25 return vinfo
.xres_virtual
* vinfo
.yres_virtual
* BPP
;
28 static void fb_cmap_save(int save
)
30 static unsigned short red
[NLEVELS
], green
[NLEVELS
], blue
[NLEVELS
];
32 int mr
= 1 << vinfo
.red
.length
;
33 int mg
= 1 << vinfo
.green
.length
;
34 int mb
= 1 << vinfo
.blue
.length
;
35 if (finfo
.visual
== FB_VISUAL_TRUECOLOR
)
38 cmap
.len
= MAX(mr
, MAX(mg
, mb
));
44 ioctl(fd
, FBIOGETCMAP
, &cmap
);
46 ioctl(fd
, FBIOPUTCMAP
, &cmap
);
51 unsigned short red
[NLEVELS
], green
[NLEVELS
], blue
[NLEVELS
];
53 int mr
= 1 << vinfo
.red
.length
;
54 int mg
= 1 << vinfo
.green
.length
;
55 int mb
= 1 << vinfo
.blue
.length
;
57 if (finfo
.visual
== FB_VISUAL_TRUECOLOR
)
60 for (i
= 0; i
< mr
; i
++)
61 red
[i
] = (65535 / (mr
- 1)) * i
;
62 for (i
= 0; i
< mg
; i
++)
63 green
[i
] = (65535 / (mg
- 1)) * i
;
64 for (i
= 0; i
< mb
; i
++)
65 blue
[i
] = (65535 / (mb
- 1)) * i
;
68 cmap
.len
= MAX(mr
, MAX(mg
, mb
));
74 ioctl(fd
, FBIOPUTCMAP
, &cmap
);
79 fd
= open(FBDEV_PATH
, O_RDWR
);
81 xerror("can't open " FBDEV_PATH
);
82 if (ioctl(fd
, FBIOGET_VSCREENINFO
, &vinfo
) == -1)
83 xerror("ioctl failed");
84 if (ioctl(fd
, FBIOGET_FSCREENINFO
, &finfo
) == -1)
85 xerror("ioctl failed");
86 fb
= mmap(NULL
, fb_len(), PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
88 xerror("can't map the framebuffer");
93 void fb_set(int r
, int c
, fbval_t
*mem
, int len
)
95 long loc
= (c
+ vinfo
.xoffset
) * BPP
+
96 (r
+ vinfo
.yoffset
) * finfo
.line_length
;
97 memcpy(fb
+ loc
, mem
, len
* BPP
);
103 munmap(fb
, fb_len());
107 static fbval_t
color_bits(struct fb_bitfield
*bf
, fbval_t v
)
109 fbval_t moved
= v
>> (8 - bf
->length
);
110 return moved
<< bf
->offset
;
113 fbval_t
fb_color(unsigned char r
, unsigned char g
, unsigned char b
)
115 return color_bits(&vinfo
.red
, r
) |
116 color_bits(&vinfo
.green
, g
) |
117 color_bits(&vinfo
.blue
, b
);
130 static unsigned char *rowaddr(int r
)
132 return fb
+ (r
+ vinfo
.yoffset
) * finfo
.line_length
;
135 static unsigned long cache
[MAXWIDTH
];
136 void fb_box(int sr
, int sc
, int er
, int ec
, fbval_t val
)
139 int pc
= sizeof(cache
[0]) / sizeof(val
);
140 int cn
= MIN((ec
- sc
) / pc
+ 1, ARRAY_SIZE(cache
));
141 unsigned long nv
= val
;
142 for (i
= 1; i
< pc
; i
++)
143 nv
= (nv
<< (sizeof(val
) * 8)) | val
;
144 for (i
= 0; i
< cn
; i
++)
146 for (i
= sr
; i
< er
; i
++)
147 memcpy(rowaddr(i
) + sc
* BPP
, cache
, (ec
- sc
) * BPP
);