-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profile: Remove x11 dependency for screen resolution
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
Showing
6 changed files
with
70 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ go.work | |
|
||
build | ||
dist | ||
|
||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |