Xorg input driver (#53)

* add xf86 input driver.

* cleanup.

* rewrite to unix socket PoC.

* add input rebuild.

* lint & docs.

* add input driver struct.

* comments, lint, socket name from config.

* add touch events to webrtc.

* switch to uint32.

* misc update logging & linting,

* fix screen size

* set touchscreen as core pointer.

* add touch to ws control.

* SendCoreEvents.

* extract to own xinput folder.

* add debounce.

* switch pressure to uint8.

* check buffer size.

* send touch events with system init.
This commit is contained in:
Miroslav Šedivý
2023-08-17 16:14:59 +02:00
committed by GitHub
parent 4cb1b3e925
commit ea5517b270
35 changed files with 1507 additions and 82 deletions

View File

@ -1,6 +1,7 @@
package types
import (
"fmt"
"image"
)
@ -19,6 +20,10 @@ type ScreenSize struct {
Rate int16
}
func (s ScreenSize) String() string {
return fmt.Sprintf("%dx%d@%d", s.Width, s.Height, s.Rate)
}
type KeyboardModifiers struct {
NumLock *bool
CapsLock *bool
@ -68,6 +73,12 @@ type DesktopManager interface {
OnFileChooserDialogClosed(listener func())
OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8))
// input driver
HasTouchSupport() bool
TouchBegin(touchId uint32, x, y int, pressure uint8) error
TouchUpdate(touchId uint32, x, y int, pressure uint8) error
TouchEnd(touchId uint32, x, y int, pressure uint8) error
// clipboard
ClipboardGetText() (*ClipboardText, error)
ClipboardSetText(data ClipboardText) error

View File

@ -43,6 +43,10 @@ const (
CONTROL_KEYPRESS = "control/keypress"
CONTROL_KEYDOWN = "control/keydown"
CONTROL_KEYUP = "control/keyup"
// touch
CONTROL_TOUCHBEGIN = "control/touchbegin"
CONTROL_TOUCHUPDATE = "control/touchupdate"
CONTROL_TOUCHEND = "control/touchend"
// actions
CONTROL_CUT = "control/cut"
CONTROL_COPY = "control/copy"

View File

@ -20,6 +20,7 @@ type SystemInit struct {
ScreenSize ScreenSize `json:"screen_size"`
Sessions map[string]SessionData `json:"sessions"`
Settings types.Settings `json:"settings"`
TouchEvents bool `json:"touch_events"`
ScreencastEnabled bool `json:"screencast_enabled"`
WebRTC SystemWebRTC `json:"webrtc"`
}
@ -129,6 +130,12 @@ type ControlKey struct {
Keysym uint32 `json:"keysym"`
}
type ControlTouch struct {
TouchId uint32 `json:"touch_id"`
*ControlPos
Pressure uint8 `json:"pressure"`
}
/////////////////////////////
// Screen
/////////////////////////////

31
pkg/xinput/dummy.go Normal file
View File

@ -0,0 +1,31 @@
package xinput
import "time"
type dummy struct{}
func NewDummy() Driver {
return &dummy{}
}
func (d *dummy) Connect() error {
return nil
}
func (d *dummy) Close() error {
return nil
}
func (d *dummy) Debounce(duration time.Duration) {}
func (d *dummy) TouchBegin(touchId uint32, x, y int, pressure uint8) error {
return nil
}
func (d *dummy) TouchUpdate(touchId uint32, x, y int, pressure uint8) error {
return nil
}
func (d *dummy) TouchEnd(touchId uint32, x, y int, pressure uint8) error {
return nil
}

61
pkg/xinput/types.go Normal file
View File

@ -0,0 +1,61 @@
package xinput
import "time"
const (
// absolute coordinates used in driver
AbsX = 0xffff
AbsY = 0xffff
)
const (
XI_TouchBegin = 18
XI_TouchUpdate = 19
XI_TouchEnd = 20
)
type Message struct {
_type uint16
touchId uint32
x int32 // can be negative?
y int32 // can be negative?
pressure uint8
}
func (msg *Message) Unpack(buffer []byte) {
msg._type = uint16(buffer[0])
msg.touchId = uint32(buffer[1]) | (uint32(buffer[2]) << 8)
msg.x = int32(buffer[3]) | (int32(buffer[4]) << 8) | (int32(buffer[5]) << 16) | (int32(buffer[6]) << 24)
msg.y = int32(buffer[7]) | (int32(buffer[8]) << 8) | (int32(buffer[9]) << 16) | (int32(buffer[10]) << 24)
msg.pressure = uint8(buffer[11])
}
func (msg *Message) Pack() []byte {
var buffer [12]byte
buffer[0] = byte(msg._type)
buffer[1] = byte(msg.touchId)
buffer[2] = byte(msg.touchId >> 8)
buffer[3] = byte(msg.x)
buffer[4] = byte(msg.x >> 8)
buffer[5] = byte(msg.x >> 16)
buffer[6] = byte(msg.x >> 24)
buffer[7] = byte(msg.y)
buffer[8] = byte(msg.y >> 8)
buffer[9] = byte(msg.y >> 16)
buffer[10] = byte(msg.y >> 24)
buffer[11] = byte(msg.pressure)
return buffer[:]
}
type Driver interface {
Connect() error
Close() error
// release touches, that were not updated for duration
Debounce(duration time.Duration)
// touch events
TouchBegin(touchId uint32, x, y int, pressure uint8) error
TouchUpdate(touchId uint32, x, y int, pressure uint8) error
TouchEnd(touchId uint32, x, y int, pressure uint8) error
}

122
pkg/xinput/xinput.go Normal file
View File

@ -0,0 +1,122 @@
/* custom xf86 input driver communication protocol */
package xinput
import (
"fmt"
"net"
"sync"
"time"
)
type driver struct {
mu sync.Mutex
socket string
conn net.Conn
debounceTouchIds map[uint32]time.Time
}
func NewDriver(socket string) Driver {
return &driver{
socket: socket,
debounceTouchIds: make(map[uint32]time.Time),
}
}
func (d *driver) Connect() error {
c, err := net.Dial("unix", d.socket)
if err != nil {
return err
}
d.conn = c
return nil
}
func (d *driver) Close() error {
return d.conn.Close()
}
func (d *driver) Debounce(duration time.Duration) {
d.mu.Lock()
defer d.mu.Unlock()
t := time.Now()
for touchId, start := range d.debounceTouchIds {
if t.Sub(start) < duration {
continue
}
msg := Message{
_type: XI_TouchEnd,
touchId: touchId,
x: -1,
y: -1,
}
_, _ = d.conn.Write(msg.Pack())
delete(d.debounceTouchIds, touchId)
}
}
func (d *driver) TouchBegin(touchId uint32, x, y int, pressure uint8) error {
d.mu.Lock()
defer d.mu.Unlock()
if _, ok := d.debounceTouchIds[touchId]; ok {
return fmt.Errorf("debounced touch id %v", touchId)
}
d.debounceTouchIds[touchId] = time.Now()
msg := Message{
_type: XI_TouchBegin,
touchId: touchId,
x: int32(x),
y: int32(y),
pressure: pressure,
}
_, err := d.conn.Write(msg.Pack())
return err
}
func (d *driver) TouchUpdate(touchId uint32, x, y int, pressure uint8) error {
d.mu.Lock()
defer d.mu.Unlock()
if _, ok := d.debounceTouchIds[touchId]; !ok {
return fmt.Errorf("unknown touch id %v", touchId)
}
d.debounceTouchIds[touchId] = time.Now()
msg := Message{
_type: XI_TouchUpdate,
touchId: touchId,
x: int32(x),
y: int32(y),
pressure: pressure,
}
_, err := d.conn.Write(msg.Pack())
return err
}
func (d *driver) TouchEnd(touchId uint32, x, y int, pressure uint8) error {
d.mu.Lock()
defer d.mu.Unlock()
if _, ok := d.debounceTouchIds[touchId]; !ok {
return fmt.Errorf("unknown touch id %v", touchId)
}
delete(d.debounceTouchIds, touchId)
msg := Message{
_type: XI_TouchEnd,
touchId: touchId,
x: int32(x),
y: int32(y),
pressure: pressure,
}
_, err := d.conn.Write(msg.Pack())
return err
}

View File

@ -185,20 +185,20 @@ func CheckKeys(duration time.Duration) {
}
// set screen configuration, create new one if not exists
func ChangeScreenSize(width int, height int, rate int16) (int, int, int16, error) {
func ChangeScreenSize(s types.ScreenSize) (types.ScreenSize, error) {
mu.Lock()
defer mu.Unlock()
// round width to 8, because of Xorg
width = width - (width % 8)
s.Width = s.Width - (s.Width % 8)
// if rate is 0, set it to 60
if rate == 0 {
rate = 60
if s.Rate == 0 {
s.Rate = 60
}
// convert variables to C types
c_width, c_height, c_rate := C.int(width), C.int(height), C.short(rate)
c_width, c_height, c_rate := C.int(s.Width), C.int(s.Height), C.short(s.Rate)
// if screen configuration already exists, just set it
status := C.XSetScreenConfiguration(c_width, c_height, c_rate)
@ -214,15 +214,15 @@ func ChangeScreenSize(width int, height int, rate int16) (int, int, int16, error
// if screen configuration was not set successfully, return error
if status != C.RRSetConfigSuccess {
err = fmt.Errorf("unknown screen configuration %dx%d@%d", width, height, rate)
err = fmt.Errorf("unknown screen configuration %s", s.String())
}
// if specified rate is not supported a BadValue error is returned
if status == C.BadValue {
err = fmt.Errorf("unsupported screen rate %d", rate)
err = fmt.Errorf("unsupported screen rate %d", s.Rate)
}
return width, height, rate, err
return s, err
}
func GetScreenSize() types.ScreenSize {