neko/internal/desktop/manager.go

136 lines
3.2 KiB
Go
Raw Normal View History

2020-11-02 04:09:48 +13:00
package desktop
import (
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/types"
"github.com/demodesk/neko/pkg/xevent"
"github.com/demodesk/neko/pkg/xinput"
"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 {
logger zerolog.Logger
wg sync.WaitGroup
shutdown chan struct{}
emmiter events.EventEmmiter
config *config.Desktop
screenSize types.ScreenSize // cached screen size
input xinput.Driver
2020-11-02 04:09:48 +13:00
}
2021-03-12 05:44:49 +13:00
func New(config *config.Desktop) *DesktopManagerCtx {
var input xinput.Driver
if config.UseInputDriver {
input = xinput.NewDriver(config.InputSocket)
} else {
input = xinput.NewDummy()
}
2020-11-02 04:09:48 +13:00
return &DesktopManagerCtx{
logger: log.With().Str("module", "desktop").Logger(),
shutdown: make(chan struct{}),
emmiter: events.New(),
config: config,
screenSize: config.ScreenSize,
input: input,
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
screenSize, err := xorg.ChangeScreenSize(manager.config.ScreenSize)
if err != nil {
manager.logger.Err(err).
Str("screen_size", screenSize.String()).
Msgf("unable to set initial screen size")
} else {
// cache screen size
manager.screenSize = screenSize
manager.logger.Info().
Str("screen_size", screenSize.String()).
Msgf("setting initial screen size")
}
err = manager.input.Connect()
if err != nil {
// TODO: fail silently to dummy driver?
manager.logger.Panic().Err(err).Msg("unable to connect to input driver")
}
2020-11-04 12:27:47 +13:00
// set up event listeners
2023-04-23 03:06:35 +12:00
xevent.Unminimize = manager.config.Unminimize
xevent.FileChooserDialog = manager.config.FileChooserDialog
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
if manager.config.FileChooserDialog {
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
const debounceDuration = 10 * time.Second
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:
xorg.CheckKeys(debounceDuration)
manager.input.Debounce(debounceDuration)
2020-11-02 04:09:48 +13:00
}
}
}()
}
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
}