summaryrefslogtreecommitdiff
path: root/demos/java/jni/gs_jni/callbacks.cpp
blob: a8ad2196d940566cb6639e444e6ed6223d22c731 (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
#include "callbacks.h"

#include "jni_util.h"

#include <string.h>

#define STDIN_SIG "(J[BI)I"
#define STDOUT_SIG "(J[BI)I"
#define STDERR_SIG "(J[BI)I"

#define POLL_SIG "(J)I"

#define DISPLAY_OPEN_SIG "(JJ)I"
#define DISPLAY_PRECLOSE_SIG "(JJ)I"
#define DISPLAY_CLOSE_SIG "(JJ)I"
#define DISPLAY_PRESIZE_SIG "(JJIIII)I"
#define DISPLAY_SIZE_SIG "(JJIIIILcom/artifex/gsjava/util/BytePointer;)I"
#define DISPLAY_SYNC_SIG "(JJ)I"
#define DISPLAY_PAGE_SIG "(JJIZ)I"
#define DISPLAY_UPDATE_SIG "(JJIIII)I"
// display memalloc
// display memfree
#define DISPLAY_SEPARATION_SIG "(JJI[BSSSS)I"
#define DISPLAY_ADJUST_BAND_HEIGHT_SIG "(JJI)I"
#define DISPLAY_RECTANGLE_REQUEST "(JJLcom/artifex/gsjava/LongReference;Lcom/artifex/gsjava/IntReference;\
Lcom/artifex/gsjava/IntReference;Lcom/artifex/gsjava/IntReference;Lcom/artifex/gsjava/IntReference;\
Lcom/artifex/gsjava/IntReference;Lcom/artifex/gsjava/IntReference;Lcom/artifex/gsjava/IntReference;\
Lcom/artifex/gsjava/IntReference;)I"

#define CHECK_AND_RETURN(E) if (E->ExceptionCheck()) { return -21; }

using namespace util;

static JNIEnv *g_env = NULL;

static jobject g_stdIn = NULL;
static jobject g_stdOut = NULL;
static jobject g_stdErr = NULL;

static jobject g_poll = NULL;

static jobject g_displayCallback = NULL;

static jobject g_callout = NULL;

void callbacks::setJNIEnv(JNIEnv *env)
{
	g_env = env;
}

void callbacks::setIOCallbacks(jobject stdIn, jobject stdOut, jobject stdErr)
{
	if (g_env)
	{
		if (g_stdIn)
			g_env->DeleteGlobalRef(g_stdIn);

		if (g_stdOut)
			g_env->DeleteGlobalRef(g_stdOut);

		if (g_stdErr)
			g_env->DeleteGlobalRef(g_stdErr);

		g_stdIn = g_env->NewGlobalRef(stdIn);
		g_stdOut = g_env->NewGlobalRef(stdOut);
		g_stdErr = g_env->NewGlobalRef(stdErr);
	}
}

int callbacks::stdInFunction(void *callerHandle, char *buf, int len)
{
	int code = 0;
	if (g_env && g_stdIn)
	{
		jbyteArray byteArray = g_env->NewByteArray(len);
		g_env->SetByteArrayRegion(byteArray, 0, len, (jbyte *)buf);
		code = callIntMethod(g_env, g_stdIn, "onStdIn", STDIN_SIG, (jlong)callerHandle, byteArray, (jint)len);
	}
	return code;
}

int callbacks::stdOutFunction(void *callerHandle, const char *str, int len)
{
	int code = 0;
	if (g_env && g_stdOut)
	{
		jbyteArray byteArray = g_env->NewByteArray(len);
		g_env->SetByteArrayRegion(byteArray, 0, len, (const jbyte *)str);
		code = callIntMethod(g_env, g_stdOut, "onStdOut", STDOUT_SIG, (jlong)callerHandle, byteArray, (jint)len);
	}
	return code;
}

int callbacks::stdErrFunction(void *callerHandle, const char *str, int len)
{
	int code = 0;
	if (g_env && g_stdErr)
	{
		jbyteArray byteArray = g_env->NewByteArray(len);
		g_env->SetByteArrayRegion(byteArray, 0, len, (const jbyte *)str);
		code = callIntMethod(g_env, g_stdErr, "onStdErr", STDERR_SIG, (jlong)callerHandle, byteArray, (jint)len);
	}
	return code;
}

void callbacks::setPollCallback(jobject poll)
{
	if (g_env)
	{
		if (g_poll)
			g_env->DeleteGlobalRef(g_poll);

		g_poll = g_env->NewGlobalRef(poll);
	}
}

int callbacks::pollFunction(void *callerHandle)
{
	int code = 0;
	if (g_env && g_poll)
	{
		code = callIntMethod(g_env, g_poll, "onPoll", POLL_SIG, (jlong)callerHandle);
	}
	return code;
}

void callbacks::setDisplayCallback(jobject displayCallback)
{
	if (g_env)
	{
		if (g_displayCallback)
		{
			g_env->DeleteGlobalRef(g_displayCallback);
			g_displayCallback = NULL;
		}

		g_displayCallback = g_env->NewGlobalRef(displayCallback);
		//g_displayCallback = displayCallback;
	}
}

void callbacks::setCalloutCallback(jobject callout)
{
	if (g_env)
	{
		if (g_callout)
			g_env->DeleteGlobalRef(g_callout);

		g_callout = g_env->NewGlobalRef(callout);
	}
}

int callbacks::calloutFunction(void *instance, void *handle, const char *deviceName, int id, int size, void *data)
{
	int code = 0;
	if (g_env && g_callout)
	{
		jsize len = strlen(deviceName);
		jbyteArray array = g_env->NewByteArray(len);
		g_env->SetByteArrayRegion(array, 0, len, (const jbyte *)deviceName);
		code = callIntMethod(g_env, g_callout, "onCallout", "(JJ[BIIJ)I", (jlong)instance, (jlong)handle, array, id, size, (jlong)data);
	}
	return code;
}

int callbacks::display::displayOpenFunction(void *handle, void *device)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		jclass clazz = g_env->GetObjectClass(g_displayCallback);
		const char *name = getClassName(g_env, clazz);
		freeClassName(name);
		code = callIntMethod(g_env, g_displayCallback, "onDisplayOpen", DISPLAY_OPEN_SIG, (jlong)handle, (jlong)device);
		CHECK_AND_RETURN(g_env);
	}
	return 0;
}

int callbacks::display::displayPrecloseFunction(void *handle, void *device)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		code = callIntMethod(g_env, g_displayCallback, "onDisplayPreclose", DISPLAY_PRECLOSE_SIG, (jlong)handle, (jlong)device);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displayCloseFunction(void *handle, void *device)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		code = callIntMethod(g_env, g_displayCallback, "onDisplayClose", DISPLAY_CLOSE_SIG, (jlong)handle, (jlong)device);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displayPresizeFunction(void *handle, void *device, int width, int height, int raster, unsigned int format)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		code = callIntMethod(g_env, g_displayCallback, "onDisplayPresize", DISPLAY_PRESIZE_SIG, (jlong)handle,
			(jlong)device, width, height, raster, (jint)format);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displaySizeFunction(void *handle, void *device, int width, int height, int raster,
	unsigned int format, unsigned char *pimage)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		jsize len = height * raster;
		jbyteArray byteArray = g_env->NewByteArray(len);
		g_env->SetByteArrayRegion(byteArray, 0, len, (signed char *)pimage);

		static const char *const bytePointerClassName = "com/artifex/gsjava/util/BytePointer";
		static const char *const nativePointerClassName = "com/artifex/gsjava/util/NativePointer";

		jclass bytePointerClass = g_env->FindClass(bytePointerClassName);
		if (bytePointerClass == NULL)
		{
			throwNoClassDefError(g_env, bytePointerClassName);
			return -21;
		}

		jclass nativePointerClass = g_env->FindClass(nativePointerClassName);
		if (nativePointerClass == NULL)
		{
			throwNoClassDefError(g_env, nativePointerClassName);
			return -21;
		}

		jmethodID constructor = g_env->GetMethodID(bytePointerClass, "<init>", "()V");
		if (constructor == NULL)
		{
			throwNoSuchMethodError(g_env, "com.artifex.gsjava.util.BytePointer.<init>()V");
			return -21;
		}
		jobject bytePointer = g_env->NewObject(bytePointerClass, constructor);

		jfieldID dataPtrID = g_env->GetFieldID(nativePointerClass, "address", "J");
		if (dataPtrID == NULL)
		{
			throwNoSuchFieldError(g_env, "address");
			return -21;
		}

		jfieldID lengthID = g_env->GetFieldID(bytePointerClass, "length", "J");
		if (lengthID == NULL)
		{
			throwNoSuchFieldError(g_env, "length");
			return -21;
		}

		g_env->SetLongField(bytePointer, dataPtrID, (jlong)pimage);
		g_env->SetLongField(bytePointer, lengthID, len);

		code = callIntMethod(g_env, g_displayCallback, "onDisplaySize", DISPLAY_SIZE_SIG, (jlong)handle,
			(jlong)device, width, height, raster, (jint)format, bytePointer);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displaySyncFunction(void *handle, void *device)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		code = callIntMethod(g_env, g_displayCallback, "onDisplaySync", DISPLAY_SYNC_SIG, (jlong)handle, (jlong)device);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displayPageFunction(void *handle, void *device, int copies, int flush)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		code = callIntMethod(g_env, g_displayCallback, "onDisplayPage", DISPLAY_PAGE_SIG, (jlong)handle,
			(jlong)device, copies, flush);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displayUpdateFunction(void *handle, void *device, int x, int y, int w, int h)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		code = callIntMethod(g_env, g_displayCallback, "onDisplayUpdate", DISPLAY_UPDATE_SIG, (jlong)handle,
			(jlong)device, x, y, w, h);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displaySeparationFunction(void *handle, void *device, int component, const char *componentName,
	unsigned short c, unsigned short m, unsigned short y, unsigned short k)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		jsize len = strlen(componentName);
		jbyteArray byteArray = g_env->NewByteArray(len);
		g_env->SetByteArrayRegion(byteArray, 0, len, (const jbyte *)componentName);
		code = callIntMethod(g_env, g_displayCallback, "onDisplaySeparation", DISPLAY_SEPARATION_SIG, (jlong)handle,
			(jlong)device, component, byteArray, c, m, y, k);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displayAdjustBandHeightFunction(void *handle, void *device, int bandHeight)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		code = callIntMethod(g_env, g_displayCallback, "onDisplayAdjustBandHeght", DISPLAY_ADJUST_BAND_HEIGHT_SIG,
			(jlong)handle, (jlong)device, bandHeight);
		CHECK_AND_RETURN(g_env);
	}
	return code;
}

int callbacks::display::displayRectangleRequestFunction(void *handle, void *device, void **memory, int *ox, int *oy,
	int *raster, int *plane_raster, int *x, int *y, int *w, int *h)
{
	int code = 0;
	if (g_env && g_displayCallback)
	{
		Reference memoryRef = Reference(g_env, toWrapperType(g_env, (jlong)*memory));
		Reference oxRef = Reference(g_env, toWrapperType(g_env, (jint)*ox));
		Reference oyRef = Reference(g_env, toWrapperType(g_env, (jint)*oy));
		Reference rasterRef = Reference(g_env, toWrapperType(g_env, (jint)*raster));
		Reference planeRasterRef = Reference(g_env, toWrapperType(g_env, (jint)*plane_raster));
		Reference xRef = Reference(g_env, toWrapperType(g_env, (jint)*x));
		Reference yRef = Reference(g_env, toWrapperType(g_env, (jint)*y));
		Reference wRef = Reference(g_env, toWrapperType(g_env, (jint)*w));
		Reference hRef = Reference(g_env, toWrapperType(g_env, (jint)*h));

		code = callIntMethod(g_env, g_displayCallback, "onDisplayRectangleRequest", DISPLAY_RECTANGLE_REQUEST,
			(jlong)handle, (jlong)device, memoryRef, oxRef, oyRef, rasterRef, planeRasterRef, xRef, yRef, wRef, hRef);

		*memory = (void *)memoryRef.longValue();
		*ox = oxRef.intValue();
		*oy = oyRef.intValue();
		*raster = rasterRef.intValue();
		*plane_raster = planeRasterRef.intValue();
		*x = xRef.intValue();
		*y = yRef.intValue();
		*w = wRef.intValue();
		*h = hRef.intValue();

		CHECK_AND_RETURN(g_env);
	}
	return code;
}