1 /* $Id: fb.c,v 1.16 2003/07/13 16:19:10 ukai Exp $ */
2 /**************************************************************************
3 fb.c 0.3 Copyright (C) 2002, hito
4 **************************************************************************/
13 #include <sys/ioctl.h>
19 #define FB_ENV "FRAMEBUFFER"
20 #define FB_DEFDEV "/dev/fb0"
22 #define MONO_OFFSET_8BIT 0x40
23 #define COLORS_MONO_8BIT 0x40
24 #define MONO_MASK_8BIT 0xFC
25 #define MONO_SHIFT_8BIT 2
27 #define COLOR_OFFSET_8BIT 0x80
28 #define COLORS_8BIT 0x80
29 #define RED_MASK_8BIT 0xC0
30 #define GREEN_MASK_8BIT 0xE0
31 #define BLUE_MASK_8BIT 0xC0
32 #define RED_SHIFT_8BIT 1
33 #define GREEN_SHIFT_8BIT 3
34 #define BLUE_SHIFT_8BIT 6
39 #define IMAGE_SIZE_MAX 10000
41 static struct fb_cmap *fb_cmap_create(struct fb_fix_screeninfo *,
42 struct fb_var_screeninfo *);
43 static void fb_cmap_destroy(struct fb_cmap *cmap);
44 static int fb_fscrn_get(int fbfp, struct fb_fix_screeninfo *scinfo);
45 static void *fb_mmap(int fbfp, struct fb_fix_screeninfo *scinfo);
46 static int fb_munmap(void *buf, struct fb_fix_screeninfo *scinfo);
47 static int fb_vscrn_get(int fbfp, struct fb_var_screeninfo *scinfo);
48 static int fb_cmap_set(int fbfp, struct fb_cmap *cmap);
49 static int fb_cmap_get(int fbfp, struct fb_cmap *cmap);
50 static int fb_cmap_init(void);
51 static int fb_get_cmap_index(int r, int g, int b);
52 static unsigned long fb_get_packed_color(int r, int g, int b);
54 static struct fb_fix_screeninfo fscinfo;
55 static struct fb_var_screeninfo vscinfo;
56 static struct fb_cmap *cmap = NULL, *cmap_org = NULL;
57 static int is_open = FALSE;
59 static size_t pixel_size = 0;
60 static unsigned char *buf = NULL;
65 char *fbdev = { FB_DEFDEV };
71 fbdev = getenv(FB_ENV);
74 if ((fbfp = open(fbdev, O_RDWR)) == -1) {
75 fprintf(stderr, "open %s error\n", fbdev);
79 if (fb_fscrn_get(fbfp, &fscinfo)) {
83 if (fb_vscrn_get(fbfp, &vscinfo)) {
87 if ((cmap = fb_cmap_create(&fscinfo, &vscinfo)) == (struct fb_cmap *)-1) {
91 if (!(buf = fb_mmap(fbfp, &fscinfo))) {
92 fprintf(stderr, "Can't allocate memory.\n");
96 if (fscinfo.type != FB_TYPE_PACKED_PIXELS) {
97 fprintf(stderr, "This type of framebuffer is not supported.\n");
101 if (fscinfo.visual == FB_VISUAL_PSEUDOCOLOR && vscinfo.bits_per_pixel == 8) {
102 if (fb_cmap_get(fbfp, cmap)) {
103 fprintf(stderr, "Can't get color map.\n");
104 fb_cmap_destroy(cmap);
114 else if ((fscinfo.visual == FB_VISUAL_TRUECOLOR ||
115 fscinfo.visual == FB_VISUAL_DIRECTCOLOR) &&
116 (vscinfo.bits_per_pixel == 15 ||
117 vscinfo.bits_per_pixel == 16 ||
118 vscinfo.bits_per_pixel == 24 || vscinfo.bits_per_pixel == 32)) {
119 pixel_size = (vscinfo.bits_per_pixel + 7) / CHAR_BIT;
122 fprintf(stderr, "This type of framebuffer is not supported.\n");
141 fb_cmap_destroy(cmap);
144 if (cmap_org != NULL) {
145 fb_cmap_set(fbfp, cmap_org);
146 fb_cmap_destroy(cmap_org);
150 fb_munmap(buf, &fscinfo);
161 /*********** fb_image_* ***********/
164 fb_image_new(int width, int height)
171 if (width > IMAGE_SIZE_MAX || height > IMAGE_SIZE_MAX || width < 1
175 image = malloc(sizeof(*image));
179 image->data = calloc(sizeof(*(image->data)), width * height * pixel_size);
180 if (image->data == NULL) {
189 image->width = width;
190 image->height = height;
191 image->rowstride = width * pixel_size;
192 image->len = width * height * pixel_size;
198 fb_image_free(FB_IMAGE * image)
203 if (image->data != NULL)
210 fb_image_pset(FB_IMAGE * image, int x, int y, int r, int g, int b)
214 if (image == NULL || is_open != TRUE || x >= image->width
215 || y >= image->height)
218 work = fb_get_packed_color(r, g, b);
219 memcpy(image->data + image->rowstride * y + pixel_size * x, &work,
224 fb_image_fill(FB_IMAGE * image, int r, int g, int b)
229 if (image == NULL || is_open != TRUE)
232 work = fb_get_packed_color(r, g, b);
234 for (offset = 0; offset < image->len; offset += pixel_size) {
235 memcpy(image->data + offset, &work, pixel_size);
240 fb_image_draw(FB_IMAGE * image, int x, int y, int sx, int sy, int width,
243 int i, offset_fb, offset_img;
245 if (image == NULL || is_open != TRUE ||
246 sx > image->width || sy > image->height ||
247 x > fb_width() || y > fb_height())
250 if (sx + width > image->width)
251 width = image->width - sx;
253 if (sy + height > image->height)
254 height = image->height - sy;
256 if (x + width > fb_width())
257 width = fb_width() - x;
259 if (y + height > fb_height())
260 height = fb_height() - y;
262 offset_fb = fscinfo.line_length * y + pixel_size * x;
263 offset_img = image->rowstride * sy + pixel_size * sx;
264 for (i = 0; i < height; i++) {
265 memcpy(buf + offset_fb, image->data + offset_img, pixel_size * width);
266 offset_fb += fscinfo.line_length;
267 offset_img += image->rowstride;
274 fb_image_copy(FB_IMAGE * dest, FB_IMAGE * src)
276 if (dest == NULL || src == NULL)
279 if (dest->len != src->len)
282 memcpy(dest->data, src->data, src->len);
285 /*********** fb_frame_* ***********/
288 fb_frame_new(int w, int h, int n)
293 if (w > IMAGE_SIZE_MAX || h > IMAGE_SIZE_MAX || w < 1 || h < 1 || n < 1)
296 frame = malloc(sizeof(*frame) * n);
300 for (i = 0; i < n; i++) {
301 frame[i] = fb_image_new(w, h);
304 frame[i]->delay = 1000;
305 if (frame[i] == NULL)
310 fb_frame_free(frame);
319 fb_frame_free(FB_IMAGE ** frame)
327 for (i = 0; i < n; i++) {
328 fb_image_free(frame[i]);
352 fb_clear(int x, int y, int w, int h, int r, int g, int b)
355 static int rr = -1, gg = -1, bb = -1;
356 static char *tmp = NULL;
358 if (is_open != TRUE || x > fb_width() || y > fb_height())
366 if (x + w > fb_width())
368 if (y + h > fb_height())
372 tmp = malloc(fscinfo.line_length);
376 if (rr != r || gg != g || bb != b) {
380 work = fb_get_packed_color(r, g, b);
381 for (i = 0; i < ww; i++)
382 memcpy(tmp + pixel_size * i, &work, pixel_size);
387 offset_fb = fscinfo.line_length * y + pixel_size * x;
388 for (i = 0; i < h; i++) {
389 memcpy(buf + offset_fb, tmp, pixel_size * w);
390 offset_fb += fscinfo.line_length;
395 /********* static functions **************/
397 fb_get_packed_color(int r, int g, int b)
399 if (pixel_size == 1) {
400 return fb_get_cmap_index(r, g, b);
404 ((r >> (CHAR_BIT - vscinfo.red.length)) << vscinfo.red.offset) +
405 ((g >> (CHAR_BIT - vscinfo.green.length)) << vscinfo.green.
407 ((b >> (CHAR_BIT - vscinfo.blue.length)) << vscinfo.blue.offset);
412 fb_get_cmap_index(int r, int g, int b)
415 if ((r & GREEN_MASK_8BIT) == (g & GREEN_MASK_8BIT)
416 && (g & GREEN_MASK_8BIT) == (b & GREEN_MASK_8BIT)) {
417 work = (r >> MONO_SHIFT_8BIT) + MONO_OFFSET_8BIT;
420 work = ((r & RED_MASK_8BIT) >> RED_SHIFT_8BIT)
421 + ((g & GREEN_MASK_8BIT) >> GREEN_SHIFT_8BIT)
422 + ((b & BLUE_MASK_8BIT) >> BLUE_SHIFT_8BIT)
436 if (cmap->len < COLOR_OFFSET_8BIT + COLORS_8BIT) {
437 fprintf(stderr, "Can't allocate enough color.\n");
441 if (cmap_org == NULL) {
443 fb_cmap_create(&fscinfo, &vscinfo)) == (struct fb_cmap *)-1) {
447 if (fb_cmap_get(fbfp, cmap_org)) {
448 fprintf(stderr, "Can't get color map.\n");
449 fb_cmap_destroy(cmap_org);
455 cmap->start = MONO_OFFSET_8BIT;
456 cmap->len = COLORS_8BIT + COLORS_MONO_8BIT;
458 for (lp = 0; lp < COLORS_MONO_8BIT; lp++) {
460 c = (lp << (MONO_SHIFT_8BIT + 8)) +
461 (lp ? (0xFFFF - (MONO_MASK_8BIT << 8)) : 0);
463 *(cmap->red + lp) = c;
465 *(cmap->green + lp) = c;
467 *(cmap->blue + lp) = c;
470 for (lp = 0; lp < COLORS_8BIT; lp++) {
472 r = lp & (RED_MASK_8BIT >> RED_SHIFT_8BIT);
473 g = lp & (GREEN_MASK_8BIT >> GREEN_SHIFT_8BIT);
474 b = lp & (BLUE_MASK_8BIT >> BLUE_SHIFT_8BIT);
476 *(cmap->red + lp + COLORS_MONO_8BIT)
477 = (r << (RED_SHIFT_8BIT + 8)) +
478 (r ? (0xFFFF - (RED_MASK_8BIT << 8)) : 0);
480 *(cmap->green + lp + COLORS_MONO_8BIT)
481 = (g << (GREEN_SHIFT_8BIT + 8)) +
482 (g ? (0xFFFF - (GREEN_MASK_8BIT << 8)) : 0);
484 *(cmap->blue + lp + COLORS_MONO_8BIT)
485 = (b << (BLUE_SHIFT_8BIT + 8)) +
486 (b ? (0xFFFF - (BLUE_MASK_8BIT << 8)) : 0);
489 if (fb_cmap_set(fbfp, cmap)) {
490 fb_cmap_destroy(cmap);
492 fprintf(stderr, "Can't set color map.\n");
499 * (struct fb_cmap) Device independent colormap information.
501 * fb_cmap_create() create colormap information
502 * fb_cmap_destroy() destroy colormap information
503 * fb_cmap_get() get information
504 * fb_cmap_set() set information
507 #define LUT_MAX (256)
509 static struct fb_cmap *
510 fb_cmap_create(struct fb_fix_screeninfo *fscinfo,
511 struct fb_var_screeninfo *vscinfo)
513 struct fb_cmap *cmap;
514 int cmaplen = LUT_MAX;
516 /* check the existence of colormap */
517 if (fscinfo->visual == FB_VISUAL_MONO01 ||
518 fscinfo->visual == FB_VISUAL_MONO10 ||
519 fscinfo->visual == FB_VISUAL_TRUECOLOR)
522 cmap = (struct fb_cmap *)malloc(sizeof(struct fb_cmap));
524 perror("cmap malloc error\n");
525 return (struct fb_cmap *)-1;
527 memset(cmap, 0, sizeof(struct fb_cmap));
529 /* Allocates memory for a colormap */
530 if (vscinfo->red.length) {
531 cmap->red = (__u16 *) malloc(sizeof(__u16) * cmaplen);
533 perror("red lut malloc error\n");
534 return (struct fb_cmap *)-1;
537 if (vscinfo->green.length) {
538 cmap->green = (__u16 *) malloc(sizeof(__u16) * cmaplen);
540 if (vscinfo->red.length)
542 perror("green lut malloc error\n");
543 return (struct fb_cmap *)-1;
546 if (vscinfo->blue.length) {
547 cmap->blue = (__u16 *) malloc(sizeof(__u16) * cmaplen);
549 if (vscinfo->red.length)
551 if (vscinfo->green.length)
553 perror("blue lut malloc error\n");
554 return (struct fb_cmap *)-1;
557 if (vscinfo->transp.length) {
558 cmap->transp = (__u16 *) malloc(sizeof(__u16) * cmaplen);
560 if (vscinfo->red.length)
562 if (vscinfo->green.length)
564 if (vscinfo->blue.length)
566 perror("transp lut malloc error\n");
567 return (struct fb_cmap *)-1;
575 fb_cmap_destroy(struct fb_cmap *cmap)
589 fb_cmap_get(int fbfp, struct fb_cmap *cmap)
591 if (ioctl(fbfp, FBIOGETCMAP, cmap)) {
592 perror("ioctl FBIOGETCMAP error\n");
599 fb_cmap_set(int fbfp, struct fb_cmap *cmap)
601 if (ioctl(fbfp, FBIOPUTCMAP, cmap)) {
602 perror("ioctl FBIOPUTCMAP error\n");
609 * access to framebuffer
611 * fb_mmap() map from framebuffer into memory
612 * fb_munmap() deletes the mappings
616 fb_mmap(int fbfp, struct fb_fix_screeninfo *scinfo)
619 if ((buf = (unsigned char *)
620 mmap(NULL, scinfo->smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fbfp,
623 perror("mmap error");
630 fb_munmap(void *buf, struct fb_fix_screeninfo *scinfo)
632 return munmap(buf, scinfo->smem_len);
636 * (struct fb_fix_screeninfo) device independent fixed information
638 * fb_fscrn_get() get information
641 fb_fscrn_get(int fbfp, struct fb_fix_screeninfo *scinfo)
643 if (ioctl(fbfp, FBIOGET_FSCREENINFO, scinfo)) {
644 perror("ioctl FBIOGET_FSCREENINFO error\n");
651 * (struct fb_var_screeninfo) device independent variable information
653 * fb_vscrn_get() get information
656 fb_vscrn_get(int fbfp, struct fb_var_screeninfo *scinfo)
658 if (ioctl(fbfp, FBIOGET_VSCREENINFO, scinfo)) {
659 perror("ioctl FBIOGET_VSCREENINFO error\n");