Skip to content

Commit 93d8e6e

Browse files
committed
Merge gitlab.com/firelizzard/go-framebuffer
1 parent 321252c commit 93d8e6e

File tree

5 files changed

+430
-153
lines changed

5 files changed

+430
-153
lines changed

dev_fb.go

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}

dev_tty.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package framebuffer
2+
3+
import "os"
4+
5+
// TTY is a wrapper around [os.File] that provides convenience methods for
6+
// interacting with a TTY.
7+
type TTY os.File
8+
9+
// OpenMyTTY opens the current process's TTY. Requires procfs mounted at /proc.
10+
func OpenMyTTY(flags int) (*TTY, error) {
11+
name, err := os.Readlink("/proc/self/fd/0")
12+
if err != nil {
13+
return nil, err
14+
}
15+
return OpenTTY(name, flags)
16+
}
17+
18+
// OpenTTY opens a TTY.
19+
func OpenTTY(name string, flags int) (*TTY, error) {
20+
f, err := os.OpenFile(name, flags, os.ModeDevice)
21+
if err != nil {
22+
return nil, err
23+
}
24+
return (*TTY)(f), nil
25+
}
26+
27+
// File converts the TTY back to a file.
28+
func (d *TTY) File() *os.File {
29+
return (*os.File)(d)
30+
}
31+
32+
// TextMode puts the TTY in text mode.
33+
func (d *TTY) TextMode() error {
34+
return d.SetMode(kKD_TEXT)
35+
}
36+
37+
// GraphicsMode puts the TTY in graphics mode.
38+
func (d *TTY) GraphicsMode() error {
39+
return d.SetMode(kKD_GRAPHICS)
40+
}
41+
42+
// SetMode sets the TTY's mode.
43+
func (d *TTY) SetMode(mode TTYMode) error {
44+
return ioctl(d.File(), kKDSETMODE, uintptr(mode))
45+
}
46+
47+
// GetMode returns the TTY's current mode.
48+
func (d *TTY) GetMode() (TTYMode, error) {
49+
return ioctlGet[TTYMode](d.File(), kKDGETMODE)
50+
}
51+
52+
type TTYMode int
53+
54+
const (
55+
TTYTextMode TTYMode = kKD_TEXT
56+
TTYGraphicsMode TTYMode = kKD_GRAPHICS
57+
)
58+
59+
// <linux/kd.h> ioctls
60+
//
61+
// 0x4B is 'K', to avoid collision with termios and vt
62+
const (
63+
kKDSETMODE = 0x4B3A
64+
kKDGETMODE = 0x4B3B
65+
66+
kKD_TEXT = 0x00
67+
kKD_GRAPHICS = 0x01
68+
)

0 commit comments

Comments
 (0)