2020-10-23 03:54:50 +13:00
|
|
|
package xorg
|
|
|
|
|
|
|
|
/*
|
2021-01-12 03:57:14 +13:00
|
|
|
#cgo linux LDFLAGS: -lX11 -lXrandr -lXtst -lXfixes
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
#include "xorg.h"
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2021-01-13 11:35:11 +13:00
|
|
|
type KbdModifiers uint8
|
2021-01-13 10:54:13 +13:00
|
|
|
|
|
|
|
const (
|
|
|
|
KBD_CAPS_LOCK KbdModifiers = 2
|
|
|
|
KBD_NUM_LOCK KbdModifiers = 16
|
|
|
|
)
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
var ScreenConfigurations = make(map[int]types.ScreenConfiguration)
|
|
|
|
|
|
|
|
var debounce_button = make(map[int]time.Time)
|
|
|
|
var debounce_key = make(map[uint64]time.Time)
|
|
|
|
var mu = sync.Mutex{}
|
|
|
|
|
2020-11-04 12:09:52 +13:00
|
|
|
func GetScreenConfigurations() {
|
2020-11-15 10:35:50 +13:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
C.XGetScreenConfigurations()
|
|
|
|
}
|
|
|
|
|
2020-11-04 12:09:52 +13:00
|
|
|
func DisplayOpen(display string) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
displayUnsafe := C.CString(display)
|
|
|
|
defer C.free(unsafe.Pointer(displayUnsafe))
|
|
|
|
|
2020-11-04 12:41:04 +13:00
|
|
|
err := C.XDisplayOpen(displayUnsafe)
|
2020-11-04 12:09:52 +13:00
|
|
|
if int(err) == 1 {
|
|
|
|
return fmt.Errorf("Could not open display %s.", display)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DisplayClose() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
C.XDisplayClose()
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func Move(x, y int) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
C.XMove(C.int(x), C.int(y))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Scroll(x, y int) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
C.XScroll(C.int(x), C.int(y))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ButtonDown(code int) error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := debounce_button[code]; ok {
|
|
|
|
return fmt.Errorf("debounced button %v", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
debounce_button[code] = time.Now()
|
|
|
|
|
|
|
|
C.XButton(C.uint(code), C.int(1))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func KeyDown(code uint64) error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := debounce_key[code]; ok {
|
|
|
|
return fmt.Errorf("debounced key %v", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
debounce_key[code] = time.Now()
|
|
|
|
|
|
|
|
C.XKey(C.ulong(code), C.int(1))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ButtonUp(code int) error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := debounce_button[code]; !ok {
|
|
|
|
return fmt.Errorf("debounced button %v", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(debounce_button, code)
|
|
|
|
|
|
|
|
C.XButton(C.uint(code), C.int(0))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func KeyUp(code uint64) error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := debounce_key[code]; !ok {
|
|
|
|
return fmt.Errorf("debounced key %v", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(debounce_key, code)
|
|
|
|
|
|
|
|
C.XKey(C.ulong(code), C.int(0))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResetKeys() {
|
2020-11-15 10:35:50 +13:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2020-11-15 10:35:50 +13:00
|
|
|
for code := range debounce_button {
|
|
|
|
C.XButton(C.uint(code), C.int(0))
|
2020-10-23 03:54:50 +13:00
|
|
|
delete(debounce_button, code)
|
|
|
|
}
|
|
|
|
|
2020-11-15 10:35:50 +13:00
|
|
|
for code := range debounce_key {
|
|
|
|
C.XKey(C.ulong(code), C.int(0))
|
2020-10-23 03:54:50 +13:00
|
|
|
delete(debounce_key, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckKeys(duration time.Duration) {
|
2020-11-15 10:35:50 +13:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
t := time.Now()
|
|
|
|
for code, start := range debounce_button {
|
|
|
|
if t.Sub(start) < duration {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-15 10:35:50 +13:00
|
|
|
C.XButton(C.uint(code), C.int(0))
|
2020-10-23 03:54:50 +13:00
|
|
|
delete(debounce_button, code)
|
|
|
|
}
|
2020-11-15 10:35:50 +13:00
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
for code, start := range debounce_key {
|
|
|
|
if t.Sub(start) < duration {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-15 10:35:50 +13:00
|
|
|
C.XKey(C.ulong(code), C.int(0))
|
2020-10-23 03:54:50 +13:00
|
|
|
delete(debounce_key, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 05:30:19 +13:00
|
|
|
func ChangeScreenSize(width int, height int, rate int16) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
for index, size := range ScreenConfigurations {
|
|
|
|
if size.Width == width && size.Height == height {
|
|
|
|
for _, fps := range size.Rates {
|
2021-01-16 05:30:19 +13:00
|
|
|
if rate == fps {
|
2020-10-23 03:54:50 +13:00
|
|
|
C.XSetScreenConfiguration(C.int(index), C.short(fps))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 07:32:52 +13:00
|
|
|
return fmt.Errorf("Unknown screen configuration %dx%d@%d.", width, height, rate)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetScreenSize() *types.ScreenSize {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
index := int(C.XGetScreenSize())
|
|
|
|
rate := int16(C.XGetScreenRate())
|
|
|
|
|
|
|
|
if conf, ok := ScreenConfigurations[index]; ok {
|
|
|
|
return &types.ScreenSize{
|
|
|
|
Width: conf.Width,
|
|
|
|
Height: conf.Height,
|
|
|
|
Rate: rate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-13 10:54:13 +13:00
|
|
|
func SetKeyboardModifier(mod KbdModifiers, active bool) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
num := C.int(0)
|
|
|
|
if active {
|
|
|
|
num = C.int(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
C.XSetKeyboardModifier(C.int(mod), num)
|
|
|
|
}
|
|
|
|
|
2021-01-13 11:35:11 +13:00
|
|
|
func GetKeyboardModifiers() KbdModifiers {
|
2020-10-23 03:54:50 +13:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
2021-01-13 11:35:11 +13:00
|
|
|
return KbdModifiers(C.XGetKeyboardModifiers())
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-01-10 10:58:18 +13:00
|
|
|
func GetCursorImage() *types.CursorImage {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
var cur *C.XFixesCursorImage
|
|
|
|
cur = C.XGetCursorImage()
|
|
|
|
defer C.XFree(unsafe.Pointer(cur))
|
|
|
|
|
|
|
|
width := uint16(cur.width)
|
|
|
|
height := uint16(cur.height)
|
|
|
|
|
|
|
|
return &types.CursorImage{
|
|
|
|
Width: width,
|
|
|
|
Height: height,
|
|
|
|
Xhot: uint16(cur.xhot),
|
|
|
|
Yhot: uint16(cur.yhot),
|
|
|
|
Serial: uint64(cur.cursor_serial),
|
|
|
|
// Xlib stores 32-bit data in longs, even if longs are 64-bits long.
|
|
|
|
Pixels: C.GoBytes(unsafe.Pointer(cur.pixels), C.int(width * height * 8)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
//export goCreateScreenSize
|
|
|
|
func goCreateScreenSize(index C.int, width C.int, height C.int, mwidth C.int, mheight C.int) {
|
|
|
|
ScreenConfigurations[int(index)] = types.ScreenConfiguration{
|
|
|
|
Width: int(width),
|
|
|
|
Height: int(height),
|
|
|
|
Rates: make(map[int]int16),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export goSetScreenRates
|
|
|
|
func goSetScreenRates(index C.int, rate_index C.int, rate C.short) {
|
|
|
|
ScreenConfigurations[int(index)].Rates[int(rate_index)] = int16(rate)
|
|
|
|
}
|