{"id":1019,"date":"2019-07-19T09:55:09","date_gmt":"2019-07-19T01:55:09","guid":{"rendered":"http:\/\/www.max-shu.com\/blog\/?p=1019"},"modified":"2019-07-19T09:55:09","modified_gmt":"2019-07-19T01:55:09","slug":"linux%e4%b8%8b%e7%9b%b4%e6%8e%a5%e5%86%99framebufferfb0%e7%9a%84%e6%96%b9%e5%bc%8f%e6%98%be%e7%a4%babmp%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"http:\/\/www.max-shu.com\/blog\/?p=1019","title":{"rendered":"linux\u4e0b\u76f4\u63a5\u5199framebuffer(fb0)\u7684\u65b9\u5f0f\u663e\u793abmp\u56fe\u50cf"},"content":{"rendered":"<p>linux\u4e0b\u7684\u663e\u793a\u8bbe\u5907\u5c31\u662f\/dev\/fb0\uff0c\u5f80\u8be5\u8bbe\u5907\u5199\u5165\u7684\u6570\u636e\u4f1a\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5199frame buffer\u8fd9\u4e2a\/dev\/fb0\u8bbe\u5907\u6765\u5b9e\u73b0bmp\u56fe\u50cf\u7684\u663e\u793a\uff0c\u800c\u4e0d\u7528\u7ba1\u662f\u5728shell\u6587\u672c\u65b9\u5f0f\u4e0b\u8fd8\u662f\u5728\u5176\u4ed6gnome\u3001qt\u3001gtk\u3001wayland\u7b49\u56fe\u5f62\u6a21\u5f0f\u4e0b\uff0c\u90fd\u80fd\u663e\u793a\u51fa\u6765\u3002\u5f53\u524d\u524d\u63d0\u662f\u4f60\u7684linux\u4e0b\u5fc5\u987b\u5177\u6709\u8be5\u8bbe\u5907\u5e76\u652f\u6301\u8bfb\u5199\uff08\u65e0\u7279\u6b8a\u5904\u7406\u7684linux\u90fd\u6709\u8be5\u8bbe\u5907\uff09\u3002<\/p>\n<p>\u4ee3\u7801\uff08\u652f\u630116\u4f4d\u300124\u4f4d\u621632\u4f4d\u989c\u8272\u6df1\u5ea6\u7684bmp\u56fe\u50cf\uff0c\u652f\u630124\u621632\u4f4d\u989c\u8272\u6df1\u5ea6\u7684fb0\u8bbe\u5907\uff09\uff1a<\/p>\n<div>\n<div>#include &lt;unistd.h&gt;<\/div>\n<div>#include &lt;stdio.h&gt;<\/div>\n<div>#include &lt;stdlib.h&gt;<\/div>\n<div>#include &lt;fcntl.h&gt;<\/div>\n<div>#include &lt;string.h&gt;<\/div>\n<div>#include &lt;linux\/fb.h&gt;<\/div>\n<div>#include &lt;sys\/mman.h&gt;<\/div>\n<div>#include &lt;sys\/ioctl.h&gt;<\/div>\n<div>#include &lt;errno.h&gt;<\/div>\n<div>\/\/14byte\u6587\u4ef6\u5934<\/div>\n<div>typedef struct<\/div>\n<div>{<\/div>\n<div>charcfType[2];\/\/\u6587\u4ef6\u7c7b\u578b\uff0c&#8221;BM&#8221;(0x4D42)<\/div>\n<div>int cfSize;\/\/\u6587\u4ef6\u5927\u5c0f\uff08\u5b57\u8282\uff09<\/div>\n<div>int cfReserved;\/\/\u4fdd\u7559\uff0c\u503c\u4e3a0<\/div>\n<div>int cfoffBits;\/\/\u6570\u636e\u533a\u76f8\u5bf9\u4e8e\u6587\u4ef6\u5934\u7684\u504f\u79fb\u91cf\uff08\u5b57\u8282\uff09<\/div>\n<div>}__attribute__((packed)) BITMAPFILEHEADER;<\/div>\n<div>\/\/__attribute__((packed))\u7684\u4f5c\u7528\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u53d6\u6d88\u7ed3\u6784\u5728\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u7684\u4f18\u5316\u5bf9\u9f50<\/div>\n<div>\/\/40byte\u4fe1\u606f\u5934<\/div>\n<div>typedef struct<\/div>\n<div>{<\/div>\n<div>charciSize[4];\/\/BITMAPFILEHEADER\u6240\u5360\u7684\u5b57\u8282\u6570<\/div>\n<div>int ciWidth;\/\/\u5bbd\u5ea6<\/div>\n<div>int ciHeight;\/\/\u9ad8\u5ea6<\/div>\n<div>charciPlanes[2];\/\/\u76ee\u6807\u8bbe\u5907\u7684\u4f4d\u5e73\u9762\u6570\uff0c\u503c\u4e3a1<\/div>\n<div>int ciBitCount;\/\/\u6bcf\u4e2a\u50cf\u7d20\u7684\u4f4d\u6570<\/div>\n<div>charciCompress[4];\/\/\u538b\u7f29\u8bf4\u660e<\/div>\n<div>charciSizeImage[4];\/\/\u7528\u5b57\u8282\u8868\u793a\u7684\u56fe\u50cf\u5927\u5c0f\uff0c\u8be5\u6570\u636e\u5fc5\u987b\u662f4\u7684\u500d\u6570<\/div>\n<div>charciXPelsPerMeter[4];\/\/\u76ee\u6807\u8bbe\u5907\u7684\u6c34\u5e73\u50cf\u7d20\u6570\/\u7c73<\/div>\n<div>charciYPelsPerMeter[4];\/\/\u76ee\u6807\u8bbe\u5907\u7684\u5782\u76f4\u50cf\u7d20\u6570\/\u7c73<\/div>\n<div>charciClrUsed[4]; \/\/\u4f4d\u56fe\u4f7f\u7528\u8c03\u8272\u677f\u7684\u989c\u8272\u6570<\/div>\n<div>charciClrImportant[4]; \/\/\u6307\u5b9a\u91cd\u8981\u7684\u989c\u8272\u6570\uff0c\u5f53\u8be5\u57df\u7684\u503c\u7b49\u4e8e\u989c\u8272\u6570\u65f6\uff08\u6216\u8005\u7b49\u4e8e0\u65f6\uff09\uff0c\u8868\u793a\u6240\u6709\u989c\u8272\u90fd\u4e00\u6837\u91cd\u8981<\/div>\n<div>}__attribute__((packed)) BITMAPINFOHEADER;<\/div>\n<div>static void FbUpdate(int fbfd, struct fb_var_screeninfo *vi) \/\/\u5c06\u8981\u6e32\u67d3\u7684\u56fe\u5f62\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u7ed8\u5236\u5230\u8bbe\u5907\u663e\u793a\u5c4f\u6765<\/div>\n<div>{<\/div>\n<div>vi-&gt;yoffset=0; \/\/from 0 to yres will be display.<\/div>\n<div>ioctl(fbfd, FBIOPUT_VSCREENINFO, vi);<\/div>\n<div>}<\/div>\n<div>static int CursorBitmapFormatConvert(char *dst, char *src, int screenXres, int screenYres, int bytes_per_pixel_screen,<\/div>\n<div>intbmpWidth, intbmpHeight, intbytes_per_pixel_bitmap)<\/div>\n<div>{<\/div>\n<div>int i ,j ;<\/div>\n<div>char*psrc = src ;<\/div>\n<div>char*pdst = dst;<\/div>\n<div>char*p = psrc;<\/div>\n<div>int oldi =0;<\/div>\n<div>int left_right = (screenXres-bmpWidth)\/2;<\/div>\n<div>int top_bottom = (screenYres-bmpHeight)\/2;<\/div>\n<div>printf(&#8220;left_right: %d, top_bottom: %d\\n&#8221;, left_right, top_bottom);<\/div>\n<div>printf(&#8220;screenXres: %d, screenYres: %d\\n&#8221;, screenXres, screenYres);<\/div>\n<div>printf(&#8220;bmpWidth: %d, bmpHeight: %d\\n&#8221;, bmpWidth, bmpHeight);<\/div>\n<div>if(left_right &lt;0&amp;&amp; top_bottom &lt;0){<\/div>\n<div>printf(&#8220;error: BMP is too big, screen X: %d, Y: %d, bmp width: %d, height: %d\\n&#8221;, screenXres, screenYres, bmpWidth, bmpHeight);<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>\/* \u7531\u4e8ebmp\u5b58\u50a8\u662f\u4ece\u540e\u9762\u5f80\u524d\u9762\uff0c\u6240\u4ee5\u9700\u8981\u5012\u5e8f\u8fdb\u884c\u8f6c\u6362 *\/<\/div>\n<div>pdst += (screenXres * bmpHeight * bytes_per_pixel_screen);<\/div>\n<div>pdst += (screenXres * top_bottom * bytes_per_pixel_screen); \/\/move to middle bmpHeight<\/div>\n<div>for(i=0; i&lt;bmpHeight; i++){<\/div>\n<div>p = psrc + (i+1) * bmpWidth * bytes_per_pixel_bitmap;<\/div>\n<div>if(i ==0)<\/div>\n<div>pdst -= (left_right * bytes_per_pixel_screen); \/\/move to middle bmpWidth<\/div>\n<div>else<\/div>\n<div>pdst -= (left_right * 2 * bytes_per_pixel_screen); \/\/move to middle bmpWidth<\/div>\n<div>for(j=0; j&lt;bmpWidth; j++){<\/div>\n<div>pdst -= bytes_per_pixel_screen;<\/div>\n<div>p -= bytes_per_pixel_bitmap;<\/div>\n<div>int kk =0;<\/div>\n<div>int k;<\/div>\n<div>for(k=0; k&lt;bytes_per_pixel_screen; k++){<\/div>\n<div>if(kk &gt;= bytes_per_pixel_bitmap)<\/div>\n<div>pdst[k] =255; \/\/ 3: alpha color<\/div>\n<div>else<\/div>\n<div>pdst[k] =p[kk]; \/\/ 0: blue, 1: green, 2: red<\/div>\n<div>kk++;<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div>pdst -= (left_right * bytes_per_pixel_screen); \/\/ move to 0 position<\/div>\n<div>return0;<\/div>\n<div>}<\/div>\n<div>int ShowBmp(char *path, int fbfd, struct fb_var_screeninfo *vinfo, char *fbp)<\/div>\n<div>{<\/div>\n<div>int i;<\/div>\n<div>FILE *fp;<\/div>\n<div>int rc;<\/div>\n<div>int line_x, line_y;<\/div>\n<div>longint location =0, BytesPerLine =0;<\/div>\n<div>char*bmp_buf =NULL;<\/div>\n<div>char* buf =NULL;<\/div>\n<div>int flen =0;<\/div>\n<div>int ret =-1;<\/div>\n<div>int total_length =0;<\/div>\n<div>BITMAPFILEHEADER FileHead;<\/div>\n<div>BITMAPINFOHEADER InfoHead;<\/div>\n<div>int width, height;<\/div>\n<div>printf(&#8220;into ShowBmp function\\n&#8221;);<\/div>\n<div>if(path ==NULL) {<\/div>\n<div>printf(&#8220;path Error,return\\n&#8221;);<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>printf(&#8220;path = %s\\n&#8221;, path);<\/div>\n<div>fp = fopen( path, &#8220;rb&#8221; );<\/div>\n<div>if(fp ==NULL){<\/div>\n<div>printf(&#8220;load cursor file open failed\\n&#8221;);<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>\/* \u6c42\u89e3\u6587\u4ef6\u957f\u5ea6 *\/<\/div>\n<div>fseek(fp,0,SEEK_SET);<\/div>\n<div>fseek(fp,0,SEEK_END);<\/div>\n<div>flen = ftell(fp);<\/div>\n<div>printf(&#8220;flen is %d\\n&#8221;,flen);<\/div>\n<div>bmp_buf = (char*)calloc(1,flen &#8211; 54);<\/div>\n<div>if(bmp_buf ==NULL){<\/div>\n<div>printf(&#8220;load &gt; malloc bmp out of memory!\\n&#8221;);<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>\/* \u518d\u79fb\u4f4d\u5230\u6587\u4ef6\u5934\u90e8 *\/<\/div>\n<div>fseek(fp,0,SEEK_SET);<\/div>\n<div>rc = fread(&amp;FileHead, sizeof(BITMAPFILEHEADER),1, fp);<\/div>\n<div>if ( rc !=1) {<\/div>\n<div>printf(&#8220;read header error!\\n&#8221;);<\/div>\n<div>fclose( fp );<\/div>\n<div>return( -2 );<\/div>\n<div>}<\/div>\n<div>\/\/\u68c0\u6d4b\u662f\u5426\u662fbmp\u56fe\u50cf<\/div>\n<div>if (memcmp(FileHead.cfType, &#8220;BM&#8221;, 2) !=0) {<\/div>\n<div>printf(&#8220;it&#8217;s not a BMP file\\n&#8221;);<\/div>\n<div>fclose( fp );<\/div>\n<div>return( -3 );<\/div>\n<div>}<\/div>\n<div>rc = fread( (char *)&amp;InfoHead, sizeof(BITMAPINFOHEADER),1, fp );<\/div>\n<div>if ( rc !=1) {<\/div>\n<div>printf(&#8220;read infoheader error!\\n&#8221;);<\/div>\n<div>fclose( fp );<\/div>\n<div>return( -4 );<\/div>\n<div>}<\/div>\n<div>width = InfoHead.ciWidth;<\/div>\n<div>height = InfoHead.ciHeight;<\/div>\n<div>printf(&#8220;FileHead.cfSize =%d byte\\n&#8221;,FileHead.cfSize);<\/div>\n<div>printf(&#8220;flen = %d\\n&#8221;, flen);<\/div>\n<div>printf(&#8220;width = %d, height = %d\\n&#8221;, width, height);<\/div>\n<div>\/\/\u8df3\u8f6c\u7684\u6570\u636e\u533a<\/div>\n<div>fseek(fp, FileHead.cfoffBits, SEEK_SET);<\/div>\n<div>printf(&#8221; FileHead.cfoffBits = %d\\n&#8221;, FileHead.cfoffBits);<\/div>\n<div>printf(&#8221; InfoHead.ciBitCount = %d\\n&#8221;, InfoHead.ciBitCount);<\/div>\n<div>int bytes_per_pixel_bitmap =InfoHead.ciBitCount\/8;<\/div>\n<div>total_length = width * height * bytes_per_pixel_bitmap;<\/div>\n<div>printf(&#8220;total_length = %d\\n&#8221;, total_length);<\/div>\n<div>\/\/\u6bcf\u884c\u5b57\u8282\u6570<\/div>\n<div>buf = bmp_buf;<\/div>\n<div>while ((ret =fread(buf,1,total_length,fp)) &gt;=0) {<\/div>\n<div>if (ret ==0) {<\/div>\n<div>usleep(100);<\/div>\n<div>continue;<\/div>\n<div>}<\/div>\n<div>printf(&#8220;ret = %d\\n&#8221;, ret);<\/div>\n<div>buf = ((char*) buf) + ret;<\/div>\n<div>total_length = total_length &#8211; ret;<\/div>\n<div>if(total_length ==0)<\/div>\n<div>break;<\/div>\n<div>}<\/div>\n<div>CursorBitmapFormatConvert(fbp, bmp_buf, (*vinfo).xres, (*vinfo).yres, (*vinfo).bits_per_pixel\/8, width, height, bytes_per_pixel_bitmap);<\/div>\n<div>free(bmp_buf);<\/div>\n<div>fclose(fp);<\/div>\n<div>printf(&#8220;show logo return 0\\n&#8221;);<\/div>\n<div>return0;<\/div>\n<div>}<\/div>\n<div>int ShowPicture(int fbfd, char *path)<\/div>\n<div>{<\/div>\n<div>struct fb_var_screeninfo vinfo;<\/div>\n<div>struct fb_fix_screeninfo finfo;<\/div>\n<div>longint screensize =0;<\/div>\n<div>struct fb_bitfield red;<\/div>\n<div>struct fb_bitfield green;<\/div>\n<div>struct fb_bitfield blue;<\/div>\n<div>\/\/\u6253\u5f00\u663e\u793a\u8bbe\u5907<\/div>\n<div>printf(&#8220;fbfd = %d\\n&#8221;, fbfd);<\/div>\n<div>if (fbfd ==-1) {<\/div>\n<div>\/\/printf(&#8220;Error opening frame buffer errno=%d (%s)\\n&#8221;,errno, strerror(errno));<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>if (ioctl(fbfd, FBIOGET_FSCREENINFO, &amp;finfo)) {<\/div>\n<div>\/\/printf(&#8220;Error\uff1areading fixed information.\\n&#8221;);<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>if (ioctl(fbfd, FBIOGET_VSCREENINFO, &amp;vinfo)) {<\/div>\n<div>\/\/printf(&#8220;Error: reading variable information.\\n&#8221;);<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>\/\/printf(&#8220;R:%x ;G:%d ;B:%d \\n&#8221;, (int)vinfo.red, vinfo.green, vinfo.blue );<\/div>\n<div>printf(&#8220;%dx%d, %dbpp\\n&#8221;, vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );<\/div>\n<div>\/\/\u8ba1\u7b97\u5c4f\u5e55\u7684\u603b\u5927\u5c0f\uff08\u5b57\u8282\uff09<\/div>\n<div>screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel \/ 8;<\/div>\n<div>printf(&#8220;screensize=%ld byte\\n&#8221;,screensize);<\/div>\n<div>\/\/\u5bf9\u8c61\u6620\u5c04<\/div>\n<div>char*fbp = (char*)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);<\/div>\n<div>if (fbp == (char*)-1) {<\/div>\n<div>printf(&#8220;Error: failed to map framebuffer device to memory.\\n&#8221;);<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>printf(&#8220;sizeof file header=%ld\\n&#8221;, sizeof(BITMAPFILEHEADER));<\/div>\n<div>\/\/\u663e\u793a\u56fe\u50cf<\/div>\n<div>ShowBmp(path, fbfd, &amp;vinfo, fbp);<\/div>\n<div>FbUpdate(fbfd, &amp;vinfo);<\/div>\n<div>\/\/\/\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u591a\u4e45<\/div>\n<div>sleep(10);<\/div>\n<div>\/\/\u5220\u9664\u5bf9\u8c61\u6620\u5c04<\/div>\n<div>munmap(fbp, screensize);<\/div>\n<div>return0;<\/div>\n<div>}<\/div>\n<div>int main()<\/div>\n<div>{<\/div>\n<div>int fbfd =0;<\/div>\n<div>fbfd = open(&#8220;\/dev\/fb0&#8221;, O_RDWR);<\/div>\n<div>if (!fbfd) {<\/div>\n<div>printf(&#8220;Error: cannot open framebuffer device.\\n&#8221;);<\/div>\n<div>return-1;<\/div>\n<div>}<\/div>\n<div>ShowPicture(fbfd, &#8220;\/tmp\/license_ckx.bmp&#8221;);<\/div>\n<div>close(fbfd);<\/div>\n<div>return0;<\/div>\n<div>}<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>linux\u4e0b\u7684\u663e\u793a\u8bbe\u5907\u5c31\u662f\/dev\/fb0\uff0c\u5f80\u8be5\u8bbe\u5907\u5199\u5165\u7684\u6570\u636e\u4f1a\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5199frame &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[748,746,747,749,750],"class_list":["post-1019","post","type-post","status-publish","format-standard","hentry","category-linuxandroid","tag-bmp","tag-fb0","tag-frame-buffer","tag-ioctl","tag-750"],"views":4215,"_links":{"self":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1019"}],"version-history":[{"count":1,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1019\/revisions"}],"predecessor-version":[{"id":1020,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1019\/revisions\/1020"}],"wp:attachment":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1019"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}