neko/internal/desktop/manager.go

89 lines
2.1 KiB
Go
Raw Normal View History

2020-11-02 04:09:48 +13:00
package desktop
import (
2020-11-04 12:27:47 +13:00
"fmt"
2020-11-02 04:09:48 +13:00
"time"
2021-01-09 10:03:50 +13:00
"sync"
2020-11-02 04:09:48 +13:00
2020-11-04 12:09:52 +13:00
"github.com/kataras/go-events"
2020-11-02 04:09:48 +13:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2020-11-04 12:27:47 +13:00
"demodesk/neko/internal/config"
2020-11-02 04:09:48 +13:00
"demodesk/neko/internal/desktop/xorg"
)
2021-01-09 10:03:50 +13:00
var mu = sync.Mutex{}
2020-11-02 04:09:48 +13:00
type DesktopManagerCtx struct {
logger zerolog.Logger
cleanup *time.Ticker
shutdown chan bool
2020-11-04 12:27:47 +13:00
emmiter events.EventEmmiter
2020-11-02 04:09:48 +13:00
display string
2020-11-04 12:27:47 +13:00
config *config.Desktop
2020-11-02 04:09:48 +13:00
}
2020-11-04 12:27:47 +13:00
func New(display string, config *config.Desktop) *DesktopManagerCtx {
2020-11-02 04:09:48 +13:00
return &DesktopManagerCtx{
logger: log.With().Str("module", "desktop").Logger(),
cleanup: time.NewTicker(1 * time.Second),
shutdown: make(chan bool),
2020-11-04 12:27:47 +13:00
emmiter: events.New(),
2020-11-02 04:09:48 +13:00
display: display,
2020-11-04 12:27:47 +13:00
config: config,
2020-11-02 04:09:48 +13:00
}
}
func (manager *DesktopManagerCtx) Start() {
2020-11-04 12:09:52 +13:00
if err := xorg.DisplayOpen(manager.display); err != nil {
manager.logger.Warn().Err(err).Msg("unable to open dispaly")
}
xorg.GetScreenConfigurations()
2020-11-02 04:09:48 +13:00
2020-11-04 12:27:47 +13:00
manager.logger.Info().
Str("screen_size", fmt.Sprintf("%dx%d@%d", manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)).
2020-11-14 05:42:04 +13:00
Msgf("setting initial screen size")
2020-11-04 12:27:47 +13:00
if err := xorg.ChangeScreenSize(manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate); err != nil {
manager.logger.Warn().Err(err).Msg("unable to set initial screen size")
}
2020-11-02 04:09:48 +13:00
go func() {
defer func() {
2020-11-04 12:09:52 +13:00
xorg.DisplayClose()
2020-11-02 04:09:48 +13:00
manager.logger.Info().Msg("shutdown")
}()
for {
select {
case <-manager.shutdown:
return
case <-manager.cleanup.C:
xorg.CheckKeys(time.Second * 10)
}
}
}()
}
func (manager *DesktopManagerCtx) OnBeforeScreenSizeChange(listener func()) {
manager.emmiter.On("before_screen_size_change", func(payload ...interface{}) {
listener()
})
}
func (manager *DesktopManagerCtx) OnAfterScreenSizeChange(listener func()) {
manager.emmiter.On("after_screen_size_change", func(payload ...interface{}) {
listener()
2020-11-04 12:27:47 +13:00
})
}
2020-11-02 04:09:48 +13:00
func (manager *DesktopManagerCtx) Shutdown() error {
2020-11-14 05:42:04 +13:00
manager.logger.Info().Msgf("desktop shutting down")
2020-11-02 04:09:48 +13:00
manager.cleanup.Stop()
manager.shutdown <- true
return nil
}