2022-09-13 08:12:47 +12:00
|
|
|
package desktop
|
|
|
|
|
|
|
|
import (
|
2022-09-14 07:40:40 +12:00
|
|
|
"fmt"
|
2022-09-13 08:12:47 +12:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-09-13 08:18:18 +12:00
|
|
|
"m1k1o/neko/internal/config"
|
2022-09-16 09:55:30 +12:00
|
|
|
"m1k1o/neko/internal/desktop/xevent"
|
2022-09-13 08:12:47 +12:00
|
|
|
"m1k1o/neko/internal/desktop/xorg"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2022-09-17 21:08:17 +12:00
|
|
|
var mu = sync.Mutex{}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
type DesktopManagerCtx struct {
|
2023-01-29 10:08:36 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
wg sync.WaitGroup
|
|
|
|
shutdown chan struct{}
|
|
|
|
beforeScreenSizeChangeChannel chan bool
|
|
|
|
afterScreenSizeChangeChannel chan int16
|
|
|
|
config *config.Desktop
|
2022-09-13 08:12:47 +12:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func New(config *config.Desktop) *DesktopManagerCtx {
|
2022-09-13 08:12:47 +12:00
|
|
|
return &DesktopManagerCtx{
|
2023-01-29 10:08:36 +13:00
|
|
|
logger: log.With().Str("module", "desktop").Logger(),
|
|
|
|
shutdown: make(chan struct{}),
|
|
|
|
beforeScreenSizeChangeChannel: make(chan bool),
|
|
|
|
afterScreenSizeChangeChannel: make(chan int16),
|
|
|
|
config: config,
|
2022-09-13 08:12:47 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) Start() {
|
2022-09-14 07:40:40 +12:00
|
|
|
if xorg.DisplayOpen(manager.config.Display) {
|
|
|
|
manager.logger.Panic().Str("display", manager.config.Display).Msg("unable to open display")
|
2022-09-13 08:12:47 +12:00
|
|
|
}
|
|
|
|
|
2022-09-14 07:40:40 +12:00
|
|
|
xorg.GetScreenConfigurations()
|
|
|
|
|
|
|
|
err := xorg.ChangeScreenSize(manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)
|
|
|
|
manager.logger.Err(err).
|
|
|
|
Str("screen_size", fmt.Sprintf("%dx%d@%d", manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)).
|
|
|
|
Msgf("setting initial screen size")
|
|
|
|
|
2022-09-16 09:55:30 +12:00
|
|
|
go xevent.EventLoop(manager.config.Display)
|
|
|
|
|
2023-01-22 11:43:04 +13:00
|
|
|
go func() {
|
|
|
|
for {
|
2023-01-30 01:28:03 +13:00
|
|
|
msg, ok := <-xevent.EventErrorChannel
|
|
|
|
if !ok {
|
2023-01-30 05:40:07 +13:00
|
|
|
manager.logger.Info().Msg("xevent error channel was closed")
|
2023-01-30 01:28:03 +13:00
|
|
|
return
|
|
|
|
}
|
2023-01-30 05:40:07 +13:00
|
|
|
|
2023-01-22 11:43:04 +13:00
|
|
|
manager.logger.Warn().
|
2023-01-29 10:08:36 +13:00
|
|
|
Uint8("error_code", msg.Error_code).
|
|
|
|
Str("message", msg.Message).
|
|
|
|
Uint8("request_code", msg.Request_code).
|
|
|
|
Uint8("minor_code", msg.Minor_code).
|
|
|
|
Msg("X event error occurred")
|
2023-01-22 11:43:04 +13:00
|
|
|
}
|
|
|
|
}()
|
2022-09-16 09:55:30 +12:00
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
manager.wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer manager.wg.Done()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-manager.shutdown:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
xorg.CheckKeys(time.Second * 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-01-29 10:08:36 +13:00
|
|
|
func (manager *DesktopManagerCtx) GetBeforeScreenSizeChangeChannel() chan bool {
|
2023-01-22 11:43:04 +13:00
|
|
|
return manager.beforeScreenSizeChangeChannel
|
2022-09-17 21:08:17 +12:00
|
|
|
}
|
|
|
|
|
2023-01-29 10:08:36 +13:00
|
|
|
func (manager *DesktopManagerCtx) GetAfterScreenSizeChangeChannel() chan int16 {
|
2023-01-22 11:43:04 +13:00
|
|
|
return manager.afterScreenSizeChangeChannel
|
2022-09-17 21:08:17 +12:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *DesktopManagerCtx) Shutdown() error {
|
|
|
|
manager.logger.Info().Msgf("desktop shutting down")
|
|
|
|
|
|
|
|
close(manager.shutdown)
|
|
|
|
manager.wg.Wait()
|
|
|
|
|
2022-09-14 07:40:40 +12:00
|
|
|
xorg.DisplayClose()
|
2022-09-13 08:12:47 +12:00
|
|
|
return nil
|
|
|
|
}
|