55 lines
1.7 KiB
Java
55 lines
1.7 KiB
Java
package com.loomcom.symon.ui;
|
|
|
|
import com.sun.jna.Library;
|
|
import com.sun.jna.Native;
|
|
import com.sun.jna.Pointer;
|
|
import com.sun.jna.ptr.IntByReference;
|
|
import com.sun.jna.ptr.PointerByReference;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class X11ResourceReader {
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(X11ResourceReader.class.getName());
|
|
|
|
|
|
public interface X11 extends Library {
|
|
X11 INSTANCE = Native.load("X11", X11.class);
|
|
|
|
Pointer XOpenDisplay(String display);
|
|
Pointer XResourceManagerString(Pointer display);
|
|
void XCloseDisplay(Pointer display);
|
|
}
|
|
|
|
public static Double readXftDpiScale() {
|
|
Pointer display = null;
|
|
try {
|
|
display = X11.INSTANCE.XOpenDisplay(null);
|
|
if (display == null) {
|
|
return null; // não conseguimos ligar ao X server (ex: Wayland puro sem XWayland)
|
|
}
|
|
|
|
Pointer resourceString = X11.INSTANCE.XResourceManagerString(display);
|
|
if (resourceString == null) {
|
|
return null;
|
|
}
|
|
|
|
String resources = resourceString.getString(0);
|
|
for (String line : resources.split("\n")) {
|
|
if (line.startsWith("Xft.dpi:")) {
|
|
int dpi = Integer.parseInt(line.split(":")[1].trim());
|
|
return Math.round((dpi / 96.0) * 4) / 4.0;
|
|
}
|
|
}
|
|
return null;
|
|
} catch (UnsatisfiedLinkError | Exception e) {
|
|
logger.debug("Could not read X11 resource manager (libX11 unavailable?)", e);
|
|
return null;
|
|
} finally {
|
|
if (display != null) {
|
|
X11.INSTANCE.XCloseDisplay(display);
|
|
}
|
|
}
|
|
}
|
|
}
|