package handler import ( "bytes" "image/color" "image/jpeg" "math" "net/http" "time" "github.com/kbinani/screenshot" "github.com/labstack/echo/v4" "gitnet.fr/deblan/remote-i3wm-go/internal/pointer" ) func CaptureHandler(c echo.Context) error { bounds := screenshot.GetDisplayBounds(0) switch c.QueryParam("type") { case "screenshot": if img, err := screenshot.CaptureRect(bounds); err == nil { var buf bytes.Buffer jpeg.Encode(&buf, img, nil) c.Response().Header().Set("Content-Type", "image/jpeg") return c.Blob(http.StatusOK, "image/jpeg", buf.Bytes()) } default: c.Response().Header().Set("Content-Type", "multipart/x-mixed-replace; boundary=frame") for { if img, err := screenshot.CaptureRect(bounds); err == nil { var buf bytes.Buffer if c.QueryParam("pointer") == "1" { currentX, currentY := pointer.Positions() pointerSize := 2 * 16.0 pixelColor := color.RGBA{ R: 255, G: 0, B: 0, A: 255, } for x := math.Max(0.0, currentX-pointerSize/2); x <= currentX+3; x++ { for y := math.Max(0.0, currentY-pointerSize/2); y < currentY+3; y++ { img.SetRGBA(int(x), int(y), pixelColor) } } } jpeg.Encode(&buf, img, nil) _, _ = c.Response().Write([]byte("--frame\r\n")) c.Response().Write([]byte("Content-Type: image/jpeg\r\n\r\n")) c.Response().Write(buf.Bytes()) c.Response().Write([]byte("\r\n")) time.Sleep(33 * time.Millisecond) } } } return nil }