| 1 |
//go:build !s390x && !ppc64le && !darwin && !windows && (linux || freebsd || openbsd || netbsd) |
| 2 |
|
| 3 |
package screenshot |
| 4 |
|
| 5 |
import ( |
| 6 |
"github.com/jezek/xgb" |
| 7 |
"github.com/jezek/xgb/xinerama" |
| 8 |
"image" |
| 9 |
) |
| 10 |
|
| 11 |
// NumActiveDisplays returns the number of active displays. |
| 12 |
func NumActiveDisplays() (num int) { |
| 13 |
defer func() { |
| 14 |
e := recover() |
| 15 |
if e != nil { |
| 16 |
num = 0 |
| 17 |
} |
| 18 |
}() |
| 19 |
|
| 20 |
c, err := xgb.NewConn() |
| 21 |
if err != nil { |
| 22 |
return 0 |
| 23 |
} |
| 24 |
defer c.Close() |
| 25 |
|
| 26 |
err = xinerama.Init(c) |
| 27 |
if err != nil { |
| 28 |
return 0 |
| 29 |
} |
| 30 |
|
| 31 |
reply, err := xinerama.QueryScreens(c).Reply() |
| 32 |
if err != nil { |
| 33 |
return 0 |
| 34 |
} |
| 35 |
|
| 36 |
num = int(reply.Number) |
| 37 |
return num |
| 38 |
} |
| 39 |
|
| 40 |
// GetDisplayBounds returns the bounds of displayIndex'th display. |
| 41 |
// The main display is displayIndex = 0. |
| 42 |
func GetDisplayBounds(displayIndex int) (rect image.Rectangle) { |
| 43 |
defer func() { |
| 44 |
e := recover() |
| 45 |
if e != nil { |
| 46 |
rect = image.Rectangle{} |
| 47 |
} |
| 48 |
}() |
| 49 |
|
| 50 |
c, err := xgb.NewConn() |
| 51 |
if err != nil { |
| 52 |
return image.Rectangle{} |
| 53 |
} |
| 54 |
defer c.Close() |
| 55 |
|
| 56 |
err = xinerama.Init(c) |
| 57 |
if err != nil { |
| 58 |
return image.Rectangle{} |
| 59 |
} |
| 60 |
|
| 61 |
reply, err := xinerama.QueryScreens(c).Reply() |
| 62 |
if err != nil { |
| 63 |
return image.Rectangle{} |
| 64 |
} |
| 65 |
|
| 66 |
if displayIndex >= int(reply.Number) { |
| 67 |
return image.Rectangle{} |
| 68 |
} |
| 69 |
|
| 70 |
primary := reply.ScreenInfo[0] |
| 71 |
x0 := int(primary.XOrg) |
| 72 |
y0 := int(primary.YOrg) |
| 73 |
|
| 74 |
screen := reply.ScreenInfo[displayIndex] |
| 75 |
x := int(screen.XOrg) - x0 |
| 76 |
y := int(screen.YOrg) - y0 |
| 77 |
w := int(screen.Width) |
| 78 |
h := int(screen.Height) |
| 79 |
rect = image.Rect(x, y, x+w, y+h) |
| 80 |
return rect |
| 81 |
} |