2020-10-23 03:54:50 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"strconv"
|
|
|
|
|
2021-02-02 10:45:44 +13:00
|
|
|
"github.com/pion/webrtc/v3"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
const (
|
2021-02-15 02:40:17 +13:00
|
|
|
OP_MOVE = 0x01
|
|
|
|
OP_SCROLL = 0x02
|
2020-11-02 04:09:48 +13:00
|
|
|
OP_KEY_DOWN = 0x03
|
2021-02-15 02:40:17 +13:00
|
|
|
OP_KEY_UP = 0x04
|
|
|
|
OP_KEY_CLK = 0x05
|
2020-11-02 04:09:48 +13:00
|
|
|
)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
type PayloadHeader struct {
|
|
|
|
Event uint8
|
|
|
|
Length uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type PayloadMove struct {
|
|
|
|
PayloadHeader
|
|
|
|
X uint16
|
|
|
|
Y uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type PayloadScroll struct {
|
|
|
|
PayloadHeader
|
|
|
|
X int16
|
|
|
|
Y int16
|
|
|
|
}
|
|
|
|
|
|
|
|
type PayloadKey struct {
|
|
|
|
PayloadHeader
|
|
|
|
Key uint64
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *WebRTCManagerCtx) handle(msg webrtc.DataChannelMessage) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
buffer := bytes.NewBuffer(msg.Data)
|
|
|
|
header := &PayloadHeader{}
|
|
|
|
hbytes := make([]byte, 3)
|
|
|
|
|
|
|
|
if _, err := buffer.Read(hbytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Read(bytes.NewBuffer(hbytes), binary.BigEndian, header); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = bytes.NewBuffer(msg.Data)
|
|
|
|
|
|
|
|
switch header.Event {
|
|
|
|
case OP_MOVE:
|
|
|
|
payload := &PayloadMove{}
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.desktop.Move(int(payload.X), int(payload.Y))
|
2020-10-23 03:54:50 +13:00
|
|
|
case OP_SCROLL:
|
|
|
|
payload := &PayloadScroll{}
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.logger.
|
|
|
|
Debug().
|
|
|
|
Str("x", strconv.Itoa(int(payload.X))).
|
|
|
|
Str("y", strconv.Itoa(int(payload.Y))).
|
|
|
|
Msg("scroll")
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.desktop.Scroll(int(payload.X), int(payload.Y))
|
2020-10-23 03:54:50 +13:00
|
|
|
case OP_KEY_DOWN:
|
|
|
|
payload := &PayloadKey{}
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Key < 8 {
|
2020-11-02 04:09:48 +13:00
|
|
|
err := manager.desktop.ButtonDown(int(payload.Key))
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
manager.logger.Warn().Err(err).Msg("button down failed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.logger.Debug().Msgf("button down %d", payload.Key)
|
|
|
|
} else {
|
2020-11-02 04:09:48 +13:00
|
|
|
err := manager.desktop.KeyDown(uint64(payload.Key))
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
manager.logger.Warn().Err(err).Msg("key down failed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.logger.Debug().Msgf("key down %d", payload.Key)
|
|
|
|
}
|
|
|
|
case OP_KEY_UP:
|
|
|
|
payload := &PayloadKey{}
|
2021-02-13 12:01:56 +13:00
|
|
|
err := binary.Read(buffer, binary.BigEndian, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Key < 8 {
|
2020-11-02 04:09:48 +13:00
|
|
|
err := manager.desktop.ButtonUp(int(payload.Key))
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
manager.logger.Warn().Err(err).Msg("button up failed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.logger.Debug().Msgf("button up %d", payload.Key)
|
|
|
|
} else {
|
2020-11-02 04:09:48 +13:00
|
|
|
err := manager.desktop.KeyUp(uint64(payload.Key))
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
manager.logger.Warn().Err(err).Msg("key up failed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.logger.Debug().Msgf("key up %d", payload.Key)
|
|
|
|
}
|
|
|
|
case OP_KEY_CLK:
|
|
|
|
// unused
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|