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 void (*fn_cupsFreeDests)(int num_dests, cups_dest_t *dests); 50 typedef ppd_file_t* (*fn_ppdOpenFile)(const char *); 51 typedef void (*fn_ppdClose)(ppd_file_t *); 52 typedef ppd_option_t* (*fn_ppdFindOption)(ppd_file_t *, const char *); 53 typedef ppd_size_t* (*fn_ppdPageSize)(ppd_file_t *, char *); 54 55 fn_cupsServer j2d_cupsServer; 56 fn_ippPort j2d_ippPort; 57 fn_httpConnect j2d_httpConnect; 58 fn_httpClose j2d_httpClose; 59 fn_cupsGetPPD j2d_cupsGetPPD; 60 fn_cupsGetDest j2d_cupsGetDest; 61 fn_cupsGetDests j2d_cupsGetDests; 62 fn_cupsFreeDests j2d_cupsFreeDests; 63 fn_ppdOpenFile j2d_ppdOpenFile; 64 fn_ppdClose j2d_ppdClose; 65 fn_ppdFindOption j2d_ppdFindOption; 66 fn_ppdPageSize j2d_ppdPageSize; 67 68 69 /* 70 * Initialize library functions. 71 * // REMIND : move tab , add dlClose before return 72 */ 73 JNIEXPORT jboolean JNICALL 74 Java_sun_print_CUPSPrinter_initIDs(JNIEnv *env, 75 jobject printObj) { 76 void *handle = dlopen(VERSIONED_JNI_LIB_NAME("cups", "2"), 77 RTLD_LAZY | RTLD_GLOBAL); 78 79 if (handle == NULL) { 80 handle = dlopen(JNI_LIB_NAME("cups"), RTLD_LAZY | RTLD_GLOBAL); 81 if (handle == NULL) { 82 return JNI_FALSE; 96 } 97 98 j2d_httpConnect = (fn_httpConnect)dlsym(handle, "httpConnect"); 99 if (j2d_httpConnect == NULL) { 100 dlclose(handle); 101 return JNI_FALSE; 102 } 103 104 j2d_httpClose = (fn_httpClose)dlsym(handle, "httpClose"); 105 if (j2d_httpClose == NULL) { 106 dlclose(handle); 107 return JNI_FALSE; 108 } 109 110 j2d_cupsGetPPD = (fn_cupsGetPPD)dlsym(handle, "cupsGetPPD"); 111 if (j2d_cupsGetPPD == NULL) { 112 dlclose(handle); 113 return JNI_FALSE; 114 } 115 116 j2d_cupsGetDest = (fn_cupsGetDest)dlsym(handle, "cupsGetDest"); 117 if (j2d_cupsGetDest == NULL) { 118 dlclose(handle); 119 return JNI_FALSE; 120 } 121 122 j2d_cupsGetDests = (fn_cupsGetDests)dlsym(handle, "cupsGetDests"); 123 if (j2d_cupsGetDests == NULL) { 124 dlclose(handle); 125 return JNI_FALSE; 126 } 127 128 j2d_cupsFreeDests = (fn_cupsFreeDests)dlsym(handle, "cupsFreeDests"); 129 if (j2d_cupsFreeDests == NULL) { 130 dlclose(handle); 131 return JNI_FALSE; 132 } 133 134 j2d_ppdOpenFile = (fn_ppdOpenFile)dlsym(handle, "ppdOpenFile"); 135 if (j2d_ppdOpenFile == NULL) { 136 dlclose(handle); 137 return JNI_FALSE; 138 139 } 140 141 j2d_ppdClose = (fn_ppdClose)dlsym(handle, "ppdClose"); 142 if (j2d_ppdClose == NULL) { 143 dlclose(handle); 144 return JNI_FALSE; 145 146 } 147 148 j2d_ppdFindOption = (fn_ppdFindOption)dlsym(handle, "ppdFindOption"); 149 if (j2d_ppdFindOption == NULL) { 150 dlclose(handle); 151 return JNI_FALSE; 152 } 153 176 cServer = JNU_NewStringPlatform(env, "localhost"); 177 } else { 178 cServer = JNU_NewStringPlatform(env, server); 179 } 180 } 181 return cServer; 182 } 183 184 /* 185 * Gets CUPS port name. 186 * 187 */ 188 JNIEXPORT jint JNICALL 189 Java_sun_print_CUPSPrinter_getCupsPort(JNIEnv *env, 190 jobject printObj) 191 { 192 int port = j2d_ippPort(); 193 return (jint) port; 194 } 195 196 197 /* 198 * Gets CUPS default printer name. 199 * 200 */ 201 JNIEXPORT jstring JNICALL 202 Java_sun_print_CUPSPrinter_getCupsDefaultPrinter(JNIEnv *env, 203 jobject printObj) 204 { 205 jstring cDefPrinter = NULL; 206 cups_dest_t *dests; 207 char *defaultPrinter = NULL; 208 int num_dests = j2d_cupsGetDests(&dests); 209 int i = 0; 210 cups_dest_t *dest = j2d_cupsGetDest(NULL, NULL, num_dests, dests); 211 if (dest != NULL) { 212 defaultPrinter = dest->name; 213 if (defaultPrinter != NULL) { 214 cDefPrinter = JNU_NewStringPlatform(env, defaultPrinter); 215 } 216 } 217 j2d_cupsFreeDests(num_dests, dests); 218 return cDefPrinter; 219 } 220 221 /* 222 * Checks if connection can be made to the server. 223 * 224 */ 225 JNIEXPORT jboolean JNICALL 226 Java_sun_print_CUPSPrinter_canConnect(JNIEnv *env, 227 jobject printObj, 228 jstring server, 229 jint port) 230 { 231 const char *serverName; 232 serverName = (*env)->GetStringUTFChars(env, server, NULL); 233 if (serverName != NULL) { 234 http_t *http = j2d_httpConnect(serverName, (int)port); 235 (*env)->ReleaseStringUTFChars(env, server, serverName); 236 if (http != NULL) { 237 j2d_httpClose(http); 238 return JNI_TRUE; 239 } |