forked from xyproto/wallutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.go
More file actions
60 lines (53 loc) · 1.86 KB
/
monitor.go
File metadata and controls
60 lines (53 loc) · 1.86 KB
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
// wallutils is a package for dealing with monitors, resolution, dpi, wallpapers, wallpaper collections, timed wallpapers and converting to the Simple Timed Wallpaper format.
package wallutils
import (
"errors"
"fmt"
)
const VersionString = "5.2.1"
// Monitor contains an ID, the width in pixels and the height in pixels
type Monitor struct {
ID uint // monitor number, from 0 and up
Width uint // width, in pixels
Height uint // height, in pixels
DPIw uint // DPI, if available (width)
DPIh uint // DPI, if available (height)
}
var errNoWaylandNoX = errors.New("could not detect neither Wayland nor X")
// String returns a string with monitor ID and resolution
func (m Monitor) String() string {
return fmt.Sprintf("[%d] %dx%d", m.ID, m.Width, m.Height)
}
// Info returns a long info string that looks different for Wayland and for X.
// The string contains all available information about the connected monitors.
func Info() (string, error) {
if WaylandCanConnect() {
return WaylandInfo()
} else if XCanConnect() {
return XInfo()
}
return "", errNoWaylandNoX
}
// Monitors returns information about all monitors, regardless of if it's under
// Wayland or X11. Will use additional plugins, if available.
func Monitors() ([]Monitor, error) {
IDs, widths, heights, wDPIs, hDPIs := []uint{}, []uint{}, []uint{}, []uint{}, []uint{}
if WaylandCanConnect() {
if err := WaylandMonitors(&IDs, &widths, &heights, &wDPIs, &hDPIs); err != nil {
return []Monitor{}, err
}
} else if XCanConnect() {
if err := XMonitors(&IDs, &widths, &heights, &wDPIs, &hDPIs); err != nil {
return []Monitor{}, err
}
}
if len(IDs) == 0 {
return []Monitor{}, errNoWaylandNoX
}
// Build and return a []Monitor slice
var monitors = []Monitor{}
for i, ID := range IDs {
monitors = append(monitors, Monitor{ID, widths[i], heights[i], wDPIs[i], hDPIs[i]})
}
return monitors, nil
}