Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.desktop/unix/native/common/awt/CUPSfuncs.c
66645 views
1
/*
2
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include <jni.h>
27
#include <jni_util.h>
28
#include <jvm_md.h>
29
#include <dlfcn.h>
30
#include <cups/cups.h>
31
#include <cups/ppd.h>
32
33
34
//#define CUPS_DEBUG
35
36
#ifdef CUPS_DEBUG
37
#define DPRINTF(x, y) fprintf(stderr, x, y);
38
#else
39
#define DPRINTF(x, y)
40
#endif
41
42
typedef const char* (*fn_cupsServer)(void);
43
typedef int (*fn_ippPort)(void);
44
typedef http_t* (*fn_httpConnect)(const char *, int);
45
typedef void (*fn_httpClose)(http_t *);
46
typedef char* (*fn_cupsGetPPD)(const char *);
47
typedef cups_dest_t* (*fn_cupsGetDest)(const char *name,
48
const char *instance, int num_dests, cups_dest_t *dests);
49
typedef int (*fn_cupsGetDests)(cups_dest_t **dests);
50
typedef void (*fn_cupsFreeDests)(int num_dests, cups_dest_t *dests);
51
typedef ppd_file_t* (*fn_ppdOpenFile)(const char *);
52
typedef void (*fn_ppdClose)(ppd_file_t *);
53
typedef ppd_option_t* (*fn_ppdFindOption)(ppd_file_t *, const char *);
54
typedef ppd_size_t* (*fn_ppdPageSize)(ppd_file_t *, char *);
55
56
fn_cupsServer j2d_cupsServer;
57
fn_ippPort j2d_ippPort;
58
fn_httpConnect j2d_httpConnect;
59
fn_httpClose j2d_httpClose;
60
fn_cupsGetPPD j2d_cupsGetPPD;
61
fn_cupsGetDest j2d_cupsGetDest;
62
fn_cupsGetDests j2d_cupsGetDests;
63
fn_cupsFreeDests j2d_cupsFreeDests;
64
fn_ppdOpenFile j2d_ppdOpenFile;
65
fn_ppdClose j2d_ppdClose;
66
fn_ppdFindOption j2d_ppdFindOption;
67
fn_ppdPageSize j2d_ppdPageSize;
68
69
70
/*
71
* Initialize library functions.
72
* // REMIND : move tab , add dlClose before return
73
*/
74
JNIEXPORT jboolean JNICALL
75
Java_sun_print_CUPSPrinter_initIDs(JNIEnv *env,
76
jobject printObj) {
77
void *handle = dlopen(VERSIONED_JNI_LIB_NAME("cups", "2"),
78
RTLD_LAZY | RTLD_GLOBAL);
79
80
if (handle == NULL) {
81
handle = dlopen(JNI_LIB_NAME("cups"), RTLD_LAZY | RTLD_GLOBAL);
82
if (handle == NULL) {
83
return JNI_FALSE;
84
}
85
}
86
87
j2d_cupsServer = (fn_cupsServer)dlsym(handle, "cupsServer");
88
if (j2d_cupsServer == NULL) {
89
dlclose(handle);
90
return JNI_FALSE;
91
}
92
93
j2d_ippPort = (fn_ippPort)dlsym(handle, "ippPort");
94
if (j2d_ippPort == NULL) {
95
dlclose(handle);
96
return JNI_FALSE;
97
}
98
99
j2d_httpConnect = (fn_httpConnect)dlsym(handle, "httpConnect");
100
if (j2d_httpConnect == NULL) {
101
dlclose(handle);
102
return JNI_FALSE;
103
}
104
105
j2d_httpClose = (fn_httpClose)dlsym(handle, "httpClose");
106
if (j2d_httpClose == NULL) {
107
dlclose(handle);
108
return JNI_FALSE;
109
}
110
111
j2d_cupsGetPPD = (fn_cupsGetPPD)dlsym(handle, "cupsGetPPD");
112
if (j2d_cupsGetPPD == NULL) {
113
dlclose(handle);
114
return JNI_FALSE;
115
}
116
117
j2d_cupsGetDest = (fn_cupsGetDest)dlsym(handle, "cupsGetDest");
118
if (j2d_cupsGetDest == NULL) {
119
dlclose(handle);
120
return JNI_FALSE;
121
}
122
123
j2d_cupsGetDests = (fn_cupsGetDests)dlsym(handle, "cupsGetDests");
124
if (j2d_cupsGetDests == NULL) {
125
dlclose(handle);
126
return JNI_FALSE;
127
}
128
129
j2d_cupsFreeDests = (fn_cupsFreeDests)dlsym(handle, "cupsFreeDests");
130
if (j2d_cupsFreeDests == NULL) {
131
dlclose(handle);
132
return JNI_FALSE;
133
}
134
135
j2d_ppdOpenFile = (fn_ppdOpenFile)dlsym(handle, "ppdOpenFile");
136
if (j2d_ppdOpenFile == NULL) {
137
dlclose(handle);
138
return JNI_FALSE;
139
140
}
141
142
j2d_ppdClose = (fn_ppdClose)dlsym(handle, "ppdClose");
143
if (j2d_ppdClose == NULL) {
144
dlclose(handle);
145
return JNI_FALSE;
146
147
}
148
149
j2d_ppdFindOption = (fn_ppdFindOption)dlsym(handle, "ppdFindOption");
150
if (j2d_ppdFindOption == NULL) {
151
dlclose(handle);
152
return JNI_FALSE;
153
}
154
155
j2d_ppdPageSize = (fn_ppdPageSize)dlsym(handle, "ppdPageSize");
156
if (j2d_ppdPageSize == NULL) {
157
dlclose(handle);
158
return JNI_FALSE;
159
}
160
161
return JNI_TRUE;
162
}
163
164
/*
165
* Gets CUPS server name.
166
*
167
*/
168
JNIEXPORT jstring JNICALL
169
Java_sun_print_CUPSPrinter_getCupsServer(JNIEnv *env,
170
jobject printObj)
171
{
172
jstring cServer = NULL;
173
const char* server = j2d_cupsServer();
174
if (server != NULL) {
175
cServer = JNU_NewStringPlatform(env, server);
176
}
177
return cServer;
178
}
179
180
/*
181
* Gets CUPS port name.
182
*
183
*/
184
JNIEXPORT jint JNICALL
185
Java_sun_print_CUPSPrinter_getCupsPort(JNIEnv *env,
186
jobject printObj)
187
{
188
int port = j2d_ippPort();
189
return (jint) port;
190
}
191
192
193
/*
194
* Gets CUPS default printer name.
195
*
196
*/
197
JNIEXPORT jstring JNICALL
198
Java_sun_print_CUPSPrinter_getCupsDefaultPrinter(JNIEnv *env,
199
jobject printObj)
200
{
201
jstring cDefPrinter = NULL;
202
cups_dest_t *dests;
203
char *defaultPrinter = NULL;
204
int num_dests = j2d_cupsGetDests(&dests);
205
int i = 0;
206
cups_dest_t *dest = j2d_cupsGetDest(NULL, NULL, num_dests, dests);
207
if (dest != NULL) {
208
defaultPrinter = dest->name;
209
if (defaultPrinter != NULL) {
210
cDefPrinter = JNU_NewStringPlatform(env, defaultPrinter);
211
}
212
}
213
j2d_cupsFreeDests(num_dests, dests);
214
return cDefPrinter;
215
}
216
217
/*
218
* Returns list of default local printers
219
*/
220
JNIEXPORT jobjectArray JNICALL
221
Java_sun_print_CUPSPrinter_getCupsDefaultPrinters(JNIEnv *env,
222
jobject printObj)
223
{
224
cups_dest_t *dests;
225
int i, j, num_dests;
226
jstring utf_str;
227
jclass cls;
228
jobjectArray nameArray = NULL;
229
230
cls = (*env)->FindClass(env, "java/lang/String");
231
CHECK_NULL_RETURN(cls, NULL);
232
233
num_dests = j2d_cupsGetDests(&dests);
234
235
if (dests == NULL) {
236
return NULL;
237
}
238
239
nameArray = (*env)->NewObjectArray(env, num_dests, cls, NULL);
240
if (nameArray == NULL) {
241
j2d_cupsFreeDests(num_dests, dests);
242
DPRINTF("CUPSfuncs::bad alloc new array\n", "")
243
return NULL;
244
}
245
246
for (i = 0; i < num_dests; i++) {
247
utf_str = JNU_NewStringPlatform(env, dests[i].name);
248
if (utf_str == NULL) {
249
(*env)->ExceptionClear(env);
250
for (j = i - 1; j >= 0; j--) {
251
utf_str = (*env)->GetObjectArrayElement(env, nameArray, j);
252
(*env)->SetObjectArrayElement(env, nameArray, j, NULL);
253
(*env)->DeleteLocalRef(env, utf_str);
254
utf_str = NULL;
255
}
256
j2d_cupsFreeDests(num_dests, dests);
257
(*env)->DeleteLocalRef(env, nameArray);
258
DPRINTF("CUPSfuncs::bad alloc new string ->name\n", "")
259
return NULL;
260
}
261
(*env)->SetObjectArrayElement(env, nameArray, i, utf_str);
262
(*env)->DeleteLocalRef(env, utf_str);
263
}
264
265
j2d_cupsFreeDests(num_dests, dests);
266
return nameArray;
267
}
268
269
/*
270
* Checks if connection can be made to the server.
271
*
272
*/
273
JNIEXPORT jboolean JNICALL
274
Java_sun_print_CUPSPrinter_canConnect(JNIEnv *env,
275
jobject printObj,
276
jstring server,
277
jint port)
278
{
279
const char *serverName;
280
serverName = (*env)->GetStringUTFChars(env, server, NULL);
281
if (serverName != NULL) {
282
http_t *http = j2d_httpConnect(serverName, (int)port);
283
(*env)->ReleaseStringUTFChars(env, server, serverName);
284
if (http != NULL) {
285
j2d_httpClose(http);
286
return JNI_TRUE;
287
}
288
}
289
return JNI_FALSE;
290
}
291
292
293
/*
294
* Returns list of media: pages + trays
295
*/
296
JNIEXPORT jobjectArray JNICALL
297
Java_sun_print_CUPSPrinter_getMedia(JNIEnv *env,
298
jobject printObj,
299
jstring printer)
300
{
301
ppd_file_t *ppd;
302
ppd_option_t *optionTray, *optionPage;
303
ppd_choice_t *choice;
304
const char *name;
305
const char *filename;
306
int i, nTrays=0, nPages=0, nTotal=0;
307
jstring utf_str;
308
jclass cls;
309
jobjectArray nameArray = NULL;
310
311
name = (*env)->GetStringUTFChars(env, printer, NULL);
312
if (name == NULL) {
313
(*env)->ExceptionClear(env);
314
JNU_ThrowOutOfMemoryError(env, "Could not create printer name");
315
return NULL;
316
}
317
318
// NOTE: cupsGetPPD returns a pointer to a filename of a temporary file.
319
// unlink() must be caled to remove the file when finished using it.
320
filename = j2d_cupsGetPPD(name);
321
(*env)->ReleaseStringUTFChars(env, printer, name);
322
CHECK_NULL_RETURN(filename, NULL);
323
324
cls = (*env)->FindClass(env, "java/lang/String");
325
CHECK_NULL_RETURN(cls, NULL);
326
327
if ((ppd = j2d_ppdOpenFile(filename)) == NULL) {
328
unlink(filename);
329
DPRINTF("CUPSfuncs::unable to open PPD %s\n", filename);
330
return NULL;
331
}
332
333
optionPage = j2d_ppdFindOption(ppd, "PageSize");
334
if (optionPage != NULL) {
335
nPages = optionPage->num_choices;
336
}
337
338
optionTray = j2d_ppdFindOption(ppd, "InputSlot");
339
if (optionTray != NULL) {
340
nTrays = optionTray->num_choices;
341
}
342
343
if ((nTotal = (nPages+nTrays) *2) > 0) {
344
nameArray = (*env)->NewObjectArray(env, nTotal, cls, NULL);
345
if (nameArray == NULL) {
346
unlink(filename);
347
j2d_ppdClose(ppd);
348
DPRINTF("CUPSfuncs::bad alloc new array\n", "")
349
if (!(*env)->ExceptionCheck(env)) {
350
JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
351
}
352
return NULL;
353
}
354
355
for (i = 0; optionPage!=NULL && i<nPages; i++) {
356
choice = (optionPage->choices)+i;
357
utf_str = JNU_NewStringPlatform(env, choice->text);
358
if (utf_str == NULL) {
359
unlink(filename);
360
j2d_ppdClose(ppd);
361
DPRINTF("CUPSfuncs::bad alloc new string ->text\n", "")
362
if (!(*env)->ExceptionCheck(env)) {
363
JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
364
}
365
return NULL;
366
}
367
(*env)->SetObjectArrayElement(env, nameArray, i*2, utf_str);
368
(*env)->DeleteLocalRef(env, utf_str);
369
utf_str = JNU_NewStringPlatform(env, choice->choice);
370
if (utf_str == NULL) {
371
unlink(filename);
372
j2d_ppdClose(ppd);
373
DPRINTF("CUPSfuncs::bad alloc new string ->choice\n", "")
374
if (!(*env)->ExceptionCheck(env)) {
375
JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
376
}
377
return NULL;
378
}
379
(*env)->SetObjectArrayElement(env, nameArray, i*2+1, utf_str);
380
(*env)->DeleteLocalRef(env, utf_str);
381
}
382
383
for (i = 0; optionTray!=NULL && i<nTrays; i++) {
384
choice = (optionTray->choices)+i;
385
utf_str = JNU_NewStringPlatform(env, choice->text);
386
if (utf_str == NULL) {
387
unlink(filename);
388
j2d_ppdClose(ppd);
389
DPRINTF("CUPSfuncs::bad alloc new string text\n", "")
390
if (!(*env)->ExceptionCheck(env)) {
391
JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
392
}
393
return NULL;
394
}
395
(*env)->SetObjectArrayElement(env, nameArray,
396
(nPages+i)*2, utf_str);
397
(*env)->DeleteLocalRef(env, utf_str);
398
utf_str = JNU_NewStringPlatform(env, choice->choice);
399
if (utf_str == NULL) {
400
unlink(filename);
401
j2d_ppdClose(ppd);
402
DPRINTF("CUPSfuncs::bad alloc new string choice\n", "")
403
if (!(*env)->ExceptionCheck(env)) {
404
JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
405
}
406
return NULL;
407
}
408
(*env)->SetObjectArrayElement(env, nameArray,
409
(nPages+i)*2+1, utf_str);
410
(*env)->DeleteLocalRef(env, utf_str);
411
}
412
}
413
j2d_ppdClose(ppd);
414
unlink(filename);
415
return nameArray;
416
}
417
418
419
/*
420
* Returns list of page sizes and imageable area.
421
*/
422
JNIEXPORT jfloatArray JNICALL
423
Java_sun_print_CUPSPrinter_getPageSizes(JNIEnv *env,
424
jobject printObj,
425
jstring printer)
426
{
427
ppd_file_t *ppd;
428
ppd_option_t *option;
429
ppd_choice_t *choice;
430
ppd_size_t *size;
431
const char *filename = NULL;
432
int i;
433
jobjectArray sizeArray = NULL;
434
jfloat *dims;
435
436
const char *name = (*env)->GetStringUTFChars(env, printer, NULL);
437
if (name == NULL) {
438
(*env)->ExceptionClear(env);
439
JNU_ThrowOutOfMemoryError(env, "Could not create printer name");
440
return NULL;
441
}
442
443
// NOTE: cupsGetPPD returns a pointer to a filename of a temporary file.
444
// unlink() must be called to remove the file after using it.
445
filename = j2d_cupsGetPPD(name);
446
(*env)->ReleaseStringUTFChars(env, printer, name);
447
CHECK_NULL_RETURN(filename, NULL);
448
if ((ppd = j2d_ppdOpenFile(filename)) == NULL) {
449
unlink(filename);
450
DPRINTF("unable to open PPD %s\n", filename)
451
return NULL;
452
}
453
option = j2d_ppdFindOption(ppd, "PageSize");
454
if (option != NULL && option->num_choices > 0) {
455
// create array of dimensions - (num_choices * 6)
456
//to cover length & height
457
DPRINTF( "CUPSfuncs::option->num_choices %d\n", option->num_choices)
458
// +1 is for storing the default media index
459
sizeArray = (*env)->NewFloatArray(env, option->num_choices*6+1);
460
if (sizeArray == NULL) {
461
unlink(filename);
462
j2d_ppdClose(ppd);
463
DPRINTF("CUPSfuncs::bad alloc new float array\n", "")
464
(*env)->ExceptionClear(env);
465
JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
466
return NULL;
467
}
468
469
dims = (*env)->GetFloatArrayElements(env, sizeArray, NULL);
470
if (dims == NULL) {
471
unlink(filename);
472
j2d_ppdClose(ppd);
473
(*env)->ExceptionClear(env);
474
JNU_ThrowOutOfMemoryError(env, "Could not create printer name");
475
return NULL;
476
}
477
for (i = 0; i<option->num_choices; i++) {
478
choice = (option->choices)+i;
479
// get the index of the default page
480
if (!strcmp(choice->choice, option->defchoice)) {
481
dims[option->num_choices*6] = (float)i;
482
}
483
size = j2d_ppdPageSize(ppd, choice->choice);
484
if (size != NULL) {
485
// paper width and height
486
dims[i*6] = size->width;
487
dims[(i*6)+1] = size->length;
488
// paper printable area
489
dims[(i*6)+2] = size->left;
490
dims[(i*6)+3] = size->top;
491
dims[(i*6)+4] = size->right;
492
dims[(i*6)+5] = size->bottom;
493
}
494
}
495
496
(*env)->ReleaseFloatArrayElements(env, sizeArray, dims, 0);
497
}
498
499
j2d_ppdClose(ppd);
500
unlink(filename);
501
return sizeArray;
502
}
503
504
/*
505
* Populates the supplied ArrayList<Integer> with resolutions.
506
* The first pair of elements will be the default resolution.
507
* If resolution isn't supported the list will be empty.
508
* If needed we can add a 2nd ArrayList<String> which would
509
* be populated with the corresponding UI name.
510
* PPD specifies the syntax for resolution as either "Ndpi" or "MxNdpi",
511
* eg 300dpi or 600x600dpi. The former is a shorthand where xres==yres.
512
* We will always expand to the latter as we use a single array list.
513
* Note: getMedia() and getPageSizes() both open the ppd file
514
* This is not going to scale forever so if we add anymore we
515
* should look to consolidate this.
516
*/
517
JNIEXPORT void JNICALL
518
Java_sun_print_CUPSPrinter_getResolutions(JNIEnv *env,
519
jobject printObj,
520
jstring printer,
521
jobject arrayList)
522
{
523
ppd_file_t *ppd = NULL;
524
ppd_option_t *resolution;
525
int defx = 0, defy = 0;
526
int resx = 0, resy = 0;
527
jclass intCls, cls;
528
jmethodID intCtr, arrListAddMID;
529
int i;
530
const char *name = NULL;
531
const char *filename = NULL;
532
533
intCls = (*env)->FindClass(env, "java/lang/Integer");
534
CHECK_NULL(intCls);
535
intCtr = (*env)->GetMethodID(env, intCls, "<init>", "(I)V");
536
CHECK_NULL(intCtr);
537
cls = (*env)->FindClass(env, "java/util/ArrayList");
538
CHECK_NULL(cls);
539
arrListAddMID =
540
(*env)->GetMethodID(env, cls, "add", "(Ljava/lang/Object;)Z");
541
CHECK_NULL(arrListAddMID);
542
543
name = (*env)->GetStringUTFChars(env, printer, NULL);
544
if (name == NULL) {
545
(*env)->ExceptionClear(env);
546
JNU_ThrowOutOfMemoryError(env, "Could not create printer name");
547
return;
548
}
549
550
551
// NOTE: cupsGetPPD returns a pointer to a filename of a temporary file.
552
// unlink() must be called to remove the file after using it.
553
filename = j2d_cupsGetPPD(name);
554
(*env)->ReleaseStringUTFChars(env, printer, name);
555
CHECK_NULL(filename);
556
if ((ppd = j2d_ppdOpenFile(filename)) == NULL) {
557
unlink(filename);
558
DPRINTF("unable to open PPD %s\n", filename)
559
}
560
resolution = j2d_ppdFindOption(ppd, "Resolution");
561
if (resolution != NULL) {
562
int matches = sscanf(resolution->defchoice, "%dx%ddpi", &defx, &defy);
563
if (matches == 2) {
564
if (defx <= 0 || defy <= 0) {
565
defx = 0;
566
defy = 0;
567
}
568
} else {
569
matches = sscanf(resolution->defchoice, "%ddpi", &defx);
570
if (matches == 1) {
571
if (defx <= 0) {
572
defx = 0;
573
} else {
574
defy = defx;
575
}
576
}
577
}
578
if (defx > 0) {
579
jobject rxObj, ryObj;
580
rxObj = (*env)->NewObject(env, intCls, intCtr, defx);
581
CHECK_NULL(rxObj);
582
ryObj = (*env)->NewObject(env, intCls, intCtr, defy);
583
CHECK_NULL(ryObj);
584
(*env)->CallBooleanMethod(env, arrayList, arrListAddMID, rxObj);
585
(*env)->CallBooleanMethod(env, arrayList, arrListAddMID, ryObj);
586
}
587
588
for (i = 0; i < resolution->num_choices; i++) {
589
char *resStr = resolution->choices[i].choice;
590
int matches = sscanf(resStr, "%dx%ddpi", &resx, &resy);
591
if (matches == 2) {
592
if (resx <= 0 || resy <= 0) {
593
resx = 0;
594
resy = 0;
595
}
596
} else {
597
matches = sscanf(resStr, "%ddpi", &resx);
598
if (matches == 1) {
599
if (resx <= 0) {
600
resx = 0;
601
} else {
602
resy = resx;
603
}
604
}
605
}
606
if (resx > 0 && (resx != defx || resy != defy )) {
607
jobject rxObj, ryObj;
608
rxObj = (*env)->NewObject(env, intCls, intCtr, resx);
609
CHECK_NULL(rxObj);
610
ryObj = (*env)->NewObject(env, intCls, intCtr, resy);
611
CHECK_NULL(ryObj);
612
(*env)->CallBooleanMethod(env, arrayList, arrListAddMID, rxObj);
613
(*env)->CallBooleanMethod(env, arrayList, arrListAddMID, ryObj);
614
}
615
}
616
}
617
618
j2d_ppdClose(ppd);
619
unlink(filename);
620
}
621
622