Skip to content

Commit

Permalink
Fix HID on Linux (qzind#852)
Browse files Browse the repository at this point in the history
* Filter hid devices down to usage page
* Avoid throwing exceptions on missing fields
* Force hid4java to use libusb on Linux
Closes qzind#853

Co-authored-by: Berenz <[email protected]>
  • Loading branch information
tresf and Berenz authored Aug 19, 2021
1 parent 4f9cdb6 commit b9fe674
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/qz/communication/H4J_HidUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import qz.utils.SystemUtilities;

import javax.usb.util.UsbUtil;
import java.util.HashSet;
import java.util.List;

public class H4J_HidUtilities {
Expand All @@ -32,6 +33,7 @@ public static JSONArray getHidDevicesJSON() throws JSONException {
List<HidDevice> devices = getHidDevices();
JSONArray devicesJSON = new JSONArray();

HashSet<String> unique = new HashSet<>();
for(HidDevice device : devices) {
JSONObject deviceJSON = new JSONObject();

Expand All @@ -42,7 +44,11 @@ public static JSONArray getHidDevicesJSON() throws JSONException {
.put("manufacturer", device.getManufacturer())
.put("product", device.getProduct());

devicesJSON.put(deviceJSON);
String uid = String.format("v%sp%su%ss%s", deviceJSON.optString("vendorId"), deviceJSON.optString("productId"), deviceJSON.optString("usagePage"), deviceJSON.optString("serial"));
if (!unique.contains(uid)) {
devicesJSON.put(deviceJSON);
unique.add(uid);
}
}

return devicesJSON;
Expand Down
11 changes: 11 additions & 0 deletions src/qz/utils/SystemUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.github.zafarkhaja.semver.Version;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.ssl.Base64;
import org.joor.Reflect;
import org.joor.ReflectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qz.common.Constants;
Expand Down Expand Up @@ -48,6 +50,15 @@ public class SystemUtilities {
private static final Logger log = LoggerFactory.getLogger(TrayManager.class);
private static final Locale defaultLocale = Locale.getDefault();

static {
if(!isWindows() && !isMac()) {
// Force hid4java to use libusb: https://github.com/qzind/tray/issues/853
try {
Reflect.on("org.hid4java.jna.HidApi").set("useLibUsbVariant", true);
} catch(ReflectException ignore) {}
}
}

private static Boolean darkDesktop;
private static Boolean darkTaskbar;
private static Boolean hasMonocle;
Expand Down
4 changes: 3 additions & 1 deletion src/qz/ws/SocketConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ public void removeDevice(DeviceOptions dOpts) {

public synchronized void openDevice(DeviceIO device, DeviceOptions dOpts) throws DeviceException {
device.open();
addDevice(dOpts, device);
if (device.isOpen()) {
addDevice(dOpts, device);
}
}

/**
Expand Down

0 comments on commit b9fe674

Please sign in to comment.