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;
}
}
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());
}
}
}
}
}
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;
}
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);
}
Aggregations