neko/internal/desktop/manager.go

102 lines
2.4 KiB
Go
Raw Normal View History

2020-11-01 16:09:48 +01:00
package desktop
import (
2020-11-04 00:27:47 +01:00
"fmt"
2021-01-08 22:03:50 +01:00
"sync"
2021-02-14 14:40:17 +01:00
"time"
2020-11-01 16:09:48 +01:00
2020-11-04 00:09:52 +01:00
"github.com/kataras/go-events"
2020-11-01 16:09:48 +01:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2020-11-04 00:27:47 +01:00
"demodesk/neko/internal/config"
2021-01-10 15:58:17 +01:00
"demodesk/neko/internal/desktop/xevent"
2021-02-14 14:40:17 +01:00
"demodesk/neko/internal/desktop/xorg"
2020-11-01 16:09:48 +01:00
)
2021-01-08 22:03:50 +01:00
var mu = sync.Mutex{}
2020-11-01 16:09:48 +01:00
type DesktopManagerCtx struct {
2021-02-14 14:40:17 +01:00
logger zerolog.Logger
cleanup *time.Ticker
shutdown chan bool
emmiter events.EventEmmiter
config *config.Desktop
2020-11-01 16:09:48 +01:00
}
2021-03-11 17:44:49 +01:00
func New(config *config.Desktop) *DesktopManagerCtx {
2020-11-01 16:09:48 +01:00
return &DesktopManagerCtx{
2021-02-14 14:40:17 +01:00
logger: log.With().Str("module", "desktop").Logger(),
cleanup: time.NewTicker(1 * time.Second),
shutdown: make(chan bool),
emmiter: events.New(),
config: config,
2020-11-01 16:09:48 +01:00
}
}
func (manager *DesktopManagerCtx) Start() {
2021-03-11 17:44:49 +01:00
if xorg.DisplayOpen(manager.config.Display) {
manager.logger.Panic().Str("display", manager.config.Display).Msg("unable to open display")
2020-11-04 00:09:52 +01:00
}
xorg.GetScreenConfigurations()
2020-11-01 16:09:48 +01:00
2020-11-04 00:27:47 +01:00
manager.logger.Info().
Str("screen_size", fmt.Sprintf("%dx%d@%d", manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)).
2020-11-13 17:42:04 +01:00
Msgf("setting initial screen size")
2020-11-04 00:27:47 +01: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")
}
2021-03-11 17:44:49 +01:00
go xevent.EventLoop(manager.config.Display)
2021-01-10 15:58:17 +01:00
// In case it was opened
go manager.CloseFileChooserDialog()
2021-01-18 22:01:11 +01:00
manager.OnEventError(func(error_code uint8, message string, request_code uint8, minor_code uint8) {
manager.logger.Warn().
Uint8("error_code", error_code).
Str("message", message).
Uint8("request_code", request_code).
Uint8("minor_code", minor_code).
Msg("X event error occured")
})
2020-11-01 16:09:48 +01:00
go func() {
defer func() {
2020-11-04 00:09:52 +01:00
xorg.DisplayClose()
2020-11-01 16:09:48 +01: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 00:27:47 +01:00
})
}
2020-11-01 16:09:48 +01:00
func (manager *DesktopManagerCtx) Shutdown() error {
2020-11-13 17:42:04 +01:00
manager.logger.Info().Msgf("desktop shutting down")
2020-11-01 16:09:48 +01:00
manager.cleanup.Stop()
manager.shutdown <- true
return nil
}