2020-11-02 04:09:48 +13:00
|
|
|
package desktop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
"demodesk/neko/internal/desktop/xorg"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DesktopManagerCtx struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
cleanup *time.Ticker
|
|
|
|
shutdown chan bool
|
|
|
|
display string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(display string) *DesktopManagerCtx {
|
|
|
|
return &DesktopManagerCtx{
|
|
|
|
logger: log.With().Str("module", "desktop").Logger(),
|
|
|
|
cleanup: time.NewTicker(1 * time.Second),
|
|
|
|
shutdown: make(chan bool),
|
|
|
|
display: display,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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) Shutdown() error {
|
|
|
|
manager.logger.Info().Msgf("remote shutting down")
|
|
|
|
|
|
|
|
manager.cleanup.Stop()
|
|
|
|
manager.shutdown <- true
|
|
|
|
return nil
|
|
|
|
}
|