From 59f9954ba074060884cc61b6099a814bf06bab5a Mon Sep 17 00:00:00 2001 From: kbinani Date: Tue, 20 Aug 2024 00:00:31 +0900 Subject: [PATCH] Switch dbus/xinerama depending on XDG_SESSION_TYPE --- internal/xwindow/xwindow.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/xwindow/xwindow.go b/internal/xwindow/xwindow.go index 53a3de3..23096a5 100644 --- a/internal/xwindow/xwindow.go +++ b/internal/xwindow/xwindow.go @@ -21,6 +21,15 @@ import ( var gCounter uint64 = 0 func Capture(x, y, width, height int) (img *image.RGBA, e error) { + sessionType := os.Getenv("XDG_SESSION_TYPE") + if sessionType == "wayland" { + return captureDbus(x, y, width, height) + } else { + return captureXinerama(x, y, width, height) + } +} + +func captureDbus(x, y, width, height int) (img *image.RGBA, e error) { c, err := dbus.ConnectSessionBus() if err != nil { return nil, fmt.Errorf("dbus.SessionBus() failed: %v", err) @@ -91,6 +100,9 @@ func Capture(x, y, width, height int) (img *image.RGBA, e error) { return nil, fmt.Errorf("png.Decode(%s) failed: %v", path, err) } canvas, err := util.CreateImage(image.Rect(0, 0, width, height)) + if err != nil { + return nil, fmt.Errorf("util.CreateImage(%v) failed: %v", path, err) + } draw.Draw(canvas, image.Rect(0, 0, width, height), img, image.Point{x, y}, draw.Src) return canvas, e } @@ -98,7 +110,7 @@ func Capture(x, y, width, height int) (img *image.RGBA, e error) { return nil, fmt.Errorf("dbus.Message doesn't contain uri") } -func Capture_(x, y, width, height int) (img *image.RGBA, e error) { +func captureXinerama(x, y, width, height int) (img *image.RGBA, e error) { defer func() { err := recover() if err != nil {