Skip to content

Commit

Permalink
Merge pull request #28 from kbinani/flatten
Browse files Browse the repository at this point in the history
Flatten project directory structure
  • Loading branch information
kbinani authored Aug 20, 2024
2 parents 30c18db + 406d4ef commit a8a2c5d
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 155 deletions.
6 changes: 2 additions & 4 deletions screenshot_darwin.go → darwin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build cgo
//go:build cgo && darwin

package screenshot

Expand Down Expand Up @@ -71,8 +71,6 @@ import (
"errors"
"image"
"unsafe"

"github.com/kbinani/screenshot/internal"
)

func Capture(x, y, width, height int) (*image.RGBA, error) {
Expand All @@ -81,7 +79,7 @@ func Capture(x, y, width, height int) (*image.RGBA, error) {
}

rect := image.Rect(0, 0, width, height)
img, err := internal.CreateImage(rect)
img, err := createImage(rect)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/kbinani/screenshot

go 1.17
go 1.21

require (
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7
github.com/jezek/xgb v1.1.0
github.com/gen2brain/shm v0.1.0
github.com/jezek/xgb v1.1.1
github.com/lxn/win v0.0.0-20210218163916-a377121e959e
)

require (
golang.org/x/sys v0.24.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
golang.org/x/sys v0.11.0 // indirect
)
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7 h1:VLEKvjGJYAMCXw0/32r9io61tEXnMWDRxMk+peyRVFc=
github.com/gen2brain/shm v0.0.0-20230802011745-f2460f5984f7/go.mod h1:uF6rMu/1nvu+5DpiRLwusA6xB8zlkNoGzKn8lmYONUo=
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/gen2brain/shm v0.1.0 h1:MwPeg+zJQXN0RM9o+HqaSFypNoNEcNpeoGp0BTSx2YY=
github.com/gen2brain/shm v0.1.0/go.mod h1:UgIcVtvmOu+aCJpqJX7GOtiN7X2ct+TKLg4RTxwPIUA=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/lxn/win v0.0.0-20210218163916-a377121e959e h1:H+t6A/QJMbhCSEH5rAuRxh+CtW96g0Or0Fxa9IKr4uc=
github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk=
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
11 changes: 0 additions & 11 deletions internal/dbus_unavailable.go

This file was deleted.

22 changes: 0 additions & 22 deletions internal/util.go

This file was deleted.

81 changes: 81 additions & 0 deletions nix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//go:build !s390x && !ppc64le && !darwin && !windows && (linux || freebsd || openbsd || netbsd)

package screenshot

import (
"github.com/jezek/xgb"
"github.com/jezek/xgb/xinerama"
"image"
)

// NumActiveDisplays returns the number of active displays.
func NumActiveDisplays() (num int) {
defer func() {
e := recover()
if e != nil {
num = 0
}
}()

c, err := xgb.NewConn()
if err != nil {
return 0
}
defer c.Close()

err = xinerama.Init(c)
if err != nil {
return 0
}

reply, err := xinerama.QueryScreens(c).Reply()
if err != nil {
return 0
}

num = int(reply.Number)
return num
}

// GetDisplayBounds returns the bounds of displayIndex'th display.
// The main display is displayIndex = 0.
func GetDisplayBounds(displayIndex int) (rect image.Rectangle) {
defer func() {
e := recover()
if e != nil {
rect = image.Rectangle{}
}
}()

c, err := xgb.NewConn()
if err != nil {
return image.Rectangle{}
}
defer c.Close()

err = xinerama.Init(c)
if err != nil {
return image.Rectangle{}
}

reply, err := xinerama.QueryScreens(c).Reply()
if err != nil {
return image.Rectangle{}
}

if displayIndex >= int(reply.Number) {
return image.Rectangle{}
}

primary := reply.ScreenInfo[0]
x0 := int(primary.XOrg)
y0 := int(primary.YOrg)

screen := reply.ScreenInfo[displayIndex]
x := int(screen.XOrg) - x0
y := int(screen.YOrg) - y0
w := int(screen.Width)
h := int(screen.Height)
rect = image.Rect(x, y, x+w, y+h)
return rect
}
5 changes: 4 additions & 1 deletion internal/dbus_available.go → nix_dbus_available.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
//go:build !s390x && !ppc64le && !darwin && !windows && (linux || openbsd || netbsd)

package internal
package screenshot

import (
"image"
"os"
)

// Capture returns screen capture of specified desktop region.
// x and y represent distance from the upper-left corner of primary display.
// Y-axis is downward direction. This means coordinates system is similar to Windows OS.
func Capture(x, y, width, height int) (img *image.RGBA, e error) {
sessionType := os.Getenv("XDG_SESSION_TYPE")
if sessionType == "wayland" {
Expand Down
14 changes: 14 additions & 0 deletions nix_dbus_unavailable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build freebsd

package screenshot

import (
"image"
)

// Capture returns screen capture of specified desktop region.
// x and y represent distance from the upper-left corner of primary display.
// Y-axis is downward direction. This means coordinates system is similar to Windows OS.
func Capture(x, y, width, height int) (img *image.RGBA, e error) {
return captureXinerama(x, y, width, height)
}
10 changes: 5 additions & 5 deletions internal/wayland.go → nix_wayland.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build !s390x && !ppc64le && !darwin && !windows && !freebsd && (linux || openbsd || netbsd)

package internal
package screenshot

import (
"fmt"
Expand All @@ -13,7 +13,7 @@ import (
"sync/atomic"
)

var gCounter uint64 = 0
var gTokenCounter uint64 = 0

func captureDbus(x, y, width, height int) (img *image.RGBA, e error) {
c, err := dbus.ConnectSessionBus()
Expand All @@ -26,7 +26,7 @@ func captureDbus(x, y, width, height int) (img *image.RGBA, e error) {
e = err
}
}(c)
token := atomic.AddUint64(&gCounter, 1)
token := atomic.AddUint64(&gTokenCounter, 1)
options := map[string]dbus.Variant{
"modal": dbus.MakeVariant(false),
"interactive": dbus.MakeVariant(false),
Expand Down Expand Up @@ -85,9 +85,9 @@ func captureDbus(x, y, width, height int) (img *image.RGBA, e error) {
if err != nil {
return nil, fmt.Errorf("png.Decode(%s) failed: %v", path, err)
}
canvas, err := CreateImage(image.Rect(0, 0, width, height))
canvas, err := createImage(image.Rect(0, 0, width, height))
if err != nil {
return nil, fmt.Errorf("util.CreateImage(%v) failed: %v", path, err)
return nil, fmt.Errorf("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
Expand Down
73 changes: 2 additions & 71 deletions internal/xwindow.go → nix_xwindow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build !s390x && !ppc64le && !darwin && !windows && (linux || freebsd || openbsd || netbsd)

package internal
package screenshot

import (
"fmt"
Expand Down Expand Up @@ -53,7 +53,7 @@ func captureXinerama(x, y, width, height int) (img *image.RGBA, e error) {
intersect := wholeScreenBounds.Intersect(targetBounds)

rect := image.Rect(0, 0, width, height)
img, err = CreateImage(rect)
img, err = createImage(rect)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,72 +132,3 @@ func captureXinerama(x, y, width, height int) (img *image.RGBA, e error) {

return img, e
}

func NumActiveDisplays() (num int) {
defer func() {
e := recover()
if e != nil {
num = 0
}
}()

c, err := xgb.NewConn()
if err != nil {
return 0
}
defer c.Close()

err = xinerama.Init(c)
if err != nil {
return 0
}

reply, err := xinerama.QueryScreens(c).Reply()
if err != nil {
return 0
}

num = int(reply.Number)
return num
}

func GetDisplayBounds(displayIndex int) (rect image.Rectangle) {
defer func() {
e := recover()
if e != nil {
rect = image.Rectangle{}
}
}()

c, err := xgb.NewConn()
if err != nil {
return image.Rectangle{}
}
defer c.Close()

err = xinerama.Init(c)
if err != nil {
return image.Rectangle{}
}

reply, err := xinerama.QueryScreens(c).Reply()
if err != nil {
return image.Rectangle{}
}

if displayIndex >= int(reply.Number) {
return image.Rectangle{}
}

primary := reply.ScreenInfo[0]
x0 := int(primary.XOrg)
y0 := int(primary.YOrg)

screen := reply.ScreenInfo[displayIndex]
x := int(screen.XOrg) - x0
y := int(screen.YOrg) - y0
w := int(screen.Width)
h := int(screen.Height)
rect = image.Rect(x, y, x+w, y+h)
return rect
}
16 changes: 16 additions & 0 deletions screenshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ func CaptureDisplay(displayIndex int) (*image.RGBA, error) {
func CaptureRect(rect image.Rectangle) (*image.RGBA, error) {
return Capture(rect.Min.X, rect.Min.Y, rect.Dx(), rect.Dy())
}

func createImage(rect image.Rectangle) (img *image.RGBA, e error) {
img = nil
e = errors.New("Cannot create image.RGBA")

defer func() {
err := recover()
if err == nil {
e = nil
}
}()
// image.NewRGBA may panic if rect is too large.
img = image.NewRGBA(rect)

return img, e
}
26 changes: 0 additions & 26 deletions screenshot_supported.go

This file was deleted.

File renamed without changes.
Loading

0 comments on commit a8a2c5d

Please sign in to comment.