neko/internal/desktop/manager.go

103 lines
2.3 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"
"github.com/demodesk/neko/internal/config"
"github.com/demodesk/neko/pkg/xevent"
"github.com/demodesk/neko/pkg/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
2021-09-02 00:00:56 +02:00
wg sync.WaitGroup
2021-10-04 14:40:20 +02:00
shutdown chan struct{}
2021-02-14 14:40:17 +01:00
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(),
2021-10-04 14:40:20 +02:00
shutdown: make(chan struct{}),
2021-02-14 14:40:17 +01:00
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
width, height, rate, err := xorg.ChangeScreenSize(manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)
2021-09-02 00:00:56 +02:00
manager.logger.Err(err).
Str("screen_size", fmt.Sprintf("%dx%d@%d", width, height, rate)).
2020-11-13 17:42:04 +01:00
Msgf("setting initial screen size")
2020-11-04 00:27:47 +01:00
2023-04-22 17:06:35 +02:00
xevent.Unminimize = manager.config.Unminimize
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")
})
2021-09-02 00:00:56 +02:00
manager.wg.Add(1)
2020-11-01 16:09:48 +01:00
go func() {
2021-09-02 00:00:56 +02:00
defer manager.wg.Done()
2021-08-19 21:19:02 +02:00
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
2020-11-01 16:09:48 +01:00
for {
select {
case <-manager.shutdown:
return
2021-08-19 21:19:02 +02:00
case <-ticker.C:
2020-11-01 16:09:48 +01: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 00:27:47 +01:00
})
}
2020-11-01 16:09:48 +01:00
func (manager *DesktopManagerCtx) Shutdown() error {
2021-09-02 00:00:56 +02:00
manager.logger.Info().Msgf("shutdown")
2020-11-01 16:09:48 +01:00
2021-09-13 23:50:16 +02:00
close(manager.shutdown)
2021-09-02 00:00:56 +02:00
manager.wg.Wait()
xorg.DisplayClose()
2020-11-01 16:09:48 +01:00
return nil
}