Add CUPS printing system bindings to c.s.j.p.unix#1718
Conversation
9730d3f to
2e49a2e
Compare
matthiasblaesing
left a comment
There was a problem hiding this comment.
Looks sane to me. For the documentation references people need to be careful. I noticed for example for CupsJob the structure definition in the documentation did not match the header file. The mapping is sane with respect to the header, so is good.
You might want to consider this change:
--- a/contrib/platform/test/com/sun/jna/platform/CupsTest.java
+++ b/contrib/platform/test/com/sun/jna/platform/CupsTest.java
@@ -27,6 +27,9 @@
import com.sun.jna.platform.unix.Cups;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
import junit.framework.TestCase;
@@ -88,7 +91,8 @@
assertNotNull("Destinations pointer should not be null", destsPtr);
// Read the first destination
- Cups.CupsDest dest = new Cups.CupsDest(destsPtr);
+ Cups.CupsDest[] dests = (Cups.CupsDest[]) new Cups.CupsDest(destsPtr).toArray(numDests);
+ Cups.CupsDest dest = dests[0];
assertNotNull("First destination name should not be null", dest.name);
System.out.println("First destination: " + dest.name
+ (dest.is_default != 0 ? " (default)" : ""));
@@ -106,7 +110,7 @@
String printerType = Cups.INSTANCE.cupsGetOption(
"printer-type", dest.num_options, dest.options);
- System.out.println(" printer-type: " + printerType);
+ System.out.println(" printer-type: " + printerType + " " + mapPrinterTypeEnumNames(printerType).toString());
}
}
} finally {
@@ -116,6 +120,28 @@
}
}
+ public List<String> mapPrinterTypeEnumNames(String printerType) throws RuntimeException, NumberFormatException {
+ try {
+ List<String> result = new ArrayList<>();
+
+ int printerTypeInt = Integer.parseInt(printerType);
+
+ for (Field f : Cups.class.getFields()) {
+ String fieldName = f.getName();
+ if (fieldName.startsWith("CUPS_PRINTER_")) {
+ int val = f.getInt(null);
+ if (((printerTypeInt & val) == val)) {
+ result.add(fieldName);
+ }
+ }
+ }
+
+ return result;
+ } catch (IllegalAccessException | IllegalArgumentException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
public void testGetDefault() {
if (!cupsAvailable) {
return;
@@ -144,7 +170,8 @@
Pointer jobsPtr = jobsRef.getValue();
assertNotNull("Jobs pointer should not be null", jobsPtr);
- Cups.CupsJob job = new Cups.CupsJob(jobsPtr);
+ Cups.CupsJob[] jobs = (Cups.CupsJob[]) new Cups.CupsJob(jobsPtr).toArray(numJobs);
+ Cups.CupsJob job = jobs[0];
assertTrue("Job ID should be positive", job.id > 0);
assertNotNull("Job destination should not be null", job.dest);
System.out.println("First job: id=" + job.id + " dest=" + job.dest
Both cupsGetDests and cupsGetJobs2 return an array of values and this demonstrates decoding. The code in mapPrinterTypeEnumNames was more for me to test what I get for my default printer.
Closes java-native-access#1710 Add Cups interface providing JNA bindings for libcups, the Common UNIX Printing System library. Includes structures (CupsDest, CupsOption, CupsJob), printer state/type constants, job state constants, and functions for destination enumeration, job management, option handling, and server/user configuration.
2e49a2e to
801734b
Compare
Yeah, I've seen that a lot in manpage docs as well, where they tell you what elements are in the structure (assuming you'll fetch via Applied your suggested changes, will merge when CI passes. |
Adds
Cupsinterface toc.s.j.p.unixproviding JNA bindings for libcups, the Common UNIX Printing System library available on Linux, macOS, and other UNIX platforms.Structures
CupsDestcups_dest_tCupsOptioncups_option_tCupsJobcups_job_tFunctions
cupsGetDests/cupsGetDests2cupsFreeDestscupsGetDestcupsGetNamedDestcupsGetDefaultcupsGetOptioncupsAddOptioncupsFreeOptionscupsGetJobs2cupsFreeJobscupsCancelJobcupsServer/cupsSetServercupsUser/cupsSetUsercupsLastError/cupsLastErrorStringConstants
IPP_PRINTER_IDLE,IPP_PRINTER_PROCESSING,IPP_PRINTER_STOPPEDCUPS_PRINTER_CLASS,CUPS_PRINTER_COLOR,CUPS_PRINTER_DUPLEX, etc.IPP_JSTATE_PENDINGthroughIPP_JSTATE_COMPLETEDCUPS_WHICHJOBS_ALL,CUPS_WHICHJOBS_ACTIVE,CUPS_WHICHJOBS_COMPLETEDCloses #1710
Test Output
On my local mac with actual printers: