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"
|
|
|
|
|
2020-11-04 12:27:47 +13:00
|
|
|
"demodesk/neko/internal/config"
|
2021-01-11 03:58:17 +13:00
|
|
|
"demodesk/neko/internal/desktop/xevent"
|
2021-02-15 02:40:17 +13:00
|
|
|
"demodesk/neko/internal/desktop/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
|
|
|
|
shutdown chan bool
|
|
|
|
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(),
|
|
|
|
shutdown: make(chan bool),
|
|
|
|
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
|
|
|
|
2020-11-04 12:27:47 +13:00
|
|
|
manager.logger.Info().
|
|
|
|
Str("screen_size", fmt.Sprintf("%dx%d@%d", manager.config.ScreenWidth, manager.config.ScreenHeight, manager.config.ScreenRate)).
|
2020-11-14 05:42:04 +13:00
|
|
|
Msgf("setting initial screen size")
|
2020-11-04 12:27:47 +13: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-12 05:44:49 +13:00
|
|
|
go xevent.EventLoop(manager.config.Display)
|
2021-01-11 03:58:17 +13:00
|
|
|
|
2021-01-20 09:01:31 +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")
|
|
|
|
})
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
go func() {
|
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:
|
2021-08-20 07:19:02 +12:00
|
|
|
xorg.DisplayClose()
|
2020-11-02 04:09:48 +13:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-11-08 05:22:25 +13:00
|
|
|
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 12:27:47 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *DesktopManagerCtx) Shutdown() error {
|
2020-11-14 05:42:04 +13:00
|
|
|
manager.logger.Info().Msgf("desktop shutting down")
|
2020-11-02 04:09:48 +13:00
|
|
|
|
|
|
|
manager.shutdown <- true
|
|
|
|
return nil
|
|
|
|
}
|