2020-11-02 04:09:48 +13:00
|
|
|
package desktop
|
|
|
|
|
|
|
|
import (
|
2021-01-22 08:44:09 +13:00
|
|
|
"image"
|
2021-01-13 11:48:15 +13:00
|
|
|
"os/exec"
|
2021-02-15 02:40:17 +13:00
|
|
|
"regexp"
|
2022-01-30 13:22:07 +13:00
|
|
|
"time"
|
2021-01-13 11:48:15 +13:00
|
|
|
|
2022-03-20 23:27:41 +13:00
|
|
|
"gitlab.com/demodesk/neko/server/internal/desktop/xorg"
|
|
|
|
"gitlab.com/demodesk/neko/server/internal/types"
|
2020-11-02 04:09:48 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) Move(x, y int) {
|
|
|
|
xorg.Move(x, y)
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:55:11 +13:00
|
|
|
func (manager *DesktopManagerCtx) GetCursorPosition() (int, int) {
|
|
|
|
return xorg.GetCursorPosition()
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *DesktopManagerCtx) Scroll(x, y int) {
|
|
|
|
xorg.Scroll(x, y)
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:25:55 +13:00
|
|
|
func (manager *DesktopManagerCtx) ButtonDown(code uint32) error {
|
2020-11-02 04:09:48 +13:00
|
|
|
return xorg.ButtonDown(code)
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:25:55 +13:00
|
|
|
func (manager *DesktopManagerCtx) KeyDown(code uint32) error {
|
2020-11-02 04:09:48 +13:00
|
|
|
return xorg.KeyDown(code)
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:25:55 +13:00
|
|
|
func (manager *DesktopManagerCtx) ButtonUp(code uint32) error {
|
2020-11-02 04:09:48 +13:00
|
|
|
return xorg.ButtonUp(code)
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:25:55 +13:00
|
|
|
func (manager *DesktopManagerCtx) KeyUp(code uint32) error {
|
2020-11-02 04:09:48 +13:00
|
|
|
return xorg.KeyUp(code)
|
|
|
|
}
|
|
|
|
|
2022-01-30 13:22:07 +13:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *DesktopManagerCtx) ResetKeys() {
|
|
|
|
xorg.ResetKeys()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) ScreenConfigurations() map[int]types.ScreenConfiguration {
|
|
|
|
return xorg.ScreenConfigurations
|
|
|
|
}
|
|
|
|
|
2021-01-16 05:30:19 +13:00
|
|
|
func (manager *DesktopManagerCtx) SetScreenSize(size types.ScreenSize) error {
|
2021-01-05 07:39:21 +13:00
|
|
|
mu.Lock()
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.emmiter.Emit("before_screen_size_change")
|
2021-01-05 07:39:21 +13:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
manager.emmiter.Emit("after_screen_size_change")
|
|
|
|
mu.Unlock()
|
|
|
|
}()
|
|
|
|
|
2021-01-16 05:30:19 +13:00
|
|
|
return xorg.ChangeScreenSize(size.Width, size.Height, size.Rate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) GetScreenSize() *types.ScreenSize {
|
|
|
|
return xorg.GetScreenSize()
|
2020-11-04 12:27:47 +13:00
|
|
|
}
|
|
|
|
|
2021-01-16 04:53:03 +13:00
|
|
|
func (manager *DesktopManagerCtx) SetKeyboardMap(kbd types.KeyboardMap) error {
|
2021-01-13 11:48:15 +13:00
|
|
|
// TOOD: Use native API.
|
2021-01-19 03:31:00 +13:00
|
|
|
cmd := exec.Command("setxkbmap", "-layout", kbd.Layout, "-variant", kbd.Variant)
|
2021-01-13 11:48:15 +13:00
|
|
|
_, err := cmd.Output()
|
|
|
|
return err
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2021-01-16 04:53:03 +13:00
|
|
|
func (manager *DesktopManagerCtx) GetKeyboardMap() (*types.KeyboardMap, error) {
|
|
|
|
// TOOD: Use native API.
|
2021-01-19 03:31:00 +13:00
|
|
|
cmd := exec.Command("setxkbmap", "-query")
|
2021-01-16 04:53:03 +13:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-13 10:54:13 +13:00
|
|
|
func (manager *DesktopManagerCtx) SetKeyboardModifiers(mod types.KeyboardModifiers) {
|
|
|
|
if mod.NumLock != nil {
|
2021-09-03 07:37:24 +12:00
|
|
|
xorg.SetKeyboardModifier(xorg.KbdModNumLock, *mod.NumLock)
|
2021-01-13 10:54:13 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if mod.CapsLock != nil {
|
2021-09-03 07:37:24 +12:00
|
|
|
xorg.SetKeyboardModifier(xorg.KbdModCapsLock, *mod.CapsLock)
|
2021-01-13 10:54:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) GetKeyboardModifiers() types.KeyboardModifiers {
|
2021-01-13 11:35:11 +13:00
|
|
|
modifiers := xorg.GetKeyboardModifiers()
|
|
|
|
|
2021-09-03 07:37:24 +12:00
|
|
|
NumLock := (modifiers & xorg.KbdModNumLock) != 0
|
|
|
|
CapsLock := (modifiers & xorg.KbdModCapsLock) != 0
|
2021-01-13 10:54:13 +13:00
|
|
|
|
|
|
|
return types.KeyboardModifiers{
|
2021-02-15 02:40:17 +13:00
|
|
|
NumLock: &NumLock,
|
2021-01-13 10:54:13 +13:00
|
|
|
CapsLock: &CapsLock,
|
|
|
|
}
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
2021-01-10 10:58:18 +13:00
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) GetCursorImage() *types.CursorImage {
|
|
|
|
return xorg.GetCursorImage()
|
|
|
|
}
|
2021-01-22 08:44:09 +13:00
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) GetScreenshotImage() *image.RGBA {
|
|
|
|
return xorg.GetScreenshotImage()
|
|
|
|
}
|