📄 scr_fbbak.c
字号:
/* * Copyright (c) 1999, 2000 Greg Haerr <greg@censoft.com> * * Microwindows Screen Driver for Linux kernel framebuffers * * Portions used from Ben Pfaff's BOGL <pfaffben@debian.org> * * Note: modify select_fb_driver() to add new framebuffer subdrivers * 2000/10/20 Modified by Song Lixin to be used under MiniGUI. */#define _GNU_SOURCE 1#include <assert.h>#include <fcntl.h>#include <limits.h>#include <linux/fb.h>#include <linux/kd.h>#include <linux/vt.h>#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <sys/io.h>#include <sys/ioctl.h>#include <sys/mman.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#ifndef __ECOS# include <syslog.h>#endif#include "native.h"#include "fb.h"#define HAVETEXTMODE 0 /* =0 for graphics only systems*/#ifndef FB_TYPE_VGA_PLANES#define FB_TYPE_VGA_PLANES 4#endif/* static variables*/static int fb; /* Framebuffer file handle. */static int status; /* 0=never inited, 1=once inited, 2=inited. */static short saved_red[16]; /* original hw palette*/static short saved_green[16];static short saved_blue[16];/* init framebuffer*/static PSD fb_open(PSD psd){ char * env; int type, visual; PSUBDRIVER subdriver;#if HAVETEXTMODE int tty;#endif struct fb_fix_screeninfo fb_fix; struct fb_var_screeninfo fb_var; assert(status < 2); /* locate and open framebuffer, get info*/ if(!(env = getenv("FRAMEBUFFER"))) env = "/dev/fb0"; fb = open(env, O_RDWR); if(fb < 0) { fprintf(stderr,"GAL ENGINE: Error when opening %s: %m. Please check kernel config.\n", env); return NULL; } if(ioctl(fb, FBIOGET_FSCREENINFO, &fb_fix) == -1 || ioctl(fb, FBIOGET_VSCREENINFO, &fb_var) == -1) { fprintf(stderr,"GAL ENGINE: Error when reading screen info: %m.\n"); goto fail; } /* setup screen device from framebuffer info*/ type = fb_fix.type; visual = fb_fix.visual; psd->xres = psd->xvirtres = fb_var.xres; psd->yres = psd->yvirtres = fb_var.yres; /* set planes from fb type*/ if (type == FB_TYPE_VGA_PLANES) psd->planes = 4; else if (type == FB_TYPE_PACKED_PIXELS) psd->planes = 1; else psd->planes = 0; /* force error later*/ psd->bpp = fb_var.bits_per_pixel; psd->ncolors = (psd->bpp >= 24)? (1 << 24): (1 << psd->bpp); /* set linelen to byte length, possibly converted later*/ psd->linelen = fb_fix.line_length; psd->size = 0; /* force subdriver init of size*/ //syslog(LOG_INFO,"psd->planes:%d,psd->bpp:%d,psd->ncolors:%d,psd->linelen:%d", psd->planes,psd->bpp,psd->ncolors,psd->linelen); psd->flags = PSF_SCREEN | PSF_HAVEBLIT; if (psd->bpp == 16) psd->flags |= PSF_HAVEOP_COPY; /* set pixel format*/ if(visual == FB_VISUAL_TRUECOLOR || visual == FB_VISUAL_DIRECTCOLOR) { switch(psd->bpp) { case 8: psd->pixtype = PF_TRUECOLOR332; break; case 16: psd->pixtype = PF_TRUECOLOR565; break; case 24: psd->pixtype = PF_TRUECOLOR888; break; case 32: psd->pixtype = PF_TRUECOLOR0888; break; default: fprintf(stderr, "GAL ENGINE: Unsupported FrameBuffer\n"); goto fail; } } else psd->pixtype = PF_PALETTE; /* select a framebuffer subdriver based on planes and bpp*/ subdriver = select_fb_subdriver(psd); if (!subdriver) { fprintf(stderr,"GAL ENGINE: No driver for screen type %d visual %d bpp %d\n", type, visual, psd->bpp); goto fail; } /* * set and initialize subdriver into screen driver * psd->size is calculated by subdriver init */ if(!set_subdriver(psd, subdriver, TRUE)) { fprintf(stderr,"GAL ENGINE: Driver initialize failed type %d visual %d bpp %d\n", type, visual, psd->bpp); goto fail; }#if HAVETEXTMODE /* open tty, enter graphics mode*/ tty = open ("/dev/tty", O_RDWR); if(tty < 0) { fprintf(stderr,"GAL ENGINE: Can't open /dev/tty: %m\n"); goto fail; } if(ioctl (tty, KDSETMODE, KD_GRAPHICS) == -1) { fprintf(stderr,"GAL ENGINE: Error when setting graphics mode: %m\n"); fprintf(stderr,"GAL ENGINE: Maybe not a virtual console.\n"); close(tty); goto fail; } close(tty);#endif /* mmap framebuffer into this address space*/ psd->size = (psd->size + getpagesize () - 1) / getpagesize () * getpagesize (); psd->addr = mmap(NULL, psd->size, PROT_READ|PROT_WRITE,MAP_SHARED,fb,0); if(psd->addr == NULL || psd->addr == (unsigned char *)-1) { fprintf(stderr,"GAL ENGINE: Error when mmaping %s: %m\n", env); goto fail; } /* save original palette*/ ioctl_getpalette(0, 16, saved_red, saved_green, saved_blue); status = 2; psd->gr_mode = MODE_SET; return psd; /* success*/fail:#if HAVETEXTMODE /* enter text mode*/ tty = open ("/dev/tty", O_RDWR); ioctl(tty, KDSETMODE, KD_TEXT); close(tty);#endif close(fb); return NULL;}/* close framebuffer*/static void fb_close(PSD psd){#if HAVETEXTMODE int tty;#endif /* if not opened, return*/ if(status != 2) return; status = 1; /* reset hw palette*/ ioctl_setpalette(0, 16, saved_red, saved_green, saved_blue); /* unmap framebuffer*/ munmap(psd->addr, psd->size); #if HAVETEXTMODE /* enter text mode*/ tty = open ("/dev/tty", O_RDWR); ioctl(tty, KDSETMODE, KD_TEXT); close(tty);#endif /* close framebuffer*/ close(fb);}static int fade = 100;static void fb_setpalette(PSD psd,int first, int count, gal_color *palette){ int i; unsigned short red[count]; unsigned short green[count]; unsigned short blue[count]; /* convert palette to framebuffer format*/ for(i=0; i < count; i++) { gal_color *p = &palette[i]; /* grayscale computation: * red[i] = green[i] = blue[i] = * (p->r * 77 + p->g * 151 + p->b * 28); */ red[i] = (p->r * fade / 100) << 8; green[i] = (p->g * fade / 100) << 8; blue[i] = (p->b * fade / 100) << 8; } ioctl_setpalette(first, count, red, green, blue);}static void fb_getpalette(PSD psd,int first, int count, gal_color *palette){ int i; unsigned short red[count]; unsigned short green[count]; unsigned short blue[count]; ioctl_getpalette(first,count,red,green,blue); for(i=0; i < count; i++) { gal_color *p = &palette[i]; /* grayscale computation: * red[i] = green[i] = blue[i] = * (p->r * 77 + p->g * 151 + p->b * 28); */ p->r = (red[i] >> 8) * 100 / fade; p->g = (green[i] >>8) * 100 / fade; p->b = (blue[i] >>8) * 100 / fade; }}/* get framebuffer palette*/void ioctl_getpalette(int start, int len, short *red, short *green, short *blue){ struct fb_cmap cmap; cmap.start = start; cmap.len = len; cmap.red = red; cmap.green = green; cmap.blue = blue; cmap.transp = NULL; ioctl(fb, FBIOGETCMAP, &cmap);}/* set framebuffer palette*/void ioctl_setpalette(int start, int len, short *red, short *green, short *blue){ struct fb_cmap cmap; cmap.start = start; cmap.len = len; cmap.red = red; cmap.green = green; cmap.blue = blue; cmap.transp = NULL; ioctl(fb, FBIOPUTCMAP, &cmap);}SCREENDEVICE scrdev = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, fb_open, fb_close, fb_setpalette, fb_getpalette, native_gen_allocatememgc, fb_mapmemgc, native_gen_freememgc, native_gen_clippoint, native_gen_fillrect, NULL, /* DrawPixel subdriver*/ NULL, /* ReadPixel subdriver*/ NULL, /* DrawHLine subdriver*/ NULL, /* PutHLine subdriver*/ NULL, /* GetHLine subdriver*/ NULL, /* DrawVLine subdriver*/ NULL, /* PutVLine subdriver*/ NULL, /* GetVLine subdriver*/ NULL, /* Blit subdriver*/ NULL, /* PutBox subdriver*/ NULL /* CopyBox subdriver*/};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -