Move emulator-base native code into its own non-Gradle directory.
[SquirrelJME.git] / emulators / emulator-base-native / c / mle_runtime.c
blob1f05057eb0fdd978358f00d1d25c0b403b51bfce
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 #include <string.h>
11 #include <stdlib.h>
13 #if defined(_WIN32) || defined(_WIN64)
14 #include <windows.h>
15 #include <libloaderapi.h>
16 #elif defined(__APPLE__)
17 #include <mach-o/dyld.h>
18 #include <stdint.h>
19 #elif defined(__linux__) || defined(__linux)
20 #include <unistd.h>
21 #include <stdint.h>
22 #elif defined(__FreeBSD__)
23 #include <sys/stat.h>
24 #include <sys/sysctl.h>
25 #include <unistd.h>
26 #include <stdint.h>
27 #endif
29 #include "squirreljme.h"
31 #define RUNTIME_CLASSNAME "cc/squirreljme/emulator/EmulatedRuntimeShelf"
33 #define RUNTIME_MEMORYPROFILE_DESC "()I"
34 #define RUNTIME_SYSTEMENV_DESC "(Ljava/lang/String;)Ljava/lang/String;"
35 #define RUNTIME_VMDESCRIPTION_DESC "(I)Ljava/lang/String;"
36 #define RUNTIME_VMSTATISTIC_DESC "(I)J"
38 JNIEXPORT void JNICALL Impl_mle_RuntimeShelf_garbageCollect(
39 JNIEnv* env, jclass classy)
41 // Does nothing
44 JNIEXPORT jint JNICALL Impl_mle_RuntimeShelf_lineEnding(
45 JNIEnv* env, jclass classy)
47 #if defined(_WIN32)
48 return 3;
49 #else
50 return 1;
51 #endif
54 JNIEXPORT jstring JNICALL Impl_mle_RuntimeShelf_vmDescription(
55 JNIEnv* env, jclass classy, jint id)
57 #define NATIVE_EXEC_PATH_LEN 768
58 char fileName[NATIVE_EXEC_PATH_LEN];
60 #if defined(__APPLE__)
61 uint32_t fileNameLen;
62 #elif defined(__FreeBSD__)
63 struct stat* statBuf;
64 int sysCtlInput[4];
65 size_t fileNameLen;
66 #elif defined(__sun) || defined(__illumos__)
67 const char* bip;
68 #endif
70 // Executable path of the VM binary (EXECUTABLE_PATH)
71 if (id == 6)
73 // Clear buffer
74 memset(fileName, 0, sizeof(fileName));
76 // Use native system APIs to get this information
77 #if defined(_WIN32) || defined(_WIN64)
78 GetModuleFileNameA(NULL, fileName, NATIVE_EXEC_PATH_LEN);
79 #elif defined(__APPLE__)
80 fileNameLen = NATIVE_EXEC_PATH_LEN;
81 _NSGetExecutablePath(fileName, &fileNameLen);
82 #elif defined(__linux__) || defined(__linux)
83 readlink("/proc/self/exe", fileName, NATIVE_EXEC_PATH_LEN);
84 #elif defined(__NetBSD__) || defined(__DragonFly__)
85 readlink("/proc/curproc/file", fileName, NATIVE_EXEC_PATH_LEN);
86 #elif defined(__FreeBSD__)
87 memset(&statBuf, 0, sizeof(statBuf));
88 if (stat("/proc/curproc/file", &statBuf) == 0)
89 readlink("/proc/curproc/file", fileName, NATIVE_EXEC_PATH_LEN);
90 else
92 // Setup systemctl input
93 sysCtlInput[0] = CTL_KERN;
94 sysCtlInput[1] = KERN_PROC;
95 sysCtlInput[2] = KERN_PROC_PATHNAME;
96 sysCtlInput[3] = -1;
98 // Perform the call
99 fileNameLen = NATIVE_EXEC_PATH_LEN;
100 sysctl(mib, 4, fileName, &fileNameLen, NULL, 0);
102 #elif defined(__sun) || defined(__illumos__)
103 bip = getexecname();
104 if (bip != NULL)
105 strncpy(fileName, bip, NATIVE_EXEC_PATH_LEN - 1);
106 #endif
108 // Convert to Java String if Valid
109 if (fileName[0] != 0)
111 fileName[NATIVE_EXEC_PATH_LEN - 1] = 0;
112 return (*env)->NewStringUTF(env, fileName);
116 return (jstring)forwardCallStaticObject(env, RUNTIME_CLASSNAME,
117 "vmDescription", RUNTIME_VMDESCRIPTION_DESC,
118 id);
119 #undef NATIVE_EXEC_PATH_LEN
122 JNIEXPORT jlong JNICALL Impl_mle_RuntimeShelf_vmStatistic(
123 JNIEnv* env, jclass classy, jint id)
125 return forwardCallStaticLong(env, RUNTIME_CLASSNAME,
126 "vmStatistic", RUNTIME_VMSTATISTIC_DESC,
127 id);
130 JNIEXPORT jint JNICALL Impl_mle_RuntimeShelf_memoryProfile(
131 JNIEnv* env, jclass classy)
133 // The value is normal
134 return 0;
137 JNIEXPORT jint JNICALL Impl_mle_RuntimeShelf_phoneModel(
138 JNIEnv* env, jclass classy)
140 // Just be a generic device here
141 return 0;
144 JNIEXPORT jobject JNICALL Impl_mle_RuntimeShelf_systemEnv(
145 JNIEnv* env, jclass classy, jstring key)
147 return forwardCallStaticObject(env, RUNTIME_CLASSNAME,
148 "systemEnv", RUNTIME_SYSTEMENV_DESC,
149 key);
152 JNIEXPORT jint JNICALL Impl_mle_RuntimeShelf_vmType(
153 JNIEnv* env, jclass classy)
155 // The value 1 is Java SE type
156 return 1;
159 static const JNINativeMethod mleRuntimeMethods[] =
161 {"garbageCollect", "()V", (void*)Impl_mle_RuntimeShelf_garbageCollect},
162 {"lineEnding", "()I", (void*)Impl_mle_RuntimeShelf_lineEnding},
163 {"memoryProfile", RUNTIME_MEMORYPROFILE_DESC, (void*)Impl_mle_RuntimeShelf_memoryProfile},
164 {"phoneModel", "()I", (void*)Impl_mle_RuntimeShelf_phoneModel},
165 {"systemEnv", RUNTIME_SYSTEMENV_DESC, (void*)Impl_mle_RuntimeShelf_systemEnv},
166 {"vmDescription", RUNTIME_VMDESCRIPTION_DESC, (void*)Impl_mle_RuntimeShelf_vmDescription},
167 {"vmStatistic", RUNTIME_VMSTATISTIC_DESC, (void*)Impl_mle_RuntimeShelf_vmStatistic},
168 {"vmType", "()I", (void*)Impl_mle_RuntimeShelf_vmType},
171 jint JNICALL mleRuntimeInit(JNIEnv* env, jclass classy)
173 return (*env)->RegisterNatives(env,
174 (*env)->FindClass(env, "cc/squirreljme/jvm/mle/RuntimeShelf"),
175 mleRuntimeMethods, sizeof(mleRuntimeMethods) /
176 sizeof(JNINativeMethod));