summaryrefslogtreecommitdiff
path: root/japi/Sane.c
blob: 3fd9996e4ad6257fa1bc4abf86e75bd86e72c707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* sane - Scanner Access Now Easy.
   Copyright (C) 1997 Jeffrey S. Freedman
   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice.  */

/**
 **	Sane.c - Native methods for the SANE Java API.
 **
 **	Written: 10/9/97 - JSF
 **/

#include "Sane.h"
#include <sane/sane.h>
#include <string.h>

#include <stdio.h>	/* Debugging */

#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Sane
 * Method:    init
 * Signature: ([I)I
 */
JNIEXPORT jint JNICALL Java_Sane_init
  (JNIEnv *env, jobject jobj, jintArray versionCode)
	{
	jsize len;			/* Gets array length. */
	jint *versionCodeBody;		/* Gets ->array. */
	SANE_Int version;		/* Gets version. */
	SANE_Status status;		/* Get SANE return. */

	status = sane_init(&version, 0);
	len = (*env)->GetArrayLength(env, versionCode);
	versionCodeBody = (*env)->GetIntArrayElements(env, versionCode, 0);
	if (len > 0)			/* Return version. */
		versionCodeBody[0] = version;
	(*env)->ReleaseIntArrayElements(env, versionCode, versionCodeBody, 0);
	return (status);
	}
/*
 * Class:     Sane
 * Method:    exit
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_Sane_exit
  (JNIEnv *env, jobject jobj)
	{
	sane_exit();
	}


/*
 * Class:     Sane
 * Method:    getDevices
 * Signature: ([LSaneDevice;Z)I
 */
JNIEXPORT jint JNICALL Java_Sane_getDevicesNative
  (JNIEnv *env, jobject jobj, jobjectArray devList, jboolean localOnly)
	{
					/* Gets device list. */
	const SANE_Device **device_list;
	SANE_Status status;		/* Gets status. */
	int devListLen;			/* Gets length of devList. */
	jobject devObj;			/* Gets each SaneDevice object. */
	jclass devClass;		/* Gets SaneDevice class. */
	jfieldID fid;			/* Gets each field ID. */
	int i;

					/* Get the list. */
	status = sane_get_devices(&device_list, localOnly);
	if (status != SANE_STATUS_GOOD)
		return (status);
					/* Get length of Java array. */
	devListLen = (*env)->GetArrayLength(env, devList);
					/* Return devices to user. */
	for (i = 0; i < devListLen - 1 && device_list[i]; i++)
		{
					/* Get Java object, class. */
		devObj = (*env)->GetObjectArrayElement(env, devList, i);
		devClass = (*env)->GetObjectClass(env, devObj);
					/* Fill in each member. */
		fid = (*env)->GetFieldID(env, devClass, "name",
							"Ljava/lang/String;");
		(*env)->SetObjectField(env, devObj, fid,
			(*env)->NewStringUTF(env, device_list[i]->name));
		fid = (*env)->GetFieldID(env, devClass, "vendor",
							"Ljava/lang/String;");
		(*env)->SetObjectField(env, devObj, fid,
			(*env)->NewStringUTF(env, device_list[i]->vendor));
		fid = (*env)->GetFieldID(env, devClass, "model",
							"Ljava/lang/String;");
		(*env)->SetObjectField(env, devObj, fid,
			(*env)->NewStringUTF(env, device_list[i]->model));
		fid = (*env)->GetFieldID(env, devClass, "type",
							"Ljava/lang/String;");
		(*env)->SetObjectField(env, devObj, fid,
			(*env)->NewStringUTF(env, device_list[i]->type));
		}
					/* End list with a null. */
	(*env)->SetObjectArrayElement(env, devList, i, 0);
	return (status);
	}

/*
 * Class:     Sane
 * Method:    open
 * Signature: (Ljava/lang/String;[J)I
 */
JNIEXPORT jint JNICALL Java_Sane_open
  (JNIEnv *env, jobject jobj, jstring deviceName, jintArray handle)
	{
	SANE_Handle sane_handle;	/* Gets handle. */
	jint s_handle;
	const char *device_name;	/* Gets dev. name. */
	int status;			/* Gets return code. */

	device_name = (*env)->GetStringUTFChars(env, deviceName, 0);
					/* Open it. */
	status = sane_open(device_name, &sane_handle);
	(*env)->ReleaseStringUTFChars(env, deviceName, device_name);
					/* Return handle. */
	s_handle = (jint) sane_handle;
	(*env)->SetIntArrayRegion(env, handle, 0, 1, &s_handle);
	return (status);
	}

/*
 * Class:     Sane
 * Method:    close
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_Sane_close
  (JNIEnv *env, jobject jobj, jint handle)
	{
	sane_close((SANE_Handle) handle);
	}

/*
 * Class:     Sane
 * Method:    getOptionNative
 * Signature: (IILSaneOption;)V
 */
JNIEXPORT void JNICALL Java_Sane_getOptionNative
  (JNIEnv *env, jobject jobj, jint handle, jint option, jobject optObj)
	{
	jclass optClass;		/* Gets its class. */
	jfieldID fid;			/* Gets each field ID. */
	jstring str;			/* Gets strings. */

					/* Get info from sane. */
	const SANE_Option_Descriptor *sopt = sane_get_option_descriptor(
			(SANE_Handle) handle, option);
					/* Get class info. */
	optClass = (*env)->GetObjectClass(env, optObj);
					/* Fill in each member. */
	fid = (*env)->GetFieldID(env, optClass, "name", "Ljava/lang/String;");
	if (!sopt)			/* Failed. */
		{			/* Set name to null. */
		(*env)->SetObjectField(env, optObj, fid, 0);
		return;
		}
					/* Return name. */
	(*env)->SetObjectField(env, optObj, fid,
				(*env)->NewStringUTF(env, sopt->name));
					/* Return title. */
	fid = (*env)->GetFieldID(env, optClass, "title", "Ljava/lang/String;");
	str = sopt->title ? (*env)->NewStringUTF(env, sopt->title) : 0;
	(*env)->SetObjectField(env, optObj, fid, str);
					/* Return descr. */
	fid = (*env)->GetFieldID(env, optClass, "desc", "Ljava/lang/String;");
	(*env)->SetObjectField(env, optObj, fid,
				(*env)->NewStringUTF(env, sopt->desc));
					/* Return type. */
	fid = (*env)->GetFieldID(env, optClass, "type", "I");
	(*env)->SetIntField(env, optObj, fid, sopt->type);
					/* Return unit. */
	fid = (*env)->GetFieldID(env, optClass, "unit", "I");
	(*env)->SetIntField(env, optObj, fid, sopt->unit);
					/* Return size. */
	fid = (*env)->GetFieldID(env, optClass, "size", "I");
	(*env)->SetIntField(env, optObj, fid, sopt->size);
					/* Return cap. */
	fid = (*env)->GetFieldID(env, optClass, "cap", "I");
	(*env)->SetIntField(env, optObj, fid, sopt->cap);
					/* Return constraint_type. */
	fid = (*env)->GetFieldID(env, optClass, "constraintType", "I");
	(*env)->SetIntField(env, optObj, fid, sopt->constraint_type);
	/*
	 *	Now for the constraint itself.
	 */
	if (sopt->constraint_type == SANE_CONSTRAINT_RANGE)
		{
					/* Create range object. */
		jclass rangeClass = (*env)->FindClass(env, "SaneRange");
		jobject range = (*env)->AllocObject(env, rangeClass);
					/* Fill in fields. */
		fid = (*env)->GetFieldID(env, rangeClass, "min", "I");
		(*env)->SetIntField(env, range, fid,
						sopt->constraint.range->min);
		fid = (*env)->GetFieldID(env, rangeClass, "max", "I");
		(*env)->SetIntField(env, range, fid,
						sopt->constraint.range->max);
		fid = (*env)->GetFieldID(env, rangeClass, "quant", "I");
		(*env)->SetIntField(env, range, fid,
						sopt->constraint.range->quant);
		fid = (*env)->GetFieldID(env, optClass, "rangeConstraint",
						"LSaneRange;");
					/* Store range. */
		(*env)->SetObjectField(env, optObj, fid, range);
		}
	else if (sopt->constraint_type == SANE_CONSTRAINT_WORD_LIST)
		{			/* Get array of integers. */
		jintArray wordList;
		jint *elements;
		int i;
					/* First word. is the length.	*/
		wordList = (*env)->NewIntArray(env,
				sopt->constraint.word_list[0]);
					/* Copy in the integers.	*/
		elements = (*env)->GetIntArrayElements(env, wordList, 0);
		for (i = 0; i < sopt->constraint.word_list[0]; i++)
			elements[i] = sopt->constraint.word_list[i];
		(*env)->ReleaseIntArrayElements(env, wordList, elements, 0);
					/* Set the field. */
		fid = (*env)->GetFieldID(env, optClass, "wordListConstraint",
									"[I");
		(*env)->SetObjectField(env, optObj, fid, wordList);
		}
	else if (sopt->constraint_type == SANE_CONSTRAINT_STRING_LIST)
		{
		jclass stringClass = (*env)->FindClass(env, "java/lang/String");
		jobjectArray stringList;
		int len;		/* Gets # elements */
		int i;

		for (len = 0; sopt->constraint.string_list[len]; len++)
			;
		stringList = (*env)->NewObjectArray(env, len + 1,
							stringClass, 0);
					/* Add each string. */
		for (i = 0; i < len; i++)
			{
			(*env)->SetObjectArrayElement(env, stringList, i,
				(*env)->NewStringUTF(env,
					sopt->constraint.string_list[i]));
			}
					/* 0 at end. */
		(*env)->SetObjectArrayElement(env, stringList, len, 0);
					/* Set the field. */
		fid = (*env)->GetFieldID(env, optClass,
			"stringListConstraint", "[Ljava/lang/String;");
		(*env)->SetObjectField(env, optObj, fid, stringList);
		}
	}

/*
 * Class:     Sane
 * Method:    getControlOption
 * Signature: (II[I[I)I
 */
JNIEXPORT jint JNICALL Java_Sane_getControlOption__II_3I_3I
  (JNIEnv *env, jobject jobj, jint handle, jint option, jintArray value,
						jintArray info)
	{
	SANE_Status status;		/* Gets status. */
	SANE_Int i;			/* Gets info. passed back. */
	int v;

	status = sane_control_option((SANE_Handle) handle, option,
			SANE_ACTION_GET_VALUE, &v, &i);
	if (value)
		(*env)->SetIntArrayRegion(env, value, 0, 1, &v);
	if (info)
		(*env)->SetIntArrayRegion(env, info, 0, 1, &i);
	return (status);
	}

/*
 * Class:     Sane
 * Method:    getControlOption
 * Signature: (II[B[I)I
 */
JNIEXPORT jint JNICALL Java_Sane_getControlOption__II_3B_3I
  (JNIEnv *env, jobject jobj, jint handle, jint option, jbyteArray value,
						jintArray info)
	{
	SANE_Status status;		/* Gets status. */
	SANE_Int i;			/* Gets info. passed back. */
	char *str;

	str = (*env)->GetByteArrayElements(env, value, 0);
	status = sane_control_option((SANE_Handle) handle, option,
			SANE_ACTION_GET_VALUE, str, &i);
	(*env)->ReleaseByteArrayElements(env, value, str, 0);
	if (info)
		(*env)->SetIntArrayRegion(env, info, 0, 1, &i);
	return (status);
	}

/*
 * Class:     Sane
 * Method:    setControlOption
 * Signature: (IIII[I)I
 */
JNIEXPORT jint JNICALL Java_Sane_setControlOption__IIII_3I
  (JNIEnv *env, jobject jobj, jint handle, jint option, jint action,
				jint value, jintArray info)
	{
	SANE_Status status;		/* Gets status. */
	SANE_Int i;			/* Gets info. passed back. */
	status = sane_control_option((SANE_Handle) handle, option, action,
					&value, &i);
	if (info)
		(*env)->SetIntArrayRegion(env, info, 0, 1, &i);
	return (status);
	}

/*
 *	Get string length.  This exists because sometimes strings seem to be
 *	padded with negatives.
 */

static int String_length
	(
	const char *str
	)
	{
	const char *ptr;
	for (ptr = str; *ptr > 0; ptr++)
		;
	return ((int) (ptr - str));
	}

/*
 * Class:     Sane
 * Method:    setControlOption
 * Signature: (IIILjava/lang/String;[I)I
 */
JNIEXPORT jint JNICALL Java_Sane_setControlOption__IIILjava_lang_String_2_3I
  (JNIEnv *env, jobject jobj, jint handle, jint option, jint action,
				jstring value, jintArray info)
	{
	SANE_Status status;		/* Gets status. */
	SANE_Int i;			/* Gets info. passed back. */
	const char *valuep;
	char buf[512];			/* Hope this is big enough. */
	int len;			/* Gets string length. */

	valuep = (*env)->GetStringUTFChars(env, value, 0);
	len = String_length(valuep);
	if (len >= sizeof(buf))
		len = sizeof(buf) - 1;
	strncpy(buf, valuep, len);
	buf[len] = 0;			/* Insure it's 0-delimited. */
	status = sane_control_option((SANE_Handle) handle, option, action,
					(void *) &buf[0], &i);
					/* +++++++Want to return new val? */
	(*env)->ReleaseStringUTFChars(env, value, valuep);
	if (info)
		(*env)->SetIntArrayRegion(env, info, 0, 1, &i);
	return (status);
	}

/*
 * Class:     Sane
 * Method:    getParameters
 * Signature: (ILSaneParameters;)I
 */
JNIEXPORT jint JNICALL Java_Sane_getParameters
  (JNIEnv *env, jobject jobj, jint handle, jobject paramsObj)
	{
	SANE_Status status;		/* Gets status.	*/
	SANE_Parameters params;		/* Gets params. */
	jclass paramsClass;		/* Gets its class. */
	jfieldID fid;			/* Gets each field ID. */

	status = sane_get_parameters((SANE_Handle) handle, &params);
					/* Get class info. */
	paramsClass = (*env)->GetObjectClass(env, paramsObj);
					/* Fill in each member. */
	fid = (*env)->GetFieldID(env, paramsClass, "format", "I");
	(*env)->SetIntField(env, paramsObj, fid, params.format);
	fid = (*env)->GetFieldID(env, paramsClass, "lastFrame", "Z");
	(*env)->SetBooleanField(env, paramsObj, fid, params.last_frame);
	fid = (*env)->GetFieldID(env, paramsClass, "bytesPerLine", "I");
	(*env)->SetIntField(env, paramsObj, fid, params.bytes_per_line);
	fid = (*env)->GetFieldID(env, paramsClass, "pixelsPerLine", "I");
	(*env)->SetIntField(env, paramsObj, fid, params.pixels_per_line);
	fid = (*env)->GetFieldID(env, paramsClass, "lines", "I");
	(*env)->SetIntField(env, paramsObj, fid, params.lines);
	fid = (*env)->GetFieldID(env, paramsClass, "depth", "I");
	(*env)->SetIntField(env, paramsObj, fid, params.depth);
	return (status);
	}

/*
 * Class:     Sane
 * Method:    start
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_Sane_start
  (JNIEnv *env, jobject jobj, jint handle)
	{
	return (sane_start((SANE_Handle) handle));
	}

/*
 * Class:     Sane
 * Method:    read
 * Signature: (I[BI[I)I
 */
JNIEXPORT jint JNICALL Java_Sane_read
  (JNIEnv *env, jobject jobj, jint handle, jbyteArray data, jint maxLength,
					jintArray length)
	{
	int status;
	jbyte *dataElements;
	int read_len;			/* # bytes read. */

					/* Get actual data ptr. */
	dataElements = (*env)->GetByteArrayElements(env, data, 0);
					/* Do the read. */
	status = sane_read((SANE_Handle) handle, dataElements,
						maxLength, &read_len);
	(*env)->ReleaseByteArrayElements(env, data, dataElements, 0);
					/* Return # bytes read. */
	(*env)->SetIntArrayRegion(env, length, 0, 1, &read_len);
	return (status);
	}

/*
 * Class:     Sane
 * Method:    cancel
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_Sane_cancel
  (JNIEnv *env, jobject jobj, jint handle)
	{
	sane_cancel((SANE_Handle) handle);
	}

/*
 * Class:     Sane
 * Method:    strstatus
 * Signature: (I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_Sane_strstatus
  (JNIEnv *env, jobject jobj, jint status)
	{
	const char *str = sane_strstatus(status);
	return ((*env)->NewStringUTF(env, str));
	}

#ifdef __cplusplus
}
#endif