Browse Source

Working on linux input event flags.

Beoran 4 years ago
parent
commit
8f3fcf8f27

+ 13 - 37
os/linux/event_codes.go

@@ -22,46 +22,22 @@ const INPUT_PROP_CNT = (INPUT_PROP_MAX + 1)
 /*
  * Event types
  */
-type EventType uint
-
-const EV_SYN = EventType(0x00)
-const EV_KEY = EventType(0x01)
-const EV_REL = EventType(0x02)
-const EV_ABS = EventType(0x03)
-const EV_MSC = EventType(0x04)
-const EV_SW  = EventType(0x05)
-const EV_LED = EventType(0x11)
-const EV_SND = EventType(0x12)
-const EV_REP = EventType(0x14)
-const EV_FF  = EventType(0x15)
-const EV_PWR = EventType(0x16)
-const EV_FF_STATUS = EventType(0x17)
-const EV_MAX = EventType(0x1f)
+const EV_SYN = 0x00
+const EV_KEY = 0x01
+const EV_REL = 0x02
+const EV_ABS = 0x03
+const EV_MSC = 0x04
+const EV_SW  = 0x05
+const EV_LED = 0x11
+const EV_SND = 0x12
+const EV_REP = 0x14
+const EV_FF  = 0x15
+const EV_PWR = 0x16
+const EV_FF_STATUS = 0x17
+const EV_MAX = 0x1f
 const EV_CNT = (EV_MAX+1)
 
 
-func (e EventType) String() string { 
-    switch e {
-        case EV_SYN: return "EV_SYN"
-        case EV_KEY: return "EV_KEY"
-        case EV_REL: return "EV_REL"
-        case EV_ABS: return "EV_ABS"
-        case EV_MSC: return "EV_MSC"
-        case EV_SW : return "EV_SW"
-        case EV_LED: return "EV_LED"
-        case EV_SND: return "EV_SND"
-        case EV_REP: return "EV_REP"
-        case EV_FF : return "EV_FF"
-        case EV_PWR: return "EV_PWR"
-        case EV_FF_STATUS: return "EV_FF_STATUS"
-        case EV_MAX: return "EV_MAX"
-        case EV_CNT: return "EV_CNT"
-        default: return "Unknown event"
-    }
-}
-
-
-
 /*
  * Synchronization events.
  */

+ 28 - 0
os/linux/event_to_string_linux.go

@@ -0,0 +1,28 @@
+package linux
+
+import "fmt" 
+
+
+func EventTypeToString(e uint) string { 
+    switch e {
+        case EV_SYN: return "EV_SYN"
+        case EV_KEY: return "EV_KEY"
+        case EV_REL: return "EV_REL"
+        case EV_ABS: return "EV_ABS"
+        case EV_MSC: return "EV_MSC"
+        case EV_SW : return "EV_SW"
+        case EV_LED: return "EV_LED"
+        case EV_SND: return "EV_SND"
+        case EV_REP: return "EV_REP"
+        case EV_FF : return "EV_FF"
+        case EV_PWR: return "EV_PWR"
+        case EV_FF_STATUS: return "EV_FF_STATUS"
+        case EV_MAX: return "EV_MAX"
+        case EV_CNT: return "EV_CNT"
+        default: return fmt.Sprintf("Unknown event %d", e)
+    }
+}
+
+
+ 
+

+ 5 - 5
os/linux/input/input_linux.go

@@ -77,16 +77,16 @@ func (d * Device) Id() (linux.INPUT_id, error) {
 const SIZEOF_LONG = uint(unsafe.Sizeof(*((*linux.UnsignedLong)(nil))))      
 const BITS_PER_LONG = SIZEOF_LONG * 8
 
-func BitsToLong(ebits linux.EventType) uint {
+func BitsToLong(ebits uint) uint {
     bits := uint(ebits)
     return ((bits) + 8 * SIZEOF_LONG - 1) / (8 * SIZEOF_LONG)
 }
 
 func TestBit(bit uint, array []linux.UnsignedLong) bool {
-    return ((array[ bit / BITS_PER_LONG ]) & (1 << (bit%BITS_PER_LONG))) != 0
+    return ((array[ bit / BITS_PER_LONG ]) >> (bit%BITS_PER_LONG)) & 1 != 0
 }
 
-func (d * Device) SupportedEvents() ([]linux.EventType, error) {
+func (d * Device) SupportedEvents() ([]uint, error) {
 	size := BitsToLong(linux.EV_MAX)
     bits := make([]linux.UnsignedLong, size)
     for i := uint(0); i < size; i ++ {
@@ -97,10 +97,10 @@ func (d * Device) SupportedEvents() ([]linux.EventType, error) {
         return nil, err
     }
     fmt.Printf("size %d, bits: %v\n", size, bits)
-    result := []linux.EventType{}
+    result := []uint{}
     for i := uint(0); i < uint(linux.EV_MAX); i++ {
         if (TestBit(i, bits)) {
-            result = append(result, linux.EventType(i))
+            result = append(result, uint(i))
 		}
     }
     

+ 2 - 1
os/linux/input/input_linux_test.go

@@ -72,7 +72,8 @@ func TestSupportedEvents(t * testing.T) {
 
 	t.Logf("Supported events:\n")
 	for i, ev := range events {
-        t.Logf("Supported: %d: %d %s", i, int(ev), ev.String())
+        evname := linux.EventTypeToString(ev)
+        t.Logf("Supported: %d: %d %s", i, int(ev), evname)
     }
 		
 }

+ 2 - 2
os/linux/input_linux.go

@@ -172,8 +172,8 @@ func EVIOCGSW(len uintptr) uint32 {
     return IOC(IOC_READ, 'E', 0x1b, len)		/* get all switch states */
 }
 
-func EVIOCGBIT(ev EventType, len uintptr) uint32 {	
-    return IOC(IOC_READ, 'E', 0x20 + (uint32(ev)), len)	/* get event bits */
+func EVIOCGBIT(ev uint32, len uintptr) uint32 {	
+    return IOC(IOC_READ, 'E', 0x20 + (ev), len)	/* get event bits */
 }
 
 func EVIOCGABS(abs uint32) uint32 {