mirror of
https://github.com/m1k1o/neko.git
synced 2025-06-02 19:03:54 +02:00
WebRTC cursor handler extracted + cache.
This commit is contained in:
parent
9d60468aea
commit
66618b9e62
3 changed files with 217 additions and 47 deletions
68
internal/webrtc/cursor/position.go
Normal file
68
internal/webrtc/cursor/position.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package cursor
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"demodesk/neko/internal/types"
|
||||
)
|
||||
|
||||
func NewPosition(desktop types.DesktopManager) *PositionCtx {
|
||||
return &PositionCtx{
|
||||
logger: log.With().Str("module", "cursor-position").Logger(),
|
||||
desktop: desktop,
|
||||
listeners: map[uintptr]*func(x, y int){},
|
||||
}
|
||||
}
|
||||
|
||||
type PositionCtx struct {
|
||||
logger zerolog.Logger
|
||||
desktop types.DesktopManager
|
||||
emitMu sync.Mutex
|
||||
listeners map[uintptr]*func(x, y int)
|
||||
}
|
||||
|
||||
func (manager *PositionCtx) Start() {
|
||||
manager.desktop.OnCursorPosition(func(x, y int) {
|
||||
for _, emit := range manager.listeners {
|
||||
(*emit)(x, y)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (manager *PositionCtx) Shutdown() {
|
||||
manager.logger.Info().Msgf("shutting down")
|
||||
|
||||
manager.emitMu.Lock()
|
||||
for key := range manager.listeners {
|
||||
delete(manager.listeners, key)
|
||||
}
|
||||
manager.emitMu.Unlock()
|
||||
}
|
||||
|
||||
func (manager *PositionCtx) GetCurrent() (x, y int) {
|
||||
return manager.desktop.GetCursorPosition()
|
||||
}
|
||||
|
||||
func (manager *PositionCtx) AddListener(listener *func(x, y int)) {
|
||||
manager.emitMu.Lock()
|
||||
defer manager.emitMu.Unlock()
|
||||
|
||||
if listener != nil {
|
||||
ptr := reflect.ValueOf(listener).Pointer()
|
||||
manager.listeners[ptr] = listener
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *PositionCtx) RemoveListener(listener *func(x, y int)) {
|
||||
manager.emitMu.Lock()
|
||||
defer manager.emitMu.Unlock()
|
||||
|
||||
if listener != nil {
|
||||
ptr := reflect.ValueOf(listener).Pointer()
|
||||
delete(manager.listeners, ptr)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue