-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from kbinani/wayland
Add a fallback capture method for Wayland
- Loading branch information
Showing
10 changed files
with
149 additions
and
18 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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build !s390x && !ppc64le && !darwin && !windows && (linux || openbsd || netbsd) | ||
|
||
package internal | ||
|
||
import ( | ||
"image" | ||
"os" | ||
) | ||
|
||
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) | ||
} | ||
} |
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,11 @@ | ||
//go:build freebsd | ||
|
||
package internal | ||
|
||
import ( | ||
"image" | ||
) | ||
|
||
func Capture(x, y, width, height int) (img *image.RGBA, e error) { | ||
return captureXinerama(x, y, width, height) | ||
} |
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,4 +1,4 @@ | ||
package util | ||
package internal | ||
|
||
import ( | ||
"errors" | ||
|
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,97 @@ | ||
//go:build !s390x && !ppc64le && !darwin && !windows && !freebsd && (linux || openbsd || netbsd) | ||
|
||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"github.com/godbus/dbus/v5" | ||
"image" | ||
"image/draw" | ||
"image/png" | ||
"net/url" | ||
"os" | ||
"sync/atomic" | ||
) | ||
|
||
var gCounter uint64 = 0 | ||
|
||
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) | ||
} | ||
defer func(c *dbus.Conn) { | ||
err := c.Close() | ||
if err != nil { | ||
e = err | ||
} | ||
}(c) | ||
token := atomic.AddUint64(&gCounter, 1) | ||
options := map[string]dbus.Variant{ | ||
"modal": dbus.MakeVariant(false), | ||
"interactive": dbus.MakeVariant(false), | ||
"handle_token": dbus.MakeVariant(token), | ||
} | ||
obj := c.Object("org.freedesktop.portal.Desktop", dbus.ObjectPath("/org/freedesktop/portal/desktop")) | ||
call := obj.Call("org.freedesktop.portal.Screenshot.Screenshot", 0, "", options) | ||
var path dbus.ObjectPath | ||
err = call.Store(&path) | ||
if err != nil { | ||
return nil, fmt.Errorf("dbus.Store() failed: %v", err) | ||
} | ||
ch := make(chan *dbus.Message) | ||
c.Eavesdrop(ch) | ||
for msg := range ch { | ||
o, ok := msg.Headers[dbus.FieldPath] | ||
if !ok { | ||
continue | ||
} | ||
s, ok := o.Value().(dbus.ObjectPath) | ||
if !ok { | ||
return nil, fmt.Errorf("dbus.FieldPath value does't have ObjectPath type") | ||
} | ||
if s != path { | ||
continue | ||
} | ||
for _, body := range msg.Body { | ||
v, ok := body.(map[string]dbus.Variant) | ||
if !ok { | ||
continue | ||
} | ||
uri, ok := v["uri"] | ||
if !ok { | ||
continue | ||
} | ||
path, ok := uri.Value().(string) | ||
if !ok { | ||
return nil, fmt.Errorf("uri is not a string") | ||
} | ||
fpath, err := url.Parse(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("url.Parse(%v) failed: %v", path, err) | ||
} | ||
if fpath.Scheme != "file" { | ||
return nil, fmt.Errorf("uri is not a file path") | ||
} | ||
file, err := os.Open(fpath.Path) | ||
if err != nil { | ||
return nil, fmt.Errorf("os.Open(%s) failed: %v", path, err) | ||
} | ||
defer func(file *os.File) { | ||
_ = file.Close() | ||
_ = os.Remove(fpath.Path) | ||
}(file) | ||
img, err := png.Decode(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("png.Decode(%s) failed: %v", path, err) | ||
} | ||
canvas, err := 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 | ||
} | ||
} | ||
return nil, fmt.Errorf("dbus.Message doesn't contain uri") | ||
} |
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