GetCursorImage returns image structure.

This commit is contained in:
Miroslav Šedivý 2021-02-25 14:03:57 +01:00
parent dc29f71b0c
commit 8f142e4b91
3 changed files with 24 additions and 28 deletions

View File

@ -243,17 +243,33 @@ func GetCursorImage() *types.CursorImage {
cur := C.XGetCursorImage() cur := C.XGetCursorImage()
defer C.XFree(unsafe.Pointer(cur)) defer C.XFree(unsafe.Pointer(cur))
width := uint16(cur.width) width := int(cur.width)
height := uint16(cur.height) height := int(cur.height)
// Xlib stores 32-bit data in longs, even if longs are 64-bits long.
pixels := C.GoBytes(unsafe.Pointer(cur.pixels), C.int(width*height*8))
img := image.NewRGBA(image.Rect(0, 0, width, height))
for row := 0; row < height; row++ {
for col := 0; col < width; col++ {
pos := ((row * height) + col) * 8
img.SetRGBA(col, row, color.RGBA{
A: pixels[pos+3],
R: pixels[pos+2],
G: pixels[pos+1],
B: pixels[pos+0],
})
}
}
return &types.CursorImage{ return &types.CursorImage{
Width: width, Width: uint16(width),
Height: height, Height: uint16(height),
Xhot: uint16(cur.xhot), Xhot: uint16(cur.xhot),
Yhot: uint16(cur.yhot), Yhot: uint16(cur.yhot),
Serial: uint64(cur.cursor_serial), Serial: uint64(cur.cursor_serial),
// Xlib stores 32-bit data in longs, even if longs are 64-bits long. Image: img,
Pixels: C.GoBytes(unsafe.Pointer(cur.pixels), C.int(width*height*8)),
} }
} }

View File

@ -10,7 +10,7 @@ type CursorImage struct {
Xhot uint16 Xhot uint16
Yhot uint16 Yhot uint16
Serial uint64 Serial uint64
Pixels []byte Image *image.RGBA
} }
type ScreenSize struct { type ScreenSize struct {

View File

@ -3,34 +3,14 @@ package utils
import ( import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"image"
"image/color"
"image/png" "image/png"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func GetCursorImage(cursor *types.CursorImage) ([]byte, error) { func GetCursorImage(cursor *types.CursorImage) ([]byte, error) {
width := int(cursor.Width)
height := int(cursor.Height)
pixels := cursor.Pixels
img := image.NewRGBA(image.Rect(0, 0, width, height))
for row := 0; row < height; row++ {
for col := 0; col < width; col++ {
pos := ((row * height) + col) * 8
img.SetRGBA(col, row, color.RGBA{
A: pixels[pos+3],
R: pixels[pos+2],
G: pixels[pos+1],
B: pixels[pos+0],
})
}
}
out := new(bytes.Buffer) out := new(bytes.Buffer)
err := png.Encode(out, img) err := png.Encode(out, cursor.Image)
if err != nil { if err != nil {
return nil, err return nil, err
} }