Skip to content

Commit

Permalink
profile: Remove x11 dependency for screen resolution
Browse files Browse the repository at this point in the history
The x11 lib depends on glibc, which makes statically linking quite painful.
For the time being, reverting to xrandr for resolution, in the future we
can revisit this - potentially when statically linking against musl.

Signed-off-by: Paulo Gomes <[email protected]>
  • Loading branch information
pjbgf committed May 14, 2024
1 parent 6ddaad2 commit 073af5a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ go.work

build
dist

.vscode/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ help: ## display Makefile's help.

.PHONY: build
build: ## build qubesome to the path set on TARGET_BIN.
go build -trimpath -ldflags '-extldflags -s -w' -o $(TARGET_BIN) main.go
go build -trimpath -tags 'netgo,osusergo,static_build' -ldflags '-extldflags -static -s -w' -o $(TARGET_BIN) main.go

.PHONY: test
test: ## run golang tests.
Expand Down
1 change: 1 addition & 0 deletions internal/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var deps map[string][]string = map[string][]string{
"profiles": {
files.DockerBinary,
files.ShBinary,
files.XrandrBinary,
},
}

Expand Down
1 change: 1 addition & 0 deletions internal/files/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ const (
XclipBinary = "/usr/bin/xclip"
DockerBinary = "/usr/bin/docker"
FireCrackerBinary = "/usr/bin/firecracker"
XrandrBinary = "/usr/bin/xrandr"
)
52 changes: 31 additions & 21 deletions internal/resolution/resolution.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
package resolution
//go:build !x11

// Depends on libx11-devel.
package resolution

/*
#cgo LDFLAGS: -lX11
import (
"bufio"
"bytes"
"fmt"
"strings"

#include <X11/Xlib.h>
*/
import "C"
import "fmt"
"github.com/qubesome/cli/internal/files"
"golang.org/x/sys/execabs"
)

// Primary returns the screen resolution for the primary display.
func Primary() (string, error) {
d := C.XOpenDisplay(nil)
if d == nil {
return "", fmt.Errorf("cannot open display")
cmd := execabs.Command(files.XrandrBinary)
output, err := cmd.Output()
if err != nil {
return "", err
}
defer C.XCloseDisplay(d)

screen := C.XDefaultScreenOfDisplay(d)
width := int(C.XWidthOfScreen(screen))
if width <= 0 {
return "", fmt.Errorf("cannot convert display width")
}
height := int(C.XHeightOfScreen(screen))
if height <= 0 {
return "", fmt.Errorf("cannot convert display height")
scanner := bufio.NewScanner(bytes.NewReader(output))
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
if !strings.Contains(text, "*") {
continue
}

fields := strings.Fields(text)
if len(fields) == 0 {
continue
}

raw := fields[0]
if strings.Contains(raw, "x") {
return raw, nil
}
}

return fmt.Sprintf("%dx%d", width, height), nil
return "", fmt.Errorf("cannot get resolution")
}
34 changes: 34 additions & 0 deletions internal/resolution/resolution_x11.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build x11

package resolution

// Depends on libx11-devel.

/*
#cgo LDFLAGS: -lX11
#include <X11/Xlib.h>
*/
import "C"
import "fmt"

// Primary returns the screen resolution for the primary display.
func Primary() (string, error) {
d := C.XOpenDisplay(nil)
if d == nil {
return "", fmt.Errorf("cannot open display")
}
defer C.XCloseDisplay(d)

screen := C.XDefaultScreenOfDisplay(d)
width := int(C.XWidthOfScreen(screen))
if width <= 0 {
return "", fmt.Errorf("cannot convert display width")
}
height := int(C.XHeightOfScreen(screen))
if height <= 0 {
return "", fmt.Errorf("cannot convert display height")
}

return fmt.Sprintf("%dx%d", width, height), nil
}

0 comments on commit 073af5a

Please sign in to comment.