示例#1
0
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()

	events := dev.EventTypes()
	abs := dev.Test(events, evdev.EvAbsolute)
	rel := dev.Test(events, evdev.EvRelative)

	if !abs && !rel {
		fmt.Fprintf(os.Stderr, "Device %q does not support relative or absolute axes.\n", node)
		return
	}

	if abs {
		absAxes(dev)
	}

	if rel {
		absAxes(dev)
	}
}
示例#2
0
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()

	// Obtain an exclusive lock on the device.
	if !dev.Grab() {
		fmt.Fprintf(os.Stderr, "Failed to abtain device lock.\n")
		return
	}

	fmt.Printf("%s is now locked\n", dev.Name())

	// Do work with device...
	<-time.After(5e9)

	// Release lock
	if !dev.Release() {
		fmt.Fprintf(os.Stderr, "Failed to release device lock.\n")
		return
	}

	fmt.Printf("%s is now unlocked\n", dev.Name())
}
示例#3
0
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()

	// Ensure this device supports the needed event types.
	if !dev.Test(dev.EventTypes(), evdev.EvForceFeedback, evdev.EvForceFeedbackStatus) {
		fmt.Fprintf(os.Stderr, "Device %q does not support force feedback events.\n", node)
		return
	}

	// Set effect gain factor, to ensure the effect strength is
	// the same on all FF devices we may be working with.
	dev.SetEffectGain(75) // 75%

	// List Force Feedback capabilities
	listCapabilities(dev)

	// Create, upload and play some effects.
	setEffects(dev)

	// Wait for incoming events or exit signals.
	poll(dev)
}
示例#4
0
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)
		}
	}
}
示例#5
0
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()

	// Ensure this device supports key/button events.
	if !dev.Test(dev.EventTypes(), evdev.EvKeys) {
		fmt.Fprintf(os.Stderr, "Device %q does not support key/button events.\n", node)
		return
	}

	// Fetch the current key/button state and display it.
	ks := dev.KeyState()
	listState(ks)

	// Alternatively, we can read individual key/button press/release events
	// from the device.
	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt, os.Kill)
	for {
		select {
		case <-signals:
			return

		case evt := <-dev.Inbox:
			if evt.Type != evdev.EvKeys {
				continue // Not a key event.
			}

			if evt.Value == 0 {
				continue // Key is released -- we want key presses.
			}

			switch evt.Code {
			case evdev.KeyA:
				fmt.Println("A was pressed")
			case evdev.KeyLeftShift:
				fmt.Println("Left Shift was pressed")
			case evdev.KeyEscape:
				fmt.Println("Escape was pressed")

			case evdev.BtnLeft:
				fmt.Println("Left button was pressed")
			case evdev.Btn3:
				fmt.Println("Button 3 was pressed")
			}
		}
	}
}
示例#6
0
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()

	// Turn off the Capslock, NumLock and ScrollLock LEDs.
	var ev evdev.Event
	ev.Type = evdev.EvLed
	ev.Value = 0

	ev.Code = evdev.LedCapsLock
	dev.Outbox <- ev

	ev.Code = evdev.LedNumLock
	dev.Outbox <- ev

	ev.Code = evdev.LedScrollLock
	dev.Outbox <- ev

	// Once every 200 milliseconds, toggle one of the LEDs.
	// Or exit if we receive an exit signal.
	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt, os.Kill)

	ev.Code = evdev.LedNumLock

	for {
		select {
		case <-signals:
			return

		case <-time.After(Timeout):
			// Turn off previous LED
			ev.Value = 0
			dev.Outbox <- ev

			// Turn on the next one.
			ev.Code = (ev.Code + 1) & 3
			ev.Value = 1
			dev.Outbox <- ev
		}
	}
}
示例#7
0
文件: input.go 项目: mikkeloscar/lis
// GetInputDevices return a InputDevs containing valid input devices.
func GetInputDevices(errors chan error) (*InputDevs, error) {
	devices := &InputDevs{
		make(map[string]*inputDev),
		make(chan struct{}),
		errors,
	}

	devNames, err := ioutil.ReadDir(deviceDir)
	if err != nil {
		return nil, err
	}

	// loop through all event devices and check if they are keyboard/mouse like
	for _, d := range devNames {
		if len(d.Name()) >= 5 && d.Name()[:5] == "event" {
			devicePath := deviceDir + d.Name()
			dev, err := evdev.Open(devicePath)
			if err != nil {
				return nil, err
			}

			name, isInput := checkDevice(dev)
			if isInput {
				// don't add the same device twice
				_, ok := devices.devs[name]
				if ok {
					dev.Close()
					continue
				}

				devices.devs[name] = &inputDev{
					devicePath,
					make(chan struct{}, 1),
					errors,
				}
			}
			// close the device, since we are not gonna use it.
			dev.Close()
		}
	}
	return devices, nil
}
示例#8
0
func OpenFLIRC(grab bool) {
	i := 0
	var dev *evdev.Device
	var err error
	for {
		node := fmt.Sprintf("/dev/input/event%d", i)
		dev, err = evdev.Open(node)
		i++
		if err != nil {
			if os.IsNotExist(err) {
				log.Fatalln("could not find FLIRC device")
			}
			log.Fatalln(err)
		}
		if strings.TrimRight(dev.Name(), "\x00") != FLIRCName {
			dev.Close()
			continue
		}
		break
	}

	if grab && !dev.Grab() {
		log.Warnln("failed to grab device")
	}
	log.Infoln("using device:", strings.TrimRight(dev.Path(), "\x00"))
	for event := range dev.Inbox {
		if event.Type != evdev.EvKeys {
			continue
		}
		if event.Value != 0 {
			continue
		}

		log.Infoln("FLIRC event", event.Code)
		for i, code := range flircMap {
			if int(event.Code) == code {
				setState(i)
				break
			}
		}
	}
}
示例#9
0
文件: input.go 项目: mikkeloscar/lis
func handleDevice(inputDevice *inputDev, activity chan struct{}) {
	dev, err := evdev.Open(inputDevice.devPath)
	if err != nil {
		inputDevice.errors <- err
		return
	}
	defer dev.Close()

	for {
		select {
		case evt := <-dev.Inbox:
			if evt.Type != evKeys && evt.Type != evRel {
				continue // not the event we are looking for
			}
			// the user is still alive
			activity <- struct{}{}
		case <-inputDevice.stop:
			return
		}
	}
}