|
| 1 | +package framebuffer |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "syscall" |
| 6 | +) |
| 7 | + |
| 8 | +// FrameBuffer is a wrapper around [os.File] that provides convenience methods |
| 9 | +// for interacting with a frame buffer device. |
| 10 | +type FrameBuffer os.File |
| 11 | + |
| 12 | +// OpenFrameBuffer opens a frame buffer device. |
| 13 | +func OpenFrameBuffer(name string, flags int) (*FrameBuffer, error) { |
| 14 | + f, err := os.OpenFile(name, flags, os.ModeDevice) |
| 15 | + if err != nil { |
| 16 | + return nil, err |
| 17 | + } |
| 18 | + return (*FrameBuffer)(f), nil |
| 19 | +} |
| 20 | + |
| 21 | +// File converts the frame buffer back to a file. |
| 22 | +func (fb *FrameBuffer) File() *os.File { |
| 23 | + return (*os.File)(fb) |
| 24 | +} |
| 25 | + |
| 26 | +// Pixels returns a byte slice memory mapped to the frame buffer. |
| 27 | +func (fb *FrameBuffer) Pixels() ([]byte, error) { |
| 28 | + vi, err := fb.VarScreenInfo() |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + fd := fb.File().Fd() |
| 34 | + length := vi.XRes * vi.YRes * vi.BitsPerPixel / 8 |
| 35 | + flags := syscall.PROT_READ | syscall.PROT_WRITE |
| 36 | + return syscall.Mmap(int(fd), 0, int(length), flags, syscall.MAP_SHARED) |
| 37 | +} |
| 38 | + |
| 39 | +// FixScreenInfo returns the fixed properties of the screen. |
| 40 | +func (fb *FrameBuffer) FixScreenInfo() (FbFixScreenInfo, error) { |
| 41 | + return ioctlGet[FbFixScreenInfo](fb.File(), kFBIOGET_FSCREENINFO) |
| 42 | +} |
| 43 | + |
| 44 | +// VarScreenInfo returns the variable properties of the screen. |
| 45 | +func (fb *FrameBuffer) VarScreenInfo() (FbVarScreenInfo, error) { |
| 46 | + return ioctlGet[FbVarScreenInfo](fb.File(), kFBIOGET_VSCREENINFO) |
| 47 | +} |
| 48 | + |
| 49 | +// <linux/fb.h> ioctls |
| 50 | +// |
| 51 | +// 0x46 is 'F' |
| 52 | +const ( |
| 53 | + kFBIOGET_VSCREENINFO = 0x4600 |
| 54 | + kFBIOPUT_VSCREENINFO = 0x4601 |
| 55 | + kFBIOGET_FSCREENINFO = 0x4602 |
| 56 | +) |
| 57 | + |
| 58 | +// <linux/fb.h> struct fb_fix_screeninfo |
| 59 | +// |
| 60 | +// Fixed parameters of a screen |
| 61 | +type FbFixScreenInfo struct { |
| 62 | + // Screen ID, e.g. "TT Builtin" |
| 63 | + ID [16]byte |
| 64 | + |
| 65 | + // Frame buffer mem (physical address) |
| 66 | + SMemStart uintptr |
| 67 | + SMemLen uint32 |
| 68 | + |
| 69 | + // Framebuffer type (see FB_TYPE_*) |
| 70 | + Type uint32 |
| 71 | + |
| 72 | + // "Interleave for interleaved Planes" |
| 73 | + TypeAux uint32 |
| 74 | + |
| 75 | + // ??? (see FB_VISUAL_) |
| 76 | + Visual uint32 |
| 77 | + |
| 78 | + XPanStep uint16 |
| 79 | + YPanStep uint16 |
| 80 | + YWrapStep uint16 |
| 81 | + |
| 82 | + // Length of a line in bytes |
| 83 | + LineLength uint32 |
| 84 | + |
| 85 | + // Memory mapped I/O (physical address) |
| 86 | + MmioStart uintptr |
| 87 | + MmioLen uint32 |
| 88 | + |
| 89 | + // "Indicate to driver which specific chip/card we have" |
| 90 | + Accel uint32 |
| 91 | + |
| 92 | + // Capabilities (see FB_CAP_*) |
| 93 | + Capabilities uint16 |
| 94 | + |
| 95 | + // Reserved |
| 96 | + _ [2]uint16 |
| 97 | +} |
| 98 | + |
| 99 | +// <linux/fb.h> struct fb_fix_screeninfo |
| 100 | +// |
| 101 | +// Variable parameters of a screen |
| 102 | +type FbVarScreenInfo struct { |
| 103 | + // Visible resolution |
| 104 | + XRes, YRes uint32 |
| 105 | + |
| 106 | + // Virtual resolution |
| 107 | + XResVirtual, YResVirtual uint32 |
| 108 | + |
| 109 | + // Offset from virtual to visible resolution |
| 110 | + XOffset, YOffset uint32 |
| 111 | + |
| 112 | + // Number of bits per pixel in the buffer |
| 113 | + BitsPerPixel uint32 |
| 114 | + |
| 115 | + // 0 = color, 1 = grayscale, >1 = FOURCC |
| 116 | + Grayscale uint32 |
| 117 | + |
| 118 | + // Pixel format |
| 119 | + Red, Green, Blue, Alpha FbBitField |
| 120 | + |
| 121 | + // A value other than zero indicates a non-standard pixel format |
| 122 | + NonStd uint32 |
| 123 | + |
| 124 | + // ??? (see FB_ACTIVATE_*) |
| 125 | + Activate uint32 |
| 126 | + |
| 127 | + // Dimensions of picture in mm |
| 128 | + Height, Width uint32 |
| 129 | + |
| 130 | + // Deprecated, used to be accel_flags (see fb_info.flags) |
| 131 | + _ uint32 |
| 132 | + |
| 133 | + // Pixel clock in pico seconds |
| 134 | + PixelClock uint32 |
| 135 | + |
| 136 | + // Time from sync to picture |
| 137 | + LeftMargin uint32 |
| 138 | + |
| 139 | + // Time from picture to sync |
| 140 | + RightMargin uint32 |
| 141 | + |
| 142 | + UpperMargin uint32 |
| 143 | + LowerMargin uint32 |
| 144 | + |
| 145 | + // Length of horizontal sync |
| 146 | + HSyncLen uint32 |
| 147 | + |
| 148 | + // Length of vertical sync |
| 149 | + VSyncLen uint32 |
| 150 | + |
| 151 | + // ??? (see FB_SYNC_*) |
| 152 | + Sync uint32 |
| 153 | + |
| 154 | + // ??? (see FB_VMODE_*) |
| 155 | + VMode uint32 |
| 156 | + |
| 157 | + // Angle we rotate counter clockwise |
| 158 | + Rotate uint32 |
| 159 | + |
| 160 | + // Color space for FOURCC-based modes |
| 161 | + ColorSpace uint32 |
| 162 | + |
| 163 | + // Reserved |
| 164 | + _ [4]uint32 |
| 165 | +} |
| 166 | + |
| 167 | +// <linux/fb.h> struct fb_bitfield |
| 168 | +// |
| 169 | +// Interpretation of offset for color fields: All offsets are from the right, |
| 170 | +// inside a "pixel" value, which is exactly 'bits_per_pixel' wide (means: you |
| 171 | +// can use the offset as right argument to <<). A pixel afterwards is a bit |
| 172 | +// stream and is written to video memory as that unmodified. |
| 173 | +// |
| 174 | +// For pseudocolor: offset and length should be the same for all color |
| 175 | +// components. Offset specifies the position of the least significant bit of the |
| 176 | +// palette index in a pixel value. Length indicates the number of available |
| 177 | +// palette entries (i.e. # of entries = 1 << length). |
| 178 | +type FbBitField struct { |
| 179 | + Offset uint32 |
| 180 | + Length uint32 |
| 181 | + MsbRight uint32 |
| 182 | +} |
0 commit comments