2021-03-01 06:37:35 +13:00
|
|
|
package cursor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
|
|
|
"github.com/demodesk/neko/pkg/utils"
|
2021-03-01 06:37:35 +13:00
|
|
|
)
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
type ImageListener interface {
|
|
|
|
SendCursorImage(cur *types.CursorImage, img []byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Image interface {
|
|
|
|
Start()
|
|
|
|
Shutdown()
|
|
|
|
GetCurrent() (cur *types.CursorImage, img []byte, err error)
|
|
|
|
AddListener(listener ImageListener)
|
|
|
|
RemoveListener(listener ImageListener)
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageEntry struct {
|
|
|
|
*types.CursorImage
|
|
|
|
ImagePNG []byte
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
type image struct {
|
2022-02-15 06:41:47 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
desktop types.DesktopManager
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
listeners map[uintptr]ImageListener
|
|
|
|
listenersMu sync.RWMutex
|
2022-02-15 06:41:47 +13:00
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
cache map[uint64]*imageEntry
|
|
|
|
cacheMu sync.RWMutex
|
|
|
|
current *imageEntry
|
2021-03-19 01:38:27 +13:00
|
|
|
maxSerial uint64
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
func NewImage(logger zerolog.Logger, desktop types.DesktopManager) *image {
|
|
|
|
return &image{
|
|
|
|
logger: logger.With().Str("submodule", "cursor-image").Logger(),
|
|
|
|
desktop: desktop,
|
|
|
|
listeners: map[uintptr]ImageListener{},
|
|
|
|
cache: map[uint64]*imageEntry{},
|
|
|
|
maxSerial: 300, // TODO: Cleanup?
|
|
|
|
}
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
func (manager *image) Start() {
|
2021-03-01 06:37:35 +13:00
|
|
|
manager.desktop.OnCursorChanged(func(serial uint64) {
|
2023-04-17 10:42:29 +12:00
|
|
|
entry, err := manager.getCached(serial)
|
2021-03-01 06:37:35 +13:00
|
|
|
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
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
manager.listenersMu.RLock()
|
|
|
|
for _, l := range manager.listeners {
|
|
|
|
if err := l.SendCursorImage(entry.CursorImage, entry.ImagePNG); err != nil {
|
|
|
|
manager.logger.Err(err).Msg("failed to set cursor image")
|
|
|
|
}
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
2023-04-17 10:42:29 +12:00
|
|
|
manager.listenersMu.RUnlock()
|
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
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
func (manager *image) 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
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
func (manager *image) 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()
|
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
manager.cacheMu.RLock()
|
2021-03-01 06:37:35 +13:00
|
|
|
entry, ok := manager.cache[serial]
|
2023-04-17 10:42:29 +12:00
|
|
|
manager.cacheMu.RUnlock()
|
2021-03-01 06:37:35 +13:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
manager.logger.Debug().Uint64("serial", serial).Msg("cache miss")
|
|
|
|
|
2021-03-01 06:37:35 +13:00
|
|
|
entry, err := manager.fetchEntry()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.cacheMu.Lock()
|
2023-04-17 10:42:29 +12:00
|
|
|
manager.cache[entry.Serial] = entry
|
2021-03-01 06:37:35 +13:00
|
|
|
manager.cacheMu.Unlock()
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
if entry.Serial != serial {
|
|
|
|
manager.logger.Warn().
|
|
|
|
Uint64("requested_serial", serial).
|
|
|
|
Uint64("received_serial", entry.Serial).
|
|
|
|
Msg("serial mismatch")
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:37:35 +13:00
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
func (manager *image) GetCurrent() (cur *types.CursorImage, img []byte, err error) {
|
2021-03-01 06:37:35 +13:00
|
|
|
if manager.current != nil {
|
2023-04-17 10:42:29 +12:00
|
|
|
return manager.current.CursorImage, manager.current.ImagePNG, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := manager.fetchEntry()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
manager.current = entry
|
|
|
|
return entry.CursorImage, entry.ImagePNG, nil
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
func (manager *image) AddListener(listener ImageListener) {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
func (manager *image) RemoveListener(listener ImageListener) {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
func (manager *image) fetchEntry() (*imageEntry, error) {
|
2021-03-01 06:37:35 +13:00
|
|
|
cur := manager.desktop.GetCursorImage()
|
|
|
|
|
|
|
|
img, err := utils.CreatePNGImage(cur.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-17 10:42:29 +12:00
|
|
|
cur.Image = nil // free memory
|
2021-03-01 06:37:35 +13:00
|
|
|
|
2023-04-17 10:42:29 +12:00
|
|
|
return &imageEntry{
|
|
|
|
CursorImage: cur,
|
|
|
|
ImagePNG: img,
|
|
|
|
}, nil
|
2021-03-01 06:37:35 +13:00
|
|
|
}
|