1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci   Copyright (C) 1997 Jeffrey S. Freedman
3141cc406Sopenharmony_ci   This file is part of the SANE package.
4141cc406Sopenharmony_ci
5141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
6141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
7141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
8141cc406Sopenharmony_ci   License, or (at your option) any later version.
9141cc406Sopenharmony_ci
10141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
11141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
12141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13141cc406Sopenharmony_ci   General Public License for more details.
14141cc406Sopenharmony_ci
15141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
16141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
17141cc406Sopenharmony_ci
18141cc406Sopenharmony_ci   As a special exception, the authors of SANE give permission for
19141cc406Sopenharmony_ci   additional uses of the libraries contained in this release of SANE.
20141cc406Sopenharmony_ci
21141cc406Sopenharmony_ci   The exception is that, if you link a SANE library with other files
22141cc406Sopenharmony_ci   to produce an executable, this does not by itself cause the
23141cc406Sopenharmony_ci   resulting executable to be covered by the GNU General Public
24141cc406Sopenharmony_ci   License.  Your use of that executable is in no way restricted on
25141cc406Sopenharmony_ci   account of linking the SANE library code into it.
26141cc406Sopenharmony_ci
27141cc406Sopenharmony_ci   This exception does not, however, invalidate any other reasons why
28141cc406Sopenharmony_ci   the executable file might be covered by the GNU General Public
29141cc406Sopenharmony_ci   License.
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ci   If you submit changes to SANE to the maintainers to be included in
32141cc406Sopenharmony_ci   a subsequent release, you agree by submitting the changes that
33141cc406Sopenharmony_ci   those changes may be distributed with this exception intact.
34141cc406Sopenharmony_ci
35141cc406Sopenharmony_ci   If you write modifications of your own for SANE, it is your choice
36141cc406Sopenharmony_ci   whether to permit this exception to apply to your modifications.
37141cc406Sopenharmony_ci   If you do not wish that, delete this exception notice.  */
38141cc406Sopenharmony_ci
39141cc406Sopenharmony_ci/**
40141cc406Sopenharmony_ci **	Sane.c - Native methods for the SANE Java API.
41141cc406Sopenharmony_ci **
42141cc406Sopenharmony_ci **	Written: 10/9/97 - JSF
43141cc406Sopenharmony_ci **/
44141cc406Sopenharmony_ci
45141cc406Sopenharmony_ci#include "Sane.h"
46141cc406Sopenharmony_ci#include <sane/sane.h>
47141cc406Sopenharmony_ci#include <string.h>
48141cc406Sopenharmony_ci
49141cc406Sopenharmony_ci#include <stdio.h>	/* Debugging */
50141cc406Sopenharmony_ci
51141cc406Sopenharmony_ci#ifdef __cplusplus
52141cc406Sopenharmony_ciextern "C" {
53141cc406Sopenharmony_ci#endif
54141cc406Sopenharmony_ci/*
55141cc406Sopenharmony_ci * Class:     Sane
56141cc406Sopenharmony_ci * Method:    init
57141cc406Sopenharmony_ci * Signature: ([I)I
58141cc406Sopenharmony_ci */
59141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_init
60141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jintArray versionCode)
61141cc406Sopenharmony_ci	{
62141cc406Sopenharmony_ci	jsize len;			/* Gets array length. */
63141cc406Sopenharmony_ci	jint *versionCodeBody;		/* Gets ->array. */
64141cc406Sopenharmony_ci	SANE_Int version;		/* Gets version. */
65141cc406Sopenharmony_ci	SANE_Status status;		/* Get SANE return. */
66141cc406Sopenharmony_ci
67141cc406Sopenharmony_ci	status = sane_init(&version, 0);
68141cc406Sopenharmony_ci	len = (*env)->GetArrayLength(env, versionCode);
69141cc406Sopenharmony_ci	versionCodeBody = (*env)->GetIntArrayElements(env, versionCode, 0);
70141cc406Sopenharmony_ci	if (len > 0)			/* Return version. */
71141cc406Sopenharmony_ci		versionCodeBody[0] = version;
72141cc406Sopenharmony_ci	(*env)->ReleaseIntArrayElements(env, versionCode, versionCodeBody, 0);
73141cc406Sopenharmony_ci	return (status);
74141cc406Sopenharmony_ci	}
75141cc406Sopenharmony_ci/*
76141cc406Sopenharmony_ci * Class:     Sane
77141cc406Sopenharmony_ci * Method:    exit
78141cc406Sopenharmony_ci * Signature: ()V
79141cc406Sopenharmony_ci */
80141cc406Sopenharmony_ciJNIEXPORT void JNICALL Java_Sane_exit
81141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj)
82141cc406Sopenharmony_ci	{
83141cc406Sopenharmony_ci	sane_exit();
84141cc406Sopenharmony_ci	}
85141cc406Sopenharmony_ci
86141cc406Sopenharmony_ci
87141cc406Sopenharmony_ci/*
88141cc406Sopenharmony_ci * Class:     Sane
89141cc406Sopenharmony_ci * Method:    getDevices
90141cc406Sopenharmony_ci * Signature: ([LSaneDevice;Z)I
91141cc406Sopenharmony_ci */
92141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_getDevicesNative
93141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jobjectArray devList, jboolean localOnly)
94141cc406Sopenharmony_ci	{
95141cc406Sopenharmony_ci					/* Gets device list. */
96141cc406Sopenharmony_ci	const SANE_Device **device_list;
97141cc406Sopenharmony_ci	SANE_Status status;		/* Gets status. */
98141cc406Sopenharmony_ci	int devListLen;			/* Gets length of devList. */
99141cc406Sopenharmony_ci	jobject devObj;			/* Gets each SaneDevice object. */
100141cc406Sopenharmony_ci	jclass devClass;		/* Gets SaneDevice class. */
101141cc406Sopenharmony_ci	jfieldID fid;			/* Gets each field ID. */
102141cc406Sopenharmony_ci	int i;
103141cc406Sopenharmony_ci
104141cc406Sopenharmony_ci					/* Get the list. */
105141cc406Sopenharmony_ci	status = sane_get_devices(&device_list, localOnly);
106141cc406Sopenharmony_ci	if (status != SANE_STATUS_GOOD)
107141cc406Sopenharmony_ci		return (status);
108141cc406Sopenharmony_ci					/* Get length of Java array. */
109141cc406Sopenharmony_ci	devListLen = (*env)->GetArrayLength(env, devList);
110141cc406Sopenharmony_ci					/* Return devices to user. */
111141cc406Sopenharmony_ci	for (i = 0; i < devListLen - 1 && device_list[i]; i++)
112141cc406Sopenharmony_ci		{
113141cc406Sopenharmony_ci					/* Get Java object, class. */
114141cc406Sopenharmony_ci		devObj = (*env)->GetObjectArrayElement(env, devList, i);
115141cc406Sopenharmony_ci		devClass = (*env)->GetObjectClass(env, devObj);
116141cc406Sopenharmony_ci					/* Fill in each member. */
117141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, devClass, "name",
118141cc406Sopenharmony_ci							"Ljava/lang/String;");
119141cc406Sopenharmony_ci		(*env)->SetObjectField(env, devObj, fid,
120141cc406Sopenharmony_ci			(*env)->NewStringUTF(env, device_list[i]->name));
121141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, devClass, "vendor",
122141cc406Sopenharmony_ci							"Ljava/lang/String;");
123141cc406Sopenharmony_ci		(*env)->SetObjectField(env, devObj, fid,
124141cc406Sopenharmony_ci			(*env)->NewStringUTF(env, device_list[i]->vendor));
125141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, devClass, "model",
126141cc406Sopenharmony_ci							"Ljava/lang/String;");
127141cc406Sopenharmony_ci		(*env)->SetObjectField(env, devObj, fid,
128141cc406Sopenharmony_ci			(*env)->NewStringUTF(env, device_list[i]->model));
129141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, devClass, "type",
130141cc406Sopenharmony_ci							"Ljava/lang/String;");
131141cc406Sopenharmony_ci		(*env)->SetObjectField(env, devObj, fid,
132141cc406Sopenharmony_ci			(*env)->NewStringUTF(env, device_list[i]->type));
133141cc406Sopenharmony_ci		}
134141cc406Sopenharmony_ci					/* End list with a null. */
135141cc406Sopenharmony_ci	(*env)->SetObjectArrayElement(env, devList, i, 0);
136141cc406Sopenharmony_ci	return (status);
137141cc406Sopenharmony_ci	}
138141cc406Sopenharmony_ci
139141cc406Sopenharmony_ci/*
140141cc406Sopenharmony_ci * Class:     Sane
141141cc406Sopenharmony_ci * Method:    open
142141cc406Sopenharmony_ci * Signature: (Ljava/lang/String;[J)I
143141cc406Sopenharmony_ci */
144141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_open
145141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jstring deviceName, jintArray handle)
146141cc406Sopenharmony_ci	{
147141cc406Sopenharmony_ci	SANE_Handle sane_handle;	/* Gets handle. */
148141cc406Sopenharmony_ci	jint s_handle;
149141cc406Sopenharmony_ci	const char *device_name;	/* Gets dev. name. */
150141cc406Sopenharmony_ci	int status;			/* Gets return code. */
151141cc406Sopenharmony_ci
152141cc406Sopenharmony_ci	device_name = (*env)->GetStringUTFChars(env, deviceName, 0);
153141cc406Sopenharmony_ci					/* Open it. */
154141cc406Sopenharmony_ci	status = sane_open(device_name, &sane_handle);
155141cc406Sopenharmony_ci	(*env)->ReleaseStringUTFChars(env, deviceName, device_name);
156141cc406Sopenharmony_ci					/* Return handle. */
157141cc406Sopenharmony_ci	s_handle = (jint) sane_handle;
158141cc406Sopenharmony_ci	(*env)->SetIntArrayRegion(env, handle, 0, 1, &s_handle);
159141cc406Sopenharmony_ci	return (status);
160141cc406Sopenharmony_ci	}
161141cc406Sopenharmony_ci
162141cc406Sopenharmony_ci/*
163141cc406Sopenharmony_ci * Class:     Sane
164141cc406Sopenharmony_ci * Method:    close
165141cc406Sopenharmony_ci * Signature: (J)V
166141cc406Sopenharmony_ci */
167141cc406Sopenharmony_ciJNIEXPORT void JNICALL Java_Sane_close
168141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle)
169141cc406Sopenharmony_ci	{
170141cc406Sopenharmony_ci	sane_close((SANE_Handle) handle);
171141cc406Sopenharmony_ci	}
172141cc406Sopenharmony_ci
173141cc406Sopenharmony_ci/*
174141cc406Sopenharmony_ci * Class:     Sane
175141cc406Sopenharmony_ci * Method:    getOptionNative
176141cc406Sopenharmony_ci * Signature: (IILSaneOption;)V
177141cc406Sopenharmony_ci */
178141cc406Sopenharmony_ciJNIEXPORT void JNICALL Java_Sane_getOptionNative
179141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle, jint option, jobject optObj)
180141cc406Sopenharmony_ci	{
181141cc406Sopenharmony_ci	jclass optClass;		/* Gets its class. */
182141cc406Sopenharmony_ci	jfieldID fid;			/* Gets each field ID. */
183141cc406Sopenharmony_ci	jstring str;			/* Gets strings. */
184141cc406Sopenharmony_ci
185141cc406Sopenharmony_ci					/* Get info from sane. */
186141cc406Sopenharmony_ci	const SANE_Option_Descriptor *sopt = sane_get_option_descriptor(
187141cc406Sopenharmony_ci			(SANE_Handle) handle, option);
188141cc406Sopenharmony_ci					/* Get class info. */
189141cc406Sopenharmony_ci	optClass = (*env)->GetObjectClass(env, optObj);
190141cc406Sopenharmony_ci					/* Fill in each member. */
191141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, optClass, "name", "Ljava/lang/String;");
192141cc406Sopenharmony_ci	if (!sopt)			/* Failed. */
193141cc406Sopenharmony_ci		{			/* Set name to null. */
194141cc406Sopenharmony_ci		(*env)->SetObjectField(env, optObj, fid, 0);
195141cc406Sopenharmony_ci		return;
196141cc406Sopenharmony_ci		}
197141cc406Sopenharmony_ci					/* Return name. */
198141cc406Sopenharmony_ci	(*env)->SetObjectField(env, optObj, fid,
199141cc406Sopenharmony_ci				(*env)->NewStringUTF(env, sopt->name));
200141cc406Sopenharmony_ci					/* Return title. */
201141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, optClass, "title", "Ljava/lang/String;");
202141cc406Sopenharmony_ci	str = sopt->title ? (*env)->NewStringUTF(env, sopt->title) : 0;
203141cc406Sopenharmony_ci	(*env)->SetObjectField(env, optObj, fid, str);
204141cc406Sopenharmony_ci					/* Return descr. */
205141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, optClass, "desc", "Ljava/lang/String;");
206141cc406Sopenharmony_ci	(*env)->SetObjectField(env, optObj, fid,
207141cc406Sopenharmony_ci				(*env)->NewStringUTF(env, sopt->desc));
208141cc406Sopenharmony_ci					/* Return type. */
209141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, optClass, "type", "I");
210141cc406Sopenharmony_ci	(*env)->SetIntField(env, optObj, fid, sopt->type);
211141cc406Sopenharmony_ci					/* Return unit. */
212141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, optClass, "unit", "I");
213141cc406Sopenharmony_ci	(*env)->SetIntField(env, optObj, fid, sopt->unit);
214141cc406Sopenharmony_ci					/* Return size. */
215141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, optClass, "size", "I");
216141cc406Sopenharmony_ci	(*env)->SetIntField(env, optObj, fid, sopt->size);
217141cc406Sopenharmony_ci					/* Return cap. */
218141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, optClass, "cap", "I");
219141cc406Sopenharmony_ci	(*env)->SetIntField(env, optObj, fid, sopt->cap);
220141cc406Sopenharmony_ci					/* Return constraint_type. */
221141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, optClass, "constraintType", "I");
222141cc406Sopenharmony_ci	(*env)->SetIntField(env, optObj, fid, sopt->constraint_type);
223141cc406Sopenharmony_ci	/*
224141cc406Sopenharmony_ci	 *	Now for the constraint itself.
225141cc406Sopenharmony_ci	 */
226141cc406Sopenharmony_ci	if (sopt->constraint_type == SANE_CONSTRAINT_RANGE)
227141cc406Sopenharmony_ci		{
228141cc406Sopenharmony_ci					/* Create range object. */
229141cc406Sopenharmony_ci		jclass rangeClass = (*env)->FindClass(env, "SaneRange");
230141cc406Sopenharmony_ci		jobject range = (*env)->AllocObject(env, rangeClass);
231141cc406Sopenharmony_ci					/* Fill in fields. */
232141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, rangeClass, "min", "I");
233141cc406Sopenharmony_ci		(*env)->SetIntField(env, range, fid,
234141cc406Sopenharmony_ci						sopt->constraint.range->min);
235141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, rangeClass, "max", "I");
236141cc406Sopenharmony_ci		(*env)->SetIntField(env, range, fid,
237141cc406Sopenharmony_ci						sopt->constraint.range->max);
238141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, rangeClass, "quant", "I");
239141cc406Sopenharmony_ci		(*env)->SetIntField(env, range, fid,
240141cc406Sopenharmony_ci						sopt->constraint.range->quant);
241141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, optClass, "rangeConstraint",
242141cc406Sopenharmony_ci						"LSaneRange;");
243141cc406Sopenharmony_ci					/* Store range. */
244141cc406Sopenharmony_ci		(*env)->SetObjectField(env, optObj, fid, range);
245141cc406Sopenharmony_ci		}
246141cc406Sopenharmony_ci	else if (sopt->constraint_type == SANE_CONSTRAINT_WORD_LIST)
247141cc406Sopenharmony_ci		{			/* Get array of integers. */
248141cc406Sopenharmony_ci		jintArray wordList;
249141cc406Sopenharmony_ci		jint *elements;
250141cc406Sopenharmony_ci		int i;
251141cc406Sopenharmony_ci					/* First word. is the length.	*/
252141cc406Sopenharmony_ci		wordList = (*env)->NewIntArray(env,
253141cc406Sopenharmony_ci				sopt->constraint.word_list[0]);
254141cc406Sopenharmony_ci					/* Copy in the integers.	*/
255141cc406Sopenharmony_ci		elements = (*env)->GetIntArrayElements(env, wordList, 0);
256141cc406Sopenharmony_ci		for (i = 0; i < sopt->constraint.word_list[0]; i++)
257141cc406Sopenharmony_ci			elements[i] = sopt->constraint.word_list[i];
258141cc406Sopenharmony_ci		(*env)->ReleaseIntArrayElements(env, wordList, elements, 0);
259141cc406Sopenharmony_ci					/* Set the field. */
260141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, optClass, "wordListConstraint",
261141cc406Sopenharmony_ci									"[I");
262141cc406Sopenharmony_ci		(*env)->SetObjectField(env, optObj, fid, wordList);
263141cc406Sopenharmony_ci		}
264141cc406Sopenharmony_ci	else if (sopt->constraint_type == SANE_CONSTRAINT_STRING_LIST)
265141cc406Sopenharmony_ci		{
266141cc406Sopenharmony_ci		jclass stringClass = (*env)->FindClass(env, "java/lang/String");
267141cc406Sopenharmony_ci		jobjectArray stringList;
268141cc406Sopenharmony_ci		int len;		/* Gets # elements */
269141cc406Sopenharmony_ci		int i;
270141cc406Sopenharmony_ci
271141cc406Sopenharmony_ci		for (len = 0; sopt->constraint.string_list[len]; len++)
272141cc406Sopenharmony_ci			;
273141cc406Sopenharmony_ci		stringList = (*env)->NewObjectArray(env, len + 1,
274141cc406Sopenharmony_ci							stringClass, 0);
275141cc406Sopenharmony_ci					/* Add each string. */
276141cc406Sopenharmony_ci		for (i = 0; i < len; i++)
277141cc406Sopenharmony_ci			{
278141cc406Sopenharmony_ci			(*env)->SetObjectArrayElement(env, stringList, i,
279141cc406Sopenharmony_ci				(*env)->NewStringUTF(env,
280141cc406Sopenharmony_ci					sopt->constraint.string_list[i]));
281141cc406Sopenharmony_ci			}
282141cc406Sopenharmony_ci					/* 0 at end. */
283141cc406Sopenharmony_ci		(*env)->SetObjectArrayElement(env, stringList, len, 0);
284141cc406Sopenharmony_ci					/* Set the field. */
285141cc406Sopenharmony_ci		fid = (*env)->GetFieldID(env, optClass,
286141cc406Sopenharmony_ci			"stringListConstraint", "[Ljava/lang/String;");
287141cc406Sopenharmony_ci		(*env)->SetObjectField(env, optObj, fid, stringList);
288141cc406Sopenharmony_ci		}
289141cc406Sopenharmony_ci	}
290141cc406Sopenharmony_ci
291141cc406Sopenharmony_ci/*
292141cc406Sopenharmony_ci * Class:     Sane
293141cc406Sopenharmony_ci * Method:    getControlOption
294141cc406Sopenharmony_ci * Signature: (II[I[I)I
295141cc406Sopenharmony_ci */
296141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_getControlOption__II_3I_3I
297141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle, jint option, jintArray value,
298141cc406Sopenharmony_ci						jintArray info)
299141cc406Sopenharmony_ci	{
300141cc406Sopenharmony_ci	SANE_Status status;		/* Gets status. */
301141cc406Sopenharmony_ci	SANE_Int i;			/* Gets info. passed back. */
302141cc406Sopenharmony_ci	int v;
303141cc406Sopenharmony_ci
304141cc406Sopenharmony_ci	status = sane_control_option((SANE_Handle) handle, option,
305141cc406Sopenharmony_ci			SANE_ACTION_GET_VALUE, &v, &i);
306141cc406Sopenharmony_ci	if (value)
307141cc406Sopenharmony_ci		(*env)->SetIntArrayRegion(env, value, 0, 1, &v);
308141cc406Sopenharmony_ci	if (info)
309141cc406Sopenharmony_ci		(*env)->SetIntArrayRegion(env, info, 0, 1, &i);
310141cc406Sopenharmony_ci	return (status);
311141cc406Sopenharmony_ci	}
312141cc406Sopenharmony_ci
313141cc406Sopenharmony_ci/*
314141cc406Sopenharmony_ci * Class:     Sane
315141cc406Sopenharmony_ci * Method:    getControlOption
316141cc406Sopenharmony_ci * Signature: (II[B[I)I
317141cc406Sopenharmony_ci */
318141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_getControlOption__II_3B_3I
319141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle, jint option, jbyteArray value,
320141cc406Sopenharmony_ci						jintArray info)
321141cc406Sopenharmony_ci	{
322141cc406Sopenharmony_ci	SANE_Status status;		/* Gets status. */
323141cc406Sopenharmony_ci	SANE_Int i;			/* Gets info. passed back. */
324141cc406Sopenharmony_ci	char *str;
325141cc406Sopenharmony_ci
326141cc406Sopenharmony_ci	str = (*env)->GetByteArrayElements(env, value, 0);
327141cc406Sopenharmony_ci	status = sane_control_option((SANE_Handle) handle, option,
328141cc406Sopenharmony_ci			SANE_ACTION_GET_VALUE, str, &i);
329141cc406Sopenharmony_ci	(*env)->ReleaseByteArrayElements(env, value, str, 0);
330141cc406Sopenharmony_ci	if (info)
331141cc406Sopenharmony_ci		(*env)->SetIntArrayRegion(env, info, 0, 1, &i);
332141cc406Sopenharmony_ci	return (status);
333141cc406Sopenharmony_ci	}
334141cc406Sopenharmony_ci
335141cc406Sopenharmony_ci/*
336141cc406Sopenharmony_ci * Class:     Sane
337141cc406Sopenharmony_ci * Method:    setControlOption
338141cc406Sopenharmony_ci * Signature: (IIII[I)I
339141cc406Sopenharmony_ci */
340141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_setControlOption__IIII_3I
341141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle, jint option, jint action,
342141cc406Sopenharmony_ci				jint value, jintArray info)
343141cc406Sopenharmony_ci	{
344141cc406Sopenharmony_ci	SANE_Status status;		/* Gets status. */
345141cc406Sopenharmony_ci	SANE_Int i;			/* Gets info. passed back. */
346141cc406Sopenharmony_ci	status = sane_control_option((SANE_Handle) handle, option, action,
347141cc406Sopenharmony_ci					&value, &i);
348141cc406Sopenharmony_ci	if (info)
349141cc406Sopenharmony_ci		(*env)->SetIntArrayRegion(env, info, 0, 1, &i);
350141cc406Sopenharmony_ci	return (status);
351141cc406Sopenharmony_ci	}
352141cc406Sopenharmony_ci
353141cc406Sopenharmony_ci/*
354141cc406Sopenharmony_ci *	Get string length.  This exists because sometimes strings seem to be
355141cc406Sopenharmony_ci *	padded with negatives.
356141cc406Sopenharmony_ci */
357141cc406Sopenharmony_ci
358141cc406Sopenharmony_cistatic int String_length
359141cc406Sopenharmony_ci	(
360141cc406Sopenharmony_ci	const char *str
361141cc406Sopenharmony_ci	)
362141cc406Sopenharmony_ci	{
363141cc406Sopenharmony_ci	const char *ptr;
364141cc406Sopenharmony_ci	for (ptr = str; *ptr > 0; ptr++)
365141cc406Sopenharmony_ci		;
366141cc406Sopenharmony_ci	return ((int) (ptr - str));
367141cc406Sopenharmony_ci	}
368141cc406Sopenharmony_ci
369141cc406Sopenharmony_ci/*
370141cc406Sopenharmony_ci * Class:     Sane
371141cc406Sopenharmony_ci * Method:    setControlOption
372141cc406Sopenharmony_ci * Signature: (IIILjava/lang/String;[I)I
373141cc406Sopenharmony_ci */
374141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_setControlOption__IIILjava_lang_String_2_3I
375141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle, jint option, jint action,
376141cc406Sopenharmony_ci				jstring value, jintArray info)
377141cc406Sopenharmony_ci	{
378141cc406Sopenharmony_ci	SANE_Status status;		/* Gets status. */
379141cc406Sopenharmony_ci	SANE_Int i;			/* Gets info. passed back. */
380141cc406Sopenharmony_ci	const char *valuep;
381141cc406Sopenharmony_ci	char buf[512];			/* Hope this is big enough. */
382141cc406Sopenharmony_ci	int len;			/* Gets string length. */
383141cc406Sopenharmony_ci
384141cc406Sopenharmony_ci	valuep = (*env)->GetStringUTFChars(env, value, 0);
385141cc406Sopenharmony_ci	len = String_length(valuep);
386141cc406Sopenharmony_ci	if (len >= sizeof(buf))
387141cc406Sopenharmony_ci		len = sizeof(buf) - 1;
388141cc406Sopenharmony_ci	strncpy(buf, valuep, len);
389141cc406Sopenharmony_ci	buf[len] = 0;			/* Insure it's 0-delimited. */
390141cc406Sopenharmony_ci	status = sane_control_option((SANE_Handle) handle, option, action,
391141cc406Sopenharmony_ci					(void *) &buf[0], &i);
392141cc406Sopenharmony_ci					/* +++++++Want to return new val? */
393141cc406Sopenharmony_ci	(*env)->ReleaseStringUTFChars(env, value, valuep);
394141cc406Sopenharmony_ci	if (info)
395141cc406Sopenharmony_ci		(*env)->SetIntArrayRegion(env, info, 0, 1, &i);
396141cc406Sopenharmony_ci	return (status);
397141cc406Sopenharmony_ci	}
398141cc406Sopenharmony_ci
399141cc406Sopenharmony_ci/*
400141cc406Sopenharmony_ci * Class:     Sane
401141cc406Sopenharmony_ci * Method:    getParameters
402141cc406Sopenharmony_ci * Signature: (ILSaneParameters;)I
403141cc406Sopenharmony_ci */
404141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_getParameters
405141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle, jobject paramsObj)
406141cc406Sopenharmony_ci	{
407141cc406Sopenharmony_ci	SANE_Status status;		/* Gets status.	*/
408141cc406Sopenharmony_ci	SANE_Parameters params;		/* Gets params. */
409141cc406Sopenharmony_ci	jclass paramsClass;		/* Gets its class. */
410141cc406Sopenharmony_ci	jfieldID fid;			/* Gets each field ID. */
411141cc406Sopenharmony_ci
412141cc406Sopenharmony_ci	status = sane_get_parameters((SANE_Handle) handle, &params);
413141cc406Sopenharmony_ci					/* Get class info. */
414141cc406Sopenharmony_ci	paramsClass = (*env)->GetObjectClass(env, paramsObj);
415141cc406Sopenharmony_ci					/* Fill in each member. */
416141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, paramsClass, "format", "I");
417141cc406Sopenharmony_ci	(*env)->SetIntField(env, paramsObj, fid, params.format);
418141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, paramsClass, "lastFrame", "Z");
419141cc406Sopenharmony_ci	(*env)->SetBooleanField(env, paramsObj, fid, params.last_frame);
420141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, paramsClass, "bytesPerLine", "I");
421141cc406Sopenharmony_ci	(*env)->SetIntField(env, paramsObj, fid, params.bytes_per_line);
422141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, paramsClass, "pixelsPerLine", "I");
423141cc406Sopenharmony_ci	(*env)->SetIntField(env, paramsObj, fid, params.pixels_per_line);
424141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, paramsClass, "lines", "I");
425141cc406Sopenharmony_ci	(*env)->SetIntField(env, paramsObj, fid, params.lines);
426141cc406Sopenharmony_ci	fid = (*env)->GetFieldID(env, paramsClass, "depth", "I");
427141cc406Sopenharmony_ci	(*env)->SetIntField(env, paramsObj, fid, params.depth);
428141cc406Sopenharmony_ci	return (status);
429141cc406Sopenharmony_ci	}
430141cc406Sopenharmony_ci
431141cc406Sopenharmony_ci/*
432141cc406Sopenharmony_ci * Class:     Sane
433141cc406Sopenharmony_ci * Method:    start
434141cc406Sopenharmony_ci * Signature: (I)I
435141cc406Sopenharmony_ci */
436141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_start
437141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle)
438141cc406Sopenharmony_ci	{
439141cc406Sopenharmony_ci	return (sane_start((SANE_Handle) handle));
440141cc406Sopenharmony_ci	}
441141cc406Sopenharmony_ci
442141cc406Sopenharmony_ci/*
443141cc406Sopenharmony_ci * Class:     Sane
444141cc406Sopenharmony_ci * Method:    read
445141cc406Sopenharmony_ci * Signature: (I[BI[I)I
446141cc406Sopenharmony_ci */
447141cc406Sopenharmony_ciJNIEXPORT jint JNICALL Java_Sane_read
448141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle, jbyteArray data, jint maxLength,
449141cc406Sopenharmony_ci					jintArray length)
450141cc406Sopenharmony_ci	{
451141cc406Sopenharmony_ci	int status;
452141cc406Sopenharmony_ci	jbyte *dataElements;
453141cc406Sopenharmony_ci	int read_len;			/* # bytes read. */
454141cc406Sopenharmony_ci
455141cc406Sopenharmony_ci					/* Get actual data ptr. */
456141cc406Sopenharmony_ci	dataElements = (*env)->GetByteArrayElements(env, data, 0);
457141cc406Sopenharmony_ci					/* Do the read. */
458141cc406Sopenharmony_ci	status = sane_read((SANE_Handle) handle, dataElements,
459141cc406Sopenharmony_ci						maxLength, &read_len);
460141cc406Sopenharmony_ci	(*env)->ReleaseByteArrayElements(env, data, dataElements, 0);
461141cc406Sopenharmony_ci					/* Return # bytes read. */
462141cc406Sopenharmony_ci	(*env)->SetIntArrayRegion(env, length, 0, 1, &read_len);
463141cc406Sopenharmony_ci	return (status);
464141cc406Sopenharmony_ci	}
465141cc406Sopenharmony_ci
466141cc406Sopenharmony_ci/*
467141cc406Sopenharmony_ci * Class:     Sane
468141cc406Sopenharmony_ci * Method:    cancel
469141cc406Sopenharmony_ci * Signature: (I)V
470141cc406Sopenharmony_ci */
471141cc406Sopenharmony_ciJNIEXPORT void JNICALL Java_Sane_cancel
472141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint handle)
473141cc406Sopenharmony_ci	{
474141cc406Sopenharmony_ci	sane_cancel((SANE_Handle) handle);
475141cc406Sopenharmony_ci	}
476141cc406Sopenharmony_ci
477141cc406Sopenharmony_ci/*
478141cc406Sopenharmony_ci * Class:     Sane
479141cc406Sopenharmony_ci * Method:    strstatus
480141cc406Sopenharmony_ci * Signature: (I)Ljava/lang/String;
481141cc406Sopenharmony_ci */
482141cc406Sopenharmony_ciJNIEXPORT jstring JNICALL Java_Sane_strstatus
483141cc406Sopenharmony_ci  (JNIEnv *env, jobject jobj, jint status)
484141cc406Sopenharmony_ci	{
485141cc406Sopenharmony_ci	const char *str = sane_strstatus(status);
486141cc406Sopenharmony_ci	return ((*env)->NewStringUTF(env, str));
487141cc406Sopenharmony_ci	}
488141cc406Sopenharmony_ci
489141cc406Sopenharmony_ci#ifdef __cplusplus
490141cc406Sopenharmony_ci}
491141cc406Sopenharmony_ci#endif
492