2021-02-13 10:13:55 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-02-15 02:40:17 +13:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2021-02-13 10:13:55 +13:00
|
|
|
|
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
OP_CURSOR_POSITION = 0x01
|
2021-02-15 02:40:17 +13:00
|
|
|
OP_CURSOR_IMAGE = 0x02
|
2021-02-13 10:13:55 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type PayloadCursorPosition struct {
|
|
|
|
PayloadHeader
|
|
|
|
X uint16
|
|
|
|
Y uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type PayloadCursorImage struct {
|
|
|
|
PayloadHeader
|
|
|
|
Width uint16
|
|
|
|
Height uint16
|
|
|
|
Xhot uint16
|
|
|
|
Yhot uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (webrtc_peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
|
|
|
|
if webrtc_peer.dataChannel == nil {
|
2021-02-13 11:46:57 +13:00
|
|
|
return fmt.Errorf("no data channel")
|
2021-02-13 10:13:55 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
data := PayloadCursorPosition{
|
|
|
|
PayloadHeader: PayloadHeader{
|
2021-02-15 02:40:17 +13:00
|
|
|
Event: OP_CURSOR_POSITION,
|
2021-02-13 10:13:55 +13:00
|
|
|
Length: 7,
|
|
|
|
},
|
|
|
|
X: uint16(x),
|
|
|
|
Y: uint16(y),
|
|
|
|
}
|
|
|
|
|
2021-02-13 11:46:57 +13:00
|
|
|
buffer := &bytes.Buffer{}
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
|
2021-02-13 11:46:57 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-13 10:13:55 +13:00
|
|
|
return webrtc_peer.dataChannel.Send(buffer.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (webrtc_peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error {
|
|
|
|
if webrtc_peer.dataChannel == nil {
|
2021-02-13 11:46:57 +13:00
|
|
|
return fmt.Errorf("no data channel")
|
2021-02-13 10:13:55 +13:00
|
|
|
}
|
|
|
|
|
2021-02-13 11:46:57 +13:00
|
|
|
img, err := utils.GetCursorImage(cur)
|
2021-02-13 10:13:55 +13:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := PayloadCursorImage{
|
|
|
|
PayloadHeader: PayloadHeader{
|
2021-02-15 02:40:17 +13:00
|
|
|
Event: OP_CURSOR_IMAGE,
|
2021-02-13 11:46:57 +13:00
|
|
|
Length: uint16(11 + len(img)),
|
2021-02-13 10:13:55 +13:00
|
|
|
},
|
2021-02-15 02:40:17 +13:00
|
|
|
Width: cur.Width,
|
2021-02-13 10:13:55 +13:00
|
|
|
Height: cur.Height,
|
2021-02-15 02:40:17 +13:00
|
|
|
Xhot: cur.Xhot,
|
|
|
|
Yhot: cur.Yhot,
|
2021-02-13 10:13:55 +13:00
|
|
|
}
|
|
|
|
|
2021-02-13 11:46:57 +13:00
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
|
2021-02-13 11:46:57 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Write(buffer, binary.BigEndian, img); err != nil {
|
2021-02-13 11:46:57 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-13 10:13:55 +13:00
|
|
|
return webrtc_peer.dataChannel.Send(buffer.Bytes())
|
|
|
|
}
|