input getdevices


原文链接: input getdevices
type procDev struct {
	name, dev, event string
	keep             bool
}

func getProcInputs() []procDev {
	f, err := os.Open("/proc/bus/input/devices")
	if err != nil { log.Fatalln(err) }
	scanner := bufio.NewScanner(f)
	var devs []procDev
	var d procDev
	for scanner.Scan() {
		line := scanner.Text()
		if len(line) == 0 {
			if d.keep {
				devs = append(devs, d)
			}
			d.keep = false
			continue
		}
		switch line[0] {
		case 'N':
			f1 := strings.Split(line, "=")
			f2 := strings.Split(f1[1], "\"")
			d.name = strings.TrimSpace(f2[1])
		case 'P':
			if strings.HasSuffix(line, "input0") &&
				!(strings.HasSuffix(d.name, "Button\"") ||
					strings.HasSuffix(d.name, "Speaker\"")) {
				d.keep = true
			}
		case 'H':
			f1 := strings.Split(line, "=")
			f2 := strings.Fields(f1[1])
			d.dev = f2[0]
			if len(f2) > 2 {
				d.dev = f2[1] // sepcial case for icon7
			}
			if len(f2) > 1 && strings.HasPrefix(f2[len(f2)-1], "event") {
				d.event = f2[len(f2)-1]
			} else {
				d.keep = false
			}
		}
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "reading standard input:", err)
		os.Exit(1)
	}
	f.Close()
	return devs
}
`