Watch changes in connected inputs

This commit is contained in:
Iwan Timmer
2014-03-31 16:52:06 +02:00
parent 230a041621
commit 3ec5066b08
2 changed files with 104 additions and 28 deletions

View File

@@ -5,7 +5,7 @@ import java.io.IOException;
import com.limelight.binding.PlatformBinding;
import com.limelight.binding.audio.FakeAudioRenderer;
import com.limelight.binding.video.FakeVideoRenderer;
import com.limelight.input.EvdevHandler;
import com.limelight.input.EvdevLoader;
import com.limelight.input.GamepadMapping;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.NvConnectionListener;
@@ -14,7 +14,6 @@ import com.limelight.nvstream.av.video.VideoDecoderRenderer;
import com.limelight.nvstream.http.NvHTTP;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
@@ -71,19 +70,6 @@ public class Limelight implements NvConnectionListener {
conn = new NvConnection(host, this, streamConfig);
if (inputs.isEmpty()) {
File input = new File("/dev/input");
String[] events = input.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith("event");
}
});
for (String event:events)
inputs.add(new File(input, event).getAbsolutePath());
}
GamepadMapping mapping = null;
if (mappingFile!=null) {
try {
@@ -94,20 +80,18 @@ public class Limelight implements NvConnectionListener {
}
} else
mapping = new GamepadMapping();
for (String input:inputs) {
try {
new EvdevHandler(conn, input, mapping).start();
} catch (FileNotFoundException ex) {
displayError("Input", "Input (" + input + ") could not be found");
return;
} catch (IOException ex) {
displayError("Input", "Input (" + input + ") could not be read");
displayError("Input", "Are you running as root?");
return;
}
}
try {
new EvdevLoader(conn, mapping, inputs).start();
} catch (FileNotFoundException ex) {
displayError("Input", "Input could not be found");
return;
} catch (IOException ex) {
displayError("Input", "Input could not be read");
displayError("Input", "Are you running as root?");
return;
}
conn.start(PlatformBinding.getDeviceName(), null,
VideoDecoderRenderer.FLAG_PREFER_QUALITY,
PlatformBinding.getAudioRenderer(audioDevice),

View File

@@ -0,0 +1,92 @@
package com.limelight.input;
import com.limelight.LimeLog;
import com.limelight.nvstream.NvConnection;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
/**
* Class that handles input devices and a new ones when connected
* @author Iwan Timmer
*/
public class EvdevLoader implements Runnable {
private NvConnection conn;
private GamepadMapping mapping;
private List<String> inputs;
private File input;
private FilenameFilter filter;
public EvdevLoader(NvConnection conn, GamepadMapping mapping, List<String> inputs) {
this.conn = conn;
this.mapping = mapping;
this.inputs = inputs;
this.input = new File("/dev/input");
this.filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith("event");
}
};
}
public void start() throws FileNotFoundException, IOException {
boolean watcher = false;
if (inputs.isEmpty()) {
watcher = true;
String[] events = input.list(filter);
for (String event:events)
inputs.add(new File(input, event).getAbsolutePath());
}
for (String input:inputs)
new EvdevHandler(conn, input, mapping).start();
if (watcher) {
Thread thread = new Thread(this);
thread.setDaemon(true);
thread.setName("Input - Search");
thread.start();
}
}
@Override
public void run() {
try {
Path evdev = Paths.get("/dev/input");
WatchService watcher = evdev.getFileSystem().newWatchService();
evdev.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
WatchKey watckKey = watcher.take();
List<WatchEvent<?>> events = watckKey.pollEvents();
for (WatchEvent event:events) {
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
String name = event.context().toString();
if (filter.accept(input, name)) {
LimeLog.info("Input " + name + " added");
new EvdevHandler(conn, new File(input, name).getAbsolutePath(), mapping).start();
}
}
}
} catch (IOException | InterruptedException ex) {
LimeLog.severe(ex.getMessage());
}
}
}