< prev index next >

src/java.desktop/unix/native/common/awt/CUPSfuncs.c

Print this page




  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 //#define CUPS_DEBUG
  34 
  35 #ifdef CUPS_DEBUG
  36 #define DPRINTF(x, y) fprintf(stderr, x, y);
  37 #else
  38 #define DPRINTF(x, y)
  39 #endif
  40 
  41 typedef const char* (*fn_cupsServer)(void);
  42 typedef int (*fn_ippPort)(void);
  43 typedef http_t* (*fn_httpConnect)(const char *, int);
  44 typedef void (*fn_httpClose)(http_t *);
  45 typedef char* (*fn_cupsGetPPD)(const char *);



  46 typedef ppd_file_t* (*fn_ppdOpenFile)(const char *);
  47 typedef void (*fn_ppdClose)(ppd_file_t *);
  48 typedef ppd_option_t* (*fn_ppdFindOption)(ppd_file_t *, const char *);
  49 typedef ppd_size_t* (*fn_ppdPageSize)(ppd_file_t *, char *);
  50 
  51 fn_cupsServer j2d_cupsServer;
  52 fn_ippPort j2d_ippPort;
  53 fn_httpConnect j2d_httpConnect;
  54 fn_httpClose j2d_httpClose;
  55 fn_cupsGetPPD j2d_cupsGetPPD;


  56 fn_ppdOpenFile j2d_ppdOpenFile;
  57 fn_ppdClose j2d_ppdClose;
  58 fn_ppdFindOption j2d_ppdFindOption;
  59 fn_ppdPageSize j2d_ppdPageSize;
  60 
  61 
  62 /*
  63  * Initialize library functions.
  64  * // REMIND : move tab , add dlClose before return
  65  */
  66 JNIEXPORT jboolean JNICALL
  67 Java_sun_print_CUPSPrinter_initIDs(JNIEnv *env,
  68                                          jobject printObj) {
  69   void *handle = dlopen(VERSIONED_JNI_LIB_NAME("cups", "2"),
  70                         RTLD_LAZY | RTLD_GLOBAL);
  71 
  72   if (handle == NULL) {
  73     handle = dlopen(JNI_LIB_NAME("cups"), RTLD_LAZY | RTLD_GLOBAL);
  74     if (handle == NULL) {
  75       return JNI_FALSE;


  89   }
  90 
  91   j2d_httpConnect = (fn_httpConnect)dlsym(handle, "httpConnect");
  92   if (j2d_httpConnect == NULL) {
  93     dlclose(handle);
  94     return JNI_FALSE;
  95   }
  96 
  97   j2d_httpClose = (fn_httpClose)dlsym(handle, "httpClose");
  98   if (j2d_httpClose == NULL) {
  99     dlclose(handle);
 100     return JNI_FALSE;
 101   }
 102 
 103   j2d_cupsGetPPD = (fn_cupsGetPPD)dlsym(handle, "cupsGetPPD");
 104   if (j2d_cupsGetPPD == NULL) {
 105     dlclose(handle);
 106     return JNI_FALSE;
 107   }
 108 












 109   j2d_ppdOpenFile = (fn_ppdOpenFile)dlsym(handle, "ppdOpenFile");
 110   if (j2d_ppdOpenFile == NULL) {
 111     dlclose(handle);
 112     return JNI_FALSE;
 113 
 114   }
 115 
 116   j2d_ppdClose = (fn_ppdClose)dlsym(handle, "ppdClose");
 117   if (j2d_ppdClose == NULL) {
 118     dlclose(handle);
 119     return JNI_FALSE;
 120 
 121   }
 122 
 123   j2d_ppdFindOption = (fn_ppdFindOption)dlsym(handle, "ppdFindOption");
 124   if (j2d_ppdFindOption == NULL) {
 125     dlclose(handle);
 126     return JNI_FALSE;
 127   }
 128 


 151             cServer = JNU_NewStringPlatform(env, "localhost");
 152         } else {
 153             cServer = JNU_NewStringPlatform(env, server);
 154         }
 155     }
 156     return cServer;
 157 }
 158 
 159 /*
 160  * Gets CUPS port name.
 161  *
 162  */
 163 JNIEXPORT jint JNICALL
 164 Java_sun_print_CUPSPrinter_getCupsPort(JNIEnv *env,
 165                                          jobject printObj)
 166 {
 167     int port = j2d_ippPort();
 168     return (jint) port;
 169 }
 170 




























 171 
 172 /*
 173  * Checks if connection can be made to the server.
 174  *
 175  */
 176 JNIEXPORT jboolean JNICALL
 177 Java_sun_print_CUPSPrinter_canConnect(JNIEnv *env,
 178                                       jobject printObj,
 179                                       jstring server,
 180                                       jint port)
 181 {
 182     const char *serverName;
 183     serverName = (*env)->GetStringUTFChars(env, server, NULL);
 184     if (serverName != NULL) {
 185         http_t *http = j2d_httpConnect(serverName, (int)port);
 186         (*env)->ReleaseStringUTFChars(env, server, serverName);
 187         if (http != NULL) {
 188             j2d_httpClose(http);
 189             return JNI_TRUE;
 190         }




  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 //#define CUPS_DEBUG
  34 
  35 #ifdef CUPS_DEBUG
  36 #define DPRINTF(x, y) fprintf(stderr, x, y);
  37 #else
  38 #define DPRINTF(x, y)
  39 #endif
  40 
  41 typedef const char* (*fn_cupsServer)(void);
  42 typedef int (*fn_ippPort)(void);
  43 typedef http_t* (*fn_httpConnect)(const char *, int);
  44 typedef void (*fn_httpClose)(http_t *);
  45 typedef char* (*fn_cupsGetPPD)(const char *);
  46 typedef cups_dest_t* (*fn_cupsGetDest)(const char *name,
  47     const char *instance, int num_dests, cups_dest_t *dests);
  48 typedef int (*fn_cupsGetDests)(cups_dest_t **dests);
  49 typedef ppd_file_t* (*fn_ppdOpenFile)(const char *);
  50 typedef void (*fn_ppdClose)(ppd_file_t *);
  51 typedef ppd_option_t* (*fn_ppdFindOption)(ppd_file_t *, const char *);
  52 typedef ppd_size_t* (*fn_ppdPageSize)(ppd_file_t *, char *);
  53 
  54 fn_cupsServer j2d_cupsServer;
  55 fn_ippPort j2d_ippPort;
  56 fn_httpConnect j2d_httpConnect;
  57 fn_httpClose j2d_httpClose;
  58 fn_cupsGetPPD j2d_cupsGetPPD;
  59 fn_cupsGetDest j2d_cupsGetDest;
  60 fn_cupsGetDests j2d_cupsGetDests;
  61 fn_ppdOpenFile j2d_ppdOpenFile;
  62 fn_ppdClose j2d_ppdClose;
  63 fn_ppdFindOption j2d_ppdFindOption;
  64 fn_ppdPageSize j2d_ppdPageSize;
  65 
  66 
  67 /*
  68  * Initialize library functions.
  69  * // REMIND : move tab , add dlClose before return
  70  */
  71 JNIEXPORT jboolean JNICALL
  72 Java_sun_print_CUPSPrinter_initIDs(JNIEnv *env,
  73                                          jobject printObj) {
  74   void *handle = dlopen(VERSIONED_JNI_LIB_NAME("cups", "2"),
  75                         RTLD_LAZY | RTLD_GLOBAL);
  76 
  77   if (handle == NULL) {
  78     handle = dlopen(JNI_LIB_NAME("cups"), RTLD_LAZY | RTLD_GLOBAL);
  79     if (handle == NULL) {
  80       return JNI_FALSE;


  94   }
  95 
  96   j2d_httpConnect = (fn_httpConnect)dlsym(handle, "httpConnect");
  97   if (j2d_httpConnect == NULL) {
  98     dlclose(handle);
  99     return JNI_FALSE;
 100   }
 101 
 102   j2d_httpClose = (fn_httpClose)dlsym(handle, "httpClose");
 103   if (j2d_httpClose == NULL) {
 104     dlclose(handle);
 105     return JNI_FALSE;
 106   }
 107 
 108   j2d_cupsGetPPD = (fn_cupsGetPPD)dlsym(handle, "cupsGetPPD");
 109   if (j2d_cupsGetPPD == NULL) {
 110     dlclose(handle);
 111     return JNI_FALSE;
 112   }
 113 
 114   j2d_cupsGetDest = (fn_cupsGetDest)dlsym(handle, "cupsGetDest");
 115   if (j2d_cupsGetDest == NULL) {
 116     dlclose(handle);
 117     return JNI_FALSE;
 118   }
 119 
 120   j2d_cupsGetDests = (fn_cupsGetDests)dlsym(handle, "cupsGetDests");
 121   if (j2d_cupsGetDests == NULL) {
 122     dlclose(handle);
 123     return JNI_FALSE;
 124   }
 125 
 126   j2d_ppdOpenFile = (fn_ppdOpenFile)dlsym(handle, "ppdOpenFile");
 127   if (j2d_ppdOpenFile == NULL) {
 128     dlclose(handle);
 129     return JNI_FALSE;
 130 
 131   }
 132 
 133   j2d_ppdClose = (fn_ppdClose)dlsym(handle, "ppdClose");
 134   if (j2d_ppdClose == NULL) {
 135     dlclose(handle);
 136     return JNI_FALSE;
 137 
 138   }
 139 
 140   j2d_ppdFindOption = (fn_ppdFindOption)dlsym(handle, "ppdFindOption");
 141   if (j2d_ppdFindOption == NULL) {
 142     dlclose(handle);
 143     return JNI_FALSE;
 144   }
 145 


 168             cServer = JNU_NewStringPlatform(env, "localhost");
 169         } else {
 170             cServer = JNU_NewStringPlatform(env, server);
 171         }
 172     }
 173     return cServer;
 174 }
 175 
 176 /*
 177  * Gets CUPS port name.
 178  *
 179  */
 180 JNIEXPORT jint JNICALL
 181 Java_sun_print_CUPSPrinter_getCupsPort(JNIEnv *env,
 182                                          jobject printObj)
 183 {
 184     int port = j2d_ippPort();
 185     return (jint) port;
 186 }
 187 
 188 
 189 /*
 190  * Gets CUPS default printer name.
 191  *
 192  */
 193 JNIEXPORT jstring JNICALL
 194 Java_sun_print_CUPSPrinter_getCupsDefaultPrinter(JNIEnv *env,
 195                                                   jobject printObj)
 196 {
 197     jstring cDefPrinter = NULL;
 198     cups_dest_t *dests;
 199     char *defaultPrinter = NULL;
 200     int num_dests = j2d_cupsGetDests(&dests);
 201     int i = 0;
 202     cups_dest_t *dest = j2d_cupsGetDest(NULL, NULL, num_dests, dests);
 203     if (dest != NULL) {
 204         for (i = 0; i < num_dests; i++) {
 205             if (dest[i].is_default) {
 206                 defaultPrinter = dest[i].name;
 207                 break;
 208             }
 209         }
 210     }
 211     if (defaultPrinter != NULL) {
 212         cDefPrinter = JNU_NewStringPlatform(env, defaultPrinter);
 213     }
 214     return cDefPrinter;
 215 }
 216 
 217 /*
 218  * Checks if connection can be made to the server.
 219  *
 220  */
 221 JNIEXPORT jboolean JNICALL
 222 Java_sun_print_CUPSPrinter_canConnect(JNIEnv *env,
 223                                       jobject printObj,
 224                                       jstring server,
 225                                       jint port)
 226 {
 227     const char *serverName;
 228     serverName = (*env)->GetStringUTFChars(env, server, NULL);
 229     if (serverName != NULL) {
 230         http_t *http = j2d_httpConnect(serverName, (int)port);
 231         (*env)->ReleaseStringUTFChars(env, server, serverName);
 232         if (http != NULL) {
 233             j2d_httpClose(http);
 234             return JNI_TRUE;
 235         }


< prev index next >