Skip to content

Commit

Permalink
Flatten project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kbinani committed Aug 20, 2024
1 parent 30c18db commit d9050fc
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 143 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
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.
7 changes: 4 additions & 3 deletions screenshot_windows.go → windows.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//go:build windows

package screenshot

import (
"errors"
"github.com/kbinani/screenshot/internal"
win "github.com/lxn/win"
"github.com/lxn/win"
"image"
"syscall"
"unsafe"
Expand All @@ -19,7 +20,7 @@ var (

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

0 comments on commit d9050fc

Please sign in to comment.