-
Notifications
You must be signed in to change notification settings - Fork 179
/
nix.go
81 lines (67 loc) · 1.45 KB
/
nix.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
//go:build !s390x && !ppc64le && !darwin && !windows && (linux || freebsd || openbsd || netbsd)
package screenshot
import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xinerama"
"image"
)
// NumActiveDisplays returns the number of active displays.
func NumActiveDisplays() (num int) {
defer func() {
e := recover()
if e != nil {
num = 0
}
}()
c, err := xgb.NewConn()
if err != nil {
return 0
}
defer c.Close()
err = xinerama.Init(c)
if err != nil {
return 0
}
reply, err := xinerama.QueryScreens(c).Reply()
if err != nil {
return 0
}
num = int(reply.Number)
return num
}
// GetDisplayBounds returns the bounds of displayIndex'th display.
// The main display is displayIndex = 0.
func GetDisplayBounds(displayIndex int) (rect image.Rectangle) {
defer func() {
e := recover()
if e != nil {
rect = image.Rectangle{}
}
}()
c, err := xgb.NewConn()
if err != nil {
return image.Rectangle{}
}
defer c.Close()
err = xinerama.Init(c)
if err != nil {
return image.Rectangle{}
}
reply, err := xinerama.QueryScreens(c).Reply()
if err != nil {
return image.Rectangle{}
}
if displayIndex >= int(reply.Number) {
return image.Rectangle{}
}
primary := reply.ScreenInfo[0]
x0 := int(primary.XOrg)
y0 := int(primary.YOrg)
screen := reply.ScreenInfo[displayIndex]
x := int(screen.XOrg) - x0
y := int(screen.YOrg) - y0
w := int(screen.Width)
h := int(screen.Height)
rect = image.Rect(x, y, x+w, y+h)
return rect
}