Search in sources :

Example 1 with EvdevDevice

use of org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice in project openhab-addons by openhab.

the class LinuxInputDiscoveryService method enrichDevice.

private boolean enrichDevice(DiscoveryResultBuilder builder, File inputDevice) {
    String label = inputDevice.getName();
    try {
        try (EvdevDevice device = new EvdevDevice(inputDevice.getAbsolutePath())) {
            String labelFromDevice = device.getName();
            boolean isKeyboard = device.has(EvdevLibrary.Type.KEY);
            if (labelFromDevice != null) {
                label = String.format("%s (%s)", labelFromDevice, inputDevice.getName());
            }
            return isKeyboard;
        } finally {
            builder.withLabel(label);
        }
    } catch (IOException | LastErrorException e) {
        logger.debug("Could not open device {}", inputDevice, e);
        return false;
    }
}
Also used : EvdevDevice(org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice) LastErrorException(org.openhab.binding.linuxinput.internal.evdev4j.LastErrorException) IOException(java.io.IOException)

Example 2 with EvdevDevice

use of org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice in project openhab-addons by openhab.

the class LinuxInputHandler method handleEventsInThread.

@Override
void handleEventsInThread() throws IOException {
    try (Selector selector = EvdevDevice.openSelector()) {
        @Nullable EvdevDevice currentDevice = device;
        if (currentDevice == null) {
            throw new IOException("trying to handle events without an device");
        }
        SelectionKey evdevReady = currentDevice.register(selector);
        logger.debug("Grabbing device {}", currentDevice);
        // ungrab will happen implicitly at device.close()
        currentDevice.grab();
        while (true) {
            if (Thread.currentThread().isInterrupted()) {
                logger.debug("Thread interrupted, exiting");
                break;
            }
            logger.trace("Waiting for event");
            selector.select(20_000);
            if (selector.selectedKeys().remove(evdevReady)) {
                while (true) {
                    Optional<EvdevDevice.InputEvent> ev = currentDevice.nextEvent();
                    if (!ev.isPresent()) {
                        break;
                    }
                    handleEvent(ev.get());
                }
            }
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) EvdevDevice(org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice) IOException(java.io.IOException) Nullable(org.eclipse.jdt.annotation.Nullable) Selector(java.nio.channels.Selector)

Example 3 with EvdevDevice

use of org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice in project openhab-addons by openhab.

the class LinuxInputHandler method delayedSetup.

@Override
boolean delayedSetup() throws IOException {
    ThingBuilder customizer = editThing();
    List<Channel> newChannels = new ArrayList<>();
    newChannels.add(keyChannel);
    EvdevDevice newDevice = new EvdevDevice(config.path);
    for (EvdevDevice.Key o : newDevice.enumerateKeys()) {
        String name = o.getName();
        Channel channel = ChannelBuilder.create(new ChannelUID(thing.getUID(), CHANNEL_GROUP_KEYPRESSES_ID, name), CoreItemFactory.CONTACT).withLabel(name).withType(CHANNEL_TYPE_KEY_PRESS).build();
        channels.put(o.getCode(), channel);
        newChannels.add(channel);
    }
    if (Objects.equals(defaultLabel, thing.getLabel())) {
        customizer.withLabel(newDevice.getName());
    }
    customizer.withChannels(newChannels);
    Map<String, String> props = getProperties(Objects.requireNonNull(newDevice));
    customizer.withProperties(props);
    updateThing(customizer.build());
    for (Channel channel : newChannels) {
        updateState(channel.getUID(), OpenClosedType.OPEN);
    }
    if (config.enable) {
        updateStatus(ThingStatus.ONLINE);
    }
    device = newDevice;
    return config.enable;
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) EvdevDevice(org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList)

Example 4 with EvdevDevice

use of org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice in project openhab-addons by openhab.

the class LinuxInputHandler method closeDevice.

@Override
protected void closeDevice() throws IOException {
    @Nullable EvdevDevice currentDevice = device;
    device = null;
    if (currentDevice != null) {
        currentDevice.close();
    }
    logger.debug("Device {} closed", this);
}
Also used : EvdevDevice(org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

EvdevDevice (org.openhab.binding.linuxinput.internal.evdev4j.EvdevDevice)4 IOException (java.io.IOException)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1 ArrayList (java.util.ArrayList)1 LastErrorException (org.openhab.binding.linuxinput.internal.evdev4j.LastErrorException)1 Channel (org.openhab.core.thing.Channel)1 ChannelUID (org.openhab.core.thing.ChannelUID)1 ThingBuilder (org.openhab.core.thing.binding.builder.ThingBuilder)1