📄 hazy_moon.c
字号:
/*************************************************************************** * hazy_moon.c * * Provide a demonstration of the framebuffer interface. * * See /usr/include/linux/fb.h for framebuffer data structures. ***************************************************************************/#include <stdio.h>#include <fcntl.h>#include <linux/fb.h>#include <linux/vt.h> /* For the virtual terminal interface. */#include <sys/mman.h> /* For mmap. */#include <asm/types.h> /* Provides __u8, __s8, etc. */#include "colormap.h"#define FB_DEVICE "/dev/fb0"#define MAX_COLORMAP_SIZE 256#define GFX_VT_NUMBER 7 /* Number of virtual terminal for graphics *//* Private functions for displaying framebuffer configuration data */static void display_fb_fixed_info(struct fb_fix_screeninfo *fixed_info);static void display_fb_var_info(struct fb_var_screeninfo *var_info);static void display_fb_colormap(struct fb_cmap *cmap);int main(int argc, char *argv[]){ int console_fd, fb_fd; int entry, row, col, img_off; struct fb_fix_screeninfo fixed_info; struct fb_var_screeninfo var_info; struct fb_cmap *new_cmap; unsigned char *frame, *visible_frame; /* pointers to video memory */ /* Change to the virtual terminal we use for graphics. */ console_fd = open("/dev/tty", O_RDWR); if(!console_fd){ printf("Error: cannot open /dev/tty"); exit(1); } ioctl(console_fd,VT_ACTIVATE,GFX_VT_NUMBER); ioctl(console_fd,VT_WAITACTIVE,GFX_VT_NUMBER); /* Access parameters that describe this framebuffer. */ fb_fd = open(FB_DEVICE, O_RDWR); if(!fb_fd){ printf("Error: cannot open framebuffer device %s.\n", FB_DEVICE); exit(1); } /* Fixed Info */ if(ioctl(fb_fd, FBIOGET_FSCREENINFO, &fixed_info) == -1){ printf("Error: cannot access fixed framebuffer info.\n"); exit(2); } else{ display_fb_fixed_info(&fixed_info); } /* Variable Info */ if(ioctl(fb_fd, FBIOGET_VSCREENINFO, &var_info) == -1){ printf("Error: cannot access variable framebuffer info.\n"); exit(3); } else{ display_fb_var_info(&var_info); } /* Colormap */ new_cmap = new_fb_cmap(MAX_COLORMAP_SIZE); if(new_cmap == NULL){ printf("Unable to allocate colormap.\n"); goto cleanup; } /* Initialize the new colormap with the old values. */ if(ioctl(fb_fd, FBIOGETCMAP, new_cmap) == -1){ printf("Unable to initialize new colormap.\n"); goto cleanup; } /* Fill in the new colormap with a gradient. */ /* We'll leave the first 16 colors intact for the linux console. */ if(new_cmap->len >= 256){ for(entry=16; entry < 256; entry++){ /* Fade from bright cyan to dark red. Colormap values are 16 bit. */ new_cmap->red[entry] = 64*256; new_cmap->green[entry] = entry * 256 +255; new_cmap->blue[entry] = entry * 256 +255; } } if(ioctl(fb_fd, FBIOPUTCMAP, new_cmap) == -1){ printf("Colormap update failed.\n"); goto cleanup; } /* Provide a memory map for the frame buffer. */ frame = mmap(0, fixed_info.smem_len, PROT_READ|PROT_WRITE, MAP_SHARED, fb_fd, 0); if(frame == (unsigned char *) -1){ printf("Unable to mmap framebuffer.\n"); goto cleanup; } /* Demonstrate the frame buffer. The offsets provide the position of the * visible frame within the virtual frame. */ visible_frame = frame + var_info.yoffset*fixed_info.line_length + var_info.xoffset; /* Paint the top and bottom borders of the visible display. */ for(col=0; col<var_info.xres; col++){ visible_frame[col] = 255; visible_frame[(var_info.yres-1)*fixed_info.line_length + col] = 255; } /* Paint the left and right borders of the visible display. */ for(row=0; row<var_info.yres; row++){ visible_frame[row*fixed_info.line_length] = 255; visible_frame[row*fixed_info.line_length + var_info.xres - 1] = 255; } /* Compute the offset for a 240x240 square in the middle of the screen. */ img_off = ((var_info.yres-240)*fixed_info.line_length + (var_info.xres-240))/2; /* Paint a 240x240 disk in the center of the visible screen. */ for(row = 0; row < 240; row++){ for(col = 0; col < 240; col++){ /* Only display points that lie within a circle. */ if((row-120)*(row-120) + (col-120)*(col-120) <= 120*120){ visible_frame[img_off + row*fixed_info.line_length + col] = 255 - row; } } }cleanup: munmap(frame, fixed_info.smem_len); free_fb_cmap(new_cmap); return 0;}static void display_fb_fixed_info(struct fb_fix_screeninfo *fixed_info){ /* Here are the values for type and visual (from "fb.h"). */ char type[5][20] = {"PACKED_PIXELS", "PLANES", "INTERLEAVED_PLANES", "TEXT", "VGA_PLANES"}; char visual[6][20] = {"MONO01", "MONO10", "TRUECOLOR", "PSEUDOCOLOR", "DIRECTCOLOR", "STATIC_PSEUDOCOLOR"}; /* Display fixed FB info. */ printf("\nFixed framebuffer info\n" " id: %16s" " smem_start: 0x%08lX\n" " smem_len: %10ld" " type: %20s\n" " type_aux: %10lu" " visual: %20s\n" " xpanstep: %10hu" " ypanstep: %10hu\n" " ywrapstep: %10hu" " line_length: %10lu\n" " mmio_start: 0x%08lx" " mmio_len: %10ld\n" " accel: %10hu\n", fixed_info->id, /* identification string eg "TT Builtin" */ fixed_info->smem_start, /* Start of frame buffer mem */ fixed_info->smem_len, /* Length of frame buffer mem */ type[fixed_info->type], /* see FB_TYPE_* */ fixed_info->type_aux, /* Interleave for interleaved Planes */ visual[fixed_info->visual], /* see FB_VISUAL_* */ fixed_info->xpanstep, /* zero if no hardware panning */ fixed_info->ypanstep, /* zero if no hardware panning */ fixed_info->ywrapstep, /* zero if no hardware ywrap */ fixed_info->line_length, /* length of a line in bytes */ fixed_info->mmio_start, /* Start of Memory Mapped I/O (phys addr) */ fixed_info->mmio_len, /* Length of Memory Mapped I/O */ fixed_info->accel); /* Indicate to driver the specific chip/card */}static void display_fb_var_info(struct fb_var_screeninfo *var_info){ /* Display interesting, variable FB info. */ /* (We break up format strings for C89 compiler compliance.) */ printf("\nVariable framebuffer info\n" " xres: %10lu" " yres: %10lu\n" " xres_virtual: %10lu" " yres_virtual: %10lu\n" " xoffset: %10lu" " yoffset: %10lu\n" " bits_per_pixel: %10lu" " grayscale: %10lu\n", var_info->xres, /* visible resolution*/ var_info->yres, var_info->xres_virtual, /* virtual resolution*/ var_info->yres_virtual, var_info->xoffset, /* offset from virtual to visible */ var_info->yoffset, /* resolution*/ var_info->bits_per_pixel, /* guess what*/ var_info->grayscale); /* != 0 Graylevels instead of colors */ /* Transpose these, for better readability with a column display. */ printf(" red.offset: %10lu" " green.offset: %10lu\n" " red.length: %10lu" " green.length: %10lu\n" " red.msb_right: %10lu" " green.msb_right: %10lu\n" " blue.offset: %10lu" " transp.offset: %10lu\n" " blue.length: %10lu" " transp.length: %10lu\n" " blue.msb_right: %10lu" " transp.msb_right: %10lu\n", var_info->red.offset, /* bitfield in fb mem if true color, */ var_info->green.offset, var_info->red.length, /* else only length is significant */ var_info->green.length, var_info->red.msb_right, var_info->green.msb_right, var_info->blue.offset, var_info->transp.offset, var_info->blue.length, var_info->transp.length, var_info->blue.msb_right, var_info->transp.msb_right); printf(" nonstd: %10lu" " activate: %10lu\n" " height: %10ld" " width: %10ld\n" " accel_flags: %10lu" " pixclock: %10lu\n" " left_margin: %10lu" " right_margin: %10lu\n" " upper_margin: %10lu" " lower_margin: %10lu\n" " hsync_len: %10lu" " vsync_len: %10lu\n" " sync: %10lu" " vmode: %10lu\n" " rotate: %10lu\n", var_info->nonstd, /* != 0 Non standard pixel format */ var_info->activate, /* see FB_ACTIVATE_**/ var_info->height, /* height of picture in mm */ var_info->width, /* width of picture in mm */ var_info->accel_flags, /* (OBSOLETE) see fb_info->flags */ var_info->pixclock, /* pixel clock in ps (pico seconds) */ var_info->left_margin, /* time from sync to picture*/ var_info->right_margin, /* time from picture to sync*/ var_info->upper_margin, /* time from sync to picture*/ var_info->lower_margin, var_info->hsync_len, /* length of horizontal sync*/ var_info->vsync_len, /* length of vertical sync*/ var_info->sync, /* see FB_SYNC_**/ var_info->vmode, /* see FB_VMODE_**/ var_info->rotate); /* angle we rotate counter clockwise */}static void display_fb_colormap(struct fb_cmap *cmap){ int entry; printf("\nColor map\n" " start: %10lu" " len: %10lu\n", cmap->start, /* First entry*/ cmap->len); /* Number of entries */ printf(" Red Green Blue\n"); for(entry=0; entry < cmap->len; entry++){ printf(" %6u %6u %6u\n", cmap->red[entry], cmap->green[entry], cmap->blue[entry]); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -