2021-02-28 18:37:35 +01:00
|
|
|
package cursor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2021-10-21 22:53:54 +02:00
|
|
|
func NewPosition() *PositionCtx {
|
2021-02-28 18:37:35 +01:00
|
|
|
return &PositionCtx{
|
2021-08-29 18:59:46 +02:00
|
|
|
logger: log.With().Str("module", "webrtc").Str("submodule", "cursor-position").Logger(),
|
2021-02-28 18:37:35 +01:00
|
|
|
listeners: map[uintptr]*func(x, y int){},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PositionCtx struct {
|
2022-02-14 17:41:47 +00:00
|
|
|
logger zerolog.Logger
|
|
|
|
|
|
|
|
listeners map[uintptr]*func(x, y int)
|
|
|
|
listenersMu sync.Mutex
|
2021-02-28 18:37:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *PositionCtx) Shutdown() {
|
2021-08-29 18:59:46 +02:00
|
|
|
manager.logger.Info().Msg("shutdown")
|
2021-02-28 18:37:35 +01:00
|
|
|
|
2022-02-14 17:41:47 +00:00
|
|
|
manager.listenersMu.Lock()
|
2021-02-28 18:37:35 +01:00
|
|
|
for key := range manager.listeners {
|
|
|
|
delete(manager.listeners, key)
|
|
|
|
}
|
2022-02-14 17:41:47 +00:00
|
|
|
manager.listenersMu.Unlock()
|
2021-02-28 18:37:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 18:50:32 +01:00
|
|
|
func (manager *PositionCtx) Set(x, y int) {
|
2022-02-14 17:41:47 +00:00
|
|
|
manager.listenersMu.Lock()
|
|
|
|
defer manager.listenersMu.Unlock()
|
|
|
|
|
2021-02-28 18:50:32 +01:00
|
|
|
for _, emit := range manager.listeners {
|
|
|
|
(*emit)(x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 18:37:35 +01:00
|
|
|
func (manager *PositionCtx) AddListener(listener *func(x, y int)) {
|
2022-02-14 17:41:47 +00:00
|
|
|
manager.listenersMu.Lock()
|
|
|
|
defer manager.listenersMu.Unlock()
|
2021-02-28 18:37:35 +01:00
|
|
|
|
|
|
|
if listener != nil {
|
|
|
|
ptr := reflect.ValueOf(listener).Pointer()
|
|
|
|
manager.listeners[ptr] = listener
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *PositionCtx) RemoveListener(listener *func(x, y int)) {
|
2022-02-14 17:41:47 +00:00
|
|
|
manager.listenersMu.Lock()
|
|
|
|
defer manager.listenersMu.Unlock()
|
2021-02-28 18:37:35 +01:00
|
|
|
|
|
|
|
if listener != nil {
|
|
|
|
ptr := reflect.ValueOf(listener).Pointer()
|
|
|
|
delete(manager.listeners, ptr)
|
|
|
|
}
|
|
|
|
}
|