remedy: fix MMAPI audio, speed up interpreter, follow system volume
- Audio: 48 kHz mixer with a 512/3072-frame ALSA ring and table-based polyphonic MIDI synth (no stutter or tempo drag under load), Standard MIDI Format 0/1, ToneControl sequences, multi-stream WAV, and MIDP VolumeControl levels mapped to perceived loudness. - Volume: track the system speaker volume and mute state live (speaker_vol, RX3 Digital Volume, SPK DAC Switch) with the same cubic gain PipeWire clients get; pause menu Audio item and volume keys now change the system speaker volume like the xdrawer slider. - JVM: key fields by name and descriptor so obfuscated classes with same-named fields of different types work; cache resolved constant pool refs, static field slots and monomorphic call targets; use a stable deque heap; raise startApp/<clinit> budgets and run Java threads on a wall-clock budget per frame. - M3G and LCDUI additions needed by Tomb Raider 3D Underworld and Castlevania: Aria of Sorrow.
This commit is contained in:
1 parent
66769d7c2e
commit
ac9f9bb19e
9 files changed
+2463
-228
No files matched your search
@@ -17,6 +17,10 @@ Release versions follow the `<year-letter><MM><DD>` format (where `2026` is `a`,
|
||||
- Added a Gambatte-style NeoUI-themed frontend browser when launched without arguments (scanning `/usr/share/games/j2me`, `~/Games`, `~/J2ME`, and `~/Downloads` with `MANIFEST.MF` / `.jad` metadata preview, pause menu, keypad control remapping, scaling/3D/audio toggles, and RMS save manager).
|
||||
- Implemented NeoUI-themed (`libneoui.so` / `PluginTheme`) high-level LCDUI components (`Form`, `List`, `TextBox`, `Alert`, `ChoiceGroup`, `Gauge`, `TextField`, `StringItem`, and `Command` bars/menus) alongside full-screen 2D `Canvas` / `GameCanvas` / `Sprite` / `TiledLayer` rendering, Nokia UI API (`com.nokia.mid.ui.*`), Adreno 304 (`FD304`) EGL + OpenGL ES 2.0 hardware-accelerated JSR-184 M3G 3D graphics (`javax.microedition.m3g.*`), HTTP/HTTPS and TCP/UDP socket networking (`javax.microedition.io.Connector`), `RecordStore` (RMS) persistence, and MMAPI (`javax.microedition.media.Manager`) ALSA audio.
|
||||
- Preinstalled J2ME apps and functional test suites in `/usr/share/games/j2me/` and `~/Games/`: `g2048.jar` (240x320 2048), `ReMEdy3DArena.jar` (JSR-184 3D hardware-accelerated arena), `AstroBlaster240.jar` (`GameCanvas` space shooter with RMS high score and tone audio), and `NetToolJ2ME.jar` (NeoUI-themed LCDUI + HTTP/TCP socket test suite).
|
||||
- Faster bytecode interpreter: resolved field/method constant pool entries and static field slots are cached per class, virtual calls use a monomorphic inline cache, and Java threads run on a wall-clock budget per frame, so asset-heavy games (for example `Castlevania: Aria of Sorrow`) load and play at full speed.
|
||||
- Fixed obfuscated MIDlets that declare several fields with the same name but different types (fields are now keyed by name and descriptor), and fixed long-running `startApp()` / `<clinit>` initializers being cut short.
|
||||
- Smoother MMAPI audio: a 48 kHz mixer with a 64 ms ALSA ring buffer and a table-based polyphonic MIDI synthesizer (no more stutter or tempo drag under CPU load), plus MIDP `VolumeControl` levels mapped to perceived loudness.
|
||||
- ReMEdy audio now follows the system speaker volume and mute state (the xdrawer **Speaker** slider, `~/.config/sxmo/speaker_vol`, and `SPK DAC Switch`) live while a MIDlet runs. The pause menu **Audio** item changes the system speaker volume with `Left` / `Right` and toggles ReMEdy sound with `OK`.
|
||||
- **Adreno 304 (`FD304`) 3D GPU hardware acceleration and Xlibre Glamor offload**:
|
||||
- Added full Qualcomm **Adreno 304 (`0x03000400`)** GPU and `qcom,msm8909-iommu` (`gfx3d_user` / `gfx3d_priv` context banks at `0x1f08000`) support to the `6.12.1-msm8916` kernel (`drm/msm` module and MSM8905/MSM8909 device trees across `nokia-argon`, `nokia-beatles`, `nokia-tough`, `nokia-sparkler`, and `nokia-leo`), configured for Adreno 304's `96 KB` (`SZ_64K + SZ_32K`) GMEM cache, `a300_pm4.fw` / `a300_pfp.fw` microcode, VBIF/ROQ/clock-gating registers, hard-reset recovery quirk, `OXILI_GDSC` power domain, and OPP frequency scaling up to `409.6 MHz`.
|
||||
- Enabled headless render-only `drm/msm` initialization (`card1` / `renderD128`) alongside the SPI `panel-mipi-dbi` display (`card0`) and enabled `FD304` (`chip_id = 0x030004ff`) in Mesa Freedreno (`libgallium`) for hardware-accelerated **OpenGL 3.1** and **OpenGL ES 3.0**.
|
||||
|
||||
@@ -241,8 +241,8 @@ Two classic X11 games (`/usr/bin/snake` and `/usr/bin/tetris`), the **Gambatte (
|
||||
- **ReMEdy J2ME (MIDP 2.0 / CLDC 1.1) emulator (`remedy` / `j2me`)**:
|
||||
- **Launch**: open **ReMEdy (J2ME)** from **System menu -> Applications** or **System menu -> Games**, open any `.jar` or `.jad` file from **Files (`xfiles`)**, or run `remedy [app.jar|app.jad]`. When launched without a file argument, opens the NeoUI-themed **ReMEdy J2ME** browser scanning `/usr/share/games/j2me`, `~/Games`, `~/J2ME`, `~/Downloads`, and user directories, displaying the selected MIDlet's `MIDlet-Name`, `MIDlet-Vendor`, `MIDlet-Version`, main class, and RMS save status.
|
||||
- **Full-screen `240x320` resolution and NeoUI-themed LCDUI**: runs in a full-screen `240x320` window (`X=0, Y=0, W=240, H=320`) and declares native `240x320` dimensions (`getWidth() == 240`, `getHeight() == 320`) to J2ME applications. Full-screen `Canvas` and `GameCanvas` games get the entire `240x320` display, while high-level LCDUI screens (`Form`, `List`, `TextBox`, `Alert`, `ChoiceGroup`, `Gauge`, `TextField`, `StringItem`, and `Command` bars/menus) render with `libneoui.so` (`PluginTheme`) and adapt automatically to the active `.Xresources` / `~/.xthemes/*.xth` system theme.
|
||||
- **Hardware-accelerated 3D (JSR-184 M3G), networking, RMS, and audio**: accelerates JSR-184 (`javax.microedition.m3g.*`) 3D scenes on the Adreno 304 (`FD304`) GPU via EGL + OpenGL ES 2.0 (with depth-buffered software fallback), supports HTTP/HTTPS and raw TCP/UDP socket connections (`javax.microedition.io.Connector`, `HttpConnection`, `SocketConnection`, `FileConnection` JSR-75), Nokia UI API (`com.nokia.mid.ui.*`), persistent `RecordStore` (RMS) storage (`~/.local/share/sxmo/remedy/rms/`), and MMAPI (`javax.microedition.media.Manager`) tone and WAV/MIDI audio over ALSA (`hw:0,0`).
|
||||
- **Keypad controls and pause menu**: maps D-Pad (`Up`/`Down`/`Left`/`Right`), `Center OK` (`FIRE`), `0`–`9`, `*`, `#`, **Left softkey** (`-6`), and **Right softkey** (`-7`) directly to MIDP 2.0 / Nokia key codes, with customizable D-Pad (`D-Pad + 2/4/6/8`, `D-Pad Only`, `2/4/6/8 Only`) and softkey modes persisted in `~/.config/sxmo/remedy.cfg`. Press **End / Power (red)** (`Escape`) during execution to open the **ReMEdy Pause Menu** (Resume, Send Left/Right Softkey Command, Screen Scaling, 3D Renderer toggle, Audio toggle, Keypad Controls remapping, Reset RMS Save Data, Load Another MIDlet, or Quit). Preinstalled MIDlets in `/usr/share/games/j2me/` and `~/Games/` include `g2048.jar`, `ReMEdy3DArena.jar` (JSR-184 3D benchmark), `AstroBlaster240.jar` (`GameCanvas` space shooter), and `NetToolJ2ME.jar` (LCDUI + HTTP/TCP socket test suite).
|
||||
- **Hardware-accelerated 3D (JSR-184 M3G), networking, RMS, and audio**: accelerates JSR-184 (`javax.microedition.m3g.*`) 3D scenes on the Adreno 304 (`FD304`) GPU via EGL + OpenGL ES 2.0 (with depth-buffered software fallback), supports HTTP/HTTPS and raw TCP/UDP socket connections (`javax.microedition.io.Connector`, `HttpConnection`, `SocketConnection`, `FileConnection` JSR-75), Nokia UI API (`com.nokia.mid.ui.*`), persistent `RecordStore` (RMS) storage (`~/.local/share/sxmo/remedy/rms/`), and MMAPI (`javax.microedition.media.Manager`) tone and WAV/MIDI audio over ALSA (`hw:0,0`). Audio follows the system speaker volume and mute state (the xdrawer **Speaker** slider) live, at the same loudness as other apps.
|
||||
- **Keypad controls and pause menu**: maps D-Pad (`Up`/`Down`/`Left`/`Right`), `Center OK` (`FIRE`), `0`–`9`, `*`, `#`, **Left softkey** (`-6`), and **Right softkey** (`-7`) directly to MIDP 2.0 / Nokia key codes, with customizable D-Pad (`D-Pad + 2/4/6/8`, `D-Pad Only`, `2/4/6/8 Only`) and softkey modes persisted in `~/.config/sxmo/remedy.cfg`. Press **End / Power (red)** (`Escape`) during execution to open the **ReMEdy Pause Menu** (Resume, Send Left/Right Softkey Command, Screen Scaling, 3D Renderer toggle, Audio (`Left`/`Right` change the system speaker volume, `OK` toggles ReMEdy sound), Keypad Controls remapping, Reset RMS Save Data, Load Another MIDlet, or Quit). Preinstalled MIDlets in `/usr/share/games/j2me/` and `~/Games/` include `g2048.jar`, `ReMEdy3DArena.jar` (JSR-184 3D benchmark), `AstroBlaster240.jar` (`GameCanvas` space shooter), and `NetToolJ2ME.jar` (LCDUI + HTTP/TCP socket test suite).
|
||||
|
||||
### 3.2 Multi-mode `fcitx5-t9` keypad reference
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large.
Load diff
+273
-55
@@ -10,6 +10,7 @@
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@@ -150,6 +151,41 @@ struct JMethodInfo {
|
||||
std::vector<JExceptionEntry> exceptionTable;
|
||||
};
|
||||
|
||||
struct JClass;
|
||||
|
||||
// Native method handler signature:
|
||||
// Receives (Jvm& vm, JThread& th, const std::vector<JVal>& args, JVal* outReturn)
|
||||
// Returns true if normal return (or yield), false if an exception was thrown on `th`.
|
||||
using NativeMethodFn = std::function<bool(
|
||||
Jvm&, JThread&, const std::vector<JVal>&, JVal*)>;
|
||||
|
||||
// Lazily resolved Fieldref / Methodref / InterfaceMethodref constant pool entry.
|
||||
// Caches the decoded strings, the typed field key, the static field storage
|
||||
// slot and a monomorphic inline cache for method dispatch so the interpreter
|
||||
// hot path avoids per-instruction string copies, concatenation and hierarchy
|
||||
// walks.
|
||||
struct CpRefCache {
|
||||
bool ready = false;
|
||||
std::string cls;
|
||||
std::string name;
|
||||
std::string desc;
|
||||
std::string fieldKey;
|
||||
char descFirst = 'I';
|
||||
|
||||
// getstatic / putstatic
|
||||
JVal* staticSlot = nullptr;
|
||||
|
||||
// invoke*
|
||||
bool sigReady = false;
|
||||
uint16_t numParams = 0;
|
||||
JType returnType = JType::Void;
|
||||
bool icValid = false;
|
||||
std::string icClass;
|
||||
JClass* icTargetCls = nullptr;
|
||||
const JMethodInfo* icTargetMethod = nullptr;
|
||||
const NativeMethodFn* icNative = nullptr;
|
||||
};
|
||||
|
||||
struct JClass {
|
||||
std::string name;
|
||||
std::string superName;
|
||||
@@ -194,6 +230,22 @@ struct JClass {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<CpRefCache> refCache;
|
||||
|
||||
CpRefCache& refAt(uint16_t idx) {
|
||||
if (refCache.size() != cp.size()) refCache.resize(cp.size());
|
||||
static CpRefCache kDummy;
|
||||
if (idx >= refCache.size()) return kDummy;
|
||||
CpRefCache& c = refCache[idx];
|
||||
if (!c.ready) {
|
||||
cpRef(idx, &c.cls, &c.name, &c.desc);
|
||||
c.fieldKey = c.desc.empty() ? c.name : (c.name + ":" + c.desc);
|
||||
c.descFirst = c.desc.empty() ? 'I' : c.desc[0];
|
||||
c.ready = true;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -244,6 +296,11 @@ struct JObject {
|
||||
}
|
||||
}
|
||||
|
||||
static std::string makeFieldKey(const std::string& name,
|
||||
const std::string& desc) {
|
||||
return desc.empty() ? name : (name + ":" + desc);
|
||||
}
|
||||
|
||||
JVal getField(const std::string& name, char descFirstChar = 'I') const {
|
||||
auto it = fields.find(name);
|
||||
if (it != fields.end()) return it->second;
|
||||
@@ -257,9 +314,22 @@ struct JObject {
|
||||
}
|
||||
}
|
||||
|
||||
JVal getTypedField(const std::string& name, const std::string& desc) const {
|
||||
if (!desc.empty()) {
|
||||
auto it = fields.find(name + ":" + desc);
|
||||
if (it != fields.end()) return it->second;
|
||||
}
|
||||
return getField(name, desc.empty() ? 'I' : desc[0]);
|
||||
}
|
||||
|
||||
void setField(const std::string& name, const JVal& v) {
|
||||
fields[name] = v;
|
||||
}
|
||||
|
||||
void setTypedField(const std::string& name, const std::string& desc,
|
||||
const JVal& v) {
|
||||
fields[makeFieldKey(name, desc)] = v;
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -304,12 +374,6 @@ struct JThread {
|
||||
std::vector<JFrame> frames;
|
||||
};
|
||||
|
||||
// Native method handler signature:
|
||||
// Receives (Jvm& vm, JThread& th, const std::vector<JVal>& args, JVal* outReturn)
|
||||
// Returns true if normal return (or yield), false if an exception was thrown on `th`.
|
||||
using NativeMethodFn = std::function<bool(
|
||||
Jvm&, JThread&, const std::vector<JVal>&, JVal*)>;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Descriptor Argument Parser
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -661,12 +725,21 @@ class Jvm {
|
||||
NativeMethodFn findNative(const std::string& className,
|
||||
const std::string& methodName,
|
||||
const std::string& descriptor) const {
|
||||
const NativeMethodFn* p = findNativePtr(className, methodName, descriptor);
|
||||
return p ? *p : NativeMethodFn{};
|
||||
}
|
||||
|
||||
// Pointer variant (unordered_map values are address-stable) used by the
|
||||
// interpreter's inline caches to avoid copying std::function objects.
|
||||
const NativeMethodFn* findNativePtr(const std::string& className,
|
||||
const std::string& methodName,
|
||||
const std::string& descriptor) const {
|
||||
std::string cur = className;
|
||||
while (!cur.empty()) {
|
||||
auto it = natives_.find(cur + "." + methodName + descriptor);
|
||||
if (it != natives_.end()) return it->second;
|
||||
if (it != natives_.end()) return &it->second;
|
||||
auto wit = nativesWildcard_.find(cur + "." + methodName);
|
||||
if (wit != nativesWildcard_.end()) return wit->second;
|
||||
if (wit != nativesWildcard_.end()) return &wit->second;
|
||||
cur = getSuperClassName(cur);
|
||||
}
|
||||
return nullptr;
|
||||
@@ -771,6 +844,22 @@ class Jvm {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// True when at least one scheduled Java thread can make progress right now
|
||||
// (runnable, or sleeping/waiting with an already-expired wake deadline).
|
||||
bool anyThreadReady() const {
|
||||
int64_t now = nowMs();
|
||||
for (const auto& th : threads_) {
|
||||
if (!th || th->frames.empty()) continue;
|
||||
if (th->state == ThreadState::Runnable) return true;
|
||||
if ((th->state == ThreadState::Sleeping ||
|
||||
th->state == ThreadState::Waiting) &&
|
||||
th->wakeTimeMs > 0 && now >= th->wakeTimeMs) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool resolveBytecodeMethod(const std::string& startClass,
|
||||
const std::string& methodName,
|
||||
const std::string& descriptor,
|
||||
@@ -806,7 +895,7 @@ class Jvm {
|
||||
JThread tempThread;
|
||||
tempThread.id = 9999;
|
||||
pushBytecodeFrame(tempThread, cls, &m, {});
|
||||
runThreadUntilReturn(tempThread, 0, 200000);
|
||||
runThreadUntilReturn(tempThread, 0, 2000000);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -846,6 +935,7 @@ class Jvm {
|
||||
f.pc = 0;
|
||||
size_t numLocals = std::max<size_t>(m->maxLocals + 4, args.size() * 2 + 4);
|
||||
f.locals.resize(numLocals);
|
||||
f.stack.reserve(static_cast<size_t>(m->maxStack) + 4);
|
||||
size_t localIdx = 0;
|
||||
for (const JVal& a : args) {
|
||||
if (localIdx >= f.locals.size()) f.locals.resize(localIdx + 4);
|
||||
@@ -970,7 +1060,7 @@ class Jvm {
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<JObject> heap_;
|
||||
std::deque<JObject> heap_;
|
||||
std::unordered_map<std::string, uint32_t> internPool_;
|
||||
std::unordered_map<std::string, std::unique_ptr<JClass>> classes_;
|
||||
std::unordered_map<std::string, std::string> builtinSupers_;
|
||||
@@ -1121,7 +1211,7 @@ class Jvm {
|
||||
v = JVal::MakeRef(internString(cls->cpUtf8(ce.idx1)));
|
||||
}
|
||||
}
|
||||
cls->staticFields[f.name] = v;
|
||||
cls->staticFields[JObject::makeFieldKey(f.name, f.descriptor)] = v;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1137,38 +1227,25 @@ class Jvm {
|
||||
}
|
||||
for (const auto& f : cls->fields) {
|
||||
if ((f.accessFlags & 0x0008) != 0) continue; // skip static
|
||||
if (heap_[objRef].fields.find(f.name) != heap_[objRef].fields.end()) {
|
||||
std::string fkey = JObject::makeFieldKey(f.name, f.descriptor);
|
||||
if (heap_[objRef].fields.find(fkey) != heap_[objRef].fields.end()) {
|
||||
continue;
|
||||
}
|
||||
char d = f.descriptor.empty() ? 'I' : f.descriptor[0];
|
||||
if (d == 'J') heap_[objRef].fields[f.name] = JVal::MakeLong(0);
|
||||
else if (d == 'F') heap_[objRef].fields[f.name] = JVal::MakeFloat(0.0f);
|
||||
else if (d == 'D') heap_[objRef].fields[f.name] = JVal::MakeDouble(0.0);
|
||||
if (d == 'J') heap_[objRef].fields[fkey] = JVal::MakeLong(0);
|
||||
else if (d == 'F') heap_[objRef].fields[fkey] = JVal::MakeFloat(0.0f);
|
||||
else if (d == 'D') heap_[objRef].fields[fkey] = JVal::MakeDouble(0.0);
|
||||
else if (d == 'L' || d == '[') {
|
||||
heap_[objRef].fields[f.name] = JVal::Null();
|
||||
heap_[objRef].fields[fkey] = JVal::Null();
|
||||
} else {
|
||||
heap_[objRef].fields[f.name] = JVal::MakeInt(0);
|
||||
heap_[objRef].fields[fkey] = JVal::MakeInt(0);
|
||||
}
|
||||
}
|
||||
cur = cls->superName;
|
||||
}
|
||||
}
|
||||
|
||||
JVal getStaticFieldVal(const std::string& className,
|
||||
const std::string& fieldName,
|
||||
const std::string& desc) {
|
||||
std::string cur = className;
|
||||
int depth = 0;
|
||||
while (!cur.empty() && depth++ < 24) {
|
||||
JClass* cls = loadClass(cur);
|
||||
if (cls) {
|
||||
ensureClassInitialized(cls);
|
||||
auto it = cls->staticFields.find(fieldName);
|
||||
if (it != cls->staticFields.end()) return it->second;
|
||||
}
|
||||
cur = getSuperClassName(cur);
|
||||
}
|
||||
char d = desc.empty() ? 'I' : desc[0];
|
||||
static JVal defaultValueForDesc(char d) {
|
||||
if (d == 'J') return JVal::MakeLong(0);
|
||||
if (d == 'F') return JVal::MakeFloat(0.0f);
|
||||
if (d == 'D') return JVal::MakeDouble(0.0);
|
||||
@@ -1176,14 +1253,57 @@ class Jvm {
|
||||
return JVal::MakeInt(0);
|
||||
}
|
||||
|
||||
// Find the storage slot of a static field, searching the referenced class,
|
||||
// its superclasses and their superinterfaces (JVMS 5.4.3.2). Classes are
|
||||
// initialized along the way. If the field is not declared anywhere, a
|
||||
// default-valued slot is created on the referenced class. Returns nullptr
|
||||
// only for classes that are not present in the JAR (pure native classes).
|
||||
JVal* findStaticSlotIn(JClass* cls, const std::string& fkey,
|
||||
const std::string& fieldName, int depth) {
|
||||
if (!cls || depth > 24) return nullptr;
|
||||
ensureClassInitialized(cls);
|
||||
auto it = cls->staticFields.find(fkey);
|
||||
if (it != cls->staticFields.end()) return &it->second;
|
||||
auto it2 = cls->staticFields.find(fieldName);
|
||||
if (it2 != cls->staticFields.end()) return &it2->second;
|
||||
for (const std::string& iface : cls->interfaces) {
|
||||
JVal* s = findStaticSlotIn(loadClass(iface), fkey, fieldName, depth + 1);
|
||||
if (s) return s;
|
||||
}
|
||||
if (!cls->superName.empty() && cls->superName != "java/lang/Object") {
|
||||
return findStaticSlotIn(loadClass(cls->superName), fkey, fieldName,
|
||||
depth + 1);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
JVal* resolveStaticSlot(const std::string& className,
|
||||
const std::string& fieldName,
|
||||
const std::string& desc) {
|
||||
std::string fkey = JObject::makeFieldKey(fieldName, desc);
|
||||
JClass* cls = loadClass(className);
|
||||
if (!cls) return nullptr;
|
||||
JVal* s = findStaticSlotIn(cls, fkey, fieldName, 0);
|
||||
if (s) return s;
|
||||
auto ins = cls->staticFields.emplace(
|
||||
fkey, defaultValueForDesc(desc.empty() ? 'I' : desc[0]));
|
||||
return &ins.first->second;
|
||||
}
|
||||
|
||||
JVal getStaticFieldVal(const std::string& className,
|
||||
const std::string& fieldName,
|
||||
const std::string& desc) {
|
||||
JVal* s = resolveStaticSlot(className, fieldName, desc);
|
||||
if (s) return *s;
|
||||
return defaultValueForDesc(desc.empty() ? 'I' : desc[0]);
|
||||
}
|
||||
|
||||
void setStaticFieldVal(const std::string& className,
|
||||
const std::string& fieldName,
|
||||
const std::string& desc,
|
||||
const JVal& val) {
|
||||
JClass* cls = loadClass(className);
|
||||
if (cls) {
|
||||
ensureClassInitialized(cls);
|
||||
cls->staticFields[fieldName] = val;
|
||||
}
|
||||
JVal* s = resolveStaticSlot(className, fieldName, desc);
|
||||
if (s) *s = val;
|
||||
}
|
||||
|
||||
// Big-endian readers for .class format
|
||||
@@ -1494,6 +1614,98 @@ class Jvm {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Cached variant of invokeMethodFromFrame used by the bytecode interpreter.
|
||||
// Static and special call sites resolve once; virtual and interface call
|
||||
// sites keep a monomorphic inline cache keyed on the receiver class.
|
||||
bool invokeMethodCached(JThread& th, CpRefCache& rc, bool isStatic,
|
||||
bool isSpecial) {
|
||||
if (!rc.sigReady) {
|
||||
MethodSigInfo sig = parseMethodDescriptor(rc.desc);
|
||||
rc.numParams = static_cast<uint16_t>(sig.paramTypes.size());
|
||||
rc.returnType = sig.returnType;
|
||||
rc.sigReady = true;
|
||||
}
|
||||
size_t numArgs = rc.numParams + (isStatic ? 0 : 1);
|
||||
std::vector<JVal> args(numArgs);
|
||||
{
|
||||
JFrame& caller = th.frames.back();
|
||||
for (size_t i = 0; i < numArgs; ++i) {
|
||||
args[numArgs - 1 - i] = caller.pop();
|
||||
}
|
||||
}
|
||||
|
||||
const std::string* lookupClass = &rc.cls;
|
||||
if (!isStatic) {
|
||||
uint32_t selfRef = args[0].ref;
|
||||
if (selfRef == 0) {
|
||||
throwException(th, "java/lang/NullPointerException", rc.name);
|
||||
return dispatchAbnormalException(th);
|
||||
}
|
||||
if (!isSpecial) {
|
||||
const JObject* selfObj = getObj(selfRef);
|
||||
if (selfObj) lookupClass = &selfObj->className;
|
||||
}
|
||||
}
|
||||
|
||||
bool monomorphic = isStatic || isSpecial;
|
||||
if (!rc.icValid || (!monomorphic && rc.icClass != *lookupClass)) {
|
||||
JClass* targetCls = nullptr;
|
||||
const JMethodInfo* targetMethod = nullptr;
|
||||
resolveBytecodeMethod(*lookupClass, rc.name, rc.desc, &targetCls,
|
||||
&targetMethod);
|
||||
const NativeMethodFn* nat = nullptr;
|
||||
if (!targetMethod) {
|
||||
nat = findNativePtr(*lookupClass, rc.name, rc.desc);
|
||||
if (!nat && *lookupClass != rc.cls) {
|
||||
nat = findNativePtr(rc.cls, rc.name, rc.desc);
|
||||
}
|
||||
}
|
||||
rc.icClass = *lookupClass;
|
||||
rc.icTargetCls = targetMethod ? targetCls : nullptr;
|
||||
rc.icTargetMethod = targetMethod;
|
||||
rc.icNative = nat;
|
||||
rc.icValid = true;
|
||||
}
|
||||
|
||||
if (rc.icTargetCls && rc.icTargetMethod) {
|
||||
pushBytecodeFrame(th, rc.icTargetCls, rc.icTargetMethod, args);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (rc.icNative && *rc.icNative) {
|
||||
JVal retVal;
|
||||
if (!(*rc.icNative)(*this, th, args, &retVal)) {
|
||||
return dispatchAbnormalException(th);
|
||||
}
|
||||
if (rc.returnType != JType::Void && !th.frames.empty()) {
|
||||
th.frames.back().push(retVal);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fallback default no-op for unimplemented constructors or void methods
|
||||
if (rc.returnType != JType::Void && !th.frames.empty()) {
|
||||
switch (rc.returnType) {
|
||||
case JType::Long:
|
||||
th.frames.back().push(JVal::MakeLong(0));
|
||||
break;
|
||||
case JType::Float:
|
||||
th.frames.back().push(JVal::MakeFloat(0.0f));
|
||||
break;
|
||||
case JType::Double:
|
||||
th.frames.back().push(JVal::MakeDouble(0.0));
|
||||
break;
|
||||
case JType::Ref:
|
||||
th.frames.back().push(JVal::Null());
|
||||
break;
|
||||
default:
|
||||
th.frames.back().push(JVal::MakeInt(0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void pushCpConstant(JFrame& f, JClass* cls, uint16_t idx) {
|
||||
if (!cls || idx == 0 || idx >= cls->cp.size()) {
|
||||
f.push(JVal::MakeInt(0));
|
||||
@@ -2076,48 +2288,56 @@ class Jvm {
|
||||
case 0xb2: { // getstatic
|
||||
uint16_t idx = be16(code.data() + f.pc);
|
||||
f.pc += 2;
|
||||
std::string clsName, fldName, fldDesc;
|
||||
f.cls->cpRef(idx, &clsName, &fldName, &fldDesc);
|
||||
JVal v = getStaticFieldVal(clsName, fldName, fldDesc);
|
||||
CpRefCache& rc = f.cls->refAt(idx);
|
||||
if (!rc.staticSlot) {
|
||||
rc.staticSlot = resolveStaticSlot(rc.cls, rc.name, rc.desc);
|
||||
}
|
||||
JVal v = rc.staticSlot ? *rc.staticSlot
|
||||
: defaultValueForDesc(rc.descFirst);
|
||||
th.frames.back().push(v);
|
||||
break;
|
||||
}
|
||||
case 0xb3: { // putstatic
|
||||
uint16_t idx = be16(code.data() + f.pc);
|
||||
f.pc += 2;
|
||||
std::string clsName, fldName, fldDesc;
|
||||
f.cls->cpRef(idx, &clsName, &fldName, &fldDesc);
|
||||
CpRefCache& rc = f.cls->refAt(idx);
|
||||
JVal v = f.pop();
|
||||
setStaticFieldVal(clsName, fldName, v);
|
||||
if (!rc.staticSlot) {
|
||||
rc.staticSlot = resolveStaticSlot(rc.cls, rc.name, rc.desc);
|
||||
}
|
||||
if (rc.staticSlot) *rc.staticSlot = v;
|
||||
break;
|
||||
}
|
||||
case 0xb4: { // getfield
|
||||
uint16_t idx = be16(code.data() + f.pc);
|
||||
f.pc += 2;
|
||||
std::string clsName, fldName, fldDesc;
|
||||
f.cls->cpRef(idx, &clsName, &fldName, &fldDesc);
|
||||
CpRefCache& rc = f.cls->refAt(idx);
|
||||
uint32_t objRef = f.popRef();
|
||||
JObject* obj = getObj(objRef);
|
||||
if (!obj) {
|
||||
throwException(th, "java/lang/NullPointerException", fldName);
|
||||
throwException(th, "java/lang/NullPointerException", rc.name);
|
||||
return dispatchAbnormalException(th);
|
||||
}
|
||||
f.push(obj->getField(fldName, fldDesc.empty() ? 'I' : fldDesc[0]));
|
||||
auto it = obj->fields.find(rc.fieldKey);
|
||||
if (it != obj->fields.end()) {
|
||||
f.push(it->second);
|
||||
} else {
|
||||
f.push(obj->getField(rc.name, rc.descFirst));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0xb5: { // putfield
|
||||
uint16_t idx = be16(code.data() + f.pc);
|
||||
f.pc += 2;
|
||||
std::string clsName, fldName, fldDesc;
|
||||
f.cls->cpRef(idx, &clsName, &fldName, &fldDesc);
|
||||
CpRefCache& rc = f.cls->refAt(idx);
|
||||
JVal v = f.pop();
|
||||
uint32_t objRef = f.popRef();
|
||||
JObject* obj = getObj(objRef);
|
||||
if (!obj) {
|
||||
throwException(th, "java/lang/NullPointerException", fldName);
|
||||
throwException(th, "java/lang/NullPointerException", rc.name);
|
||||
return dispatchAbnormalException(th);
|
||||
}
|
||||
obj->setField(fldName, v);
|
||||
obj->fields[rc.fieldKey] = v;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -2128,10 +2348,8 @@ class Jvm {
|
||||
case 0xb9: { // invokeinterface
|
||||
uint16_t idx = be16(code.data() + f.pc);
|
||||
f.pc += (op == 0xb9) ? 4 : 2;
|
||||
std::string clsName, mName, mDesc;
|
||||
f.cls->cpRef(idx, &clsName, &mName, &mDesc);
|
||||
return invokeMethodFromFrame(th, clsName, mName, mDesc,
|
||||
op == 0xb8, op == 0xb7);
|
||||
CpRefCache& rc = f.cls->refAt(idx);
|
||||
return invokeMethodCached(th, rc, op == 0xb8, op == 0xb7);
|
||||
}
|
||||
|
||||
// Object & Array creation
|
||||
|
||||
@@ -1885,6 +1885,22 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
model = static_cast<M3gTransformData*>(trObj->nativeData.get())->mat;
|
||||
}
|
||||
ctx->m3g->renderVertexBuffer(v, surf, a[1].ref, a[2].ref, a[3].ref, model);
|
||||
} else if (firstObj->nativeData) {
|
||||
// render(Node node, Transform transform) or render(World world)
|
||||
Mat4 model = Mat4::Identity();
|
||||
JObject* trObj = v.getObj(a[2].ref);
|
||||
if (trObj && trObj->nativeData) {
|
||||
model = static_cast<M3gTransformData*>(trObj->nativeData.get())->mat;
|
||||
}
|
||||
if (firstObj->className == "javax/microedition/m3g/Mesh") {
|
||||
auto* md = static_cast<M3gMeshData*>(firstObj->nativeData.get());
|
||||
for (size_t i = 0; i < md->submeshRefs.size(); ++i) {
|
||||
uint32_t appRef =
|
||||
(i < md->appearanceRefs.size()) ? md->appearanceRefs[i] : 0;
|
||||
ctx->m3g->renderVertexBuffer(v, surf, md->vertexBufferRef,
|
||||
md->submeshRefs[i], appRef, model);
|
||||
}
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
@@ -1892,6 +1908,11 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
vm.registerWildcardNative("javax/microedition/m3g/Graphics3D", "addLight",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::FromInt(0); });
|
||||
vm.registerWildcardNative("javax/microedition/m3g/Graphics3D", "getProperties",
|
||||
[](Jvm& v, JThread&, JValue*) -> JValue {
|
||||
return JValue::FromRef(
|
||||
v.allocObject("java/util/Hashtable"));
|
||||
});
|
||||
|
||||
// VertexArray
|
||||
vm.registerWildcardNative(
|
||||
@@ -1982,6 +2003,26 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/VertexBuffer", "setTexCoords",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (o) {
|
||||
if (!o->nativeData) o->nativeData = std::make_shared<M3gVertexBufferData>();
|
||||
auto* vb = static_cast<M3gVertexBufferData*>(o->nativeData.get());
|
||||
vb->texCoordsRef = a[2].ref;
|
||||
vb->texScale = (a[3].f != 0.0f) ? a[3].f : 1.0f;
|
||||
JObject* biasArr = v.getObj(a[4].ref);
|
||||
if (biasArr && biasArr->floatArr.size() >= 2) {
|
||||
vb->texBias[0] = biasArr->floatArr[0];
|
||||
vb->texBias[1] = biasArr->floatArr[1];
|
||||
if (biasArr->floatArr.size() >= 3) {
|
||||
vb->texBias[2] = biasArr->floatArr[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/VertexBuffer", "setDefaultColor",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
@@ -2059,7 +2100,14 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
"javax/microedition/m3g/Transform", "<init>",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (o) o->nativeData = std::make_shared<M3gTransformData>();
|
||||
if (o) {
|
||||
auto td = std::make_shared<M3gTransformData>();
|
||||
JObject* src = v.getObj(a[1].ref);
|
||||
if (src && src->nativeData) {
|
||||
td->mat = static_cast<M3gTransformData*>(src->nativeData.get())->mat;
|
||||
}
|
||||
o->nativeData = td;
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
@@ -2072,6 +2120,42 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Transform", "set",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
JObject* arg = v.getObj(a[1].ref);
|
||||
if (!o || !arg) return JValue::Null();
|
||||
if (!o->nativeData) o->nativeData = std::make_shared<M3gTransformData>();
|
||||
auto* tr = static_cast<M3gTransformData*>(o->nativeData.get());
|
||||
if (arg->arrayKind == ArrayKind::Float && arg->floatArr.size() >= 16) {
|
||||
// M3G float[16] is row-major; convert to column-major Mat4
|
||||
for (int row = 0; row < 4; ++row) {
|
||||
for (int col = 0; col < 4; ++col) {
|
||||
tr->mat.m[col * 4 + row] = arg->floatArr[row * 4 + col];
|
||||
}
|
||||
}
|
||||
} else if (arg->nativeData) {
|
||||
tr->mat = static_cast<M3gTransformData*>(arg->nativeData.get())->mat;
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Transform", "get",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
JObject* arg = v.getObj(a[1].ref);
|
||||
if (o && o->nativeData && arg && arg->arrayKind == ArrayKind::Float &&
|
||||
arg->floatArr.size() >= 16) {
|
||||
auto* tr = static_cast<M3gTransformData*>(o->nativeData.get());
|
||||
for (int row = 0; row < 4; ++row) {
|
||||
for (int col = 0; col < 4; ++col) {
|
||||
arg->floatArr[row * 4 + col] = tr->mat.m[col * 4 + row];
|
||||
}
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Transform", "postTranslate",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
@@ -2105,6 +2189,22 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Transform", "postMultiply",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
JObject* other = v.getObj(a[1].ref);
|
||||
if (o && other && other->nativeData) {
|
||||
if (!o->nativeData) o->nativeData = std::make_shared<M3gTransformData>();
|
||||
auto* tr = static_cast<M3gTransformData*>(o->nativeData.get());
|
||||
auto* otr = static_cast<M3gTransformData*>(other->nativeData.get());
|
||||
tr->mat = Mat4::Multiply(tr->mat, otr->mat);
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Transform", "invert",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
|
||||
// Camera
|
||||
vm.registerWildcardNative(
|
||||
@@ -2126,7 +2226,7 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
return JValue::Null();
|
||||
});
|
||||
|
||||
// Background & Appearance
|
||||
// Background, Mesh, Image2D, Texture2D, Appearance, PolygonMode, CompositingMode, Material
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Background", "<init>",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
@@ -2146,17 +2246,155 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Appearance", "<init>",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
"javax/microedition/m3g/Background", "setColorClearEnable",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (o) {
|
||||
if (!o->nativeData) o->nativeData = std::make_shared<M3gBackgroundData>();
|
||||
static_cast<M3gBackgroundData*>(o->nativeData.get())->colorClearEnabled =
|
||||
(a[1].i != 0);
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/PolygonMode", "<init>",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
"javax/microedition/m3g/Background", "setDepthClearEnable",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (o) {
|
||||
if (!o->nativeData) o->nativeData = std::make_shared<M3gBackgroundData>();
|
||||
static_cast<M3gBackgroundData*>(o->nativeData.get())->depthClearEnabled =
|
||||
(a[1].i != 0);
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/CompositingMode", "<init>",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
"javax/microedition/m3g/Mesh", "<init>",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (!o) return JValue::Null();
|
||||
auto md = std::make_shared<M3gMeshData>();
|
||||
md->vertexBufferRef = a[1].ref;
|
||||
JObject* subArg = v.getObj(a[2].ref);
|
||||
JObject* appArg = v.getObj(a[3].ref);
|
||||
if (subArg && subArg->arrayKind == ArrayKind::Ref) {
|
||||
md->submeshRefs = subArg->refArr;
|
||||
} else if (a[2].ref != 0) {
|
||||
md->submeshRefs.push_back(a[2].ref);
|
||||
}
|
||||
if (appArg && appArg->arrayKind == ArrayKind::Ref) {
|
||||
md->appearanceRefs = appArg->refArr;
|
||||
} else if (a[3].ref != 0) {
|
||||
md->appearanceRefs.push_back(a[3].ref);
|
||||
}
|
||||
o->nativeData = md;
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Material", "<init>",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
"javax/microedition/m3g/Mesh", "getVertexBuffer",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (o && o->nativeData) {
|
||||
return JValue::FromRef(
|
||||
static_cast<M3gMeshData*>(o->nativeData.get())->vertexBufferRef);
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Mesh", "getIndexBuffer",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (o && o->nativeData) {
|
||||
auto* md = static_cast<M3gMeshData*>(o->nativeData.get());
|
||||
if (a[1].i >= 0 &&
|
||||
static_cast<size_t>(a[1].i) < md->submeshRefs.size()) {
|
||||
return JValue::FromRef(md->submeshRefs[a[1].i]);
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Mesh", "getAppearance",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (o && o->nativeData) {
|
||||
auto* md = static_cast<M3gMeshData*>(o->nativeData.get());
|
||||
if (a[1].i >= 0 &&
|
||||
static_cast<size_t>(a[1].i) < md->appearanceRefs.size()) {
|
||||
return JValue::FromRef(md->appearanceRefs[a[1].i]);
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Mesh", "setAppearance",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
JObject* o = v.getObj(a[0].ref);
|
||||
if (o && o->nativeData) {
|
||||
auto* md = static_cast<M3gMeshData*>(o->nativeData.get());
|
||||
int idx = a[1].i;
|
||||
if (idx >= 0) {
|
||||
if (static_cast<size_t>(idx) >= md->appearanceRefs.size()) {
|
||||
md->appearanceRefs.resize(idx + 1, 0);
|
||||
}
|
||||
md->appearanceRefs[idx] = a[2].ref;
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
|
||||
for (const char* m3gCls :
|
||||
{"javax/microedition/m3g/Object3D",
|
||||
"javax/microedition/m3g/Transformable",
|
||||
"javax/microedition/m3g/Node",
|
||||
"javax/microedition/m3g/Group",
|
||||
"javax/microedition/m3g/World",
|
||||
"javax/microedition/m3g/Light",
|
||||
"javax/microedition/m3g/Image2D",
|
||||
"javax/microedition/m3g/Texture2D",
|
||||
"javax/microedition/m3g/Appearance",
|
||||
"javax/microedition/m3g/PolygonMode",
|
||||
"javax/microedition/m3g/CompositingMode",
|
||||
"javax/microedition/m3g/Material",
|
||||
"javax/microedition/m3g/Fog"}) {
|
||||
vm.registerWildcardNative(
|
||||
m3gCls, "<init>",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
for (const char* noOpMethod :
|
||||
{"set", "setBlending", "setFiltering", "setWrapping", "setTexture",
|
||||
"setPolygonMode", "setCompositingMode", "setMaterial", "setFog",
|
||||
"setCulling", "setShading", "setWinding",
|
||||
"setPerspectiveCorrectionEnable", "setTwoSidedLightingEnable",
|
||||
"setLocalCameraLightingEnable", "setAlphaThreshold",
|
||||
"setAlphaWriteEnable", "setColorWriteEnable", "setDepthTestEnable",
|
||||
"setDepthWriteEnable", "setDepthOffset", "setColor",
|
||||
"setVertexColorTrackingEnable", "setTranslation", "setOrientation",
|
||||
"setScale", "setTransform", "setRenderingEnable", "setPickingEnable",
|
||||
"setScope", "addChild", "removeChild", "setActiveCamera",
|
||||
"setBackground", "setIntensity", "setMode", "setAttenuation",
|
||||
"setSpotAngle", "setSpotExponent", "setUserID", "setUserObject"}) {
|
||||
vm.registerWildcardNative(
|
||||
m3gCls, noOpMethod,
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
}
|
||||
vm.registerWildcardNative(
|
||||
m3gCls, "animate",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::FromInt(0); });
|
||||
vm.registerWildcardNative(
|
||||
m3gCls, "find",
|
||||
[](Jvm&, JThread&, JValue* a) -> JValue {
|
||||
return JValue::FromRef(a[0].ref);
|
||||
});
|
||||
}
|
||||
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Loader", "load",
|
||||
[](Jvm& v, JThread&, JValue*) -> JValue {
|
||||
uint32_t arr = v.allocRefArray("javax/microedition/m3g/Object3D", 1);
|
||||
uint32_t wref = v.allocObject("javax/microedition/m3g/World");
|
||||
v.getObj(arr)->refArr[0] = wref;
|
||||
return JValue::FromRef(arr);
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -147,6 +147,13 @@ struct M3gBackgroundData {
|
||||
bool depthClearEnabled = true;
|
||||
};
|
||||
|
||||
struct M3gMeshData {
|
||||
uint32_t vertexBufferRef = 0;
|
||||
std::vector<uint32_t> submeshRefs;
|
||||
std::vector<uint32_t> appearanceRefs;
|
||||
std::vector<uint32_t> childrenRefs;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Adreno 304 (FD304) EGL + GLES2 Offscreen 3D Renderer (with SW Fallback)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -201,8 +201,22 @@ class RemedyEmulator {
|
||||
bool leftSoftDown = false;
|
||||
bool rightSoftDown = false;
|
||||
bool chordTriggered = false;
|
||||
bool rightSoftLongTriggered = false;
|
||||
int pendingSoftkey = 0; // 1=left, 2=right
|
||||
int64_t pendingSoftkeyDeadlineMs = 0;
|
||||
int64_t rightSoftPressMs = 0;
|
||||
|
||||
// Minimum 80ms / 2-tick hold for Canvas key presses so polling game loops
|
||||
// (e.g. 58ms sleep in Castlevania / Tomb Raider) never miss brisk key taps
|
||||
struct ActiveCanvasKey {
|
||||
uint32_t dispRef = 0;
|
||||
int mask = 0;
|
||||
int64_t pressedAtMs = 0;
|
||||
uint64_t pressedAtTick = 0;
|
||||
bool releaseQueued = false;
|
||||
};
|
||||
std::unordered_map<int, ActiveCanvasKey> activeCanvasKeys;
|
||||
uint64_t midletTickCount = 0;
|
||||
|
||||
// FPS counter
|
||||
int frameCounter = 0;
|
||||
@@ -285,6 +299,7 @@ class RemedyEmulator {
|
||||
}
|
||||
|
||||
void openXdrawerAndRestoreGrab() {
|
||||
cldcRuntime.sound.setPaused(true);
|
||||
ungrabKeyboard();
|
||||
std::system("dwm-plugin xdrawer >/dev/null 2>&1");
|
||||
usleep(120000);
|
||||
@@ -298,6 +313,8 @@ class RemedyEmulator {
|
||||
XRaiseWindow(neoWin.dpy, neoWin.win);
|
||||
XSetInputFocus(neoWin.dpy, neoWin.win, RevertToParent, CurrentTime);
|
||||
grabKeyboard();
|
||||
cldcRuntime.sound.refreshSystemVolume(true);
|
||||
cldcRuntime.sound.setPaused(mode != EmuUiMode::RUNNING);
|
||||
neoWin.checkReloadTheme();
|
||||
appCtx.repaintPending = true;
|
||||
}
|
||||
@@ -404,6 +421,9 @@ class RemedyEmulator {
|
||||
}
|
||||
|
||||
bool loadMidletSuite(const std::string& path) {
|
||||
cldcRuntime.sound.stopAll();
|
||||
activeCanvasKeys.clear();
|
||||
midletTickCount = 0;
|
||||
std::string errMsg;
|
||||
if (!loadSuiteFromJarOrJad(path, &jar, &suite, &errMsg)) {
|
||||
statusBanner = errMsg.empty() ? "Failed to open JAR/JAD" : errMsg;
|
||||
@@ -448,7 +468,8 @@ class RemedyEmulator {
|
||||
auto [initCls, initM] =
|
||||
vm->resolveMethod(mainClass, "<init>", "()V", false);
|
||||
if (initM && !initM->code.empty()) {
|
||||
vm->invokeManagedSync(mainTh, initCls, initM, {JValue::FromRef(midletRef)});
|
||||
vm->invokeManagedSync(mainTh, initCls, initM,
|
||||
{JValue::FromRef(midletRef)}, 5000000);
|
||||
}
|
||||
mainTh.pendingExceptionRef = 0;
|
||||
mainTh.frames.clear();
|
||||
@@ -456,7 +477,7 @@ class RemedyEmulator {
|
||||
vm->resolveMethod(mainClass, "startApp", "()V", false);
|
||||
if (startM && !startM->code.empty()) {
|
||||
vm->invokeManagedSync(mainTh, startCls, startM,
|
||||
{JValue::FromRef(midletRef)});
|
||||
{JValue::FromRef(midletRef)}, 25000000);
|
||||
}
|
||||
|
||||
currentJarOrJadPath = path;
|
||||
@@ -504,13 +525,49 @@ class RemedyEmulator {
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Step timers and background Java threads (game loops, socket/HTTP threads)
|
||||
// 3. Step timers and background Java threads (game loops, socket/HTTP threads).
|
||||
// Use a wall-clock budget (~60% of the frame interval) instead of a fixed
|
||||
// instruction count so CPU-heavy loaders (level/asset unpacking in games
|
||||
// like Castlevania) run at full interpreter speed, while idle games that
|
||||
// sleep in Thread.sleep() / wait() still return to poll() immediately.
|
||||
cldcRuntime.tickTimersAndSerialCallbacks(*vm);
|
||||
vm->stepThreads(15000);
|
||||
{
|
||||
int64_t frameMs = 1000 / std::max(1, cfg.fpsLimit);
|
||||
int64_t budgetMs = std::max<int64_t>(6, (frameMs * 6) / 10);
|
||||
int64_t deadline = Jvm::NowMs() + budgetMs;
|
||||
int slices = 0;
|
||||
do {
|
||||
vm->stepThreads(20000);
|
||||
++slices;
|
||||
} while (slices < 64 && !vm->exitRequested && !appCtx.midletDestroyed &&
|
||||
vm->anyThreadReady() && Jvm::NowMs() < deadline);
|
||||
}
|
||||
midletTickCount++;
|
||||
|
||||
if (!activeCanvasKeys.empty()) {
|
||||
int64_t nowMs = Jvm::NowMs();
|
||||
for (auto it = activeCanvasKeys.begin(); it != activeCanvasKeys.end();) {
|
||||
if (it->second.releaseQueued &&
|
||||
(nowMs - it->second.pressedAtMs >= 80) &&
|
||||
(midletTickCount - it->second.pressedAtTick >= 2)) {
|
||||
uint32_t dRef = it->second.dispRef;
|
||||
int mask = it->second.mask;
|
||||
int kc = it->first;
|
||||
it = activeCanvasKeys.erase(it);
|
||||
if (mask != 0) appCtx.latchedKeyStates &= ~mask;
|
||||
sendCanvasKeyEvent(dRef, "keyReleased", kc);
|
||||
appCtx.repaintPending = true;
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (appCtx.midletDestroyed || vm->exitRequested) {
|
||||
cldcRuntime.sound.stopAll();
|
||||
hasLoadedMidlet = false;
|
||||
mode = EmuUiMode::BROWSER;
|
||||
renderBrowserMenu();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -695,8 +752,18 @@ class RemedyEmulator {
|
||||
" FPS >");
|
||||
items.push_back(std::string("FPS Overlay: ") +
|
||||
(cfg.showFps ? "< Shown >" : "< Hidden >"));
|
||||
items.push_back(std::string("Audio (MMAPI): ") +
|
||||
(cfg.audioEnabled ? "< ON >" : "< Muted >"));
|
||||
// "Audio" shows the system speaker volume (shared with xdrawer and every
|
||||
// other app); Left/Right change it system-wide, OK toggles ReMEdy sound.
|
||||
int sysVol = cldcRuntime.sound.volumePct.load();
|
||||
std::string volLabel;
|
||||
if (!cfg.audioEnabled) {
|
||||
volLabel = "< Off >";
|
||||
} else if (cldcRuntime.sound.isSystemMuted()) {
|
||||
volLabel = "< Speaker muted >";
|
||||
} else {
|
||||
volLabel = "< " + std::to_string(sysVol) + "% >";
|
||||
}
|
||||
items.push_back(std::string("Audio (MMAPI): ") + volLabel);
|
||||
items.push_back("Keypad Controls...");
|
||||
if (hasLoadedMidlet) {
|
||||
items.push_back("Restart MIDlet");
|
||||
@@ -899,7 +966,8 @@ class RemedyEmulator {
|
||||
}
|
||||
// Forward -6 to Canvas.keyPressed(-6) if fullScreen Canvas has no commands
|
||||
if (isCanvasDisplayable(*vm, dispRef)) {
|
||||
sendCanvasKeyEvent(dispRef, "keyPressed", -6);
|
||||
pressCanvasKey(dispRef, -6, 0);
|
||||
if (!leftSoftDown) releaseCanvasKey(-6);
|
||||
}
|
||||
} else if (softkeyIdx == 2) {
|
||||
if (rightCmd != 0) {
|
||||
@@ -908,8 +976,15 @@ class RemedyEmulator {
|
||||
appCtx.repaintPending = true;
|
||||
return;
|
||||
}
|
||||
// If MIDlet registered commands only on Left Softkey or none at all,
|
||||
// Right Softkey opens the ReMEdy Pause Menu (or forwards -7 if Canvas handles it)
|
||||
// If active Displayable is a Canvas with no LCDUI commands attached,
|
||||
// forward Right Softkey (-7) directly to Canvas.keyPressed(-7) so full-screen
|
||||
// games (Castlevania, Tomb Raider, AstroBlaster, etc.) receive Right Softkey.
|
||||
// Holding Right Softkey >= 500ms or pressing End/Power (Escape) opens Pause Menu.
|
||||
if (isCanvasDisplayable(*vm, dispRef) && st->commands.empty()) {
|
||||
pressCanvasKey(dispRef, -7, 0);
|
||||
if (!rightSoftDown) releaseCanvasKey(-7);
|
||||
return;
|
||||
}
|
||||
mode = EmuUiMode::PAUSED;
|
||||
renderPauseMenu();
|
||||
}
|
||||
@@ -929,6 +1004,47 @@ class RemedyEmulator {
|
||||
}
|
||||
}
|
||||
|
||||
void pressCanvasKey(uint32_t dispRef, int kc, int mask) {
|
||||
if (!vm || dispRef == 0 || kc == 0) return;
|
||||
auto existing = activeCanvasKeys.find(kc);
|
||||
if (existing != activeCanvasKeys.end()) {
|
||||
if (existing->second.releaseQueued) {
|
||||
if (existing->second.mask != 0) {
|
||||
appCtx.latchedKeyStates &= ~existing->second.mask;
|
||||
}
|
||||
sendCanvasKeyEvent(existing->second.dispRef, "keyReleased", kc);
|
||||
}
|
||||
activeCanvasKeys.erase(existing);
|
||||
}
|
||||
ActiveCanvasKey ak;
|
||||
ak.dispRef = dispRef;
|
||||
ak.mask = mask;
|
||||
ak.pressedAtMs = Jvm::NowMs();
|
||||
ak.pressedAtTick = midletTickCount;
|
||||
ak.releaseQueued = false;
|
||||
activeCanvasKeys[kc] = ak;
|
||||
if (mask != 0) appCtx.latchedKeyStates |= mask;
|
||||
sendCanvasKeyEvent(dispRef, "keyPressed", kc);
|
||||
appCtx.repaintPending = true;
|
||||
}
|
||||
|
||||
void releaseCanvasKey(int kc) {
|
||||
auto it = activeCanvasKeys.find(kc);
|
||||
if (it == activeCanvasKeys.end()) return;
|
||||
int64_t nowMs = Jvm::NowMs();
|
||||
if ((nowMs - it->second.pressedAtMs >= 80) &&
|
||||
(midletTickCount - it->second.pressedAtTick >= 2)) {
|
||||
uint32_t dRef = it->second.dispRef;
|
||||
int mask = it->second.mask;
|
||||
activeCanvasKeys.erase(it);
|
||||
if (mask != 0) appCtx.latchedKeyStates &= ~mask;
|
||||
sendCanvasKeyEvent(dRef, "keyReleased", kc);
|
||||
appCtx.repaintPending = true;
|
||||
} else {
|
||||
it->second.releaseQueued = true;
|
||||
}
|
||||
}
|
||||
|
||||
void handleHighLevelKeyPress(uint32_t dispRef, KeySym ks) {
|
||||
JObject* dispObj = vm->getObj(dispRef);
|
||||
LcduiScreenState* st = ensureScreenState(*vm, dispRef);
|
||||
@@ -1106,6 +1222,15 @@ class RemedyEmulator {
|
||||
ks = XK_Escape;
|
||||
}
|
||||
|
||||
if (ks == XF86XK_AudioLowerVolume || ks == XF86XK_AudioRaiseVolume) {
|
||||
int step = (ks == XF86XK_AudioRaiseVolume) ? 5 : -5;
|
||||
int cur = cldcRuntime.sound.volumePct.load();
|
||||
int newVol = std::clamp(((cur + step) / 5) * 5, 0, 100);
|
||||
cldcRuntime.sound.setSystemVolume(newVol);
|
||||
if (mode == EmuUiMode::PAUSED) renderPauseMenu();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Softkey chord detection (both softkeys pressed within 45 ms -> xdrawer)
|
||||
bool isLeftSoft = (ks == XK_F1 || ks == XK_Super_L);
|
||||
bool isRightSoft = (ks == XK_F2 || (ks == XK_BackSpace && mode != EmuUiMode::RUNNING));
|
||||
@@ -1132,7 +1257,11 @@ class RemedyEmulator {
|
||||
}
|
||||
|
||||
if (isLeftSoft) leftSoftDown = true;
|
||||
if (isRightSoft) rightSoftDown = true;
|
||||
if (isRightSoft) {
|
||||
rightSoftDown = true;
|
||||
rightSoftPressMs = Jvm::NowMs();
|
||||
rightSoftLongTriggered = false;
|
||||
}
|
||||
if (leftSoftDown && rightSoftDown) {
|
||||
chordTriggered = true;
|
||||
pendingSoftkey = 0;
|
||||
@@ -1158,18 +1287,26 @@ class RemedyEmulator {
|
||||
ks == _EVDEVK(0x247)) {
|
||||
ks = XK_F2;
|
||||
}
|
||||
if (ks == XK_F1 || ks == XK_Super_L) leftSoftDown = false;
|
||||
if (ks == XK_F2 || ks == XK_BackSpace) rightSoftDown = false;
|
||||
bool wasLeftSoft = (ks == XK_F1 || ks == XK_Super_L);
|
||||
bool wasRightSoft = (ks == XK_F2 || ks == XK_BackSpace);
|
||||
if (wasLeftSoft) leftSoftDown = false;
|
||||
if (wasRightSoft) rightSoftDown = false;
|
||||
if (!leftSoftDown && !rightSoftDown) chordTriggered = false;
|
||||
|
||||
if (mode == EmuUiMode::RUNNING && vm && appCtx.currentDisplayableRef != 0 &&
|
||||
isCanvasDisplayable(*vm, appCtx.currentDisplayableRef)) {
|
||||
if (wasLeftSoft) {
|
||||
releaseCanvasKey(-6);
|
||||
return;
|
||||
}
|
||||
if (wasRightSoft) {
|
||||
releaseCanvasKey(-7);
|
||||
return;
|
||||
}
|
||||
int mask = 0;
|
||||
int kc = mapKeysymToMidpKeyCode(ks, &mask);
|
||||
if (mask != 0) appCtx.latchedKeyStates &= ~mask;
|
||||
if (kc != 0 && !appCtx.commandMenuOpen) {
|
||||
sendCanvasKeyEvent(appCtx.currentDisplayableRef, "keyReleased", kc);
|
||||
appCtx.repaintPending = true;
|
||||
releaseCanvasKey(kc);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1293,9 +1430,16 @@ class RemedyEmulator {
|
||||
cfg.showFps = !cfg.showFps;
|
||||
cfg.save();
|
||||
} else if (choice.rfind("Audio (MMAPI):", 0) == 0) {
|
||||
cfg.audioEnabled = !cfg.audioEnabled;
|
||||
cldcRuntime.sound.enabled = cfg.audioEnabled;
|
||||
cfg.save();
|
||||
if (ks == XK_Left || ks == XK_Right) {
|
||||
int cur = cldcRuntime.sound.volumePct.load();
|
||||
int step = (ks == XK_Left) ? -5 : 5;
|
||||
cldcRuntime.sound.setSystemVolume(
|
||||
std::clamp(((cur + step) / 5) * 5, 0, 100));
|
||||
} else {
|
||||
cfg.audioEnabled = !cfg.audioEnabled;
|
||||
cldcRuntime.sound.enabled = cfg.audioEnabled;
|
||||
cfg.save();
|
||||
}
|
||||
} else if (choice == "Keypad Controls...") {
|
||||
controlsSel = 0;
|
||||
controlsScroll = 0;
|
||||
@@ -1401,10 +1545,8 @@ class RemedyEmulator {
|
||||
|
||||
int mask = 0;
|
||||
int kc = mapKeysymToMidpKeyCode(ks, &mask);
|
||||
if (mask != 0) appCtx.latchedKeyStates |= mask;
|
||||
if (kc != 0) {
|
||||
sendCanvasKeyEvent(dispRef, "keyPressed", kc);
|
||||
appCtx.repaintPending = true;
|
||||
pressCanvasKey(dispRef, kc, mask);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -1429,6 +1571,7 @@ class RemedyEmulator {
|
||||
bool running = true;
|
||||
while (running) {
|
||||
updateTimer(cfg.fpsLimit);
|
||||
cldcRuntime.sound.setPaused(mode != EmuUiMode::RUNNING);
|
||||
while (XPending(neoWin.dpy) > 0) {
|
||||
XEvent ev;
|
||||
XNextEvent(neoWin.dpy, &ev);
|
||||
@@ -1475,6 +1618,18 @@ class RemedyEmulator {
|
||||
}
|
||||
if (!running) break;
|
||||
|
||||
if (mode == EmuUiMode::RUNNING && rightSoftDown && !leftSoftDown &&
|
||||
!chordTriggered && !rightSoftLongTriggered &&
|
||||
Jvm::NowMs() - rightSoftPressMs >= 500) {
|
||||
rightSoftLongTriggered = true;
|
||||
pendingSoftkey = 0;
|
||||
releaseCanvasKey(-7);
|
||||
pauseSel = 0;
|
||||
pauseScroll = 0;
|
||||
mode = EmuUiMode::PAUSED;
|
||||
renderPauseMenu();
|
||||
}
|
||||
|
||||
if (pendingSoftkey != 0 && Jvm::NowMs() >= pendingSoftkeyDeadlineMs) {
|
||||
int s = pendingSoftkey;
|
||||
pendingSoftkey = 0;
|
||||
@@ -1488,7 +1643,7 @@ class RemedyEmulator {
|
||||
pfds[0].events = POLLIN;
|
||||
pfds[1].fd = tfd;
|
||||
pfds[1].events = POLLIN;
|
||||
int timeoutMs = (pendingSoftkey != 0) ? 10 : 50;
|
||||
int timeoutMs = (pendingSoftkey != 0 || rightSoftDown) ? 10 : 50;
|
||||
int pr = poll(pfds, (tfd >= 0) ? 2 : 1, timeoutMs);
|
||||
if (pr > 0 && tfd >= 0 && (pfds[1].revents & POLLIN)) {
|
||||
uint64_t exp = 0;
|
||||
|
||||
Reference in new issue
Block a user