├── 386 ├── fbgrab ├── fbshow └── fbtouch ├── .gitignore ├── COPYING ├── Makefile ├── README ├── amd64 ├── fbgrab ├── fbshow └── fbtouch ├── arm ├── fbgrab ├── fbshow └── fbtouch ├── fbgrab.go ├── fbshow.go ├── fbtouch.go └── src └── framebuffer └── framebuffer.go /.gitignore: -------------------------------------------------------------------------------- 1 | *.png 2 | .*.swp 3 | -------------------------------------------------------------------------------- /386/fbgrab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/386/fbgrab -------------------------------------------------------------------------------- /386/fbshow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/386/fbshow -------------------------------------------------------------------------------- /386/fbtouch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/386/fbtouch -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | .PHONY: all 3 | all: $(addsuffix /fbshow, 386 amd64 arm) $(addsuffix /fbgrab, 386 amd64 arm) $(addsuffix /fbtouch, 386 amd64 arm) 4 | 5 | %/fbgrab: fbgrab.go 6 | GOARCH=$(@D) GOPATH=$(CURDIR) go build -ldflags -s -o $@ $< 7 | 8 | %/fbshow: fbshow.go 9 | GOARCH=$(@D) GOPATH=$(CURDIR) go build -ldflags -s -o $@ $< 10 | 11 | %/fbtouch: fbtouch.go 12 | GOARCH=$(@D) GOPATH=$(CURDIR) go build -ldflags -s -o $@ $< 13 | 14 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | framebuffer - access Linux framebuffer as draw.Image 3 | Written in 2016 by 4 | To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. 5 | You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . 6 | 7 | There are precompiled, static binaries for 386, amd64 and arm 8 | 9 | This will create an fbgrab.png file from /dev/fb0: 10 | 11 | # sudo amd64/fbgrab 12 | 13 | This will show an image file on /dev/fb0: 14 | 15 | # sudo amd64/fbshow file 16 | 17 | This will visualize touchscreen events from /dev/input/event0 on /dev/fb0: 18 | 19 | # sudo amd64/fbtouch /dev/input/event0 20 | 21 | -------------------------------------------------------------------------------- /amd64/fbgrab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/amd64/fbgrab -------------------------------------------------------------------------------- /amd64/fbshow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/amd64/fbshow -------------------------------------------------------------------------------- /amd64/fbtouch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/amd64/fbtouch -------------------------------------------------------------------------------- /arm/fbgrab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/arm/fbgrab -------------------------------------------------------------------------------- /arm/fbshow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/arm/fbshow -------------------------------------------------------------------------------- /arm/fbtouch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xdsopl/framebuffer/ed6572609f8ce388373ce70aa7b6ac077d48adc4/arm/fbtouch -------------------------------------------------------------------------------- /fbgrab.go: -------------------------------------------------------------------------------- 1 | /* 2 | fbgrab - grab PNG from Linux framebuffer 3 | Written in 2016 by 4 | To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. 5 | You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . 6 | */ 7 | 8 | package main 9 | 10 | import ( 11 | "os" 12 | "fmt" 13 | "image/png" 14 | "framebuffer" 15 | ) 16 | 17 | func die(err interface{}) { 18 | fmt.Println(err) 19 | os.Exit(1) 20 | } 21 | 22 | func main() { 23 | fb, err := framebuffer.Open("/dev/fb0") 24 | if err != nil { die(err) } 25 | name := "fbgrab.png" 26 | file, err := os.Create(name) 27 | if err != nil { die(err) } 28 | if err := png.Encode(file, fb); err != nil { die(err) } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /fbshow.go: -------------------------------------------------------------------------------- 1 | /* 2 | fbshow - show image on Linux framebuffer 3 | Written in 2016 by 4 | To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. 5 | You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . 6 | */ 7 | 8 | package main 9 | 10 | import ( 11 | "os" 12 | "fmt" 13 | "flag" 14 | "time" 15 | "image" 16 | "image/draw" 17 | _ "image/jpeg" 18 | _ "image/png" 19 | "image/gif" 20 | "framebuffer" 21 | ) 22 | 23 | func die(err interface{}) { 24 | fmt.Println(err) 25 | os.Exit(1) 26 | } 27 | 28 | func main() { 29 | flag.Parse() 30 | if len(flag.Args()) != 1 { die("usage: fbshow file") } 31 | name := flag.Args()[0] 32 | file, err := os.Open(name) 33 | if err != nil { die(err) } 34 | _, str, err := image.DecodeConfig(file) 35 | if err != nil { die(err) } 36 | _, err = file.Seek(0, 0) 37 | if err != nil { die(err) } 38 | fb, err := framebuffer.Open("/dev/fb0") 39 | if err != nil { die(err) } 40 | if str == "gif" { 41 | all, err := gif.DecodeAll(file) 42 | if err != nil { die(err) } 43 | acc := image.NewRGBA(all.Image[0].Bounds()) 44 | dst := acc.Bounds().Sub(acc.Bounds().Min).Add(fb.Bounds().Min).Add(fb.Bounds().Size().Sub(acc.Bounds().Size()).Div(2)) 45 | src := acc.Bounds().Min 46 | for { 47 | for idx, img := range all.Image { 48 | begin := time.Now() 49 | draw.Draw(acc, acc.Bounds(), img, src, draw.Over) 50 | draw.Draw(fb, dst, acc, src, draw.Src) 51 | time.Sleep(time.Duration(all.Delay[idx]) * 10 * time.Millisecond - time.Since(begin)) 52 | } 53 | } 54 | return 55 | } 56 | img, _, err := image.Decode(file) 57 | if err != nil { die(err) } 58 | dst := img.Bounds().Sub(img.Bounds().Min).Add(fb.Bounds().Min).Add(fb.Bounds().Size().Sub(img.Bounds().Size()).Div(2)) 59 | src := img.Bounds().Min 60 | draw.Draw(fb, dst, img, src, draw.Src) 61 | } 62 | 63 | -------------------------------------------------------------------------------- /fbtouch.go: -------------------------------------------------------------------------------- 1 | /* 2 | fbtouch - show touchscreen events on Linux framebuffer 3 | Written in 2017 by 4 | To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. 5 | You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . 6 | */ 7 | 8 | package main 9 | 10 | import ( 11 | "os" 12 | "fmt" 13 | "flag" 14 | "bytes" 15 | "errors" 16 | "unsafe" 17 | "syscall" 18 | "image" 19 | "image/draw" 20 | "image/color" 21 | "framebuffer" 22 | "encoding/binary" 23 | ) 24 | 25 | func die(err interface{}) { 26 | fmt.Println(err) 27 | os.Exit(1) 28 | } 29 | 30 | type InputEvent struct { 31 | Time syscall.Timeval 32 | Type, Code uint16 33 | Value int32 34 | } 35 | 36 | type InputAbsInfo struct { 37 | Value, Minimum, Maximum, Fuzz, Flat, Resolution int32 38 | } 39 | 40 | func EVIOCGABS(abs uintptr) uintptr { 41 | return 2149074240 + abs 42 | } 43 | 44 | func GetAbsInfo(ev *os.File, abs uintptr) (InputAbsInfo, error) { 45 | var tmp InputAbsInfo 46 | _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, ev.Fd(), EVIOCGABS(abs), uintptr(unsafe.Pointer(&tmp))) 47 | if errno != 0 { 48 | return tmp, &os.SyscallError{"SYS_IOCTL", errno} 49 | } 50 | return tmp, nil 51 | } 52 | 53 | const EVIOCGRAB = 1074021776 54 | 55 | func GrabDevice(ev *os.File) error { 56 | _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, ev.Fd(), EVIOCGRAB, 1) 57 | if errno != 0 { 58 | return &os.SyscallError{"SYS_IOCTL", errno} 59 | } 60 | return nil 61 | } 62 | 63 | const EventTypeSyn = 0x00 64 | const EventTypeKey = 0x01 65 | const EventTypeAbs = 0x03 66 | const EventCodeAbsX = 0x00 67 | const EventCodeAbsY = 0x01 68 | const EventCodeAbsMtSlot = 0x2f 69 | const EventCodeAbsMtPositionX = 0x35 70 | const EventCodeAbsMtPositionY = 0x36 71 | const EventCodeAbsMtTrackingId = 0x39 72 | const EventCodeBtnTouch = 0x14a 73 | 74 | type Position struct { 75 | X, Y int 76 | } 77 | 78 | func GetEvents(ev *os.File) ([]InputEvent, error) { 79 | const ieMax = 64 80 | const ieSize = int(unsafe.Sizeof(InputEvent{})) 81 | buf := make([]byte, ieMax * ieSize) 82 | n, err := ev.Read(buf) 83 | if err != nil { return nil, err } 84 | if n == 0 { return nil, nil } 85 | if n % ieSize != 0 { return nil, errors.New("partial read") } 86 | ieNum := n / ieSize 87 | iev := make([]InputEvent, ieNum) 88 | err = binary.Read(bytes.NewBuffer(buf[:n]), binary.LittleEndian, &iev) 89 | if err != nil { return nil, err } 90 | return iev, nil 91 | } 92 | 93 | func square(fb draw.Image, pos Position, col color.Color, radius int) { 94 | for i := -radius; i <= radius; i++ { 95 | fb.Set(pos.X+i, pos.Y-radius, col) 96 | fb.Set(pos.X+i, pos.Y+radius, col) 97 | fb.Set(pos.X-radius, pos.Y+i, col) 98 | fb.Set(pos.X+radius, pos.Y+i, col) 99 | } 100 | } 101 | 102 | func singleTouch(fb draw.Image, ev *os.File, absX, absY InputAbsInfo, ghost bool, radius int) { 103 | pos := Position{-1, -1} 104 | touching := false 105 | old := pos 106 | for { 107 | iev, err := GetEvents(ev) 108 | if err != nil { die(err) } 109 | for _, ie := range iev { 110 | switch ie.Type { 111 | case EventTypeSyn: 112 | if !ghost { 113 | square(fb, old, color.Black, radius) 114 | } 115 | old = pos 116 | if touching { 117 | square(fb, pos, color.White, radius) 118 | } 119 | case EventTypeKey: 120 | switch ie.Code { 121 | case EventCodeBtnTouch: 122 | if ie.Value == 0 { 123 | touching = false 124 | } else { 125 | touching = true 126 | } 127 | } 128 | case EventTypeAbs: 129 | switch ie.Code { 130 | case EventCodeAbsX: 131 | pos.X = fb.Bounds().Min.X + (fb.Bounds().Dx() * int(ie.Value - absX.Minimum)) / int(absX.Maximum - absX.Minimum) 132 | case EventCodeAbsY: 133 | pos.Y = fb.Bounds().Min.Y + (fb.Bounds().Dy() * int(ie.Value - absY.Minimum)) / int(absY.Maximum - absY.Minimum) 134 | } 135 | } 136 | } 137 | } 138 | } 139 | 140 | func multiTouch(fb draw.Image, ev *os.File, absX, absY, absN InputAbsInfo, ghost bool, radius int) { 141 | pos := make([]Position, absN.Maximum + 1) 142 | old := make([]Position, absN.Maximum + 1) 143 | copy(old, pos) 144 | touching := make([]bool, absN.Maximum + 1) 145 | n := int(absN.Value) 146 | for { 147 | iev, err := GetEvents(ev) 148 | if err != nil { die(err) } 149 | for _, ie := range iev { 150 | switch ie.Type { 151 | case EventTypeSyn: 152 | for i := absN.Minimum; i <= absN.Maximum; i++ { 153 | if !ghost { 154 | square(fb, old[i], color.Black, radius) 155 | } 156 | if touching[i] { 157 | square(fb, pos[i], color.White, radius) 158 | } 159 | } 160 | copy(old, pos) 161 | case EventTypeAbs: 162 | switch ie.Code { 163 | case EventCodeAbsMtSlot: 164 | if absN.Minimum <= ie.Value && ie.Value <= absN.Maximum { 165 | n = int(ie.Value) 166 | } 167 | case EventCodeAbsMtTrackingId: 168 | if ie.Value == -1 { 169 | touching[n] = false 170 | } else { 171 | touching[n] = true 172 | } 173 | case EventCodeAbsMtPositionX: 174 | pos[n].X = fb.Bounds().Min.X + (fb.Bounds().Dx() * int(ie.Value - absX.Minimum)) / int(absX.Maximum - absX.Minimum) 175 | case EventCodeAbsMtPositionY: 176 | pos[n].Y = fb.Bounds().Min.Y + (fb.Bounds().Dy() * int(ie.Value - absY.Minimum)) / int(absY.Maximum - absY.Minimum) 177 | } 178 | } 179 | } 180 | } 181 | } 182 | 183 | func main() { 184 | flag.Parse() 185 | if len(flag.Args()) < 1 || len(flag.Args()) > 3 { die("usage: fbtouch /dev/input/eventN [ghost|dots]") } 186 | name := flag.Args()[0] 187 | var ghost bool 188 | radius := 35 189 | if len(flag.Args()) == 2 { 190 | mode := flag.Args()[1] 191 | if mode == "ghost" || mode == "dots" { ghost = true } 192 | if mode == "dots" { radius = 1 } 193 | } 194 | ev, err := os.Open(name) 195 | if err != nil { die(err) } 196 | err = GrabDevice(ev) 197 | if err != nil { die(err) } 198 | absX, err := GetAbsInfo(ev, EventCodeAbsX) 199 | if err != nil { die(err) } 200 | absY, err := GetAbsInfo(ev, EventCodeAbsY) 201 | if err != nil { die(err) } 202 | fb, err := framebuffer.Open("/dev/fb0") 203 | if err != nil { die(err) } 204 | draw.Draw(fb, fb.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src) 205 | absN, err := GetAbsInfo(ev, EventCodeAbsMtSlot) 206 | if err != nil { 207 | singleTouch(fb, ev, absX, absY, ghost, radius) 208 | } else { 209 | multiTouch(fb, ev, absX, absY, absN, ghost, radius) 210 | } 211 | } 212 | 213 | -------------------------------------------------------------------------------- /src/framebuffer/framebuffer.go: -------------------------------------------------------------------------------- 1 | /* 2 | framebuffer - access Linux framebuffer as draw.Image 3 | Written in 2016 by 4 | To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. 5 | You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . 6 | */ 7 | 8 | package framebuffer 9 | 10 | import ( 11 | //"fmt" 12 | "os" 13 | "unsafe" 14 | "syscall" 15 | "image" 16 | "image/draw" 17 | "image/color" 18 | ) 19 | 20 | const FBIOGET_VSCREENINFO = 0x4600 21 | const FBIOPUT_VSCREENINFO = 0x4601 22 | const FBIOGET_FSCREENINFO = 0x4602 23 | const FB_TYPE_PACKED_PIXELS = 0 24 | const FB_VISUAL_TRUECOLOR = 2 25 | 26 | // dont worry about uintptr .. there is compatibility code in the kernel :( 27 | type FixScreenInfo struct { 28 | Id [16]byte 29 | Smem_start uintptr 30 | Smem_len, Type, Type_aux, Visual uint32 31 | Xpanstep, Ypanstep, Ywrapstep uint16 32 | Line_length uint32 33 | Mmio_start uintptr 34 | Mmio_len, Accel uint32 35 | Capabilities uint16 36 | Reserved [2]uint16 37 | } 38 | 39 | type BitField struct { 40 | Offset, Length, Msb_right uint32 41 | } 42 | 43 | type VarScreenInfo struct { 44 | Xres, Yres, 45 | Xres_virtual, Yres_virtual, 46 | Xoffset, Yoffset, 47 | Bits_per_pixel, Grayscale uint32 48 | Red, Green, Blue, Transp BitField 49 | Nonstd, Activate, 50 | Height, Width, 51 | Accel_flags, Pixclock, 52 | Left_margin, Right_margin, Upper_margin, Lower_margin, 53 | Hsync_len, Vsync_len, Sync, 54 | Vmode, Rotate, Colorspace uint32 55 | Reserved [4]uint32 56 | } 57 | 58 | type BGR565 struct { 59 | Pix []uint8 60 | Stride int 61 | Rect image.Rectangle 62 | } 63 | 64 | func (p *BGR565) Bounds() image.Rectangle { return p.Rect } 65 | func (p *BGR565) ColorModel() color.Model { return color.NRGBAModel } 66 | func (p *BGR565) PixOffset(x, y int) int { return y * p.Stride + x * 2 } 67 | 68 | func (p *BGR565) Set(x, y int, c color.Color) { 69 | if !(image.Point{x, y}.In(p.Rect)) { return } 70 | i := p.PixOffset(x, y) 71 | c1 := color.NRGBAModel.Convert(c).(color.NRGBA) 72 | p.Pix[i+0] = (c1.B >> 3) | ((c1.G >> 2) << 5) 73 | p.Pix[i+1] = (c1.G >> 5) | ((c1.R >> 3) << 3) 74 | } 75 | 76 | func (p *BGR565) At(x, y int) color.Color { 77 | if !(image.Point{x, y}.In(p.Rect)) { return color.NRGBA{} } 78 | i := p.PixOffset(x, y) 79 | return color.NRGBA{(p.Pix[i+1] >> 3) << 3, (p.Pix[i+1] << 5) | ((p.Pix[i+0] >> 5) << 2), p.Pix[i+0] << 3, 255} 80 | } 81 | 82 | type BGR struct { 83 | Pix []uint8 84 | Stride int 85 | Rect image.Rectangle 86 | } 87 | 88 | func (p *BGR) Bounds() image.Rectangle { return p.Rect } 89 | func (p *BGR) ColorModel() color.Model { return color.NRGBAModel } 90 | func (p *BGR) PixOffset(x, y int) int { return y * p.Stride + x * 3 } 91 | 92 | func (p *BGR) Set(x, y int, c color.Color) { 93 | if !(image.Point{x, y}.In(p.Rect)) { return } 94 | i := p.PixOffset(x, y) 95 | c1 := color.NRGBAModel.Convert(c).(color.NRGBA) 96 | p.Pix[i+0] = c1.B 97 | p.Pix[i+1] = c1.G 98 | p.Pix[i+2] = c1.R 99 | } 100 | 101 | func (p *BGR) At(x, y int) color.Color { 102 | if !(image.Point{x, y}.In(p.Rect)) { return color.NRGBA{} } 103 | i := p.PixOffset(x, y) 104 | return color.NRGBA{p.Pix[i+2], p.Pix[i+1], p.Pix[i+0], 255} 105 | } 106 | 107 | type BGR32 struct { 108 | Pix []uint8 109 | Stride int 110 | Rect image.Rectangle 111 | } 112 | 113 | func (p *BGR32) Bounds() image.Rectangle { return p.Rect } 114 | func (p *BGR32) ColorModel() color.Model { return color.NRGBAModel } 115 | func (p *BGR32) PixOffset(x, y int) int { return y * p.Stride + x * 4 } 116 | 117 | func (p *BGR32) Set(x, y int, c color.Color) { 118 | if !(image.Point{x, y}.In(p.Rect)) { return } 119 | i := p.PixOffset(x, y) 120 | c1 := color.NRGBAModel.Convert(c).(color.NRGBA) 121 | p.Pix[i+0] = c1.B 122 | p.Pix[i+1] = c1.G 123 | p.Pix[i+2] = c1.R 124 | } 125 | 126 | func (p *BGR32) At(x, y int) color.Color { 127 | if !(image.Point{x, y}.In(p.Rect)) { return color.NRGBA{} } 128 | i := p.PixOffset(x, y) 129 | return color.NRGBA{p.Pix[i+2], p.Pix[i+1], p.Pix[i+0], 255} 130 | } 131 | 132 | type NBGRA struct { 133 | Pix []uint8 134 | Stride int 135 | Rect image.Rectangle 136 | } 137 | 138 | func (p *NBGRA) Bounds() image.Rectangle { return p.Rect } 139 | func (p *NBGRA) ColorModel() color.Model { return color.NRGBAModel } 140 | func (p *NBGRA) PixOffset(x, y int) int { return y * p.Stride + x * 4 } 141 | 142 | func (p *NBGRA) Set(x, y int, c color.Color) { 143 | if !(image.Point{x, y}.In(p.Rect)) { return } 144 | i := p.PixOffset(x, y) 145 | c1 := color.NRGBAModel.Convert(c).(color.NRGBA) 146 | p.Pix[i+0] = c1.B 147 | p.Pix[i+1] = c1.G 148 | p.Pix[i+2] = c1.R 149 | p.Pix[i+3] = c1.A 150 | } 151 | 152 | func (p *NBGRA) At(x, y int) color.Color { 153 | if !(image.Point{x, y}.In(p.Rect)) { return color.NRGBA{} } 154 | i := p.PixOffset(x, y) 155 | return color.NRGBA{p.Pix[i+2], p.Pix[i+1], p.Pix[i+0], p.Pix[i+3]} 156 | } 157 | 158 | type UnsupportedError string 159 | func (e UnsupportedError) Error() string { return "framebuffer: " + string(e) } 160 | 161 | func Open(name string) (draw.Image, error) { 162 | file, err := os.OpenFile(name, os.O_RDWR, os.ModeDevice) 163 | if err != nil { return nil, err } 164 | var fixInfo FixScreenInfo 165 | if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FBIOGET_FSCREENINFO, uintptr(unsafe.Pointer(&fixInfo))); errno != 0 { 166 | return nil, &os.SyscallError{"SYS_IOCTL", errno} 167 | } 168 | if fixInfo.Type != FB_TYPE_PACKED_PIXELS { 169 | return nil, UnsupportedError("fixInfo.Type != FB_TYPE_PACKED_PIXELS") 170 | } 171 | if fixInfo.Visual != FB_VISUAL_TRUECOLOR { 172 | return nil, UnsupportedError("fixInfo.Visual != FB_VISUAL_TRUECOLOR") 173 | } 174 | var varInfo VarScreenInfo 175 | if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), FBIOGET_VSCREENINFO, uintptr(unsafe.Pointer(&varInfo))); errno != 0 { 176 | return nil, &os.SyscallError{"SYS_IOCTL", errno} 177 | } 178 | //fmt.Println("Red.Offset =", varInfo.Red.Offset, "Red.Length =", varInfo.Red.Length, "Red.Msb_right =", varInfo.Red.Msb_right) 179 | //fmt.Println("Green.Offset =", varInfo.Green.Offset, "Green.Length =", varInfo.Green.Length, "Green.Msb_right =", varInfo.Green.Msb_right) 180 | //fmt.Println("Blue.Offset =", varInfo.Blue.Offset, "Blue.Length =", varInfo.Blue.Length, "Blue.Msb_right =", varInfo.Blue.Msb_right) 181 | //fmt.Println("Transp.Offset =", varInfo.Transp.Offset, "Transp.Length =", varInfo.Transp.Length, "Transp.Msb_right =", varInfo.Transp.Msb_right) 182 | //fmt.Println("varInfo.Xres =", varInfo.Xres, "varInfo.Yres =", varInfo.Yres, "varInfo.Xoffset =", varInfo.Xoffset, "varInfo.Yoffset =", varInfo.Yoffset) 183 | mmap, err := syscall.Mmap(int(file.Fd()), 0, int(fixInfo.Smem_len), syscall.PROT_READ | syscall.PROT_WRITE, syscall.MAP_SHARED) 184 | if err != nil { return nil, err } 185 | switch varInfo.Bits_per_pixel { 186 | case 32: 187 | if varInfo.Blue.Length != 8 { return nil, UnsupportedError("varInfo.Blue.Length != 8") } 188 | if varInfo.Blue.Offset != 0 { return nil, UnsupportedError("varInfo.Blue.Offset != 0") } 189 | if varInfo.Green.Length != 8 { return nil, UnsupportedError("varInfo.Green.Length != 8") } 190 | if varInfo.Green.Offset != 8 { return nil, UnsupportedError("varInfo.Green.Offset != 8") } 191 | if varInfo.Red.Length != 8 { return nil, UnsupportedError("varInfo.Red.Length != 8") } 192 | if varInfo.Red.Offset != 16 { return nil, UnsupportedError("varInfo.Red.Offset != 16") } 193 | if varInfo.Transp.Length == 0 { 194 | return &BGR32{mmap, int(fixInfo.Line_length), image.Rect(0, 0, int(varInfo.Xres), int(varInfo.Yres)).Add(image.Point{int(varInfo.Xoffset), int(varInfo.Yoffset)})}, nil 195 | } else if varInfo.Transp.Length == 8 && varInfo.Transp.Offset == 24 { 196 | return &NBGRA{mmap, int(fixInfo.Line_length), image.Rect(0, 0, int(varInfo.Xres), int(varInfo.Yres)).Add(image.Point{int(varInfo.Xoffset), int(varInfo.Yoffset)})}, nil 197 | } 198 | case 24: 199 | if varInfo.Blue.Length != 8 { return nil, UnsupportedError("varInfo.Blue.Length != 8") } 200 | if varInfo.Blue.Offset != 0 { return nil, UnsupportedError("varInfo.Blue.Offset != 0") } 201 | if varInfo.Green.Length != 8 { return nil, UnsupportedError("varInfo.Green.Length != 8") } 202 | if varInfo.Green.Offset != 8 { return nil, UnsupportedError("varInfo.Green.Offset != 8") } 203 | if varInfo.Red.Length != 8 { return nil, UnsupportedError("varInfo.Red.Length != 8") } 204 | if varInfo.Red.Offset != 16 { return nil, UnsupportedError("varInfo.Red.Offset != 16") } 205 | if varInfo.Transp.Length != 0 { return nil, UnsupportedError("varInfo.Transp.Length != 0") } 206 | return &BGR{mmap, int(fixInfo.Line_length), image.Rect(0, 0, int(varInfo.Xres), int(varInfo.Yres)).Add(image.Point{int(varInfo.Xoffset), int(varInfo.Yoffset)})}, nil 207 | case 16: 208 | if varInfo.Blue.Length != 5 { return nil, UnsupportedError("varInfo.Blue.Length != 5") } 209 | if varInfo.Blue.Offset != 0 { return nil, UnsupportedError("varInfo.Blue.Offset != 0") } 210 | if varInfo.Green.Length != 6 { return nil, UnsupportedError("varInfo.Green.Length != 6") } 211 | if varInfo.Green.Offset != 5 { return nil, UnsupportedError("varInfo.Green.Offset != 5") } 212 | if varInfo.Red.Length != 5 { return nil, UnsupportedError("varInfo.Red.Length != 5") } 213 | if varInfo.Red.Offset != 11 { return nil, UnsupportedError("varInfo.Red.Offset != 11") } 214 | if varInfo.Transp.Length != 0 { return nil, UnsupportedError("varInfo.Transp.Length != 0") } 215 | return &BGR565{mmap, int(fixInfo.Line_length), image.Rect(0, 0, int(varInfo.Xres), int(varInfo.Yres)).Add(image.Point{int(varInfo.Xoffset), int(varInfo.Yoffset)})}, nil 216 | } 217 | return nil, UnsupportedError("unsupported pixel format") 218 | } 219 | 220 | --------------------------------------------------------------------------------