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