broadcast cursor position via WebSockets.

This commit is contained in:
Miroslav Šedivý 2021-02-11 18:36:27 +01:00
parent e8286dec96
commit f22922191a
5 changed files with 61 additions and 1 deletions

View File

@ -9,8 +9,16 @@ import (
"demodesk/neko/internal/desktop/xorg"
)
var mousePosX int
var mousePosY int
func (manager *DesktopManagerCtx) Move(x, y int) {
xorg.Move(x, y)
mousePosX, mousePosY = x, y
}
func (manager *DesktopManagerCtx) GetMousePositon() (x, y int) {
return mousePosX, mousePosY
}
func (manager *DesktopManagerCtx) Scroll(x, y int) {

View File

@ -48,6 +48,7 @@ type DesktopManager interface {
// xorg
Move(x, y int)
GetMousePositon() (x, y int)
Scroll(x, y int)
ButtonDown(code int) error
KeyDown(code uint64) error

View File

@ -47,7 +47,8 @@ const (
)
const (
CURSOR_IMAGE = "cursor/image"
CURSOR_IMAGE = "cursor/image"
CURSOR_POSITION = "cursor/position"
)
const (

View File

@ -172,6 +172,13 @@ type CursorImage struct {
Y uint16 `json:"y"`
}
type CursorPosition struct {
Event string `json:"event,omitempty"`
MemberId string `json:"member_id"`
X uint16 `json:"x"`
Y uint16 `json:"y"`
}
/////////////////////////////
// Broadcast
/////////////////////////////

View File

@ -41,6 +41,7 @@ type WebSocketManagerCtx struct {
desktop types.DesktopManager
handler *handler.MessageHandlerCtx
handlers []types.HandlerFunction
shutdown chan bool
}
func (ws *WebSocketManagerCtx) Start() {
@ -133,9 +134,51 @@ func (ws *WebSocketManagerCtx) Start() {
})
ws.fileChooserDialogEvents()
go func() {
ws.logger.Debug().Msg("cursor position broadcast start")
defer func() {
ws.logger.Debug().Msg("cursor position broadcast shutdown")
}()
var posX int
var posY int
for {
select {
case <-ws.shutdown:
return
default:
time.Sleep(40 * time.Millisecond)
session := ws.sessions.GetHost()
if session == nil {
continue
}
x, y := ws.desktop.GetMousePositon()
if posX == x && posY == y {
continue
}
memberId := session.ID()
posX = x
posY = y
ws.sessions.Broadcast(message.CursorPosition{
Event: event.CURSOR_POSITION,
MemberId: memberId,
X: uint16(x),
Y: uint16(y),
}, []string{ memberId })
}
}
}()
}
func (ws *WebSocketManagerCtx) Shutdown() error {
ws.shutdown <- true
return nil
}