Add link to posts because IFTTT really hates feeds without links.
[SquirrelJME.git] / emulators / emulator-base-native / headers / squirreljme.h
blobc76457df5a62805a2a9cf9fdad07bc578ddd9608
1 /* -*- Mode: C++; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #ifndef __SQUIRRELJME_H__
11 #define __SQUIRRELJME_H__
13 #include <jni.h>
15 #include "lib/scritchui/scritchui.h"
16 #include "sjme/debug.h"
17 #include "sjme/charSeq.h"
19 /** Initializing methods. */
20 jint JNICALL mleDebugInit(JNIEnv* env, jclass classy);
21 jint JNICALL mleDylibBaseObjectInit(JNIEnv* env, jclass classy);
22 jint JNICALL mleJarInit(JNIEnv* env, jclass classy);
23 jint JNICALL mleMathInit(JNIEnv* env, jclass classy);
24 jint JNICALL mleMidiInit(JNIEnv* env, jclass classy);
25 jint JNICALL mleNativeArchiveInit(JNIEnv* env, jclass classy);
26 jint JNICALL mleNativeScritchDylibInit(JNIEnv* env, jclass classy);
27 jint JNICALL mleNativeScritchInterfaceInit(JNIEnv* env, jclass classy);
28 jint JNICALL mleObjectInit(JNIEnv* env, jclass classy);
29 jint JNICALL mlePencilInit(JNIEnv* env, jclass classy);
30 jint JNICALL mlePencilFontInit(JNIEnv* env, jclass classy);
31 jint JNICALL mleReflectionInit(JNIEnv* env, jclass classy);
32 jint JNICALL mleRuntimeInit(JNIEnv* env, jclass classy);
33 jint JNICALL mleTaskInit(JNIEnv* env, jclass classy);
34 jint JNICALL mleTerminalInit(JNIEnv* env, jclass classy);
35 jint JNICALL mleTypeInit(JNIEnv* env, jclass classy);
36 jint JNICALL mleThreadInit(JNIEnv* env, jclass classy);
38 /** Useful macros, structures, and functions for forwarding. */
39 // Stores forwarded information
40 typedef struct forwardMethod
42 jclass xclass;
43 jmethodID xmeth;
44 } forwardMethod;
46 // Find forwarded method
47 forwardMethod JNICALL findForwardMethod(JNIEnv* env,
48 const char* const classy, const char* const name, const char* const type);
50 // Call static methods
51 void JNICALL forwardCallStaticVoid(JNIEnv* env,
52 const char* const classy, const char* const name, const char* const type,
53 ...);
54 jint JNICALL forwardCallStaticInteger(JNIEnv* env,
55 const char* const classy, const char* const name, const char* const type,
56 ...);
57 jlong JNICALL forwardCallStaticLong(JNIEnv* env,
58 const char* const classy, const char* const name, const char* const type,
59 ...);
60 jobject JNICALL forwardCallStaticObject(JNIEnv* env,
61 const char* const classy, const char* const name, const char* const type,
62 ...);
63 jboolean JNICALL forwardCallStaticBoolean(JNIEnv* env,
64 const char* const classy, const char* const name, const char* const type,
65 ...);
67 #define FORWARD_init(funcName, forwardFuncs) \
68 jint JNICALL funcName(JNIEnv* env, jclass classy) \
69 { \
70 return (*env)->RegisterNatives(env, \
71 (*env)->FindClass(env, FORWARD_CLASS), \
72 forwardFuncs, sizeof(forwardFuncs) / sizeof(JNINativeMethod)); \
75 #define FORWARD_stringy(x) #x
77 #define FORWARD_paste(x, y) x ## y
79 #define FORWARD_from(x) x
81 #define FORWARD_list(className, methodName) \
82 {FORWARD_stringy(methodName), \
83 FORWARD_from(FORWARD_paste(FORWARD_DESC_, methodName)), \
84 (void*)Impl_mle_ ## className ## _ ## methodName}
86 #define FORWARD_IMPL_none()
88 #define FORWARD_IMPL_args(...) , __VA_ARGS__
90 #define FORWARD_IMPL_pass(...) , __VA_ARGS__
92 #define FORWARD_IMPL_none()
94 #define FORWARD_FUNC_NAME(className, methodName) \
95 Impl_mle_ ## className ## _ ## methodName
97 #define FORWARD_IMPL_VOID(className, methodName, args, pass) \
98 JNIEXPORT void JNICALL Impl_mle_ ## className ## _ ## methodName( \
99 JNIEnv* env, jclass classy args) \
101 forwardCallStaticVoid(env, FORWARD_NATIVE_CLASS, \
102 FORWARD_stringy(methodName), \
103 FORWARD_from(FORWARD_paste(FORWARD_DESC_, methodName)) \
104 pass); \
107 #define FORWARD_IMPL(className, methodName, rtype, rjava, args, pass) \
108 JNIEXPORT rtype JNICALL Impl_mle_ ## className ## _ ## methodName( \
109 JNIEnv* env, jclass classy args) \
111 return FORWARD_paste(forwardCallStatic, rjava)(env, \
112 FORWARD_NATIVE_CLASS, \
113 FORWARD_stringy(methodName), \
114 FORWARD_from(FORWARD_paste(FORWARD_DESC_, methodName)) \
115 pass); \
118 #define DESC_ARRAY(x) "[" x
119 #define DESC_CLASS(x) "L" x ";"
120 #define DESC_BOOLEAN "Z"
121 #define DESC_BYTE "B"
122 #define DESC_SHORT "S"
123 #define DESC_CHAR "C"
124 #define DESC_CHARACTER DESC_CHAR
125 #define DESC_INT "I"
126 #define DESC_INTEGER DESC_INT
127 #define DESC_LONG "J"
128 #define DESC_FLOAT "F"
129 #define DESC_DOUBLE "D"
130 #define DESC_VOID "V"
131 #define DESC_OBJECT DESC_CLASS("java/lang/Object")
132 #define DESC_STRING DESC_CLASS("java/lang/String")
133 #define DESC_BYTE_BUFFER DESC_CLASS("java/nio/ByteBuffer")
135 #define DESC_JARPACKAGE \
136 DESC_CLASS("cc/squirreljme/jvm/mle/brackets/JarPackageBracket")
137 #define DESC_PENCIL \
138 DESC_CLASS("cc/squirreljme/jvm/mle/brackets/PencilBracket")
139 #define DESC_PENCILFONT \
140 DESC_CLASS("cc/squirreljme/jvm/mle/brackets/PencilFontBracket")
142 #define DESC_DYLIB_COLLECTOR \
143 DESC_CLASS("cc/squirreljme/emulator/scritchui/dylib/__Collector__")
144 #define DESC_DYLIB_BASE \
145 DESC_CLASS("cc/squirreljme/emulator/scritchui/dylib/DylibBaseObject")
146 #define DESC_DYLIB_HAS_OBJECT_POINTER \
147 DESC_CLASS("cc/squirreljme/emulator/scritchui/dylib/DylibHasObjectPointer")
148 #define DESC_DYLIB_PENCIL \
149 DESC_CLASS("cc/squirreljme/emulator/scritchui/dylib/DylibPencilObject")
150 #define DESC_DYLIB_PENCIL_BASIC \
151 DESC_CLASS("cc/squirreljme/emulator/scritchui/dylib/DylibPencilBasicObject")
152 #define DESC_DYLIB_PENCIL_UI \
153 DESC_CLASS("cc/squirreljme/emulator/scritchui/dylib/DylibPencilUiObject")
154 #define DESC_DYLIB_PENCILFONT \
155 DESC_CLASS("cc/squirreljme/emulator/scritchui/dylib/DylibPencilFontObject")
157 #define DESC_SCRITCHUI_ACTIVATE_LISTENER DESC_CLASS( \
158 "cc/squirreljme/jvm/mle/scritchui/callbacks/ScritchActivateListener")
159 #define DESC_SCRITCHUI_CLOSE_LISTENER DESC_CLASS( \
160 "cc/squirreljme/jvm/mle/scritchui/callbacks/ScritchCloseListener")
161 #define DESC_SCRITCHUI_INPUT_LISTENER DESC_CLASS( \
162 "cc/squirreljme/jvm/mle/scritchui/callbacks/ScritchInputListener")
163 #define DESC_SCRITCHUI_PAINT_LISTENER DESC_CLASS( \
164 "cc/squirreljme/jvm/mle/scritchui/callbacks/ScritchPaintListener")
165 #define DESC_SCRITCHUI_MENU_ITEM_ACTIVATE_LISTENER DESC_CLASS( \
166 "cc/squirreljme/jvm/mle/scritchui/callbacks/" \
167 "ScritchMenuItemActivateListener")
168 #define DESC_SCRITCHUI_SIZE_SUGGEST_LISTENER DESC_CLASS( \
169 "cc/squirreljme/jvm/mle/scritchui/callbacks/ScritchSizeSuggestListener")
170 #define DESC_SCRITCHUI_VIEW_LISTENER DESC_CLASS( \
171 "cc/squirreljme/jvm/mle/scritchui/callbacks/ScritchViewListener")
172 #define DESC_SCRITCHUI_VISIBLE_LISTENER DESC_CLASS( \
173 "cc/squirreljme/jvm/mle/scritchui/callbacks/ScritchVisibleListener")
175 #define DESC_SCRITCHUI_COMPONENT DESC_CLASS( \
176 "cc/squirreljme/jvm/mle/scritchui/brackets/ScritchComponentBracket")
177 #define DESC_SCRITCHUI_LIST DESC_CLASS( \
178 "cc/squirreljme/jvm/mle/scritchui/brackets/ScritchListBracket")
179 #define DESC_SCRITCHUI_MENUKIND DESC_CLASS( \
180 "cc/squirreljme/jvm/mle/scritchui/brackets/ScritchMenuKindBracket")
181 #define DESC_SCRITCHUI_PENCIL DESC_CLASS( \
182 "cc/squirreljme/jvm/mle/scritchui/brackets/ScritchPencilBracket")
183 #define DESC_SCRITCHUI_VIEW DESC_CLASS( \
184 "cc/squirreljme/jvm/mle/scritchui/brackets/ScritchViewBracket")
185 #define DESC_SCRITCHUI_WINDOW DESC_CLASS( \
186 "cc/squirreljme/jvm/mle/scritchui/brackets/ScritchWindowBracket")
188 /** Debug handlers for JNI code. */
189 extern sjme_debug_handlerFunctions sjme_jni_debugHandlers;
192 * Common check and forward call.
194 * @param failRet The failing return value.
195 * @param funcMember The function member to check and to call.
196 * @param args Arguments to the function.
197 * @since 2024/06/13
199 #define CHECK_AND_FORWARD(failRet, funcMember, args) \
200 do { if (funcMember == NULL) \
202 sjme_jni_throwMLECallError(env, SJME_ERROR_NOT_IMPLEMENTED); \
203 return (failRet); \
206 /* Forward. */ \
207 result = (failRet); \
208 if (sjme_error_is(error = funcMember args)) \
210 sjme_jni_throwMLECallError(env, error); \
211 return (failRet); \
212 } } while(0)
215 * Checks to see if a virtual machine call failed.
217 * @param env The Java environment.
218 * @return If there is an exception.
219 * @since 2023/12/29
221 sjme_jboolean sjme_jni_checkVMException(JNIEnv* env);
224 * Directly map integer array.
226 * @param env The Java environment.
227 * @param buf The input array to map.
228 * @param off The offset into the array.
229 * @param len The length of the array.
230 * @return The resultant raw object for the array.
231 * @since 2024/04/24
233 jintArray sjme_jni_mappedArrayInt(JNIEnv* env,
234 jint* buf, jint off, jint len);
237 * Throws a @c MLECallError .
239 * @param env The current Java environment.
240 * @param code The error code.
241 * @since 2024/04/16
243 void sjme_jni_throwMLECallError(JNIEnv* env, sjme_errorCode code);
246 * Throws the given throwable type.
248 * @param env The current Java environment.
249 * @param code The error code.
250 * @param type The type of exception to throw.
251 * @since 2024/04/16
253 void sjme_jni_throwThrowable(JNIEnv* env, sjme_errorCode code,
254 sjme_lpcstr type);
257 * Throws a @c VMException .
259 * @param env The current Java environment.
260 * @param code The error code.
261 * @since 2023/12/08
263 void sjme_jni_throwVMException(JNIEnv* env, sjme_errorCode code);
266 * Recovers a pointer from a @c DylibBaseObject .
268 * @param env The current Java environment.
269 * @param className The class this must be.
270 * @param instance The @c DylibBaseObject instance.
271 * @return The resultant pointer.
272 * @since 2024/06/12
274 void* sjme_jni_recoverPointer(JNIEnv* env, sjme_lpcstr className,
275 jobject instance);
278 * Recovers a pointer from a @c DylibPencilObject .
280 * @param env The current Java environment.
281 * @param g The @c DylibPencilObject instance.
282 * @return The resultant pointer.
283 * @since 2024/06/25
285 sjme_scritchui_pencil sjme_jni_recoverPencil(JNIEnv* env, jobject g);
288 * Recovers a pointer from a @c DylibPencilFontObject .
290 * @param env The current Java environment.
291 * @param fontInstance The @c DylibPencilFontObject instance.
292 * @return The resultant pointer.
293 * @since 2024/06/25
295 sjme_scritchui_pencilFont sjme_jni_recoverFont(JNIEnv* env,
296 jobject fontInstance);
299 * Fills in the front end information.
301 * @param env The environment used.
302 * @param into What is being written to.
303 * @param ref The object reference, if any.
304 * @return Any resultant error, if any.
305 * @since 2024/06/27
307 sjme_errorCode sjme_jni_fillFrontEnd(JNIEnv* env, sjme_frontEnd* into,
308 jobject ref);
311 * Recovers the Java environment pointer.
313 * @param outEnv The resultant environment.
314 * @param inVm The input virtual machine.
315 * @return Any resultant error, if any.
316 * @since 2024/06/27
318 sjme_errorCode sjme_jni_recoverEnv(
319 sjme_attrInOutNotNull JNIEnv** outEnv,
320 sjme_attrInNotNull JavaVM* inVm);
323 * Recovers the Java environment pointer.
325 * @param outEnv The resultant environment.
326 * @param inFrontEnd The input front end.
327 * @return Any resultant error, if any.
328 * @since 2024/06/27
330 sjme_errorCode sjme_jni_recoverEnvFrontEnd(
331 sjme_attrInOutNotNull JNIEnv** outEnv,
332 sjme_attrInNotNull const sjme_frontEnd* inFrontEnd);
335 * Initializes a character sequence from a Java String.
337 * @param env The Java environment.
338 * @param inOutSeq The input/output character sequence.
339 * @param inString The input string to wrap.
340 * @return Any resultant error, if any.
341 * @since 2024/06/26
343 sjme_errorCode sjme_jni_jstringCharSeqStatic(
344 sjme_attrInNotNull JNIEnv* env,
345 sjme_attrInNotNull sjme_charSeq* inOutSeq,
346 sjme_attrInNotNull jstring inString);
349 * Maps input @c sjme_jlong to @c jlong .
351 * @param value The input value.
352 * @return The resultant value.
353 * @since 2024/06/30
355 jlong sjme_jni_jlong(sjme_jlong value);
358 * Pushes a weak link bound to an object.
360 * @param env The Java environment.
361 * @param javaObject The object to bind.
362 * @param nativeWeak The native weak to bind from.
363 * @return Any resultant error, if any.
364 * @since 2024/07/11
366 sjme_errorCode sjme_jni_pushWeakLink(
367 sjme_attrInNotNull JNIEnv* env,
368 sjme_attrInNotNull jobject javaObject,
369 sjme_attrInNotNull sjme_alloc_weak nativeWeak);
372 * Returns the Java type of the given array.
374 * @param env The Java environment.
375 * @param array The array to get the type of.
376 * @param outJavaType The resultant type.
377 * @return Any resultant error.
378 * @since 2024/07/11
380 sjme_errorCode sjme_jni_arrayType(
381 sjme_attrInNotNull JNIEnv* env,
382 sjme_attrInNotNull jobject array,
383 sjme_attrOutNotNull sjme_basicTypeId* outType);
386 * Gets the elements of an array.
388 * @param env The environment.
389 * @param array The array object.
390 * @param rawBuf The raw output buffer.
391 * @param isCopy Is the array a copy?
392 * @param typeSize The size of the element type.
393 * @return Any resultant error.
394 * @since 2024/07/11
396 sjme_errorCode sjme_jni_arrayGetElements(
397 sjme_attrInNotNull JNIEnv* env,
398 sjme_attrInNotNull jobject array,
399 sjme_attrOutNotNull sjme_pointer* rawBuf,
400 sjme_attrOutNotNull jboolean* isCopy,
401 sjme_attrOutNullable sjme_jint* typeSize);
404 * Gets the elements of an array.
406 * @param env The environment.
407 * @param array The array object.
408 * @param rawBuf The raw output buffer.
409 * @return Any resultant error.
410 * @since 2024/07/11
412 sjme_errorCode sjme_jni_arrayReleaseElements(
413 sjme_attrInNotNull JNIEnv* env,
414 sjme_attrInNotNull jarray array,
415 sjme_attrInNotNull sjme_pointer rawBuf);
417 #endif /* __SQUIRRELJME_H__ */