forked from mewbak/evdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (146 loc) · 3.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package main
import (
"flag"
"fmt"
"github.com/jteeuwen/evdev"
"os"
"os/signal"
"strings"
)
func main() {
node := parseArgs()
// Create and open our device.
dev, err := evdev.Open(node)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
// Make sure it is closed once we are done.
defer dev.Close()
// Fetch the driver version for this device.
major, minor, revision := dev.Version()
// Fetch device identity.
id := dev.Id()
// Fetch the bitset, specifying the supported event types.
events := dev.EventTypes()
// Fetch device name.
name := dev.Name()
// Fetch physical device mapping.
path := dev.Path()
// Fetch device serial code.
// For most devices this is not defined and yields an empty string.
serial := dev.Serial()
// Display all the collected information about our device.
fmt.Printf(" Node : %s\n", node)
fmt.Printf(" Name : %s\n", name)
fmt.Printf(" Path : %s\n", path)
fmt.Printf(" Serial : %s\n", serial)
fmt.Printf(" Driver : %d.%d.%d\n", major, minor, revision)
fmt.Printf(" Vendor : %04x\n", id.Vendor)
fmt.Printf(" Product : %04x\n", id.Product)
fmt.Printf(" Version : %04x\n", id.Version)
fmt.Printf(" Bus : %s\n", busName(id.BusType))
fmt.Printf(" Events : %s\n", listEvents(events))
// Read events from the device, until we exit the program.
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill)
for {
select {
case <-signals:
return
case evt := <-dev.Inbox:
fmt.Printf("%+v\n", evt)
}
}
}
// busName returns the string equivalent of the given bus type.
func busName(bus uint16) string {
switch bus {
case evdev.BusPCI:
return "PCI"
case evdev.BusISAPNP:
return "ISA Plug & Play"
case evdev.BusUSB:
return "USB"
case evdev.BusHIL:
return "HIL"
case evdev.BusBluetooth:
return "Bluetooth"
case evdev.BusVirtual:
return "Virtual"
case evdev.BusISA:
return "ISA"
case evdev.BusI8042:
return "I8042"
case evdev.BusXTKBD:
return "XT Keyboard"
case evdev.BusRS232:
return "RS232"
case evdev.BusGamePort:
return "Game Port"
case evdev.BusParPort:
return "Parallel Port"
case evdev.BusAmiga:
return "Amiga"
case evdev.BusADB:
return "ADB"
case evdev.BusI2C:
return "I2C"
case evdev.BusHost:
return "Host"
case evdev.BusGSC:
return "GSC"
case evdev.BusAtari:
return "Atari"
case evdev.BusSPI:
return "SPI"
}
return "Unknown"
}
// listEvents lists the event types supported by the device.
func listEvents(set evdev.Bitset) string {
var list []string
for n := 0; n < set.Len(); n++ {
if !set.Test(n) {
continue
}
switch n {
case evdev.EvSync:
list = append(list, "Sync Events")
case evdev.EvKey:
list = append(list, "Keys or Buttons")
case evdev.EvRelative:
list = append(list, "Relative Axes")
case evdev.EvAbsolute:
list = append(list, "Absolute Axes")
case evdev.EvMisc:
list = append(list, "Miscellaneous")
case evdev.EvLed:
list = append(list, "LEDs")
case evdev.EvSound:
list = append(list, "Sounds")
case evdev.EvRepeat:
list = append(list, "Repeat")
case evdev.EvForceFeedback,
evdev.EvForceFeedbackStatus:
list = append(list, "Force Feedback")
case evdev.EvPower:
list = append(list, "Power Management")
case evdev.EvSw:
list = append(list, "Binary switches")
default:
list = append(list, fmt.Sprintf("Unknown (0x%02x)", n))
}
}
return strings.Join(list, ", ")
}
func parseArgs() string {
flag.Parse()
if flag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "Usage: %s <node>\n", os.Args[0])
os.Exit(1)
}
return flag.Args()[0]
}