neko/internal/desktop/manager.go

103 lines
2.3 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"
2021-01-09 10:03:50 +13:00
"sync"
2021-02-15 02:40:17 +13:00
"time"
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"
"github.com/demodesk/neko/internal/config"
"github.com/demodesk/neko/pkg/xevent"
"github.com/demodesk/neko/pkg/xorg"
2020-11-02 04:09:48 +13:00
)
2021-01-09 10:03:50 +13:00
var mu = sync.Mutex{}
2020-11-02 04:09:48 +13:00
type DesktopManagerCtx struct {
2021-02-15 02:40:17 +13:00
logger zerolog.Logger
2021-09-02 10:00:56 +12:00
wg sync.WaitGroup
2021-10-05 01:40:20 +13:00
shutdown chan struct{}
2021-02-15 02:40:17 +13:00
emmiter events.EventEmmiter
config *config.Desktop
2020-11-02 04:09:48 +13:00
}
2021-03-12 05:44:49 +13:00
func New(config *config.Desktop) *DesktopManagerCtx {
2020-11-02 04:09:48 +13:00
return &DesktopManagerCtx{
2021-02-15 02:40:17 +13:00
logger: log.With().Str("module", "desktop").Logger(),
2021-10-05 01:40:20 +13:00
shutdown: make(chan struct{}),
2021-02-15 02:40:17 +13:00
emmiter: events.New(),
config: config,
2020-11-02 04:09:48 +13:00
}
}
func (manager *DesktopManagerCtx) Start() {
2021-03-12 05:44:49 +13:00
if xorg.DisplayOpen(manager.config.Display) {
manager.logger.Panic().Str("display", manager.config.Display).Msg("unable to open display")
2020-11-04 12:09:52 +13:00
}
xorg.GetScreenConfigurations()
2020-11-02 04:09:48 +13:00
width, height, rate, err := xorg.ChangeScreenSize(manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)
2021-09-02 10:00:56 +12:00
manager.logger.Err(err).
Str("screen_size", fmt.Sprintf("%dx%d@%d", width, height, rate)).
2020-11-14 05:42:04 +13:00
Msgf("setting initial screen size")
2020-11-04 12:27:47 +13:00
2023-04-23 03:06:35 +12:00
xevent.Unminimize = manager.config.Unminimize
2021-03-12 05:44:49 +13:00
go xevent.EventLoop(manager.config.Display)
2021-01-11 03:58:17 +13:00
// In case it was opened
go manager.CloseFileChooserDialog()
2021-01-19 10:01:11 +13: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")
})
2021-09-02 10:00:56 +12:00
manager.wg.Add(1)
2020-11-02 04:09:48 +13:00
go func() {
2021-09-02 10:00:56 +12:00
defer manager.wg.Done()
2021-08-20 07:19:02 +12:00
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
2020-11-02 04:09:48 +13:00
for {
select {
case <-manager.shutdown:
return
2021-08-20 07:19:02 +12:00
case <-ticker.C:
2020-11-02 04:09:48 +13:00
xorg.CheckKeys(time.Second * 10)
}
}
}()
}
func (manager *DesktopManagerCtx) OnBeforeScreenSizeChange(listener func()) {
manager.emmiter.On("before_screen_size_change", func(payload ...any) {
listener()
})
}
func (manager *DesktopManagerCtx) OnAfterScreenSizeChange(listener func()) {
manager.emmiter.On("after_screen_size_change", func(payload ...any) {
listener()
2020-11-04 12:27:47 +13:00
})
}
2020-11-02 04:09:48 +13:00
func (manager *DesktopManagerCtx) Shutdown() error {
2021-09-02 10:00:56 +12:00
manager.logger.Info().Msgf("shutdown")
2020-11-02 04:09:48 +13:00
2021-09-14 09:50:16 +12:00
close(manager.shutdown)
2021-09-02 10:00:56 +12:00
manager.wg.Wait()
xorg.DisplayClose()
2020-11-02 04:09:48 +13:00
return nil
}