Example #1
0
func main() {
	var dev *evdev.InputDevice
	var events []evdev.InputEvent
	var err error

	switch len(os.Args) {
	case 1:
		dev, err = select_device()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	case 2:
		dev, err = evdev.Open(os.Args[1])
		if err != nil {
			fmt.Printf("unable to open input device: %s\n", os.Args[1])
			os.Exit(1)
		}
	default:
		fmt.Printf(usage + "\n")
		os.Exit(1)
	}

	info := fmt.Sprintf("bus 0x%04x, vendor 0x%04x, product 0x%04x, version 0x%04x",
		dev.Bustype, dev.Vendor, dev.Product, dev.Version)

	repeat_info := dev.GetRepeatRate()

	fmt.Printf("Evdev protocol version: %d\n", dev.EvdevVersion)
	fmt.Printf("Device name: %s\n", dev.Name)
	fmt.Printf("Device info: %s\n", info)
	fmt.Printf("Repeat settings: repeat %d. delay %d\n", repeat_info[0], repeat_info[1])
	fmt.Printf("Device capabilities:\n")

	// for ctype, codes := range dev.Capabilities {
	// 	fmt.Printf("  Type %s %d\n", ctype.Name, ctype.Type)
	// 	for i := range codes {
	// 		fmt.Printf("   Code %d %s\n", codes[i].Code, codes[i].Name)
	// 	}
	// }

	fmt.Printf("Listening for events ...\n")

	for {
		events, err = dev.Read()
		for i := range events {
			str := format_event(&events[i])
			fmt.Println(str)
		}
	}
}
Example #2
0
// Select a device from a list of accessible input devices.
func select_device() (*evdev.InputDevice, error) {
	fns, _ := evdev.ListInputDevices(device_glob)
	devices := make([]*evdev.InputDevice, 0)

	for i := range fns {
		dev, err := evdev.Open(fns[i])
		if err == nil {
			devices = append(devices, dev)
		}
	}

	lines := make([]string, 0)
	max := 0
	if len(devices) > 0 {
		for i := range devices {
			dev := devices[i]
			str := fmt.Sprintf("%-3d %-20s %-35s %s", i, dev.Fn, dev.Name, dev.Phys)
			if len(str) > max {
				max = len(str)
			}
			lines = append(lines, str)
		}
		fmt.Printf("%-3s %-20s %-35s %s\n", "ID", "Device", "Name", "Phys")
		fmt.Printf(strings.Repeat("-", max) + "\n")
		fmt.Printf(strings.Join(lines, "\n") + "\n")

		var choice int
		choice_max := len(lines) - 1

	ReadChoice:
		fmt.Printf("Select device [0-%d]: ", choice_max)
		_, err := fmt.Scan(&choice)
		if err != nil || choice > choice_max || choice < 0 {
			goto ReadChoice
		}

		return devices[choice], nil
	}

	errmsg := fmt.Sprintf("no accessible input devices matched by %s", device_glob)
	return nil, errors.New(errmsg)
}