Files

212 lines
6.3 KiB
Go
Executable File

// Package capture grabs the Windows desktop via GDI (BitBlt + GetDIBits).
// This is plain user-mode Win32, no drivers or hooks — it sees exactly what
// a local user sitting at the screen would see.
package capture
import (
"fmt"
"image"
"unsafe"
"golang.org/x/sys/windows"
)
var (
user32 = windows.NewLazySystemDLL("user32.dll")
gdi32 = windows.NewLazySystemDLL("gdi32.dll")
procGetDC = user32.NewProc("GetDC")
procReleaseDC = user32.NewProc("ReleaseDC")
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
procCreateCompatDC = gdi32.NewProc("CreateCompatibleDC")
procDeleteDC = gdi32.NewProc("DeleteDC")
procCreateCompatBmp = gdi32.NewProc("CreateCompatibleBitmap")
procSelectObject = gdi32.NewProc("SelectObject")
procDeleteObject = gdi32.NewProc("DeleteObject")
procBitBlt = gdi32.NewProc("BitBlt")
procGetDIBits = gdi32.NewProc("GetDIBits")
procGetCursorInfo = user32.NewProc("GetCursorInfo")
procGetIconInfo = user32.NewProc("GetIconInfo")
procDrawIconEx = user32.NewProc("DrawIconEx")
)
const (
smXVirtualScreen = 76
smYVirtualScreen = 77
smCXVirtualScreen = 78
smCYVirtualScreen = 79
srcCopy = 0x00CC0020
biRGB = 0
dibRGBColors = 0
cursorShowing = 0x00000001
diNormal = 0x0003
)
type point struct {
X, Y int32
}
// cursorInfo mirrors Win32 CURSORINFO.
type cursorInfo struct {
CbSize uint32
Flags uint32
HCursor uintptr
PtScreenPos point
}
// iconInfo mirrors Win32 ICONINFO. GetIconInfo allocates hbmMask/hbmColor
// as new GDI bitmap objects on every call — callers must DeleteObject them.
type iconInfo struct {
FIcon int32
XHotspot uint32
YHotspot uint32
HbmMask uintptr
HbmColor uintptr
}
type bitmapInfoHeader struct {
Size uint32
Width int32
Height int32
Planes uint16
BitCount uint16
Compression uint32
SizeImage uint32
XPelsPerMeter int32
YPelsPerMeter int32
ClrUsed uint32
ClrImportant uint32
}
// drawCursorOverlay draws the current mouse cursor onto hMemDC at its
// actual screen position, so it ends up baked into the captured bitmap.
// originX/originY are the virtual-desktop origin CaptureScreen captured
// from (see VirtualScreenBounds) — needed to translate the cursor's
// screen-space position into our bitmap's local (0,0)-based coordinates.
func drawCursorOverlay(hMemDC uintptr, originX, originY int) {
ci := cursorInfo{CbSize: uint32(unsafe.Sizeof(cursorInfo{}))}
ret, _, _ := procGetCursorInfo.Call(uintptr(unsafe.Pointer(&ci)))
if ret == 0 || ci.Flags&cursorShowing == 0 || ci.HCursor == 0 {
return
}
var ii iconInfo
if ret, _, _ := procGetIconInfo.Call(ci.HCursor, uintptr(unsafe.Pointer(&ii))); ret == 0 {
return
}
if ii.HbmMask != 0 {
defer procDeleteObject.Call(ii.HbmMask)
}
if ii.HbmColor != 0 {
defer procDeleteObject.Call(ii.HbmColor)
}
x := int(ci.PtScreenPos.X) - int(ii.XHotspot) - originX
y := int(ci.PtScreenPos.Y) - int(ii.YHotspot) - originY
procDrawIconEx.Call(hMemDC, uintptr(x), uintptr(y), ci.HCursor, 0, 0, 0, 0, uintptr(diNormal))
}
// VirtualScreenBounds returns the origin and size of the full virtual
// desktop spanning every monitor, not just the primary one. The origin
// can be negative — a monitor positioned above or to the left of the
// primary display puts part of the virtual desktop at negative
// coordinates.
func VirtualScreenBounds() (x, y, width, height int) {
x0, _, _ := procGetSystemMetrics.Call(uintptr(smXVirtualScreen))
y0, _, _ := procGetSystemMetrics.Call(uintptr(smYVirtualScreen))
w, _, _ := procGetSystemMetrics.Call(uintptr(smCXVirtualScreen))
h, _, _ := procGetSystemMetrics.Call(uintptr(smCYVirtualScreen))
return int(int32(x0)), int(int32(y0)), int(int32(w)), int(int32(h))
}
// ScreenSize returns the dimensions CaptureScreen actually captures: the
// full virtual desktop spanning every monitor.
func ScreenSize() (int, int) {
_, _, w, h := VirtualScreenBounds()
return w, h
}
// CaptureScreen grabs every monitor's combined virtual desktop as one
// RGBA image, as if all monitors were a single wide/tall screen.
func CaptureScreen() (*image.RGBA, error) {
originX, originY, width, height := VirtualScreenBounds()
if width <= 0 || height <= 0 {
return nil, fmt.Errorf("capture: zero-size screen")
}
hScreenDC, _, _ := procGetDC.Call(0)
if hScreenDC == 0 {
return nil, fmt.Errorf("capture: GetDC failed")
}
defer procReleaseDC.Call(0, hScreenDC)
hMemDC, _, _ := procCreateCompatDC.Call(hScreenDC)
if hMemDC == 0 {
return nil, fmt.Errorf("capture: CreateCompatibleDC failed")
}
defer procDeleteDC.Call(hMemDC)
hBitmap, _, _ := procCreateCompatBmp.Call(hScreenDC, uintptr(width), uintptr(height))
if hBitmap == 0 {
return nil, fmt.Errorf("capture: CreateCompatibleBitmap failed")
}
defer procDeleteObject.Call(hBitmap)
oldObj, _, _ := procSelectObject.Call(hMemDC, hBitmap)
defer procSelectObject.Call(hMemDC, oldObj)
ret, _, _ := procBitBlt.Call(hMemDC, 0, 0, uintptr(width), uintptr(height), hScreenDC, uintptr(originX), uintptr(originY), uintptr(srcCopy))
if ret == 0 {
return nil, fmt.Errorf("capture: BitBlt failed")
}
// BitBlt doesn't capture the hardware cursor — it's composited by the
// GPU/DWM as a separate overlay, not part of the raster desktop image.
// Draw it onto our copy explicitly so the viewer can actually see it.
drawCursorOverlay(hMemDC, originX, originY)
hdr := bitmapInfoHeader{
Width: int32(width),
Height: -int32(height), // negative = top-down DIB, avoids a manual vertical flip
Planes: 1,
BitCount: 32,
Compression: biRGB,
}
hdr.Size = uint32(unsafe.Sizeof(hdr))
buf := make([]byte, width*height*4)
ret, _, _ = procGetDIBits.Call(
hMemDC,
hBitmap,
0,
uintptr(height),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&hdr)),
uintptr(dibRGBColors),
)
if ret == 0 {
return nil, fmt.Errorf("capture: GetDIBits failed")
}
img := image.NewRGBA(image.Rect(0, 0, width, height))
// GDI gives us BGRA; image.RGBA wants RGBA.
for i := 0; i < width*height; i++ {
b := buf[i*4+0]
g := buf[i*4+1]
r := buf[i*4+2]
a := buf[i*4+3]
if a == 0 {
a = 255 // desktop bitmaps report 0 alpha; the screen has no real transparency
}
img.Pix[i*4+0] = r
img.Pix[i*4+1] = g
img.Pix[i*4+2] = b
img.Pix[i*4+3] = a
}
return img, nil
}