Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type Keyboard interface {
// The key can be any of the predefined keycodes from keycodes.go.
KeyUp(key int) error

// FetchSysPath will return the syspath to the device file.
FetchSyspath() (string, error)

io.Closer
}

Expand Down Expand Up @@ -123,3 +126,7 @@ func createVKeyboardDevice(path string, name []byte) (fd *os.File, err error) {
func keyCodeInRange(key int) bool {
return key >= keyReserved && key <= keyMax
}

func (vk vKeyboard) FetchSyspath() (string, error) {
return fetchSyspath(vk.deviceFile)
}
17 changes: 17 additions & 0 deletions keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,20 @@ func TestKeyDownFailsIfDeviceIsClosed(t *testing.T) {
t.Fatalf("Expected KeyPress to fail, but no error was returned.")
}
}

func TestKeyboardSyspath(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}

sysPath, err := vk.FetchSyspath()
if err != nil {
t.Fatalf("Failed to fetch syspath. Last error was: %s\n", err)
}

if sysPath[:32] != "/sys/devices/virtual/input/input" {
t.Fatalf("Expected syspath to start with /sys/devices/virtual/input/input, but got %s", sysPath)
}
t.Logf("Syspath: %s", sysPath)
}
7 changes: 7 additions & 0 deletions mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type Mouse interface {
// Wheel will simulate a wheel movement.
Wheel(horizontal bool, delta int32) error

// FetchSysPath will return the syspath to the device file.
FetchSyspath() (string, error)

io.Closer
}

Expand Down Expand Up @@ -282,3 +285,7 @@ func assertNotNegative(val int32) error {
}
return nil
}

func (vRel vMouse) FetchSyspath() (string, error) {
return fetchSyspath(vRel.deviceFile)
}
17 changes: 17 additions & 0 deletions mouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,20 @@ func TestMouseWheelFailsIfDeviceIsClosed(t *testing.T) {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseSyspath(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}

sysPath, err := relDev.FetchSyspath()
if err != nil {
t.Fatalf("Failed to fetch syspath. Last error was: %s\n", err)
}

if sysPath[:32] != "/sys/devices/virtual/input/input" {
t.Fatalf("Expected syspath to start with /sys/devices/virtual/input/input, but got %s", sysPath)
}
t.Logf("Syspath: %s", sysPath)
}
7 changes: 7 additions & 0 deletions touchpad.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type TouchPad interface {
// TouchUp will end or ,more precisely, unset the touch event issued by TouchDown
TouchUp() error

// FetchSysPath will return the syspath to the device file.
FetchSyspath() (string, error)

io.Closer
}

Expand Down Expand Up @@ -210,3 +213,7 @@ func sendAbsEvent(deviceFile *os.File, xPos int32, yPos int32) error {

return syncEvents(deviceFile)
}

func (vTouch vTouchPad) FetchSyspath() (string, error) {
return fetchSyspath(vTouch.deviceFile)
}
18 changes: 18 additions & 0 deletions touchpad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,21 @@ func TestSingleTouchEvent(t *testing.T) {
}

}

func TestTouchPadSyspath(t *testing.T) {
dev, err := CreateTouchPad("/dev/uinput", []byte("TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}

sysPath, err := dev.FetchSyspath()
if err != nil {
t.Fatalf("Failed to fetch syspath. Last error was: %s\n", err)
}

if sysPath[:32] != "/sys/devices/virtual/input/input" {
t.Fatalf("Expected syspath to start with /sys/devices/virtual/input/input, but got %s", sysPath)
}

t.Logf("Syspath: %s", sysPath)
}
11 changes: 11 additions & 0 deletions uinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import (
"os"
"syscall"
"time"
"unsafe"
)

func validateDevicePath(path string) error {
Expand Down Expand Up @@ -168,6 +169,16 @@ func releaseDevice(deviceFile *os.File) (err error) {
return ioctl(deviceFile, uiDevDestroy, uintptr(0))
}

func fetchSyspath(deviceFile *os.File) (string, error) {
sysInputDir := "/sys/devices/virtual/input/"
// 64 for name + 1 for null byte
path := make([]byte, 65)
err := ioctl(deviceFile, uiGetSysname, uintptr(unsafe.Pointer(&path[0])))

sysInputDir = sysInputDir + string(path)
return sysInputDir, err
}

// Note that mice and touch pads do have buttons as well. Therefore, this function is used
// by all currently available devices and resides in the main source file.
func sendBtnEvent(deviceFile *os.File, keys []int, btnState int) (err error) {
Expand Down
14 changes: 9 additions & 5 deletions uinputdefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ const (
uinputMaxNameSize = 80
uiDevCreate = 0x5501
uiDevDestroy = 0x5502
uiSetEvBit = 0x40045564
uiSetKeyBit = 0x40045565
uiSetRelBit = 0x40045566
uiSetAbsBit = 0x40045567
busUsb = 0x03
// this is for 64 length buffer to store name
// for another length generate using : (len << 16) | 0x8000552C
uiGetSysname = 0x8041552c
uiSetEvBit = 0x40045564
uiSetKeyBit = 0x40045565

uiSetRelBit = 0x40045566
uiSetAbsBit = 0x40045567
busUsb = 0x03
)

// input event codes as specified in input-event-codes.h
Expand Down