Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HID on Linux #852

Merged
merged 3 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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