2021-03-01 06:37:35 +13:00
|
|
|
package cursor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2022-03-20 23:43:00 +13:00
|
|
|
"gitlab.com/demodesk/neko/server/pkg/types"
|
|
|
|
"gitlab.com/demodesk/neko/server/pkg/utils"
|
2021-03-01 06:37:35 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewImage(desktop types.DesktopManager) *ImageCtx {
|
|
|
|
return &ImageCtx{
|
2021-08-30 04:59:46 +12:00
|
|
|
logger: log.With().Str("module", "webrtc").Str("submodule", "cursor-image").Logger(),
|
2021-03-01 06:37:35 +13:00
|
|
|
desktop: desktop,
|
|
|
|
listeners: map[uintptr]*func(entry *ImageEntry){},
|
|
|
|
cache: map[uint64]*ImageEntry{},
|
2021-03-19 01:38:27 +13:00
|
|
|
maxSerial: 200, // TODO: Cleanup?
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImageCtx struct {
|
2022-02-15 06:41:47 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
desktop types.DesktopManager
|
|
|
|
|
|
|
|
listeners map[uintptr]*func(entry *ImageEntry)
|
|
|
|
listenersMu sync.Mutex
|
|
|
|
|
2021-03-01 06:37:35 +13:00
|
|
|
cache map[uint64]*ImageEntry
|
2022-02-15 06:41:47 +13:00
|
|
|
cacheMu sync.Mutex
|
2021-03-01 06:37:35 +13:00
|
|
|
current *ImageEntry
|
2021-03-19 01:38:27 +13:00
|
|
|
maxSerial uint64
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
type ImageEntry struct {
|
|
|
|
Cursor *types.CursorImage
|
|
|
|
Image []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ImageCtx) Start() {
|
|
|
|
manager.desktop.OnCursorChanged(func(serial uint64) {
|
|
|
|
entry, err := manager.GetCached(serial)
|
|
|
|
if err != nil {
|
2021-09-02 09:10:06 +12:00
|
|
|
manager.logger.Err(err).Msg("failed to get cursor image")
|
2021-03-01 06:37:35 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-19 01:38:27 +13:00
|
|
|
manager.current = entry
|
2022-02-15 06:41:47 +13:00
|
|
|
|
|
|
|
manager.listenersMu.Lock()
|
2021-03-01 06:37:35 +13:00
|
|
|
for _, emit := range manager.listeners {
|
|
|
|
(*emit)(entry)
|
|
|
|
}
|
2022-02-15 06:41:47 +13:00
|
|
|
manager.listenersMu.Unlock()
|
2021-03-01 06:37:35 +13:00
|
|
|
})
|
2021-08-30 04:59:46 +12:00
|
|
|
|
|
|
|
manager.logger.Info().Msg("starting")
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ImageCtx) Shutdown() {
|
2021-08-30 04:59:46 +12:00
|
|
|
manager.logger.Info().Msg("shutdown")
|
2021-03-01 06:37:35 +13:00
|
|
|
|
2022-02-15 06:41:47 +13:00
|
|
|
manager.listenersMu.Lock()
|
2021-03-01 06:37:35 +13:00
|
|
|
for key := range manager.listeners {
|
|
|
|
delete(manager.listeners, key)
|
|
|
|
}
|
2022-02-15 06:41:47 +13:00
|
|
|
manager.listenersMu.Unlock()
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ImageCtx) GetCached(serial uint64) (*ImageEntry, error) {
|
2021-03-01 06:52:37 +13:00
|
|
|
// zero means no serial available
|
2021-03-19 01:38:27 +13:00
|
|
|
if serial == 0 || serial > manager.maxSerial {
|
2021-03-26 00:20:30 +13:00
|
|
|
manager.logger.Debug().Uint64("serial", serial).Msg("cache bypass")
|
2021-03-01 06:52:37 +13:00
|
|
|
return manager.fetchEntry()
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:37:35 +13:00
|
|
|
manager.cacheMu.Lock()
|
|
|
|
entry, ok := manager.cache[serial]
|
|
|
|
manager.cacheMu.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := manager.fetchEntry()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.cacheMu.Lock()
|
|
|
|
manager.cache[serial] = entry
|
|
|
|
manager.cacheMu.Unlock()
|
|
|
|
|
2021-03-26 00:20:30 +13:00
|
|
|
manager.logger.Debug().Uint64("serial", serial).Msg("cache miss")
|
2021-03-01 06:37:35 +13:00
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:50:32 +13:00
|
|
|
func (manager *ImageCtx) Get() (*ImageEntry, error) {
|
2021-03-01 06:37:35 +13:00
|
|
|
if manager.current != nil {
|
|
|
|
return manager.current, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return manager.fetchEntry()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ImageCtx) AddListener(listener *func(entry *ImageEntry)) {
|
2022-02-15 06:41:47 +13:00
|
|
|
manager.listenersMu.Lock()
|
|
|
|
defer manager.listenersMu.Unlock()
|
2021-03-01 06:37:35 +13:00
|
|
|
|
|
|
|
if listener != nil {
|
|
|
|
ptr := reflect.ValueOf(listener).Pointer()
|
|
|
|
manager.listeners[ptr] = listener
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ImageCtx) RemoveListener(listener *func(entry *ImageEntry)) {
|
2022-02-15 06:41:47 +13:00
|
|
|
manager.listenersMu.Lock()
|
|
|
|
defer manager.listenersMu.Unlock()
|
2021-03-01 06:37:35 +13:00
|
|
|
|
|
|
|
if listener != nil {
|
|
|
|
ptr := reflect.ValueOf(listener).Pointer()
|
|
|
|
delete(manager.listeners, ptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ImageCtx) fetchEntry() (*ImageEntry, error) {
|
|
|
|
cur := manager.desktop.GetCursorImage()
|
|
|
|
|
|
|
|
img, err := utils.CreatePNGImage(cur.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &ImageEntry{
|
|
|
|
Cursor: cur,
|
|
|
|
Image: img,
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|