neko/server/internal/desktop/xorg.go

150 lines
3.3 KiB
Go
Raw Permalink Normal View History

2022-09-13 08:12:47 +12:00
package desktop
import (
2022-09-14 07:40:40 +12:00
"image"
2022-09-13 08:12:47 +12:00
"os/exec"
2022-09-14 07:40:40 +12:00
"regexp"
"time"
2022-09-13 08:12:47 +12:00
"m1k1o/neko/internal/desktop/xorg"
"m1k1o/neko/internal/types"
)
func (manager *DesktopManagerCtx) Move(x, y int) {
xorg.Move(x, y)
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) GetCursorPosition() (int, int) {
return xorg.GetCursorPosition()
}
2022-09-13 08:12:47 +12:00
func (manager *DesktopManagerCtx) Scroll(x, y int) {
xorg.Scroll(x, y)
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) ButtonDown(code uint32) error {
2022-09-13 08:12:47 +12:00
return xorg.ButtonDown(code)
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) KeyDown(code uint32) error {
2022-09-13 08:12:47 +12:00
return xorg.KeyDown(code)
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) ButtonUp(code uint32) error {
2022-09-13 08:12:47 +12:00
return xorg.ButtonUp(code)
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) KeyUp(code uint32) error {
2022-09-13 08:12:47 +12:00
return xorg.KeyUp(code)
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) ButtonPress(code uint32) error {
xorg.ResetKeys()
defer xorg.ResetKeys()
return xorg.ButtonDown(code)
}
func (manager *DesktopManagerCtx) KeyPress(codes ...uint32) error {
xorg.ResetKeys()
defer xorg.ResetKeys()
for _, code := range codes {
if err := xorg.KeyDown(code); err != nil {
return err
}
}
if len(codes) > 1 {
time.Sleep(10 * time.Millisecond)
}
return nil
}
2022-09-13 08:12:47 +12:00
func (manager *DesktopManagerCtx) ResetKeys() {
xorg.ResetKeys()
}
func (manager *DesktopManagerCtx) ScreenConfigurations() map[int]types.ScreenConfiguration {
return xorg.ScreenConfigurations
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) SetScreenSize(size types.ScreenSize) error {
2022-09-17 21:08:17 +12:00
mu.Lock()
2023-01-30 06:00:56 +13:00
manager.GetScreenSizeChangeChannel() <- true
2022-09-17 21:08:17 +12:00
defer func() {
2023-01-30 06:00:56 +13:00
manager.GetScreenSizeChangeChannel() <- false
2022-09-17 21:08:17 +12:00
mu.Unlock()
}()
2022-09-14 07:40:40 +12:00
return xorg.ChangeScreenSize(size.Width, size.Height, size.Rate)
}
2022-09-13 08:12:47 +12:00
func (manager *DesktopManagerCtx) GetScreenSize() *types.ScreenSize {
return xorg.GetScreenSize()
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) SetKeyboardMap(kbd types.KeyboardMap) error {
// TOOD: Use native API.
cmd := exec.Command("setxkbmap", "-layout", kbd.Layout, "-variant", kbd.Variant)
_, err := cmd.Output()
return err
}
func (manager *DesktopManagerCtx) GetKeyboardMap() (*types.KeyboardMap, error) {
// TOOD: Use native API.
cmd := exec.Command("setxkbmap", "-query")
res, err := cmd.Output()
if err != nil {
return nil, err
}
kbd := types.KeyboardMap{}
re := regexp.MustCompile(`layout:\s+(.*)\n`)
arr := re.FindStringSubmatch(string(res))
if len(arr) > 1 {
kbd.Layout = arr[1]
}
re = regexp.MustCompile(`variant:\s+(.*)\n`)
arr = re.FindStringSubmatch(string(res))
if len(arr) > 1 {
kbd.Variant = arr[1]
}
return &kbd, nil
}
func (manager *DesktopManagerCtx) SetKeyboardModifiers(mod types.KeyboardModifiers) {
if mod.NumLock != nil {
xorg.SetKeyboardModifier(xorg.KbdModNumLock, *mod.NumLock)
}
if mod.CapsLock != nil {
xorg.SetKeyboardModifier(xorg.KbdModCapsLock, *mod.CapsLock)
}
}
func (manager *DesktopManagerCtx) GetKeyboardModifiers() types.KeyboardModifiers {
modifiers := xorg.GetKeyboardModifiers()
NumLock := (modifiers & xorg.KbdModNumLock) != 0
CapsLock := (modifiers & xorg.KbdModCapsLock) != 0
return types.KeyboardModifiers{
NumLock: &NumLock,
CapsLock: &CapsLock,
}
}
func (manager *DesktopManagerCtx) GetCursorImage() *types.CursorImage {
return xorg.GetCursorImage()
2022-09-13 08:12:47 +12:00
}
2022-09-14 07:40:40 +12:00
func (manager *DesktopManagerCtx) GetScreenshotImage() *image.RGBA {
return xorg.GetScreenshotImage()
2022-09-13 08:12:47 +12:00
}