0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <jni.h>
0N/A#include <jni_util.h>
4632N/A#include <jvm_md.h>
0N/A#include <dlfcn.h>
0N/A#include <cups/cups.h>
0N/A#include <cups/ppd.h>
0N/A
0N/A//#define CUPS_DEBUG
0N/A
0N/A#ifdef CUPS_DEBUG
0N/A#define DPRINTF(x, y) fprintf(stderr, x, y);
0N/A#else
0N/A#define DPRINTF(x, y)
0N/A#endif
0N/A
0N/Atypedef const char* (*fn_cupsServer)(void);
0N/Atypedef int (*fn_ippPort)(void);
0N/Atypedef http_t* (*fn_httpConnect)(const char *, int);
0N/Atypedef void (*fn_httpClose)(http_t *);
0N/Atypedef char* (*fn_cupsGetPPD)(const char *);
0N/Atypedef ppd_file_t* (*fn_ppdOpenFile)(const char *);
0N/Atypedef void (*fn_ppdClose)(ppd_file_t *);
0N/Atypedef ppd_option_t* (*fn_ppdFindOption)(ppd_file_t *, const char *);
0N/Atypedef ppd_size_t* (*fn_ppdPageSize)(ppd_file_t *, char *);
0N/A
0N/Afn_cupsServer j2d_cupsServer;
0N/Afn_ippPort j2d_ippPort;
0N/Afn_httpConnect j2d_httpConnect;
0N/Afn_httpClose j2d_httpClose;
0N/Afn_cupsGetPPD j2d_cupsGetPPD;
0N/Afn_ppdOpenFile j2d_ppdOpenFile;
0N/Afn_ppdClose j2d_ppdClose;
0N/Afn_ppdFindOption j2d_ppdFindOption;
0N/Afn_ppdPageSize j2d_ppdPageSize;
0N/A
0N/A
0N/A/*
0N/A * Initialize library functions.
0N/A * // REMIND : move tab , add dlClose before return
0N/A */
0N/AJNIEXPORT jboolean JNICALL
0N/AJava_sun_print_CUPSPrinter_initIDs(JNIEnv *env,
0N/A jobject printObj) {
4632N/A void *handle = dlopen(VERSIONED_JNI_LIB_NAME("cups", "2"),
4632N/A RTLD_LAZY | RTLD_GLOBAL);
0N/A
0N/A if (handle == NULL) {
4632N/A handle = dlopen(JNI_LIB_NAME("cups"), RTLD_LAZY | RTLD_GLOBAL);
0N/A if (handle == NULL) {
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A
0N/A j2d_cupsServer = (fn_cupsServer)dlsym(handle, "cupsServer");
0N/A if (j2d_cupsServer == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A j2d_ippPort = (fn_ippPort)dlsym(handle, "ippPort");
0N/A if (j2d_ippPort == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A j2d_httpConnect = (fn_httpConnect)dlsym(handle, "httpConnect");
0N/A if (j2d_httpConnect == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A j2d_httpClose = (fn_httpClose)dlsym(handle, "httpClose");
0N/A if (j2d_httpClose == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A j2d_cupsGetPPD = (fn_cupsGetPPD)dlsym(handle, "cupsGetPPD");
0N/A if (j2d_cupsGetPPD == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A j2d_ppdOpenFile = (fn_ppdOpenFile)dlsym(handle, "ppdOpenFile");
0N/A if (j2d_ppdOpenFile == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A
0N/A }
0N/A
0N/A j2d_ppdClose = (fn_ppdClose)dlsym(handle, "ppdClose");
0N/A if (j2d_ppdClose == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A
0N/A }
0N/A
0N/A j2d_ppdFindOption = (fn_ppdFindOption)dlsym(handle, "ppdFindOption");
0N/A if (j2d_ppdFindOption == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A j2d_ppdPageSize = (fn_ppdPageSize)dlsym(handle, "ppdPageSize");
0N/A if (j2d_ppdPageSize == NULL) {
0N/A dlclose(handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/A/*
0N/A * Gets CUPS server name.
0N/A *
0N/A */
0N/AJNIEXPORT jstring JNICALL
0N/AJava_sun_print_CUPSPrinter_getCupsServer(JNIEnv *env,
0N/A jobject printObj)
0N/A{
0N/A jstring cServer = NULL;
0N/A const char* server = j2d_cupsServer();
0N/A if (server != NULL) {
0N/A // Is this a local domain socket?
0N/A if (strncmp(server, "/", 1) == 0) {
0N/A cServer = JNU_NewStringPlatform(env, "localhost");
0N/A } else {
0N/A cServer = JNU_NewStringPlatform(env, server);
0N/A }
0N/A }
0N/A return cServer;
0N/A}
0N/A
0N/A/*
0N/A * Gets CUPS port name.
0N/A *
0N/A */
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_print_CUPSPrinter_getCupsPort(JNIEnv *env,
0N/A jobject printObj)
0N/A{
0N/A int port = j2d_ippPort();
0N/A return (jint) port;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Checks if connection can be made to the server.
0N/A *
0N/A */
0N/AJNIEXPORT jboolean JNICALL
0N/AJava_sun_print_CUPSPrinter_canConnect(JNIEnv *env,
0N/A jobject printObj,
0N/A jstring server,
0N/A jint port)
0N/A{
0N/A const char *serverName;
0N/A serverName = (*env)->GetStringUTFChars(env, server, NULL);
0N/A if (serverName != NULL) {
0N/A http_t *http = j2d_httpConnect(serverName, (int)port);
0N/A (*env)->ReleaseStringUTFChars(env, server, serverName);
0N/A if (http != NULL) {
0N/A j2d_httpClose(http);
0N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A return JNI_FALSE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Returns list of media: pages + trays
0N/A */
0N/AJNIEXPORT jobjectArray JNICALL
0N/AJava_sun_print_CUPSPrinter_getMedia(JNIEnv *env,
0N/A jobject printObj,
0N/A jstring printer)
0N/A{
0N/A ppd_file_t *ppd;
0N/A ppd_option_t *optionTray, *optionPage;
0N/A ppd_choice_t *choice;
0N/A const char *name;
0N/A const char *filename;
0N/A int i, nTrays=0, nPages=0, nTotal=0;
0N/A jstring utf_str;
0N/A jclass cls;
0N/A jobjectArray nameArray = NULL;
0N/A
0N/A name = (*env)->GetStringUTFChars(env, printer, NULL);
0N/A if (name == NULL) {
0N/A return NULL;
0N/A }
0N/A
0N/A // NOTE: cupsGetPPD returns a pointer to a filename of a temporary file.
0N/A // unlink() must be caled to remove the file when finished using it.
0N/A filename = j2d_cupsGetPPD(name);
0N/A (*env)->ReleaseStringUTFChars(env, printer, name);
0N/A
0N/A cls = (*env)->FindClass(env, "java/lang/String");
0N/A
0N/A if (filename == NULL) {
0N/A return NULL;
0N/A }
0N/A
0N/A if ((ppd = j2d_ppdOpenFile(filename)) == NULL) {
0N/A unlink(filename);
0N/A DPRINTF("CUPSfuncs::unable to open PPD %s\n", filename);
0N/A return NULL;
0N/A }
0N/A
0N/A optionPage = j2d_ppdFindOption(ppd, "PageSize");
0N/A if (optionPage != NULL) {
0N/A nPages = optionPage->num_choices;
0N/A }
0N/A
0N/A optionTray = j2d_ppdFindOption(ppd, "InputSlot");
0N/A if (optionTray != NULL) {
0N/A nTrays = optionTray->num_choices;
0N/A }
0N/A
0N/A if ((nTotal = (nPages+nTrays) *2) > 0) {
0N/A nameArray = (*env)->NewObjectArray(env, nTotal, cls, NULL);
0N/A if (nameArray == NULL) {
0N/A unlink(filename);
0N/A j2d_ppdClose(ppd);
0N/A DPRINTF("CUPSfuncs::bad alloc new array\n", "")
0N/A JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
0N/A return NULL;
0N/A }
0N/A
0N/A for (i = 0; optionPage!=NULL && i<nPages; i++) {
0N/A choice = (optionPage->choices)+i;
0N/A utf_str = JNU_NewStringPlatform(env, choice->text);
0N/A if (utf_str == NULL) {
0N/A unlink(filename);
0N/A j2d_ppdClose(ppd);
0N/A DPRINTF("CUPSfuncs::bad alloc new string ->text\n", "")
0N/A JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
0N/A return NULL;
0N/A }
0N/A (*env)->SetObjectArrayElement(env, nameArray, i*2, utf_str);
0N/A (*env)->DeleteLocalRef(env, utf_str);
0N/A utf_str = JNU_NewStringPlatform(env, choice->choice);
0N/A if (utf_str == NULL) {
0N/A unlink(filename);
0N/A j2d_ppdClose(ppd);
0N/A DPRINTF("CUPSfuncs::bad alloc new string ->choice\n", "")
0N/A JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
0N/A return NULL;
0N/A }
0N/A (*env)->SetObjectArrayElement(env, nameArray, i*2+1, utf_str);
0N/A (*env)->DeleteLocalRef(env, utf_str);
0N/A }
0N/A
0N/A for (i = 0; optionTray!=NULL && i<nTrays; i++) {
0N/A choice = (optionTray->choices)+i;
0N/A utf_str = JNU_NewStringPlatform(env, choice->text);
0N/A if (utf_str == NULL) {
0N/A unlink(filename);
0N/A j2d_ppdClose(ppd);
0N/A DPRINTF("CUPSfuncs::bad alloc new string text\n", "")
0N/A JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
0N/A return NULL;
0N/A }
0N/A (*env)->SetObjectArrayElement(env, nameArray,
0N/A (nPages+i)*2, utf_str);
0N/A (*env)->DeleteLocalRef(env, utf_str);
0N/A utf_str = JNU_NewStringPlatform(env, choice->choice);
0N/A if (utf_str == NULL) {
0N/A unlink(filename);
0N/A j2d_ppdClose(ppd);
0N/A DPRINTF("CUPSfuncs::bad alloc new string choice\n", "")
0N/A JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
0N/A return NULL;
0N/A }
0N/A (*env)->SetObjectArrayElement(env, nameArray,
0N/A (nPages+i)*2+1, utf_str);
0N/A (*env)->DeleteLocalRef(env, utf_str);
0N/A }
0N/A }
0N/A j2d_ppdClose(ppd);
0N/A unlink(filename);
0N/A return nameArray;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Returns list of page sizes and imageable area.
0N/A */
0N/AJNIEXPORT jfloatArray JNICALL
0N/AJava_sun_print_CUPSPrinter_getPageSizes(JNIEnv *env,
0N/A jobject printObj,
0N/A jstring printer)
0N/A{
0N/A ppd_file_t *ppd;
0N/A ppd_option_t *option;
0N/A ppd_choice_t *choice;
0N/A ppd_size_t *size;
0N/A
0N/A const char *name = (*env)->GetStringUTFChars(env, printer, NULL);
0N/A const char *filename;
0N/A int i;
0N/A jobjectArray sizeArray = NULL;
0N/A jfloat *dims;
0N/A
0N/A // NOTE: cupsGetPPD returns a pointer to a filename of a temporary file.
0N/A // unlink() must be called to remove the file after using it.
0N/A filename = j2d_cupsGetPPD(name);
0N/A (*env)->ReleaseStringUTFChars(env, printer, name);
0N/A if (filename == NULL) {
0N/A return NULL;
0N/A }
0N/A if ((ppd = j2d_ppdOpenFile(filename)) == NULL) {
0N/A unlink(filename);
0N/A DPRINTF("unable to open PPD %s\n", filename)
0N/A return NULL;
0N/A }
0N/A option = j2d_ppdFindOption(ppd, "PageSize");
0N/A if (option != NULL && option->num_choices > 0) {
0N/A // create array of dimensions - (num_choices * 6)
0N/A //to cover length & height
0N/A DPRINTF( "CUPSfuncs::option->num_choices %d\n", option->num_choices)
0N/A sizeArray = (*env)->NewFloatArray(env, option->num_choices*6);
0N/A if (sizeArray == NULL) {
0N/A unlink(filename);
0N/A j2d_ppdClose(ppd);
0N/A DPRINTF("CUPSfuncs::bad alloc new float array\n", "")
0N/A JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
0N/A return NULL;
0N/A }
0N/A
0N/A dims = (*env)->GetFloatArrayElements(env, sizeArray, NULL);
0N/A for (i = 0; i<option->num_choices; i++) {
0N/A choice = (option->choices)+i;
0N/A size = j2d_ppdPageSize(ppd, choice->choice);
0N/A if (size != NULL) {
0N/A // paper width and height
0N/A dims[i*6] = size->width;
0N/A dims[(i*6)+1] = size->length;
0N/A // paper printable area
0N/A dims[(i*6)+2] = size->left;
0N/A dims[(i*6)+3] = size->top;
0N/A dims[(i*6)+4] = size->right;
0N/A dims[(i*6)+5] = size->bottom;
0N/A }
0N/A }
0N/A
0N/A (*env)->ReleaseFloatArrayElements(env, sizeArray, dims, 0);
0N/A }
0N/A
0N/A j2d_ppdClose(ppd);
0N/A unlink(filename);
0N/A return sizeArray;
0N/A}