Skip to content

Commit

Permalink
Add MacOS debug
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderScherbatiy committed May 21, 2024
1 parent 9f470ed commit 1fed0eb
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public final class CPrinterJob extends RasterPrinterJob {
Toolkit.getDefaultToolkit();
}

private static final boolean JAVA_PRINT_DEBUG = "true".equals(System.getenv("JAVA_PRINT_DEBUG"));

/**
* Presents a dialog to the user for changing the properties of
* the print job.
Expand Down Expand Up @@ -195,6 +197,9 @@ protected void setAttributes(PrintRequestAttributeSet attributes) throws Printer
}

outputBin = getOutputBinValue(attributes.get(OutputBin.class));
if (JAVA_PRINT_DEBUG) {
System.out.printf("[CPrinterJob] setAttributes outputBin: %s%n", outputBin);
}

PageRanges pageRangesAttr = (PageRanges)attributes.get(PageRanges.class);
if (isSupportedValue(pageRangesAttr, attributes)) {
Expand Down Expand Up @@ -664,12 +669,19 @@ private String getPrinterTray() {
}

private String getOutputBin() {
if (JAVA_PRINT_DEBUG) {
System.out.printf("[CPrinterJob] getOutputBin: %s%n", outputBin);
}

return outputBin;
}

private void setOutputBin(String outputBinName) {

OutputBin outputBin = toOutputBin(outputBinName);
if (JAVA_PRINT_DEBUG) {
System.out.printf("[CPrinterJob] setOutputBin name: %s, bin: %s%n", outputBinName, outputBin);
}
if (outputBin != null) {
attributes.add(outputBin);
}
Expand Down
121 changes: 121 additions & 0 deletions src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,113 @@
#define NS_LANDSCAPE NSLandscapeOrientation
#endif

static int isDebug() {
static int debug = -1;
if (debug == -1) {
char* debugEnv = getenv("JAVA_PRINT_DEBUG");
debug = (debugEnv != NULL && strcmp("true", debugEnv) == 0) ? JNI_TRUE : JNI_FALSE;
}
return debug;
}

@interface PrintDebug : NSObject
@end

@implementation PrintDebug

+ (void)showDict:(NSMutableDictionary*)dict {
NSLog(@"number of elements: %ld", [dict count]);

for (NSValue *key in dict) {
NSValue *val = dict[key];
NSLog(@"key: %@, value: %@ [class key: %@, value: %@]",
key, val, [key className], [val className]);
}
NSLog(@"--- --- ---");
NSLog(@"\n");
}

+ (void)showPrintInfo:(NSPrintInfo*)printInfo withMessage:(NSString *)msg {

if (!isDebug()) {
return;
}

NSLog(@"Debug printInfo: %@", msg);

NSLog(@"PrintInfo dictionary");
[PrintDebug showDict: [printInfo dictionary]];

NSLog(@"PrintInfo printSettings");
[PrintDebug showDict: [printInfo printSettings]];
}

+ (void)showPresets:(NSPrintInfo*)printInfo {

NSLog(@"[native] Show PrintInfo presets");

PMPrinter pr;

PMPrintSession printSession = (PMPrintSession)[printInfo PMPrintSession];
OSStatus status = PMSessionGetCurrentPrinter(printSession, &pr);

if (status != noErr) {
NSLog(@" PMSessionGetCurrentPrinter error: %d", status);
return;
}

if (pr == nil) {
NSLog(@"CPrinterJob PMSessionGetCurrentPrinter printer is nil");
return;
}

CFArrayRef presetsList = nil;
status = PMPrinterCopyPresets(pr, &presetsList);

if (status != noErr) {
NSLog(@" PMPrinterCopyPresets is err: %d", status);
return;
}

if (presetsList == nil) {
NSLog(@" presetsList is null!");
return;
}


CFIndex arrayCount = CFArrayGetCount(presetsList);
NSLog(@" Preset list arrayCount: %ld", arrayCount);

for (CFIndex index = 0; index < arrayCount; index++)
{

NSLog(@" index: %ld", index);

PMPreset preset = (PMPreset)CFArrayGetValueAtIndex(presetsList, index);
CFStringRef presetName = nil;
if (PMPresetCopyName(preset, &presetName) == noErr && CFStringGetLength(presetName) > 0)
{
NSLog(@" presetName: '%@'", presetName);

NSDictionary* dict = nil;
if (PMPresetGetAttributes(preset, (CFDictionaryRef*)(&dict)) == noErr)
{
if (dict == nil) {
NSLog(@" dict is null");
} else {
NSLog(@" dict is not null");
NSLog(@"Show dict for Preset");
[PrintDebug showDict: [printInfo dictionary]];
}
}
CFRelease(presetName);
}
}
CFRelease(presetsList);
}

@end

static NSPrintInfo* createDefaultNSPrintInfo(JNIEnv* env, jstring printer)
{
NSPrintInfo* defaultPrintInfo = [[NSPrintInfo sharedPrintInfo] copy];
Expand Down Expand Up @@ -452,6 +559,9 @@ static void nsPrintInfoToJavaPrinterJob(JNIEnv* env, NSPrintInfo* src, jobject d
}

NSString* outputBin = [[src printSettings] objectForKey:@"OutputBin"];
if (isDebug()) {
NSLog(@"[CPrinterJob] nsPrintInfoToJavaPrinterJob outputBin: %@", outputBin);
}
if (outputBin != nil) {
jstring outputBinName = NSStringToJavaString(env, outputBin);
(*env)->CallVoidMethod(env, dstPrinterJob, jm_setOutputBin, outputBinName);
Expand Down Expand Up @@ -552,10 +662,17 @@ static void javaPrinterJobToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobj
CHECK_EXCEPTION();
if (outputBin != NULL) {
NSString *nsOutputBinStr = JavaStringToNSString(env, outputBin);
if (isDebug()) {
NSLog(@"[CPrinterJob] javaPrinterJobToNSPrintInfo nsOutputBinStr: %@", nsOutputBinStr);
}
if (nsOutputBinStr != nil) {
[[dst printSettings] setObject:nsOutputBinStr forKey:@"OutputBin"];
}
}

if (isDebug()) {
[PrintDebug showPrintInfo: dst withMessage: @"CPrinterJob javaPrinterJobToNSPrintInfo"];
}
}

/*
Expand Down Expand Up @@ -715,6 +832,10 @@ static void javaPrinterJobToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobj

PrintModel* printModel = [[PrintModel alloc] initWithPrintInfo:printInfo];

if (isDebug()) {
[PrintDebug showPrintInfo: printInfo withMessage: @"CPrinterJob printLoop"];
}

(void)[printModel runPrintLoopWithView:printerView waitUntilDone:blocks withEnv:env];

// Only set this if we got far enough to call runPrintLoopWithView, or we will spin CPrinterJob.print() forever!
Expand Down

0 comments on commit 1fed0eb

Please sign in to comment.