-
Notifications
You must be signed in to change notification settings - Fork 178
/
nix_xwindow.go
134 lines (114 loc) · 2.81 KB
/
nix_xwindow.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//go:build !s390x && !ppc64le && !darwin && !windows && (linux || freebsd || openbsd || netbsd)
package screenshot
import (
"fmt"
"github.com/gen2brain/shm"
"github.com/jezek/xgb"
mshm "github.com/jezek/xgb/shm"
"github.com/jezek/xgb/xinerama"
"github.com/jezek/xgb/xproto"
"image"
"image/color"
)
func captureXinerama(x, y, width, height int) (img *image.RGBA, e error) {
defer func() {
err := recover()
if err != nil {
img = nil
e = fmt.Errorf("%v", err)
}
}()
c, err := xgb.NewConn()
if err != nil {
return nil, err
}
defer c.Close()
err = xinerama.Init(c)
if err != nil {
return nil, err
}
reply, err := xinerama.QueryScreens(c).Reply()
if err != nil {
return nil, err
}
primary := reply.ScreenInfo[0]
x0 := int(primary.XOrg)
y0 := int(primary.YOrg)
useShm := true
err = mshm.Init(c)
if err != nil {
useShm = false
}
screen := xproto.Setup(c).DefaultScreen(c)
wholeScreenBounds := image.Rect(0, 0, int(screen.WidthInPixels), int(screen.HeightInPixels))
targetBounds := image.Rect(x+x0, y+y0, x+x0+width, y+y0+height)
intersect := wholeScreenBounds.Intersect(targetBounds)
rect := image.Rect(0, 0, width, height)
img, err = createImage(rect)
if err != nil {
return nil, err
}
// Paint with opaque black
index := 0
for iy := 0; iy < height; iy++ {
j := index
for ix := 0; ix < width; ix++ {
img.Pix[j+3] = 255
j += 4
}
index += img.Stride
}
if !intersect.Empty() {
var data []byte
if useShm {
shmSize := intersect.Dx() * intersect.Dy() * 4
shmId, err := shm.Get(shm.IPC_PRIVATE, shmSize, shm.IPC_CREAT|0777)
if err != nil {
return nil, err
}
seg, err := mshm.NewSegId(c)
if err != nil {
return nil, err
}
data, err = shm.At(shmId, 0, 0)
if err != nil {
return nil, err
}
mshm.Attach(c, seg, uint32(shmId), false)
defer mshm.Detach(c, seg)
defer func() {
_ = shm.Rm(shmId)
}()
defer func() {
_ = shm.Dt(data)
}()
_, err = mshm.GetImage(c, xproto.Drawable(screen.Root),
int16(intersect.Min.X), int16(intersect.Min.Y),
uint16(intersect.Dx()), uint16(intersect.Dy()), 0xffffffff,
byte(xproto.ImageFormatZPixmap), seg, 0).Reply()
if err != nil {
return nil, err
}
} else {
xImg, err := xproto.GetImage(c, xproto.ImageFormatZPixmap, xproto.Drawable(screen.Root),
int16(intersect.Min.X), int16(intersect.Min.Y),
uint16(intersect.Dx()), uint16(intersect.Dy()), 0xffffffff).Reply()
if err != nil {
return nil, err
}
data = xImg.Data
}
// BitBlt by hand
offset := 0
for iy := intersect.Min.Y; iy < intersect.Max.Y; iy++ {
for ix := intersect.Min.X; ix < intersect.Max.X; ix++ {
r := data[offset+2]
g := data[offset+1]
b := data[offset]
img.SetRGBA(ix-(x+x0), iy-(y+y0), color.RGBA{r, g, b, 255})
offset += 4
}
}
}
return img, e
}