Browse Source

Refactoring linux code a bit.

Beoran 4 years ago
parent
commit
947247df44
3 changed files with 149 additions and 0 deletions
  1. 72 0
      os/linux/input/input_linux.go
  2. 66 0
      os/linux/input/input_linux_test.go
  3. 11 0
      os/linux/linux_linux.go

+ 72 - 0
os/linux/input/input_linux.go

@@ -0,0 +1,72 @@
+/* package input is a thing Go wrapper around the Linux kernel input event 
+ * system. */
+package input
+
+import "gitlab.com/beoran/galago/os/linux"
+import "os"
+import "unsafe"
+import "syscall"
+import "fmt"
+import "path/filepath"
+
+
+// Device models an input device
+type Device struct { 
+    *os.File
+}
+
+const Directory = "/dev/input"
+
+func Open(name string) (*Device, error) {
+    f, err := os.OpenFile(filepath.Join(Directory, name), os.O_RDWR, 0666)
+    if err != nil {
+        return nil, err
+    }
+    return &Device{File: f}, nil
+} 
+
+func List() ([]string, error) {
+    dir, err := os.Open(Directory)
+    if err != nil {
+        return nil, err
+    }
+    return dir.Readdirnames(-1)
+}
+
+
+// Icotl performs an ioctl on the given de
+func (d * Device) Ioctl(code uint32, pointer unsafe.Pointer) error {
+    fmt.Printf("ioctl: %d %d %d\n", uintptr(d.Fd()), uintptr(code), uintptr(pointer))
+    _, _, errno := syscall.Syscall(
+        syscall.SYS_IOCTL,
+        uintptr(d.Fd()),
+        uintptr(code),
+        uintptr(pointer))
+    if (errno != 0) {
+        return errno
+    }
+    return nil
+}
+
+
+func (d * Device) DriverVersion() (int32, error) {
+    res := int32(0) 
+    data := unsafe.Pointer(&res)
+    err := d.Ioctl(linux.EVIOCGVERSION, data)
+    return res, err
+}
+
+func (d * Device) Name() (string, error) {
+    buffer := [256]byte{}
+    err := d.Ioctl(linux.EVIOCGNAME(uintptr(len(buffer))), unsafe.Pointer(&buffer))
+    return string(buffer[0:len(buffer)]), err
+}
+
+func (d * Device) Id() (linux.INPUT_id, error) {
+    var result linux.INPUT_id 
+    err := d.Ioctl(linux.EVIOCGID, unsafe.Pointer(&result))
+    return result, err
+}
+
+
+

+ 66 - 0
os/linux/input/input_linux_test.go

@@ -0,0 +1,66 @@
+package input
+
+import "testing"
+// import "os"
+// import "syscall"
+// import "fmt"
+import "gitlab.com/beoran/galago/os/linux"
+
+
+
+const IN = "by-id/usb-0583_USB_2-axis_8-button_gamepad-event-joystick"
+
+func TestGetDriverVersion(t * testing.T) {
+    device , err := Open(IN)
+    if err != nil {
+        t.Errorf("Error Open: %s\n", err)
+        return
+    }
+    defer device.Close()
+    version, err := device.DriverVersion()
+    if err != nil {
+        t.Errorf("Error GetDriverVersion: %s (%s %x)\n", err, 
+                    "EVIOCGVERSION", linux.EVIOCGVERSION)
+        return
+    }
+    if version != linux.EV_VERSION {
+        t.Errorf("Version %x should be %x", version, linux.EV_VERSION)
+    }
+    t.Logf("GetDriverVersion: %d", version)
+}
+
+
+func TestGetName(t * testing.T) {
+    device , err := Open(IN)
+    if err != nil {
+        t.Errorf("Error Open: %s\n", err)
+        return
+    }
+    defer device.Close()
+    name, err := device.Name()
+    if err != nil {
+        t.Errorf("Error GetName: %s\n", err)
+        return
+    }
+    t.Logf("GetName: %s", name)
+}
+
+func TestGetId(t * testing.T) {
+    device , err := Open(IN)
+    if err != nil {
+        t.Errorf("Error Open: %s\n", err)
+        return
+    }
+    defer device.Close()
+    id, err := device.Id()
+    if err != nil {
+        t.Errorf("Error GetId: %s\n", err)
+        return
+    }
+    t.Logf("GetId: bustype 0x%x, vendor 0x%x, product 0x%x, version 0x%x", 
+        id.Bustype, id.Vendor, id.Product, id.Version)
+}
+
+
+
+

+ 11 - 0
os/linux/linux_linux.go

@@ -0,0 +1,11 @@
+
+/* linux is a package that is a Go port of the functionality in 
+ * the header directory <linux/XXX> as far as is needed for input event
+ * handing. Defines have been makked either to consts 
+ * or functions in case they were macros with parameters. 
+ * Structs have been ported attempting to have the same size in Go and in C.
+ * This package is not documented, but the names are, as far as is possible, 
+ * keeping go export rule into consideration, the same as in C. 
+ * Please see the Linux kernel documentation for more explanations.
+ */
+package linux