Skip to content

Commit da463b3

Browse files
committed
Separate monitor detection code into different program
1 parent a919ffb commit da463b3

18 files changed

Lines changed: 116 additions & 31 deletions

File tree

‎Makefile‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
# Installation paths
22
PREFIX ?= /usr/local
33
BINDIR ?= $(PREFIX)/bin
4+
LIBEXECDIR ?= $(PREFIX)/libexec
45
SYSCONFDIR := $(PREFIX)/etc
56

67
# Compilers and tools
78
GO ?= go
89

10+
# Compiler flags
11+
GOOS ?= $(shell $(GO) env | grep '^GOOS' | cut -d'=' -f2 | tr -d "'")
12+
GOARCH ?= $(shell $(GO) env | grep '^GOARCH' | cut -d'=' -f2 | tr -d "'")
13+
LDFLAGS ?= -w
14+
915
# Build-time variables
1016
VERSION ?= $(shell git describe --tags --dirty)
1117

12-
build:
13-
mkdir -p build
14-
cd src; $(GO) build -ldflags "-w -X 'main.StormfetchVersion=$(VERSION)' -X 'main.SystemConfigDir=$(SYSCONFDIR)'" -o ../build/stormfetch stormfetch
18+
build: build-stormfetch build-stormfetch-monitor-detection
19+
20+
build-stormfetch:
21+
install -d build
22+
cd src/stormfetch; GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build -ldflags "$(LDFLAGS) -X 'main.StormfetchVersion=$(VERSION)' -X 'main.SystemConfigDir=$(SYSCONFDIR)' -X 'main.LibexecDir=$(LIBEXECDIR)'" -o ../../build/stormfetch
23+
24+
build-stormfetch-monitor-detection:
25+
install -d build
26+
cd src/stormfetch-monitor-detection; GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build -ldflags "$(LDFLAGS)" -o ../../build/stormfetch-monitor-detection
27+
28+
install: install-stormfetch install-stormfetch-monitor-detection install-config
1529

16-
install: build/stormfetch config/
17-
# Create directoriy
30+
install-stormfetch: build/stormfetch
31+
# Create directory
1832
install -dm755 $(DESTDIR)$(BINDIR)
1933
# Install binary
2034
install -m755 build/stormfetch $(DESTDIR)$(BINDIR)/stormfetch
2135

36+
install-stormfetch-monitor-detection: build/stormfetch-monitor-detection
37+
# Create directory
38+
install -dm755 $(DESTDIR)$(LIBEXECDIR)
39+
# Install binary
40+
install -m755 build/stormfetch-monitor-detection $(DESTDIR)$(LIBEXECDIR)/stormfetch-monitor-detection
41+
2242
install-config:
2343
# Create directory
2444
install -dm755 $(DESTDIR)$(SYSCONFDIR)
@@ -27,9 +47,10 @@ install-config:
2747

2848
uninstall:
2949
-rm -f $(DESTDIR)$(BINDIR)/stormfetch
50+
-rm -f $(DESTDIR)$(LIBEXEDIR)/stormfetch-monitor-detection
3051
-rm -rf $(DESTDIR)$(SYSCONFDIR)/stormfetch
3152

3253
clean:
3354
rm -r build/
3455

35-
.PHONY: build install install-config uninstall clean
56+
.PHONY: build build-stormfetch build-stormfetch-monitor-detection install install-config uninstall clean
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module stormfetch-monitor-detection
2+
3+
go 1.24.0
4+
5+
require github.com/go-gl/glfw/v3.3/glfw v0.0.0-20260406072232-3ac4aa2bb164
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20260406072232-3ac4aa2bb164 h1:ddtotcEXIT7dFhSeXIWjK8YMFYl5bB2NdYQwgv6Uqjk=
2+
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20260406072232-3ac4aa2bb164/go.mod h1:SyRD8YfuKk+ZXlDqYiqe1qMSqjNgtHzBTG810KUagMc=
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
8+
"github.com/go-gl/glfw/v3.3/glfw"
9+
)
10+
11+
func main() {
12+
defer func() {
13+
if r := recover(); r != nil {
14+
os.Exit(1)
15+
}
16+
}()
17+
18+
err := glfw.Init()
19+
if err != nil {
20+
os.Exit(1)
21+
}
22+
23+
for _, monitor := range glfw.GetMonitors() {
24+
mode := monitor.GetVideoMode()
25+
26+
fmt.Print(monitor.GetName() + ",")
27+
fmt.Print(strconv.Itoa(mode.Width) + ",")
28+
fmt.Print(strconv.Itoa(mode.Height) + ",")
29+
fmt.Print(strconv.Itoa(mode.RefreshRate))
30+
fmt.Println()
31+
}
32+
defer glfw.Terminate()
33+
}
File renamed without changes.
File renamed without changes.

src/go.mod renamed to src/stormfetch/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module stormfetch
33
go 1.24.0
44

55
require (
6-
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728
76
github.com/jackmordaunt/ghw v1.0.5
87
github.com/mitchellh/go-ps v1.0.0
98
gopkg.in/yaml.v3 v3.0.1

src/go.sum renamed to src/stormfetch/go.sum

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a h1:vxnBhFDDT+xzxf1jTJKMKZw3H0swfWk9RpWbBbDK5+0=
2-
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
3-
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728 h1:RkGhqHxEVAvPM0/R+8g7XRwQnHatO0KAuVcwHo8q9W8=
4-
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20250301202403-da16c1255728/go.mod h1:SyRD8YfuKk+ZXlDqYiqe1qMSqjNgtHzBTG810KUagMc=
5-
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
6-
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
71
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
82
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
9-
github.com/jackmordaunt/ghw v1.0.4 h1:as+COFuPuXaNQC3WqzoHS/E2JYWZU7gN8ompNTUxNxs=
10-
github.com/jackmordaunt/ghw v1.0.4/go.mod h1:4dReYvJ36CoAzIxlEx8du25Qi/YqKYvEGE9QJoRXiK8=
113
github.com/jackmordaunt/ghw v1.0.5 h1:3rTXwu0D9RkungV7/WlhC8HVuDlPaq0aKY8u0I51jLY=
124
github.com/jackmordaunt/ghw v1.0.5/go.mod h1:VpFlLXnJErgoRttR3WOxun4v5EE8/xfB4cK26G5q2U0=
135
github.com/jackmordaunt/pcidb v1.0.1 h1:uLLZa6kD5P39r2cwMyJJkxmuHfH9Wq19gYQEbYcB0Z4=
@@ -21,10 +13,7 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2113
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2214
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
2315
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
24-
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2516
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26-
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
27-
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2817
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
2918
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
3019
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"os/exec"
7+
"path"
68
"slices"
79
"strconv"
810
"strings"
911

10-
"github.com/go-gl/glfw/v3.3/glfw"
1112
"github.com/jackmordaunt/ghw"
1213
)
1314

@@ -29,6 +30,7 @@ type GPU struct {
2930
}
3031

3132
type Monitor struct {
33+
Name string
3234
Width int
3335
Height int
3436
RefreshRate int
@@ -195,22 +197,55 @@ func GetMotherboardModel() string {
195197

196198
func GetMonitors() []Monitor {
197199
var monitors []Monitor
200+
198201
if GetDisplayProtocol() != "" {
199-
err := glfw.Init()
202+
// Find stormfetch-monitor-detection program
203+
execPath, _ := os.Executable()
204+
possiblePaths := []string{
205+
path.Join(path.Dir(execPath), "stormfetch-monitor-detection"),
206+
path.Join(LibexecDir, "stormfetch-monitor-detection"),
207+
}
208+
209+
monitorDetectionPath := ""
210+
for _, path := range possiblePaths {
211+
if _, err := os.Stat(path); err == nil {
212+
monitorDetectionPath = path
213+
break
214+
}
215+
}
216+
if monitorDetectionPath == "" {
217+
return nil
218+
}
219+
220+
// Get monitor detection program output
221+
output, err := exec.Command(monitorDetectionPath).Output()
200222
if err != nil {
201-
panic(err)
223+
return nil
202224
}
203225

204-
for _, monitor := range glfw.GetMonitors() {
205-
mode := monitor.GetVideoMode()
226+
// Parse output
227+
for line := range strings.SplitSeq(strings.TrimSpace(string(output)), "\n") {
228+
lineSplit := strings.Split(line, ",")
229+
if len(lineSplit) != 4 {
230+
continue
231+
}
232+
233+
name := lineSplit[0]
234+
width, err := strconv.Atoi(lineSplit[1])
235+
height, err := strconv.Atoi(lineSplit[2])
236+
refreshRate, err := strconv.Atoi(lineSplit[3])
237+
if err != nil {
238+
continue
239+
}
206240

207241
monitors = append(monitors, Monitor{
208-
Width: mode.Width,
209-
Height: mode.Height,
210-
RefreshRate: mode.RefreshRate,
242+
Name: name,
243+
Width: width,
244+
Height: height,
245+
RefreshRate: refreshRate,
211246
})
212247
}
213-
defer glfw.Terminate()
214248
}
249+
215250
return monitors
216251
}

src/main.go renamed to src/stormfetch/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
// Build-time variables
1313
var StormfetchVersion = "dev"
1414
var SystemConfigDir = "/etc/"
15+
var LibexecDir = "/libexec/"
1516

1617
// Flag variables
1718
var ShowVersion = false

0 commit comments

Comments
 (0)