Add Bluetooth audio/HID/discoverability/alias support, fix SD card hotplug & caller ID matching, and expand ReMEdy J2ME JSRs + 64MB GC
This commit is contained in:
1 parent
456d3fbc42
commit
471916424e
51 files changed
+2961
-305
No files matched your search
@@ -1007,8 +1007,24 @@ class PcmAudioOut {
|
||||
private:
|
||||
void audioThreadMain() {
|
||||
snd_pcm_t* h = nullptr;
|
||||
const char* devs[] = {"hw:0,0", "default", "plughw:0,0"};
|
||||
for (const char* dev : devs) {
|
||||
bool preferDefault = (::access("/run/bluetoothd.pid", F_OK) == 0);
|
||||
if (!preferDefault) {
|
||||
FILE* af = std::fopen("/tmp/sxmo_audio_out_device", "r");
|
||||
if (af) {
|
||||
char abuf[64] = {};
|
||||
if (std::fgets(abuf, sizeof(abuf), af)) {
|
||||
if (std::strstr(abuf, "Bluetooth") || std::strstr(abuf, "bluetooth")) {
|
||||
preferDefault = true;
|
||||
}
|
||||
}
|
||||
std::fclose(af);
|
||||
}
|
||||
}
|
||||
const char* devsHwFirst[] = {"hw:0,0", "default", "plughw:0,0"};
|
||||
const char* devsDefFirst[] = {"default", "hw:0,0", "plughw:0,0"};
|
||||
const char** devs = preferDefault ? devsDefFirst : devsHwFirst;
|
||||
for (int di = 0; di < 3; ++di) {
|
||||
const char* dev = devs[di];
|
||||
if (snd_pcm_open(&h, dev, SND_PCM_STREAM_PLAYBACK, 0) >= 0) {
|
||||
if (configureLowLatency(h)) {
|
||||
break;
|
||||
@@ -1018,7 +1034,8 @@ class PcmAudioOut {
|
||||
}
|
||||
}
|
||||
if (!h) {
|
||||
for (const char* dev : devs) {
|
||||
for (int di = 0; di < 3; ++di) {
|
||||
const char* dev = devs[di];
|
||||
if (snd_pcm_open(&h, dev, SND_PCM_STREAM_PLAYBACK, 0) >= 0) {
|
||||
if (snd_pcm_set_params(h, SND_PCM_FORMAT_S16_LE,
|
||||
SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000, 1,
|
||||
@@ -2253,6 +2270,10 @@ int main(int argc, char** argv) {
|
||||
usedByAction = true;
|
||||
}
|
||||
}
|
||||
if (!usedByAction && tok == "OK") {
|
||||
actionDown[ACT_A] = isPress;
|
||||
usedByAction = true;
|
||||
}
|
||||
if (isPress && !usedByAction) {
|
||||
if (tok == "1") {
|
||||
cfg.scaleMode = (cfg.scaleMode + 1) % 4;
|
||||
|
||||
@@ -333,6 +333,7 @@ class AlsaSoundEngine {
|
||||
int playerId = -1;
|
||||
bool active = false;
|
||||
bool loop = false;
|
||||
size_t rawSize = 0;
|
||||
float gain = 0.8f;
|
||||
std::vector<MidiEvent> events;
|
||||
size_t eventIdx = 0;
|
||||
@@ -777,10 +778,20 @@ class AlsaSoundEngine {
|
||||
auto it = playerGains_.find(playerId);
|
||||
if (it != playerGains_.end()) g = it->second;
|
||||
|
||||
if (midiSeq_.active && midiSeq_.playerId == playerId &&
|
||||
midiSeq_.rawSize == mid.size() && midiSeq_.currentFrame > 0 &&
|
||||
midiSeq_.currentFrame < midiSeq_.totalFrames) {
|
||||
midiSeq_.loop = loop;
|
||||
midiSeq_.gain = g;
|
||||
lastActiveMs_ = nowMs();
|
||||
return;
|
||||
}
|
||||
|
||||
midiSeq_ = MidiSequence{};
|
||||
midiSeq_.playerId = playerId;
|
||||
midiSeq_.active = true;
|
||||
midiSeq_.loop = loop;
|
||||
midiSeq_.rawSize = mid.size();
|
||||
midiSeq_.gain = g;
|
||||
midiSeq_.events = std::move(frameEvents);
|
||||
midiSeq_.totalFrames =
|
||||
@@ -1132,12 +1143,32 @@ class AlsaSoundEngine {
|
||||
snd_pcm_close(pcm);
|
||||
pcm = nullptr;
|
||||
}
|
||||
if (!enabled || sysMuted) {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
voices_.clear();
|
||||
for (auto it = pcmStreams_.begin(); it != pcmStreams_.end();) {
|
||||
if (!it->loop) {
|
||||
it = pcmStreams_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
if (midiSeq_.active && !midiSeq_.loop) {
|
||||
midiSeq_.active = false;
|
||||
midiVoices_.clear();
|
||||
}
|
||||
}
|
||||
::usleep(10000);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!pcm) {
|
||||
for (const char* dev : {"hw:0,0", "default", "plughw:0,0"}) {
|
||||
bool preferDefault = (::access("/run/bluetoothd.pid", F_OK) == 0);
|
||||
const char* devsHwFirst[] = {"hw:0,0", "default", "plughw:0,0"};
|
||||
const char* devsDefFirst[] = {"default", "hw:0,0", "plughw:0,0"};
|
||||
const char** devs = preferDefault ? devsDefFirst : devsHwFirst;
|
||||
for (int di = 0; di < 3; ++di) {
|
||||
const char* dev = devs[di];
|
||||
if (snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0) == 0) {
|
||||
if (!configureLowLatency(pcm, kSampleRate, kPeriod, kBuffer)) {
|
||||
snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE,
|
||||
@@ -1577,13 +1608,29 @@ class CldcNetMediaRuntime {
|
||||
});
|
||||
|
||||
// java/lang/System & Runtime
|
||||
uint32_t sysOutRef = vm.allocObject("java/io/PrintStream");
|
||||
uint32_t sysErrRef = vm.allocObject("java/io/PrintStream");
|
||||
vm.setStaticFieldVal("java/lang/System", "out", "Ljava/io/PrintStream;",
|
||||
JVal::MakeRef(sysOutRef));
|
||||
vm.setStaticFieldVal("java/lang/System", "err", "Ljava/io/PrintStream;",
|
||||
JVal::MakeRef(sysErrRef));
|
||||
uint32_t boolTrueRef = vm.allocObject("java/lang/Boolean");
|
||||
vm.getObj(boolTrueRef)->setField("value", JVal::MakeInt(1));
|
||||
uint32_t boolFalseRef = vm.allocObject("java/lang/Boolean");
|
||||
vm.getObj(boolFalseRef)->setField("value", JVal::MakeInt(0));
|
||||
vm.setStaticFieldVal("java/lang/Boolean", "TRUE", "Ljava/lang/Boolean;",
|
||||
JVal::MakeRef(boolTrueRef));
|
||||
vm.setStaticFieldVal("java/lang/Boolean", "FALSE", "Ljava/lang/Boolean;",
|
||||
JVal::MakeRef(boolFalseRef));
|
||||
|
||||
vm.registerNative("java/lang/System", "currentTimeMillis", "()J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(wallTimeMs());
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/System", "gc", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal*) {
|
||||
v.collectGarbage();
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/System", "exit", "(I)V",
|
||||
@@ -1653,6 +1700,7 @@ class CldcNetMediaRuntime {
|
||||
else if (key == "microedition.locale") val = "en-US";
|
||||
else if (key == "microedition.m3g.version") val = "1.1";
|
||||
else if (key == "microedition.io.file.FileConnection.version") val = "1.0";
|
||||
else if (key == "bluetooth.api.version") val = "1.1";
|
||||
else if (key == "file.separator") val = "/";
|
||||
else if (key == "fileconn.dir.photos") val = "file:///home/user/Pictures/";
|
||||
else if (key == "fileconn.dir.music") val = "file:///home/user/Music/";
|
||||
@@ -1674,17 +1722,18 @@ class CldcNetMediaRuntime {
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Runtime", "freeMemory", "()J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(16 * 1024 * 1024LL);
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(v.freeHeapBytes());
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Runtime", "totalMemory", "()J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(32 * 1024 * 1024LL);
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(v.totalHeapBytes());
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Runtime", "gc", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) {
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal*) {
|
||||
v.collectGarbage();
|
||||
return true;
|
||||
});
|
||||
|
||||
@@ -2380,6 +2429,182 @@ class CldcNetMediaRuntime {
|
||||
*r = JVal::MakeRef(v.allocString(std::to_string(val)));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Integer", "toBinaryString",
|
||||
"(I)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
uint32_t u = static_cast<uint32_t>(a[0].i);
|
||||
if (u == 0) {
|
||||
*r = JVal::MakeRef(v.allocString("0"));
|
||||
return true;
|
||||
}
|
||||
std::string s;
|
||||
while (u > 0) {
|
||||
s.insert(s.begin(), (u & 1) ? '1' : '0');
|
||||
u >>= 1;
|
||||
}
|
||||
*r = JVal::MakeRef(v.allocString(s));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Integer", "toOctalString",
|
||||
"(I)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), "%o",
|
||||
static_cast<uint32_t>(a[0].i));
|
||||
*r = JVal::MakeRef(v.allocString(buf));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "isDigit", "(C)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeInt((a[0].i >= '0' && a[0].i <= '9') ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "isLowerCase", "(C)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeInt((a[0].i >= 'a' && a[0].i <= 'z') ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "isUpperCase", "(C)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeInt((a[0].i >= 'A' && a[0].i <= 'Z') ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "toLowerCase", "(C)C",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int c = a[0].i;
|
||||
if (c >= 'A' && c <= 'Z') c += ('a' - 'A');
|
||||
*r = JVal::MakeInt(c);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "toUpperCase", "(C)C",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int c = a[0].i;
|
||||
if (c >= 'a' && c <= 'z') c -= ('a' - 'A');
|
||||
*r = JVal::MakeInt(c);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Character", "digit", "(CI)I",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int c = a[0].i;
|
||||
int radix = a[1].i;
|
||||
int d = -1;
|
||||
if (c >= '0' && c <= '9') d = c - '0';
|
||||
else if (c >= 'a' && c <= 'z') d = 10 + (c - 'a');
|
||||
else if (c >= 'A' && c <= 'Z') d = 10 + (c - 'A');
|
||||
if (d >= radix) d = -1;
|
||||
*r = JVal::MakeInt(d);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (!self || a.size() < 2) return true;
|
||||
float fv = (a[1].type == JType::Double)
|
||||
? static_cast<float>(a[1].d)
|
||||
: a[1].f;
|
||||
self->setField("value", JVal::MakeFloat(fv));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "floatValue", "()F",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
const JObject* self = v.getObj(a[0].ref);
|
||||
*r = JVal::MakeFloat(self ? self->getField("value", 'F').f : 0.0f);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "parseFloat", "(Ljava/lang/String;)F",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeFloat(
|
||||
std::strtof(v.getStringUtf8(a[0].ref).c_str(), nullptr));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "valueOf", "(Ljava/lang/String;)Ljava/lang/Float;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
float fv = std::strtof(v.getStringUtf8(a[0].ref).c_str(), nullptr);
|
||||
uint32_t ref = v.allocObject("java/lang/Float");
|
||||
v.getObj(ref)->setField("value", JVal::MakeFloat(fv));
|
||||
*r = JVal::MakeRef(ref);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "toString", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
float fv = (a[0].type == JType::Ref)
|
||||
? (v.getObj(a[0].ref)
|
||||
? v.getObj(a[0].ref)->getField("value", 'F').f
|
||||
: 0.0f)
|
||||
: a[0].f;
|
||||
char buf[64];
|
||||
std::snprintf(buf, sizeof(buf), "%g", fv);
|
||||
*r = JVal::MakeRef(v.allocString(buf));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "floatToIntBits", "(F)I",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int32_t bits = 0;
|
||||
std::memcpy(&bits, &a[0].f, sizeof(int32_t));
|
||||
*r = JVal::MakeInt(bits);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Float", "intBitsToFloat", "(I)F",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
float fv = 0.0f;
|
||||
std::memcpy(&fv, &a[0].i, sizeof(float));
|
||||
*r = JVal::MakeFloat(fv);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "<init>", "(D)V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2) {
|
||||
self->setField("value", JVal::MakeDouble(a[1].d));
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "doubleValue", "()D",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
const JObject* self = v.getObj(a[0].ref);
|
||||
*r = JVal::MakeDouble(self ? self->getField("value", 'D').d : 0.0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "parseDouble", "(Ljava/lang/String;)D",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeDouble(
|
||||
std::strtod(v.getStringUtf8(a[0].ref).c_str(), nullptr));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "valueOf", "(Ljava/lang/String;)Ljava/lang/Double;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
double dv = std::strtod(v.getStringUtf8(a[0].ref).c_str(), nullptr);
|
||||
uint32_t ref = v.allocObject("java/lang/Double");
|
||||
v.getObj(ref)->setField("value", JVal::MakeDouble(dv));
|
||||
*r = JVal::MakeRef(ref);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "toString", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
double dv = (a[0].type == JType::Ref)
|
||||
? (v.getObj(a[0].ref)
|
||||
? v.getObj(a[0].ref)->getField("value", 'D').d
|
||||
: 0.0)
|
||||
: a[0].d;
|
||||
char buf[64];
|
||||
std::snprintf(buf, sizeof(buf), "%g", dv);
|
||||
*r = JVal::MakeRef(v.allocString(buf));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "doubleToLongBits", "(D)J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
int64_t bits = 0;
|
||||
std::memcpy(&bits, &a[0].d, sizeof(int64_t));
|
||||
*r = JVal::MakeLong(bits);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/lang/Double", "longBitsToDouble", "(J)D",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
double dv = 0.0;
|
||||
std::memcpy(&dv, &a[0].l, sizeof(double));
|
||||
*r = JVal::MakeDouble(dv);
|
||||
return true;
|
||||
});
|
||||
|
||||
// java/lang/Math
|
||||
vm.registerNative("java/lang/Math", "abs", "(I)I",
|
||||
@@ -2982,7 +3207,10 @@ class CldcNetMediaRuntime {
|
||||
vm.registerNative("java/util/Hashtable", "clear", "()V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self) self->fields.clear();
|
||||
if (self) {
|
||||
self->fields.clear();
|
||||
self->refArr.clear();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/util/Hashtable", "size", "()I",
|
||||
@@ -2993,6 +3221,45 @@ class CldcNetMediaRuntime {
|
||||
: 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/util/Hashtable", "isEmpty", "()Z",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
*r = JVal::MakeInt(
|
||||
(!self || self->fields.empty()) ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/util/Hashtable", "keys",
|
||||
"()Ljava/util/Enumeration;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
uint32_t en = v.allocObject("java/util/Enumeration");
|
||||
JObject* eo = v.getObj(en);
|
||||
if (self && eo) {
|
||||
eo->nativeTag = 0;
|
||||
for (const auto& kv : self->fields) {
|
||||
eo->refArr.push_back(v.allocString(kv.first));
|
||||
}
|
||||
}
|
||||
*r = JVal::MakeRef(en);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative("java/util/Hashtable", "elements",
|
||||
"()Ljava/util/Enumeration;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
uint32_t en = v.allocObject("java/util/Enumeration");
|
||||
JObject* eo = v.getObj(en);
|
||||
if (self && eo) {
|
||||
eo->nativeTag = 0;
|
||||
for (const auto& kv : self->fields) {
|
||||
if (kv.second.type == JType::Ref) {
|
||||
eo->refArr.push_back(kv.second.ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
*r = JVal::MakeRef(en);
|
||||
return true;
|
||||
});
|
||||
|
||||
// java/util/Timer & TimerTask
|
||||
vm.registerNative("java/util/Timer", "<init>", "*",
|
||||
@@ -3436,6 +3703,199 @@ class CldcNetMediaRuntime {
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// Reader & InputStreamReader (UTF-8 / ISO-8859-1 character stream decoder)
|
||||
auto readOneUtf8Char = [](NativeInputStream* nis) -> int {
|
||||
if (!nis) return -1;
|
||||
int b = nis->readByte();
|
||||
if (b < 0) return -1;
|
||||
if ((b & 0x80) == 0) return b;
|
||||
if ((b & 0xE0) == 0xC0) {
|
||||
int b2 = nis->readByte();
|
||||
if (b2 < 0) return b;
|
||||
return ((b & 0x1F) << 6) | (b2 & 0x3F);
|
||||
}
|
||||
if ((b & 0xF0) == 0xE0) {
|
||||
int b2 = nis->readByte();
|
||||
int b3 = nis->readByte();
|
||||
if (b2 < 0 || b3 < 0) return b;
|
||||
return ((b & 0x0F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F);
|
||||
}
|
||||
return b;
|
||||
};
|
||||
for (const char* rdrCls : {"java/io/Reader", "java/io/InputStreamReader"}) {
|
||||
vm.registerNative(
|
||||
rdrCls, "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2 && a[1].type == JType::Ref) {
|
||||
JObject* src = v.getObj(a[1].ref);
|
||||
if (src && src->nativeData) {
|
||||
self->nativeData = src->nativeData;
|
||||
}
|
||||
self->setField("in", a[1]);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "read", "()I",
|
||||
[readOneUtf8Char](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||||
JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
*r = JVal::MakeInt(readOneUtf8Char(nis.get()));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "read", "([C)I",
|
||||
[readOneUtf8Char](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||||
JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
JObject* arr = v.getObj(a[1].ref);
|
||||
if (!nis || !arr || arr->arrayKind != ArrayKind::Char) {
|
||||
*r = JVal::MakeInt(-1);
|
||||
return true;
|
||||
}
|
||||
int len = static_cast<int>(arr->charArr.size());
|
||||
if (len == 0) {
|
||||
*r = JVal::MakeInt(0);
|
||||
return true;
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
int c = readOneUtf8Char(nis.get());
|
||||
if (c < 0) break;
|
||||
arr->charArr[i] = static_cast<uint16_t>(c);
|
||||
++count;
|
||||
}
|
||||
*r = JVal::MakeInt(count == 0 ? -1 : count);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "read", "([CII)I",
|
||||
[readOneUtf8Char](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||||
JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
JObject* arr = v.getObj(a[1].ref);
|
||||
int off = a[2].i;
|
||||
int len = a[3].i;
|
||||
if (!nis || !arr || arr->arrayKind != ArrayKind::Char || off < 0 ||
|
||||
len < 0 || off + len > static_cast<int>(arr->charArr.size())) {
|
||||
*r = JVal::MakeInt(-1);
|
||||
return true;
|
||||
}
|
||||
if (len == 0) {
|
||||
*r = JVal::MakeInt(0);
|
||||
return true;
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
int c = readOneUtf8Char(nis.get());
|
||||
if (c < 0) break;
|
||||
arr->charArr[off + i] = static_cast<uint16_t>(c);
|
||||
++count;
|
||||
}
|
||||
*r = JVal::MakeInt(count == 0 ? -1 : count);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "skip", "(J)J",
|
||||
[readOneUtf8Char](Jvm& v, JThread&, const std::vector<JVal>& a,
|
||||
JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
int64_t want = std::max<int64_t>(0, a[1].l);
|
||||
int64_t skipped = 0;
|
||||
while (skipped < want && readOneUtf8Char(nis.get()) >= 0) {
|
||||
++skipped;
|
||||
}
|
||||
*r = JVal::MakeLong(skipped);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "ready", "()Z",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
*r = JVal::MakeInt((nis && nis->available() > 0) ? 1 : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "markSupported", "()Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "mark", "(I)V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
if (nis) nis->markPos = nis->pos;
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "reset", "()V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
auto nis = getOrCreateInputStream(v, a[0].ref);
|
||||
if (nis) nis->pos = nis->markPos;
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
rdrCls, "close", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
|
||||
// Writer & OutputStreamWriter
|
||||
for (const char* wrCls : {"java/io/Writer", "java/io/OutputStreamWriter"}) {
|
||||
vm.registerNative(
|
||||
wrCls, "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2 && a[1].type == JType::Ref) {
|
||||
JObject* sub = v.getObj(a[1].ref);
|
||||
if (sub && sub->nativeData) self->nativeData = sub->nativeData;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
wrCls, "write", "(I)V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||||
if (nos) {
|
||||
uint16_t ch = static_cast<uint16_t>(a[1].i);
|
||||
std::string s = Jvm::u16ToUtf8(&ch, 1);
|
||||
nos->writeBytes(reinterpret_cast<const uint8_t*>(s.data()),
|
||||
static_cast<int>(s.size()));
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
wrCls, "write", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
auto nos = getOrCreateOutputStream(v, a[0].ref);
|
||||
if (!nos || a.size() < 2 || a[1].type != JType::Ref) return true;
|
||||
JObject* arg = v.getObj(a[1].ref);
|
||||
if (!arg) return true;
|
||||
std::string s;
|
||||
if (arg->arrayKind == ArrayKind::Char) {
|
||||
int off = (a.size() >= 4) ? a[2].i : 0;
|
||||
int len = (a.size() >= 4) ? a[3].i
|
||||
: static_cast<int>(arg->charArr.size());
|
||||
off = std::clamp(off, 0, static_cast<int>(arg->charArr.size()));
|
||||
len = std::clamp(len, 0,
|
||||
static_cast<int>(arg->charArr.size()) - off);
|
||||
s = Jvm::u16ToUtf8(arg->charArr.data() + off, len);
|
||||
} else {
|
||||
s = v.getStringUtf8(a[1].ref);
|
||||
}
|
||||
nos->writeBytes(reinterpret_cast<const uint8_t*>(s.data()),
|
||||
static_cast<int>(s.size()));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
wrCls, "flush", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
wrCls, "close", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
}
|
||||
|
||||
void registerRms(Jvm& vm) {
|
||||
@@ -4161,6 +4621,7 @@ class CldcNetMediaRuntime {
|
||||
"javax/microedition/media/Control",
|
||||
"javax/microedition/media/control/VolumeControl",
|
||||
"javax/microedition/media/control/ToneControl",
|
||||
"javax/microedition/media/control/MIDIControl",
|
||||
"com/nokia/mid/sound/Sound"}) {
|
||||
vm.registerNative(
|
||||
pCls, "<init>", "*",
|
||||
@@ -4331,13 +4792,12 @@ class CldcNetMediaRuntime {
|
||||
return true;
|
||||
}
|
||||
if (np->state == 400 && !np->data.empty()) {
|
||||
bool isMidi = (np->data.size() >= 4 &&
|
||||
std::memcmp(np->data.data(), "MThd", 4) == 0);
|
||||
bool isLooping = (np->loopCount < 0 || np->loopCount > 1);
|
||||
if (!isMidi && !isLooping) {
|
||||
// One-shot WAV SFX play asynchronously in the 6-channel mixer;
|
||||
// report PREFETCHED (300) so games that check getState() == 400
|
||||
// before starting music or rapid SFX never drop sounds.
|
||||
if (!isLooping) {
|
||||
// One-shot WAV/MIDI SFX and jingles play asynchronously in the
|
||||
// mixer; report PREFETCHED (300) so games that check
|
||||
// getState() == 400 before loading a level or playing SFX
|
||||
// never deadlock.
|
||||
np->state = 300;
|
||||
} else if (!sound.isPlayerActive(np->playerId)) {
|
||||
np->state = 300; // Transition back to PREFETCHED when finished
|
||||
@@ -4414,9 +4874,12 @@ class CldcNetMediaRuntime {
|
||||
? v.getStringUtf8(a[1].ref)
|
||||
: "VolumeControl";
|
||||
std::string clsName =
|
||||
(ctrlType.find("ToneControl") != std::string::npos)
|
||||
? "javax/microedition/media/control/ToneControl"
|
||||
: "javax/microedition/media/control/VolumeControl";
|
||||
"javax/microedition/media/control/VolumeControl";
|
||||
if (ctrlType.find("ToneControl") != std::string::npos) {
|
||||
clsName = "javax/microedition/media/control/ToneControl";
|
||||
} else if (ctrlType.find("MIDIControl") != std::string::npos) {
|
||||
clsName = "javax/microedition/media/control/MIDIControl";
|
||||
}
|
||||
uint32_t cref = v.allocObject(clsName);
|
||||
JObject* co = v.getObj(cref);
|
||||
if (co && self) {
|
||||
@@ -4519,7 +4982,296 @@ class CldcNetMediaRuntime {
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
pCls, "shortMidiEvent", "(III)V",
|
||||
[this, ensureNativePlayer](Jvm& v, JThread&,
|
||||
const std::vector<JVal>& a, JVal*) {
|
||||
if (a.size() >= 4) {
|
||||
int status = a[1].i & 0xF0;
|
||||
int data1 = a[2].i & 0x7F;
|
||||
int data2 = a[3].i & 0x7F;
|
||||
if (status == 0x90 && data2 > 0) {
|
||||
NativePlayer* np = ensureNativePlayer(v.getObj(a[0].ref));
|
||||
int vol = np ? (np->muted ? 0 : np->volumeLevel) : 80;
|
||||
sound.playTone(data1, 90,
|
||||
std::clamp((data2 * vol) / 127, 0, 100));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
pCls, "longMidiEvent", "([BII)I",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
*r = JVal::MakeInt(a.size() >= 4 ? a[3].i : 0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
pCls, "setChannelVolume", "(II)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
pCls, "setProgram", "(III)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
|
||||
// StreamConnectionNotifier & ServerSocketConnection
|
||||
for (const char* scnCls :
|
||||
{"javax/microedition/io/StreamConnectionNotifier",
|
||||
"javax/microedition/io/ServerSocketConnection"}) {
|
||||
vm.registerNative(
|
||||
scnCls, "acceptAndOpen",
|
||||
"()Ljavax/microedition/io/StreamConnection;",
|
||||
[](Jvm& v, JThread& th, const std::vector<JVal>&, JVal*) {
|
||||
v.throwException(th, "java/io/IOException",
|
||||
"Connection closed");
|
||||
return false;
|
||||
});
|
||||
vm.registerNative(
|
||||
scnCls, "close", "()V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
|
||||
// JSR-82 Bluetooth API (javax.bluetooth.*)
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getLocalDevice",
|
||||
"()Ljavax/bluetooth/LocalDevice;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("javax/bluetooth/LocalDevice"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getProperty",
|
||||
"(Ljava/lang/String;)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("1.1"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getDiscoveryAgent",
|
||||
"()Ljavax/bluetooth/DiscoveryAgent;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("javax/bluetooth/DiscoveryAgent"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getFriendlyName",
|
||||
"()Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("Nokia 8110 4G"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getBluetoothAddress",
|
||||
"()Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("001122334455"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "setDiscoverable", "(I)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getDiscoverable", "()I",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(0x9E8B33);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "getRecord",
|
||||
"(Ljavax/microedition/io/Connection;)Ljavax/bluetooth/ServiceRecord;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("javax/bluetooth/ServiceRecord"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/LocalDevice", "updateRecord",
|
||||
"(Ljavax/bluetooth/ServiceRecord;)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "startInquiry",
|
||||
"(ILjavax/bluetooth/DiscoveryListener;)Z",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
if (a.size() >= 3 && a[2].ref != 0) {
|
||||
v.invokeMethodSync(a[2].ref, "inquiryCompleted", "(I)V",
|
||||
{JVal::MakeInt(0)});
|
||||
}
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "cancelInquiry",
|
||||
"(Ljavax/bluetooth/DiscoveryListener;)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "searchServices",
|
||||
"([I[Ljavax/bluetooth/UUID;Ljavax/bluetooth/RemoteDevice;Ljavax/bluetooth/DiscoveryListener;)I",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
if (a.size() >= 5 && a[4].ref != 0) {
|
||||
v.invokeMethodSync(a[4].ref, "serviceSearchCompleted", "(II)V",
|
||||
{JVal::MakeInt(1), JVal::MakeInt(4)});
|
||||
}
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "cancelServiceSearch", "(I)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DiscoveryAgent", "retrieveDevices",
|
||||
"(I)[Ljavax/bluetooth/RemoteDevice;",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::Null();
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/RemoteDevice", "getFriendlyName",
|
||||
"(Z)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("BT Device"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/RemoteDevice", "getBluetoothAddress",
|
||||
"()Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("001122334455"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "getConnectionURL",
|
||||
"(IZ)Ljava/lang/String;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocString("btspp://001122334455:1"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "getHostDevice",
|
||||
"()Ljavax/bluetooth/RemoteDevice;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("javax/bluetooth/RemoteDevice"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "getAttributeValue",
|
||||
"(I)Ljavax/bluetooth/DataElement;",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::Null();
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "setAttributeValue",
|
||||
"(ILjavax/bluetooth/DataElement;)Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(1);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/ServiceRecord", "setDeviceServiceClasses", "(I)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DataElement", "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 3) self->setField("value", a[2]);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DataElement", "addElement",
|
||||
"(Ljavax/bluetooth/DataElement;)V",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2) self->refArr.push_back(a[1].ref);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/DataElement", "getValue", "()Ljava/lang/Object;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal* r) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && !self->refArr.empty()) {
|
||||
uint32_t en = v.allocObject("java/util/Enumeration");
|
||||
JObject* eo = v.getObj(en);
|
||||
eo->refArr = self->refArr;
|
||||
eo->nativeTag = 0;
|
||||
*r = JVal::MakeRef(en);
|
||||
} else {
|
||||
*r = self ? self->getField("value", 'L') : JVal::Null();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/bluetooth/UUID", "<init>", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>& a, JVal*) {
|
||||
JObject* self = v.getObj(a[0].ref);
|
||||
if (self && a.size() >= 2 && a[1].type == JType::Ref) {
|
||||
self->strUtf8 = v.getStringUtf8(a[1].ref);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// JSR-75 FileSystemRegistry & FileConnection extras
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileSystemRegistry", "listRoots",
|
||||
"()Ljava/util/Enumeration;",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
uint32_t en = v.allocObject("java/util/Enumeration");
|
||||
JObject* eo = v.getObj(en);
|
||||
if (eo) {
|
||||
eo->nativeTag = 0;
|
||||
eo->refArr.push_back(v.allocString("root1/"));
|
||||
}
|
||||
*r = JVal::MakeRef(en);
|
||||
return true;
|
||||
});
|
||||
for (const char* fcMethod : {"exists", "isDirectory", "canRead", "canWrite"}) {
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileConnection", fcMethod, "()Z",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeInt(0);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileConnection", "fileSize", "()J",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeLong(0);
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileConnection", "list", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(v.allocObject("java/util/Enumeration"));
|
||||
return true;
|
||||
});
|
||||
for (const char* fcVoid : {"create", "mkdir", "delete", "truncate"}) {
|
||||
vm.registerNative(
|
||||
"javax/microedition/io/file/FileConnection", fcVoid, "*",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
|
||||
// JSR-120 Wireless Messaging API (javax.wireless.messaging.*)
|
||||
vm.registerNative(
|
||||
"javax/wireless/messaging/MessageConnection", "newMessage", "*",
|
||||
[](Jvm& v, JThread&, const std::vector<JVal>&, JVal* r) {
|
||||
*r = JVal::MakeRef(
|
||||
v.allocObject("javax/wireless/messaging/TextMessage"));
|
||||
return true;
|
||||
});
|
||||
vm.registerNative(
|
||||
"javax/wireless/messaging/MessageConnection", "send",
|
||||
"(Ljavax/wireless/messaging/Message;)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
vm.registerNative(
|
||||
"javax/wireless/messaging/TextMessage", "setPayloadText",
|
||||
"(Ljava/lang/String;)V",
|
||||
[](Jvm&, JThread&, const std::vector<JVal>&, JVal*) { return true; });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+229
-52
@@ -442,6 +442,9 @@ class Jvm {
|
||||
MidletSuiteInfo* suiteInfo = nullptr;
|
||||
RmsManager* rms = nullptr;
|
||||
bool exitRequested = false;
|
||||
std::function<void(const JObject&, const std::function<void(uint32_t)>&)>
|
||||
traceNativeRefs;
|
||||
std::function<void(const std::function<void(uint32_t)>&)> traceExtraRoots;
|
||||
|
||||
Jvm() {
|
||||
heap_.emplace_back(); // id 0 == null
|
||||
@@ -454,21 +457,33 @@ class Jvm {
|
||||
|
||||
// Heap allocation helpers
|
||||
JObject* getObj(uint32_t ref) {
|
||||
if (ref == 0 || ref >= heap_.size()) return nullptr;
|
||||
if (ref == 0 || ref >= heap_.size() || heap_[ref].id == 0) return nullptr;
|
||||
return &heap_[ref];
|
||||
}
|
||||
|
||||
const JObject* getObj(uint32_t ref) const {
|
||||
if (ref == 0 || ref >= heap_.size()) return nullptr;
|
||||
if (ref == 0 || ref >= heap_.size() || heap_[ref].id == 0) return nullptr;
|
||||
return &heap_[ref];
|
||||
}
|
||||
|
||||
uint32_t allocObject(const std::string& className) {
|
||||
uint32_t allocHeapSlot(JObject&& obj) {
|
||||
if (!freeSlots_.empty()) {
|
||||
uint32_t id = freeSlots_.back();
|
||||
freeSlots_.pop_back();
|
||||
obj.id = id;
|
||||
heap_[id] = std::move(obj);
|
||||
return id;
|
||||
}
|
||||
uint32_t id = static_cast<uint32_t>(heap_.size());
|
||||
JObject obj;
|
||||
obj.id = id;
|
||||
obj.className = className;
|
||||
heap_.push_back(std::move(obj));
|
||||
return id;
|
||||
}
|
||||
|
||||
uint32_t allocObject(const std::string& className) {
|
||||
JObject obj;
|
||||
obj.className = className;
|
||||
uint32_t id = allocHeapSlot(std::move(obj));
|
||||
initInstanceDefaultFields(id, className);
|
||||
return id;
|
||||
}
|
||||
@@ -486,14 +501,11 @@ class Jvm {
|
||||
std::string readString(uint32_t ref) const { return getStringUtf8(ref); }
|
||||
|
||||
uint32_t allocString(const std::string& utf8) {
|
||||
uint32_t id = static_cast<uint32_t>(heap_.size());
|
||||
JObject obj;
|
||||
obj.id = id;
|
||||
obj.className = "java/lang/String";
|
||||
obj.isString = true;
|
||||
obj.strUtf8 = utf8;
|
||||
heap_.push_back(std::move(obj));
|
||||
return id;
|
||||
return allocHeapSlot(std::move(obj));
|
||||
}
|
||||
|
||||
uint32_t internString(const std::string& utf8) {
|
||||
@@ -579,9 +591,7 @@ class Jvm {
|
||||
|
||||
uint32_t allocPrimitiveArray(uint8_t atype, int32_t count) {
|
||||
if (count < 0) count = 0;
|
||||
uint32_t id = static_cast<uint32_t>(heap_.size());
|
||||
JObject obj;
|
||||
obj.id = id;
|
||||
switch (atype) {
|
||||
case 4: // T_BOOLEAN
|
||||
obj.className = "[Z";
|
||||
@@ -629,15 +639,12 @@ class Jvm {
|
||||
obj.intArr.assign(count, 0);
|
||||
break;
|
||||
}
|
||||
heap_.push_back(std::move(obj));
|
||||
return id;
|
||||
return allocHeapSlot(std::move(obj));
|
||||
}
|
||||
|
||||
uint32_t allocRefArray(const std::string& elemClass, int32_t count) {
|
||||
if (count < 0) count = 0;
|
||||
uint32_t id = static_cast<uint32_t>(heap_.size());
|
||||
JObject obj;
|
||||
obj.id = id;
|
||||
if (!elemClass.empty() && elemClass[0] == '[') {
|
||||
obj.className = "[" + elemClass;
|
||||
} else {
|
||||
@@ -645,8 +652,7 @@ class Jvm {
|
||||
}
|
||||
obj.arrayKind = ArrayKind::Ref;
|
||||
obj.refArr.assign(count, 0);
|
||||
heap_.push_back(std::move(obj));
|
||||
return id;
|
||||
return allocHeapSlot(std::move(obj));
|
||||
}
|
||||
|
||||
uint32_t allocMultiArray(const std::string& desc, const int32_t* dims,
|
||||
@@ -830,7 +836,7 @@ class Jvm {
|
||||
|
||||
bool invokeManagedSync(JThread& th, JClass* cls, const JMethodInfo* m,
|
||||
const std::vector<JVal>& args,
|
||||
int maxSteps = 500000) {
|
||||
int maxSteps = 50000000) {
|
||||
if (!cls || !m || !m->hasCode) return false;
|
||||
size_t depth = th.frames.size();
|
||||
pushBytecodeFrame(th, cls, m, args);
|
||||
@@ -895,7 +901,7 @@ class Jvm {
|
||||
JThread tempThread;
|
||||
tempThread.id = 9999;
|
||||
pushBytecodeFrame(tempThread, cls, &m, {});
|
||||
runThreadUntilReturn(tempThread, 0, 2000000);
|
||||
runThreadUntilReturn(tempThread, 0, 20000000);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -946,12 +952,12 @@ class Jvm {
|
||||
}
|
||||
|
||||
// Synchronously invoke a method on an object (e.g. MIDlet.startApp, Canvas.paint,
|
||||
//Canvas.keyPressed, CommandListener.commandAction, Runnable.run)
|
||||
// Canvas.keyPressed, CommandListener.commandAction, Runnable.run)
|
||||
bool invokeMethodSync(uint32_t objRef, const std::string& methodName,
|
||||
const std::string& descriptor,
|
||||
const std::vector<JVal>& extraArgs = {},
|
||||
JVal* outRet = nullptr,
|
||||
int maxSteps = 500000) {
|
||||
int maxSteps = 50000000) {
|
||||
JObject* obj = getObj(objRef);
|
||||
if (!obj) return false;
|
||||
std::vector<JVal> allArgs;
|
||||
@@ -993,15 +999,32 @@ class Jvm {
|
||||
// Execute up to `maxSteps` bytecode instructions on `th` until `th.frames.size() <= targetDepth`
|
||||
bool runThreadUntilReturn(JThread& th, size_t targetDepth, int maxSteps,
|
||||
JVal* outRet = nullptr) {
|
||||
activeSyncThreads_.push_back(&th);
|
||||
int steps = 0;
|
||||
bool ok = true;
|
||||
while (steps < maxSteps && th.frames.size() > targetDepth &&
|
||||
!exitRequested) {
|
||||
if (!stepOneInstruction(th, outRet, targetDepth)) {
|
||||
return false;
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
if (th.yieldSlice || th.state != ThreadState::Runnable) {
|
||||
th.yieldSlice = false;
|
||||
th.state = ThreadState::Runnable;
|
||||
th.wakeTimeMs = 0;
|
||||
th.waitingOnRef = 0;
|
||||
stepThreads(5000);
|
||||
}
|
||||
++steps;
|
||||
}
|
||||
return th.pendingExceptionRef == 0;
|
||||
if (!activeSyncThreads_.empty() && activeSyncThreads_.back() == &th) {
|
||||
activeSyncThreads_.pop_back();
|
||||
} else {
|
||||
activeSyncThreads_.erase(
|
||||
std::remove(activeSyncThreads_.begin(), activeSyncThreads_.end(), &th),
|
||||
activeSyncThreads_.end());
|
||||
}
|
||||
return ok && th.pendingExceptionRef == 0;
|
||||
}
|
||||
|
||||
// Execute a quantum slice of instructions on a scheduled thread
|
||||
@@ -1059,8 +1082,171 @@ class Jvm {
|
||||
return "";
|
||||
}
|
||||
|
||||
static constexpr int64_t kTotalHeapBudgetBytes = 64LL * 1024 * 1024;
|
||||
|
||||
int64_t totalHeapBytes() const { return kTotalHeapBudgetBytes; }
|
||||
|
||||
int64_t usedHeapBytes() const {
|
||||
int64_t used = 0;
|
||||
for (size_t i = 1; i < heap_.size(); ++i) {
|
||||
const JObject& o = heap_[i];
|
||||
if (o.id == 0) continue;
|
||||
used += 64;
|
||||
used += static_cast<int64_t>(o.strUtf8.size());
|
||||
used += static_cast<int64_t>(o.byteArr.size());
|
||||
used += static_cast<int64_t>(o.charArr.size()) * 2;
|
||||
used += static_cast<int64_t>(o.shortArr.size()) * 2;
|
||||
used += static_cast<int64_t>(o.intArr.size()) * 4;
|
||||
used += static_cast<int64_t>(o.longArr.size()) * 8;
|
||||
used += static_cast<int64_t>(o.floatArr.size()) * 4;
|
||||
used += static_cast<int64_t>(o.doubleArr.size()) * 8;
|
||||
used += static_cast<int64_t>(o.refArr.size()) * 4;
|
||||
used += static_cast<int64_t>(o.fields.size()) * 32;
|
||||
}
|
||||
return std::min<int64_t>(used, kTotalHeapBudgetBytes - 4 * 1024 * 1024);
|
||||
}
|
||||
|
||||
int64_t freeHeapBytes() const {
|
||||
return std::max<int64_t>(4 * 1024 * 1024,
|
||||
kTotalHeapBudgetBytes - usedHeapBytes());
|
||||
}
|
||||
|
||||
// Mark-and-sweep garbage collector over Jvm::heap_
|
||||
size_t collectGarbage(const std::vector<uint32_t>& extraRoots = {}) {
|
||||
if (heap_.size() <= 1) return 0;
|
||||
std::vector<uint8_t> marked(heap_.size(), 0);
|
||||
std::vector<uint32_t> worklist;
|
||||
worklist.reserve(1024);
|
||||
|
||||
auto markRef = [&](uint32_t r) {
|
||||
if (r > 0 && r < heap_.size() && !marked[r] && heap_[r].id != 0) {
|
||||
marked[r] = 1;
|
||||
worklist.push_back(r);
|
||||
}
|
||||
};
|
||||
|
||||
for (uint32_t r : extraRoots) markRef(r);
|
||||
if (traceExtraRoots) traceExtraRoots(markRef);
|
||||
for (const auto& kv : internPool_) markRef(kv.second);
|
||||
|
||||
for (const auto& ckv : classes_) {
|
||||
const JClass* cls = ckv.second.get();
|
||||
if (!cls) continue;
|
||||
for (const auto& fkv : cls->staticFields) {
|
||||
if (fkv.second.type == JType::Ref) markRef(fkv.second.ref);
|
||||
}
|
||||
for (const auto& ce : cls->cp) {
|
||||
if (ce.cachedStringRef != 0) markRef(ce.cachedStringRef);
|
||||
}
|
||||
}
|
||||
|
||||
auto markThread = [&](const JThread& th) {
|
||||
markRef(th.threadObjRef);
|
||||
markRef(th.pendingExceptionRef);
|
||||
markRef(th.waitingOnRef);
|
||||
for (const auto& f : th.frames) {
|
||||
for (const auto& lv : f.locals) {
|
||||
if (lv.type == JType::Ref) markRef(lv.ref);
|
||||
}
|
||||
for (const auto& sv : f.stack) {
|
||||
if (sv.type == JType::Ref) markRef(sv.ref);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (const auto& th : threads_) {
|
||||
if (th) markThread(*th);
|
||||
}
|
||||
for (const JThread* sth : activeSyncThreads_) {
|
||||
if (sth) markThread(*sth);
|
||||
}
|
||||
|
||||
while (!worklist.empty()) {
|
||||
uint32_t cur = worklist.back();
|
||||
worklist.pop_back();
|
||||
const JObject& obj = heap_[cur];
|
||||
for (uint32_t r : obj.refArr) markRef(r);
|
||||
for (const auto& fkv : obj.fields) {
|
||||
if (fkv.second.type == JType::Ref) markRef(fkv.second.ref);
|
||||
}
|
||||
if (traceNativeRefs && obj.nativeData) {
|
||||
traceNativeRefs(obj, markRef);
|
||||
}
|
||||
}
|
||||
|
||||
freeSlots_.clear();
|
||||
size_t swept = 0;
|
||||
for (uint32_t i = static_cast<uint32_t>(heap_.size()) - 1; i >= 1; --i) {
|
||||
if (!marked[i]) {
|
||||
if (heap_[i].id != 0) {
|
||||
heap_[i] = JObject{};
|
||||
++swept;
|
||||
}
|
||||
freeSlots_.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Reap finished background threads that have no remaining frames
|
||||
threads_.erase(
|
||||
std::remove_if(threads_.begin(), threads_.end(),
|
||||
[](const std::unique_ptr<JThread>& t) {
|
||||
return !t ||
|
||||
(t->state == ThreadState::Finished &&
|
||||
t->frames.empty());
|
||||
}),
|
||||
threads_.end());
|
||||
|
||||
return swept;
|
||||
}
|
||||
|
||||
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) {
|
||||
std::string name = className;
|
||||
for (char& c : name) {
|
||||
if (c == '.') c = '/';
|
||||
}
|
||||
if (!name.empty() && name[0] == 'L' && name.back() == ';') {
|
||||
name = name.substr(1, name.size() - 2);
|
||||
}
|
||||
auto synth = std::make_unique<JClass>();
|
||||
synth->name = name;
|
||||
synth->superName = getSuperClassName(name);
|
||||
synth->clinitStarted = true;
|
||||
synth->clinitDone = true;
|
||||
cls = synth.get();
|
||||
classes_[name] = std::move(synth);
|
||||
}
|
||||
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) {
|
||||
JVal* s = resolveStaticSlot(className, fieldName, desc);
|
||||
if (s) *s = val;
|
||||
}
|
||||
|
||||
private:
|
||||
std::deque<JObject> heap_;
|
||||
std::vector<uint32_t> freeSlots_;
|
||||
std::vector<JThread*> activeSyncThreads_;
|
||||
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_;
|
||||
@@ -1179,8 +1365,28 @@ class Jvm {
|
||||
"java/lang/RuntimeException";
|
||||
builtinSupers_["java/lang/ClassCastException"] =
|
||||
"java/lang/RuntimeException";
|
||||
builtinSupers_["java/lang/NumberFormatException"] =
|
||||
"java/lang/IllegalArgumentException";
|
||||
builtinSupers_["java/lang/IndexOutOfBoundsException"] =
|
||||
"java/lang/RuntimeException";
|
||||
builtinSupers_["java/lang/StringIndexOutOfBoundsException"] =
|
||||
"java/lang/IndexOutOfBoundsException";
|
||||
builtinSupers_["java/lang/NegativeArraySizeException"] =
|
||||
"java/lang/RuntimeException";
|
||||
builtinSupers_["java/lang/SecurityException"] =
|
||||
"java/lang/RuntimeException";
|
||||
builtinSupers_["java/lang/OutOfMemoryError"] = "java/lang/Throwable";
|
||||
builtinSupers_["java/lang/Error"] = "java/lang/Throwable";
|
||||
builtinSupers_["java/io/IOException"] = "java/lang/Exception";
|
||||
builtinSupers_["java/io/EOFException"] = "java/io/IOException";
|
||||
builtinSupers_["java/io/UnsupportedEncodingException"] =
|
||||
"java/io/IOException";
|
||||
builtinSupers_["javax/microedition/io/ConnectionNotFoundException"] =
|
||||
"java/io/IOException";
|
||||
builtinSupers_["javax/bluetooth/BluetoothStateException"] =
|
||||
"java/io/IOException";
|
||||
builtinSupers_["javax/microedition/media/MediaException"] =
|
||||
"java/lang/Exception";
|
||||
builtinSupers_["javax/microedition/rms/RecordStoreException"] =
|
||||
"java/lang/Exception";
|
||||
builtinSupers_["javax/microedition/rms/RecordStoreNotFoundException"] =
|
||||
@@ -1277,35 +1483,6 @@ class Jvm {
|
||||
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) {
|
||||
JVal* s = resolveStaticSlot(className, fieldName, desc);
|
||||
if (s) *s = val;
|
||||
}
|
||||
|
||||
// Big-endian readers for .class format
|
||||
static uint16_t be16(const uint8_t* p) {
|
||||
return (static_cast<uint16_t>(p[0]) << 8) | p[1];
|
||||
|
||||
@@ -80,6 +80,8 @@ struct NativeImageSurface {
|
||||
int height = 320;
|
||||
bool mutableImg = true;
|
||||
bool isMutable = true;
|
||||
int maxDrawnX = 0;
|
||||
int maxDrawnY = 0;
|
||||
std::vector<uint32_t> pixels; // 0xAARRGGBB
|
||||
|
||||
NativeImageSurface(int w = 240, int h = 320, uint32_t fill = 0xFFFFFFFFu)
|
||||
@@ -96,21 +98,32 @@ struct NativeImageSurface {
|
||||
|
||||
void clear(uint32_t fill = 0xFF000000u) {
|
||||
std::fill(pixels.begin(), pixels.end(), fill);
|
||||
maxDrawnX = 0;
|
||||
maxDrawnY = 0;
|
||||
}
|
||||
|
||||
void resize(int w, int h, uint32_t fill = 0xFF000000u) {
|
||||
width = std::max(1, w);
|
||||
height = std::max(1, h);
|
||||
maxDrawnX = 0;
|
||||
maxDrawnY = 0;
|
||||
pixels.assign(static_cast<size_t>(width) * height, fill);
|
||||
}
|
||||
|
||||
static bool DecodeFromMemory(const uint8_t* data, size_t len,
|
||||
NativeImageSurface* out);
|
||||
|
||||
inline uint32_t getPixel(int x, int y) const {
|
||||
if (x < 0 || y < 0 || x >= width || y >= height) return 0;
|
||||
return pixels[static_cast<size_t>(y) * width + x];
|
||||
}
|
||||
|
||||
inline void blendPixel(int x, int y, uint32_t srcArgb) {
|
||||
if (x < 0 || y < 0 || x >= width || y >= height) return;
|
||||
uint32_t a = (srcArgb >> 24) & 0xFFu;
|
||||
if (a == 0) return;
|
||||
if (x > maxDrawnX) maxDrawnX = x;
|
||||
if (y > maxDrawnY) maxDrawnY = y;
|
||||
uint32_t& dst = pixels[static_cast<size_t>(y) * width + x];
|
||||
if (a == 255) {
|
||||
dst = srcArgb | 0xFF000000u;
|
||||
@@ -318,6 +331,8 @@ struct NativeGraphicsState {
|
||||
int x1 = std::min({target->width, clipX + clipW, x + w});
|
||||
int y1 = std::min({target->height, clipY + clipH, y + h});
|
||||
if (x0 >= x1 || y0 >= y1) return;
|
||||
if (x1 - 1 > target->maxDrawnX) target->maxDrawnX = x1 - 1;
|
||||
if (y1 - 1 > target->maxDrawnY) target->maxDrawnY = y1 - 1;
|
||||
uint32_t a = (colorArgb >> 24) & 0xFFu;
|
||||
for (int py = y0; py < y1; ++py) {
|
||||
uint32_t* row = target->pixels.data() + static_cast<size_t>(py) * target->width;
|
||||
@@ -541,6 +556,7 @@ struct LcduiScreenState {
|
||||
std::string title;
|
||||
std::string tickerText;
|
||||
uint32_t listenerRef = 0;
|
||||
uint32_t selectCommandRef = 0;
|
||||
std::vector<LcduiCommandInfo> commands;
|
||||
bool fullScreen = true;
|
||||
int selectedIdx = 0;
|
||||
@@ -548,6 +564,7 @@ struct LcduiScreenState {
|
||||
// For List / ChoiceGroup:
|
||||
int choiceType = 1; // 1=EXCLUSIVE, 2=MULTIPLE, 3=IMPLICIT
|
||||
std::vector<std::string> listItems;
|
||||
std::vector<uint32_t> listItemRefs;
|
||||
std::vector<uint32_t> listImages;
|
||||
std::vector<bool> listSelected;
|
||||
// For Form:
|
||||
|
||||
@@ -28,10 +28,14 @@ struct RuntimeAppContext {
|
||||
uint32_t currentDisplayableRef = 0;
|
||||
uint32_t midletRef = 0;
|
||||
uint32_t sharedGraphicsRef = 0;
|
||||
uint32_t graphics3dRef = 0;
|
||||
uint32_t listSelectCommandRef = 0;
|
||||
|
||||
bool midletDestroyed = false;
|
||||
bool midletPaused = false;
|
||||
bool repaintPending = true;
|
||||
bool inPaint = false;
|
||||
bool paintedInStep = false;
|
||||
bool commandMenuOpen = false;
|
||||
int commandMenuSel = 0;
|
||||
int latchedKeyStates = 0;
|
||||
@@ -58,6 +62,7 @@ struct LcduiItemState {
|
||||
int choiceType = 1;
|
||||
int selectedIdx = 0;
|
||||
std::vector<std::string> choices;
|
||||
std::vector<uint32_t> choiceRefs;
|
||||
std::vector<bool> choiceSel;
|
||||
uint32_t imageRef = 0;
|
||||
};
|
||||
@@ -144,6 +149,93 @@ inline uint32_t createGraphicsForSurface(Jvm& vm, NativeImageSurface* surf) {
|
||||
}
|
||||
|
||||
inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
// Register GC root and nativeData reference tracers
|
||||
vm.traceExtraRoots = [ctx](const std::function<void(uint32_t)>& mark) {
|
||||
if (!ctx) return;
|
||||
mark(ctx->displayRef);
|
||||
mark(ctx->currentDisplayableRef);
|
||||
mark(ctx->midletRef);
|
||||
mark(ctx->sharedGraphicsRef);
|
||||
mark(ctx->graphics3dRef);
|
||||
mark(ctx->listSelectCommandRef);
|
||||
for (uint32_t r : ctx->serialRunnables) mark(r);
|
||||
if (ctx->m3g) {
|
||||
mark(ctx->m3g->boundGraphicsRef);
|
||||
mark(ctx->m3g->boundTargetRef);
|
||||
}
|
||||
};
|
||||
vm.traceNativeRefs = [&vm](const JObject& obj,
|
||||
const std::function<void(uint32_t)>& mark) {
|
||||
if (!obj.nativeData) return;
|
||||
const std::string& cn = obj.className;
|
||||
if (cn == "javax/microedition/lcdui/Form" ||
|
||||
cn == "javax/microedition/lcdui/List" ||
|
||||
cn == "javax/microedition/lcdui/TextBox" ||
|
||||
cn == "javax/microedition/lcdui/Alert" ||
|
||||
cn == "javax/microedition/lcdui/Canvas" ||
|
||||
cn == "javax/microedition/lcdui/game/GameCanvas" ||
|
||||
cn == "com/nokia/mid/ui/FullCanvas" ||
|
||||
vm.isSubclassOf(cn, "javax/microedition/lcdui/Displayable")) {
|
||||
auto* st = static_cast<LcduiScreenState*>(obj.nativeData.get());
|
||||
mark(st->listenerRef);
|
||||
mark(st->selectCommandRef);
|
||||
mark(st->alertNextDisplayableRef);
|
||||
for (const auto& c : st->commands) mark(c.cmdRef);
|
||||
for (uint32_t r : st->listItemRefs) mark(r);
|
||||
for (uint32_t r : st->listImages) mark(r);
|
||||
for (uint32_t r : st->formItemRefs) mark(r);
|
||||
} else if (cn == "javax/microedition/lcdui/StringItem" ||
|
||||
cn == "javax/microedition/lcdui/ImageItem" ||
|
||||
cn == "javax/microedition/lcdui/TextField" ||
|
||||
cn == "javax/microedition/lcdui/ChoiceGroup" ||
|
||||
cn == "javax/microedition/lcdui/Gauge" ||
|
||||
vm.isSubclassOf(cn, "javax/microedition/lcdui/Item")) {
|
||||
auto* is = static_cast<LcduiItemState*>(obj.nativeData.get());
|
||||
mark(is->imageRef);
|
||||
for (uint32_t r : is->choiceRefs) mark(r);
|
||||
} else if (cn == "javax/microedition/lcdui/game/Sprite" ||
|
||||
vm.isSubclassOf(cn, "javax/microedition/lcdui/game/Sprite")) {
|
||||
auto* sp = static_cast<SpriteNativeState*>(obj.nativeData.get());
|
||||
mark(sp->imageRef);
|
||||
} else if (cn == "javax/microedition/lcdui/game/TiledLayer" ||
|
||||
vm.isSubclassOf(cn, "javax/microedition/lcdui/game/TiledLayer")) {
|
||||
auto* tl = static_cast<TiledLayerNativeState*>(obj.nativeData.get());
|
||||
mark(tl->imageRef);
|
||||
} else if (cn == "javax/microedition/m3g/VertexBuffer") {
|
||||
auto* vb = static_cast<M3gVertexBufferData*>(obj.nativeData.get());
|
||||
mark(vb->positionsRef);
|
||||
mark(vb->normalsRef);
|
||||
mark(vb->colorsRef);
|
||||
mark(vb->texCoordsRef);
|
||||
} else if (cn == "javax/microedition/m3g/Mesh" ||
|
||||
cn == "javax/microedition/m3g/MorphingMesh" ||
|
||||
cn == "javax/microedition/m3g/SkinnedMesh") {
|
||||
auto* md = static_cast<M3gMeshData*>(obj.nativeData.get());
|
||||
mark(md->vertexBufferRef);
|
||||
for (uint32_t r : md->submeshRefs) mark(r);
|
||||
for (uint32_t r : md->appearanceRefs) mark(r);
|
||||
for (uint32_t r : md->childrenRefs) mark(r);
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize List.SELECT_COMMAND and AlertType.* static constants
|
||||
uint32_t selCmd = vm.allocObject("javax/microedition/lcdui/Command");
|
||||
if (JObject* co = vm.getObj(selCmd)) {
|
||||
co->setField("label", JValue::FromRef(vm.internString("")));
|
||||
co->setField("type", JValue::FromInt(1)); // SCREEN
|
||||
co->setField("priority", JValue::FromInt(0));
|
||||
}
|
||||
ctx->listSelectCommandRef = selCmd;
|
||||
vm.setStaticFieldVal("javax/microedition/lcdui/List", "SELECT_COMMAND",
|
||||
"Ljavax/microedition/lcdui/Command;",
|
||||
JValue::FromRef(selCmd));
|
||||
for (const char* atName :
|
||||
{"INFO", "WARNING", "ERROR", "ALARM", "CONFIRMATION"}) {
|
||||
uint32_t atRef = vm.allocObject("javax/microedition/lcdui/AlertType");
|
||||
vm.setStaticFieldVal("javax/microedition/lcdui/AlertType", atName,
|
||||
"Ljavax/microedition/lcdui/AlertType;",
|
||||
JValue::FromRef(atRef));
|
||||
}
|
||||
// -------------------------------------------------------------------------
|
||||
// javax.microedition.midlet.MIDlet
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -363,11 +455,41 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
ctx->repaintPending = true;
|
||||
return JValue::Null();
|
||||
});
|
||||
v.registerWildcardNative(cls, "serviceRepaints",
|
||||
[ctx](Jvm&, JThread&, JValue*) -> JValue {
|
||||
ctx->repaintPending = true;
|
||||
return JValue::Null();
|
||||
});
|
||||
v.registerWildcardNative(
|
||||
cls, "serviceRepaints",
|
||||
[ctx](Jvm& vmRef, JThread&, JValue*) -> JValue {
|
||||
uint32_t dispRef = ctx->currentDisplayableRef;
|
||||
if (!ctx->inPaint && dispRef != 0 &&
|
||||
isCanvasDisplayable(vmRef, dispRef)) {
|
||||
JObject* dispObj = vmRef.getObj(dispRef);
|
||||
if (dispObj) {
|
||||
ctx->inPaint = true;
|
||||
ctx->repaintPending = false;
|
||||
JObject* gObj = vmRef.getObj(ctx->sharedGraphicsRef);
|
||||
if (gObj && gObj->nativeData) {
|
||||
auto* gs =
|
||||
static_cast<NativeGraphicsState*>(gObj->nativeData.get());
|
||||
gs->resetTarget(&ctx->canvasFb);
|
||||
}
|
||||
auto [rc, rm] = vmRef.resolveMethod(
|
||||
dispObj->className, "paint",
|
||||
"(Ljavax/microedition/lcdui/Graphics;)V", false);
|
||||
if (rm && !rm->code.empty()) {
|
||||
JThread paintTh;
|
||||
vmRef.invokeManagedSync(
|
||||
paintTh, rc, rm,
|
||||
{JValue::FromRef(dispRef),
|
||||
JValue::FromRef(ctx->sharedGraphicsRef)},
|
||||
25000000);
|
||||
}
|
||||
ctx->inPaint = false;
|
||||
ctx->paintedInStep = true;
|
||||
return JValue::Null();
|
||||
}
|
||||
}
|
||||
ctx->repaintPending = true;
|
||||
return JValue::Null();
|
||||
});
|
||||
v.registerWildcardNative(cls, "setTitle",
|
||||
[ctx](Jvm& vmRef, JThread&, JValue* a) -> JValue {
|
||||
LcduiScreenState* st = ensureScreenState(vmRef, a[0].ref);
|
||||
@@ -1129,6 +1251,11 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/lcdui/Graphics", "getDisplayColor",
|
||||
[](Jvm&, JThread&, JValue* a) -> JValue {
|
||||
return JValue::FromInt(a[1].i & 0x00FFFFFF);
|
||||
});
|
||||
|
||||
// com.nokia.mid.ui.DirectUtils / DirectGraphics
|
||||
vm.registerWildcardNative(
|
||||
@@ -1154,6 +1281,193 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
auto surf = std::make_shared<NativeImageSurface>(w, h, true, col);
|
||||
return JValue::FromRef(createImageObjectFromSurface(v, surf));
|
||||
});
|
||||
for (const char* dgCls :
|
||||
{"com/nokia/mid/ui/DirectGraphics",
|
||||
"javax/microedition/lcdui/Graphics"}) {
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "setARGBColor",
|
||||
[getGraphicsState](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
if (auto* g = getGraphicsState(v, a[0].ref)) {
|
||||
g->colorArgb = static_cast<uint32_t>(a[1].i);
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "getAlphaComponent",
|
||||
[getGraphicsState](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
auto* g = getGraphicsState(v, a[0].ref);
|
||||
return JValue::FromInt(
|
||||
g ? static_cast<int32_t>((g->colorArgb >> 24) & 0xFFu) : 255);
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "getNativePixelFormat",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue {
|
||||
return JValue::FromInt(8888); // TYPE_INT_8888_ARGB
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "drawTriangle",
|
||||
[getGraphicsState](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
if (auto* g = getGraphicsState(v, a[0].ref)) {
|
||||
if (a[7].i != 0) g->colorArgb = static_cast<uint32_t>(a[7].i);
|
||||
g->drawLine(a[1].i, a[2].i, a[3].i, a[4].i);
|
||||
g->drawLine(a[3].i, a[4].i, a[5].i, a[6].i);
|
||||
g->drawLine(a[5].i, a[6].i, a[1].i, a[2].i);
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "fillTriangle",
|
||||
[getGraphicsState, dgCls](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
if (auto* g = getGraphicsState(v, a[0].ref)) {
|
||||
if (a[7].i != 0 && dgCls[0] == 'c') {
|
||||
g->colorArgb = static_cast<uint32_t>(a[7].i);
|
||||
}
|
||||
g->fillTriangle(a[1].i, a[2].i, a[3].i, a[4].i, a[5].i, a[6].i);
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "drawPolygon",
|
||||
[getGraphicsState](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
auto* g = getGraphicsState(v, a[0].ref);
|
||||
JObject* xArr = v.getObj(a[1].ref);
|
||||
JObject* yArr = v.getObj(a[3].ref);
|
||||
if (!g || !xArr || !yArr) return JValue::Null();
|
||||
int xOff = a[2].i, yOff = a[4].i, n = a[5].i;
|
||||
g->colorArgb = static_cast<uint32_t>(a[6].i);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int j = (i + 1) % n;
|
||||
if (xOff + i >= 0 && yOff + i >= 0 && xOff + j >= 0 &&
|
||||
yOff + j >= 0 &&
|
||||
static_cast<size_t>(xOff + i) < xArr->intArr.size() &&
|
||||
static_cast<size_t>(yOff + i) < yArr->intArr.size() &&
|
||||
static_cast<size_t>(xOff + j) < xArr->intArr.size() &&
|
||||
static_cast<size_t>(yOff + j) < yArr->intArr.size()) {
|
||||
g->drawLine(xArr->intArr[xOff + i], yArr->intArr[yOff + i],
|
||||
xArr->intArr[xOff + j], yArr->intArr[yOff + j]);
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "fillPolygon",
|
||||
[getGraphicsState](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
auto* g = getGraphicsState(v, a[0].ref);
|
||||
JObject* xArr = v.getObj(a[1].ref);
|
||||
JObject* yArr = v.getObj(a[3].ref);
|
||||
if (!g || !xArr || !yArr) return JValue::Null();
|
||||
int xOff = a[2].i, yOff = a[4].i, n = a[5].i;
|
||||
g->colorArgb = static_cast<uint32_t>(a[6].i);
|
||||
for (int i = 1; i + 1 < n; ++i) {
|
||||
if (xOff + i + 1 < static_cast<int>(xArr->intArr.size()) &&
|
||||
yOff + i + 1 < static_cast<int>(yArr->intArr.size())) {
|
||||
g->fillTriangle(xArr->intArr[xOff], yArr->intArr[yOff],
|
||||
xArr->intArr[xOff + i], yArr->intArr[yOff + i],
|
||||
xArr->intArr[xOff + i + 1],
|
||||
yArr->intArr[yOff + i + 1]);
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "drawPixels",
|
||||
[getGraphicsState](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
auto* g = getGraphicsState(v, a[0].ref);
|
||||
JObject* arr = v.getObj(a[1].ref);
|
||||
if (!g || !arr) return JValue::Null();
|
||||
bool trans = (a[2].i != 0);
|
||||
int offset = a[3].i, scanlen = a[4].i, x = a[5].i, y = a[6].i;
|
||||
int w = a[7].i, h = a[8].i, manip = a[9].i, fmt = a[10].i;
|
||||
bool flipX = (manip & 8192) != 0;
|
||||
bool flipY = (manip & 16384) != 0;
|
||||
int rot = manip & 0x1FFF;
|
||||
if (rot == 180) {
|
||||
flipX = !flipX;
|
||||
flipY = !flipY;
|
||||
}
|
||||
for (int row = 0; row < h; ++row) {
|
||||
int sRow = flipY ? (h - 1 - row) : row;
|
||||
for (int col = 0; col < w; ++col) {
|
||||
int sCol = flipX ? (w - 1 - col) : col;
|
||||
int idx = offset + sRow * scanlen + sCol;
|
||||
if (idx < 0) continue;
|
||||
uint32_t argb = 0;
|
||||
if (arr->arrayKind == ArrayKind::Int &&
|
||||
static_cast<size_t>(idx) < arr->intArr.size()) {
|
||||
argb = static_cast<uint32_t>(arr->intArr[idx]);
|
||||
if (!trans || fmt == 888) argb |= 0xFF000000u;
|
||||
} else if (arr->arrayKind == ArrayKind::Short &&
|
||||
static_cast<size_t>(idx) < arr->shortArr.size()) {
|
||||
uint16_t spx = static_cast<uint16_t>(arr->shortArr[idx]);
|
||||
if (fmt == 565) {
|
||||
uint32_t r = ((spx >> 11) & 0x1Fu) * 255u / 31u;
|
||||
uint32_t gr = ((spx >> 5) & 0x3Fu) * 255u / 63u;
|
||||
uint32_t b = (spx & 0x1Fu) * 255u / 31u;
|
||||
argb = 0xFF000000u | (r << 16) | (gr << 8) | b;
|
||||
} else if (fmt == 1555) {
|
||||
uint32_t al = (!trans || (spx & 0x8000u)) ? 255u : 0u;
|
||||
uint32_t r = ((spx >> 10) & 0x1Fu) * 255u / 31u;
|
||||
uint32_t gr = ((spx >> 5) & 0x1Fu) * 255u / 31u;
|
||||
uint32_t b = (spx & 0x1Fu) * 255u / 31u;
|
||||
argb = (al << 24) | (r << 16) | (gr << 8) | b;
|
||||
} else {
|
||||
// 4444 or 444
|
||||
uint32_t al = (!trans || fmt == 444)
|
||||
? 255u
|
||||
: (((spx >> 12) & 0xFu) * 17u);
|
||||
uint32_t r = ((spx >> 8) & 0xFu) * 17u;
|
||||
uint32_t gr = ((spx >> 4) & 0xFu) * 17u;
|
||||
uint32_t b = (spx & 0xFu) * 17u;
|
||||
argb = (al << 24) | (r << 16) | (gr << 8) | b;
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if ((argb >> 24) == 0) continue;
|
||||
g->putClipped(x + g->transX + col, y + g->transY + row, argb);
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
dgCls, "getPixels",
|
||||
[getGraphicsState](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
auto* g = getGraphicsState(v, a[0].ref);
|
||||
JObject* arr = v.getObj(a[1].ref);
|
||||
if (!g || !g->target || !arr) return JValue::Null();
|
||||
int offset = a[3].i, scanlen = a[4].i, x = a[5].i, y = a[6].i;
|
||||
int w = a[7].i, h = a[8].i, fmt = a[9].i;
|
||||
for (int row = 0; row < h; ++row) {
|
||||
for (int col = 0; col < w; ++col) {
|
||||
int idx = offset + row * scanlen + col;
|
||||
if (idx < 0) continue;
|
||||
uint32_t argb = g->target->getPixel(x + g->transX + col,
|
||||
y + g->transY + row);
|
||||
if (arr->arrayKind == ArrayKind::Int &&
|
||||
static_cast<size_t>(idx) < arr->intArr.size()) {
|
||||
arr->intArr[idx] = static_cast<int32_t>(argb);
|
||||
} else if (arr->arrayKind == ArrayKind::Short &&
|
||||
static_cast<size_t>(idx) < arr->shortArr.size()) {
|
||||
uint32_t al = (argb >> 24) & 0xFFu;
|
||||
uint32_t r = (argb >> 16) & 0xFFu;
|
||||
uint32_t gr = (argb >> 8) & 0xFFu;
|
||||
uint32_t b = argb & 0xFFu;
|
||||
uint16_t spx = 0;
|
||||
if (fmt == 565) {
|
||||
spx = static_cast<uint16_t>(((r >> 3) << 11) |
|
||||
((gr >> 2) << 5) | (b >> 3));
|
||||
} else {
|
||||
spx = static_cast<uint16_t>(((al >> 4) << 12) |
|
||||
((r >> 4) << 8) |
|
||||
((gr >> 4) << 4) | (b >> 4));
|
||||
}
|
||||
arr->shortArr[idx] = static_cast<int16_t>(spx);
|
||||
}
|
||||
}
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
}
|
||||
vm.registerWildcardNative("com/nokia/mid/ui/DeviceControl", "setLights",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue { return JValue::Null(); });
|
||||
vm.registerWildcardNative("com/nokia/mid/ui/DeviceControl", "startVibra",
|
||||
@@ -1243,15 +1557,19 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
// List
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/lcdui/List", "<init>",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
[ctx](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
LcduiScreenState* st = ensureScreenState(v, a[0].ref);
|
||||
if (st) {
|
||||
st->title = v.readString(a[1].ref);
|
||||
st->choiceType = a[2].i;
|
||||
if (st->choiceType == 3) {
|
||||
st->selectCommandRef = ctx->listSelectCommandRef;
|
||||
}
|
||||
JObject* strArr = v.getObj(a[3].ref);
|
||||
if (strArr) {
|
||||
for (uint32_t sref : strArr->refArr) {
|
||||
st->listItems.push_back(v.readString(sref));
|
||||
st->listItemRefs.push_back(sref);
|
||||
st->listImages.push_back(0);
|
||||
st->listSelected.push_back(false);
|
||||
}
|
||||
@@ -1265,11 +1583,28 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
LcduiScreenState* st = ensureScreenState(v, a[0].ref);
|
||||
if (!st) return JValue::FromInt(0);
|
||||
st->listItems.push_back(v.readString(a[1].ref));
|
||||
st->listItemRefs.push_back(a[1].ref);
|
||||
st->listImages.push_back(a[2].ref);
|
||||
st->listSelected.push_back(false);
|
||||
ctx->repaintPending = true;
|
||||
return JValue::FromInt(static_cast<int32_t>(st->listItems.size()) - 1);
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/lcdui/List", "insert",
|
||||
[ctx](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
LcduiScreenState* st = ensureScreenState(v, a[0].ref);
|
||||
if (st) {
|
||||
int idx = std::clamp(
|
||||
a[1].i, 0, static_cast<int>(st->listItems.size()));
|
||||
st->listItems.insert(st->listItems.begin() + idx,
|
||||
v.readString(a[2].ref));
|
||||
st->listItemRefs.insert(st->listItemRefs.begin() + idx, a[2].ref);
|
||||
st->listImages.insert(st->listImages.begin() + idx, a[3].ref);
|
||||
st->listSelected.insert(st->listSelected.begin() + idx, false);
|
||||
ctx->repaintPending = true;
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/lcdui/List", "set",
|
||||
[ctx](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
@@ -1277,6 +1612,7 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
if (st && a[1].i >= 0 &&
|
||||
static_cast<size_t>(a[1].i) < st->listItems.size()) {
|
||||
st->listItems[a[1].i] = v.readString(a[2].ref);
|
||||
st->listItemRefs[a[1].i] = a[2].ref;
|
||||
st->listImages[a[1].i] = a[3].ref;
|
||||
ctx->repaintPending = true;
|
||||
}
|
||||
@@ -1289,6 +1625,7 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
if (st && a[1].i >= 0 &&
|
||||
static_cast<size_t>(a[1].i) < st->listItems.size()) {
|
||||
st->listItems.erase(st->listItems.begin() + a[1].i);
|
||||
st->listItemRefs.erase(st->listItemRefs.begin() + a[1].i);
|
||||
st->listImages.erase(st->listImages.begin() + a[1].i);
|
||||
st->listSelected.erase(st->listSelected.begin() + a[1].i);
|
||||
ctx->repaintPending = true;
|
||||
@@ -1301,6 +1638,7 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
LcduiScreenState* st = ensureScreenState(v, a[0].ref);
|
||||
if (st) {
|
||||
st->listItems.clear();
|
||||
st->listItemRefs.clear();
|
||||
st->listImages.clear();
|
||||
st->listSelected.clear();
|
||||
st->selectedIdx = 0;
|
||||
@@ -1319,6 +1657,11 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
LcduiScreenState* st = ensureScreenState(v, a[0].ref);
|
||||
if (st && a[1].i >= 0 &&
|
||||
static_cast<size_t>(a[1].i) < st->listItems.size()) {
|
||||
if (static_cast<size_t>(a[1].i) <
|
||||
st->listItemRefs.size() &&
|
||||
st->listItemRefs[a[1].i] != 0) {
|
||||
return JValue::FromRef(st->listItemRefs[a[1].i]);
|
||||
}
|
||||
return JValue::FromRef(
|
||||
v.internString(st->listItems[a[1].i]));
|
||||
}
|
||||
@@ -1351,7 +1694,9 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
return JValue::FromInt(st->selectedIdx == a[1].i ? 1 : 0);
|
||||
});
|
||||
vm.registerWildcardNative("javax/microedition/lcdui/List", "setSelectCommand",
|
||||
[](Jvm&, JThread&, JValue*) -> JValue {
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
LcduiScreenState* st = ensureScreenState(v, a[0].ref);
|
||||
if (st) st->selectCommandRef = a[1].ref;
|
||||
return JValue::Null();
|
||||
});
|
||||
|
||||
@@ -1531,6 +1876,7 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
if (arr) {
|
||||
for (uint32_t sref : arr->refArr) {
|
||||
is->choices.push_back(v.readString(sref));
|
||||
is->choiceRefs.push_back(sref);
|
||||
is->choiceSel.push_back(false);
|
||||
}
|
||||
}
|
||||
@@ -1543,6 +1889,7 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
LcduiItemState* is = ensureItemState(v, a[0].ref);
|
||||
if (!is) return JValue::FromInt(0);
|
||||
is->choices.push_back(v.readString(a[1].ref));
|
||||
is->choiceRefs.push_back(a[1].ref);
|
||||
is->choiceSel.push_back(false);
|
||||
ctx->repaintPending = true;
|
||||
return JValue::FromInt(static_cast<int32_t>(is->choices.size()) - 1);
|
||||
@@ -1571,10 +1918,32 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
LcduiItemState* is = ensureItemState(v, a[0].ref);
|
||||
if (is && a[1].i >= 0 &&
|
||||
static_cast<size_t>(a[1].i) < is->choices.size()) {
|
||||
if (static_cast<size_t>(a[1].i) < is->choiceRefs.size() &&
|
||||
is->choiceRefs[a[1].i] != 0) {
|
||||
return JValue::FromRef(is->choiceRefs[a[1].i]);
|
||||
}
|
||||
return JValue::FromRef(v.internString(is->choices[a[1].i]));
|
||||
}
|
||||
return JValue::FromRef(v.internString(""));
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/lcdui/ChoiceGroup", "isSelected",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
LcduiItemState* is = ensureItemState(v, a[0].ref);
|
||||
if (!is) return JValue::FromInt(0);
|
||||
if (is->choiceType == 2 && a[1].i >= 0 &&
|
||||
static_cast<size_t>(a[1].i) < is->choiceSel.size()) {
|
||||
return JValue::FromInt(is->choiceSel[a[1].i] ? 1 : 0);
|
||||
}
|
||||
return JValue::FromInt(is->selectedIdx == a[1].i ? 1 : 0);
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/lcdui/ChoiceGroup", "size",
|
||||
[](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
LcduiItemState* is = ensureItemState(v, a[0].ref);
|
||||
return JValue::FromInt(
|
||||
is ? static_cast<int32_t>(is->choices.size()) : 0);
|
||||
});
|
||||
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/lcdui/Gauge", "<init>",
|
||||
@@ -1610,6 +1979,17 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
LcduiItemState* is = ensureItemState(v, a[0].ref);
|
||||
return JValue::FromInt(is ? is->maxValue : 100);
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/lcdui/Gauge", "setMaxValue",
|
||||
[ctx](Jvm& v, JThread&, JValue* a) -> JValue {
|
||||
LcduiItemState* is = ensureItemState(v, a[0].ref);
|
||||
if (is) {
|
||||
is->maxValue = std::max(1, a[1].i);
|
||||
is->value = std::clamp(is->value, 0, is->maxValue);
|
||||
ctx->repaintPending = true;
|
||||
}
|
||||
return JValue::Null();
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// javax.microedition.lcdui.game.Sprite / TiledLayer / LayerManager
|
||||
@@ -1781,12 +2161,11 @@ inline void registerLcduiAndM3gNatives(Jvm& vm, RuntimeAppContext* ctx) {
|
||||
// -------------------------------------------------------------------------
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Graphics3D", "getInstance",
|
||||
[](Jvm& v, JThread&, JValue*) -> JValue {
|
||||
static uint32_t g3dRef = 0;
|
||||
if (g3dRef == 0) {
|
||||
g3dRef = v.allocObject("javax/microedition/m3g/Graphics3D");
|
||||
[ctx](Jvm& v, JThread&, JValue*) -> JValue {
|
||||
if (ctx->graphics3dRef == 0) {
|
||||
ctx->graphics3dRef = v.allocObject("javax/microedition/m3g/Graphics3D");
|
||||
}
|
||||
return JValue::FromRef(g3dRef);
|
||||
return JValue::FromRef(ctx->graphics3dRef);
|
||||
});
|
||||
vm.registerWildcardNative(
|
||||
"javax/microedition/m3g/Graphics3D", "bindTarget",
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <X11/XF86keysym.h>
|
||||
#include <X11/keysym.h>
|
||||
#include <malloc.h>
|
||||
#include <poll.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/timerfd.h>
|
||||
@@ -65,6 +66,7 @@ static const char* const kActionNames[ACT_COUNT] = {
|
||||
|
||||
struct RemedyConfig {
|
||||
bool hwAccel3D = true;
|
||||
int scaleMode = 0; // 0=Auto, 1=Full Stretch, 2=1:1 (240x320)
|
||||
int softbarMode = 0; // 0=Auto (non-fullscreen Canvas), 1=Always Overlay, 2=Hidden
|
||||
int fpsLimit = 30; // 30 or 60 FPS
|
||||
bool audioEnabled = true;
|
||||
@@ -95,6 +97,7 @@ struct RemedyConfig {
|
||||
std::string v = line.substr(eq + 1);
|
||||
int iv = std::atoi(v.c_str());
|
||||
if (k == "hwAccel3D") hwAccel3D = (iv != 0);
|
||||
else if (k == "scaleMode") scaleMode = std::clamp(iv, 0, 2);
|
||||
else if (k == "softbarMode") softbarMode = std::clamp(iv, 0, 2);
|
||||
else if (k == "fpsLimit") fpsLimit = (iv == 60) ? 60 : 30;
|
||||
else if (k == "audioEnabled") audioEnabled = (iv != 0);
|
||||
@@ -115,6 +118,7 @@ struct RemedyConfig {
|
||||
std::ofstream f(p, std::ios::trunc);
|
||||
if (!f) return;
|
||||
f << "hwAccel3D=" << (hwAccel3D ? 1 : 0) << "\n";
|
||||
f << "scaleMode=" << scaleMode << "\n";
|
||||
f << "softbarMode=" << softbarMode << "\n";
|
||||
f << "fpsLimit=" << fpsLimit << "\n";
|
||||
f << "audioEnabled=" << (audioEnabled ? 1 : 0) << "\n";
|
||||
@@ -336,6 +340,7 @@ class RemedyEmulator {
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
unloadMidletSuite();
|
||||
if (canvasXimg) {
|
||||
canvasXimg->data = nullptr;
|
||||
XDestroyImage(canvasXimg);
|
||||
@@ -347,6 +352,29 @@ class RemedyEmulator {
|
||||
neoWin.destroy();
|
||||
}
|
||||
|
||||
void unloadMidletSuite() {
|
||||
cldcRuntime.sound.stopAll();
|
||||
activeCanvasKeys.clear();
|
||||
if (vm && hasLoadedMidlet && appCtx.midletRef != 0) {
|
||||
JObject* mo = vm->getObj(appCtx.midletRef);
|
||||
if (mo) {
|
||||
auto [dc, dm] =
|
||||
vm->resolveMethod(mo->className, "destroyApp", "(Z)V", false);
|
||||
if (dm && !dm->code.empty()) {
|
||||
JThread dTh;
|
||||
vm->invokeManagedSync(
|
||||
dTh, dc, dm,
|
||||
{JValue::FromRef(appCtx.midletRef), JValue::FromInt(1)},
|
||||
1000000);
|
||||
}
|
||||
}
|
||||
}
|
||||
vm.reset();
|
||||
appCtx = RuntimeAppContext{};
|
||||
jar = ZipArchive{};
|
||||
hasLoadedMidlet = false;
|
||||
}
|
||||
|
||||
void grabKeyboard() {
|
||||
if (!neoWin.dpy || neoWin.win == None) return;
|
||||
if (isExternalOverlayActive()) {
|
||||
@@ -490,8 +518,7 @@ class RemedyEmulator {
|
||||
}
|
||||
|
||||
bool loadMidletSuite(const std::string& path) {
|
||||
cldcRuntime.sound.stopAll();
|
||||
activeCanvasKeys.clear();
|
||||
unloadMidletSuite();
|
||||
midletTickCount = 0;
|
||||
std::string errMsg;
|
||||
if (!loadSuiteFromJarOrJad(path, &jar, &suite, &errMsg)) {
|
||||
@@ -538,7 +565,7 @@ class RemedyEmulator {
|
||||
vm->resolveMethod(mainClass, "<init>", "()V", false);
|
||||
if (initM && !initM->code.empty()) {
|
||||
vm->invokeManagedSync(mainTh, initCls, initM,
|
||||
{JValue::FromRef(midletRef)}, 5000000);
|
||||
{JValue::FromRef(midletRef)}, 20000000);
|
||||
}
|
||||
mainTh.pendingExceptionRef = 0;
|
||||
mainTh.frames.clear();
|
||||
@@ -546,7 +573,7 @@ class RemedyEmulator {
|
||||
vm->resolveMethod(mainClass, "startApp", "()V", false);
|
||||
if (startM && !startM->code.empty()) {
|
||||
vm->invokeManagedSync(mainTh, startCls, startM,
|
||||
{JValue::FromRef(midletRef)}, 25000000);
|
||||
{JValue::FromRef(midletRef)}, 50000000);
|
||||
}
|
||||
|
||||
currentJarOrJadPath = path;
|
||||
@@ -598,20 +625,27 @@ class RemedyEmulator {
|
||||
// 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.
|
||||
// sleep in Thread.sleep() / wait() or finish a serviceRepaints() frame
|
||||
// return to poll() immediately.
|
||||
cldcRuntime.tickTimersAndSerialCallbacks(*vm);
|
||||
{
|
||||
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;
|
||||
appCtx.paintedInStep = false;
|
||||
do {
|
||||
vm->stepThreads(20000);
|
||||
++slices;
|
||||
} while (slices < 64 && !vm->exitRequested && !appCtx.midletDestroyed &&
|
||||
vm->anyThreadReady() && Jvm::NowMs() < deadline);
|
||||
!appCtx.paintedInStep && vm->anyThreadReady() &&
|
||||
Jvm::NowMs() < deadline);
|
||||
}
|
||||
midletTickCount++;
|
||||
if (midletTickCount % 120 == 0 ||
|
||||
vm->usedHeapBytes() > 24 * 1024 * 1024) {
|
||||
vm->collectGarbage();
|
||||
}
|
||||
|
||||
if (!activeCanvasKeys.empty()) {
|
||||
int64_t nowMs = Jvm::NowMs();
|
||||
@@ -633,8 +667,7 @@ class RemedyEmulator {
|
||||
}
|
||||
|
||||
if (appCtx.midletDestroyed || vm->exitRequested) {
|
||||
cldcRuntime.sound.stopAll();
|
||||
hasLoadedMidlet = false;
|
||||
unloadMidletSuite();
|
||||
mode = EmuUiMode::BROWSER;
|
||||
renderBrowserMenu();
|
||||
return;
|
||||
@@ -671,7 +704,9 @@ class RemedyEmulator {
|
||||
|
||||
// Active Displayable is a full-screen 240x320 Canvas / GameCanvas
|
||||
JObject* dispObj = vm->getObj(dispRef);
|
||||
if (dispObj && appCtx.repaintPending) {
|
||||
if (dispObj && appCtx.repaintPending && !appCtx.inPaint) {
|
||||
appCtx.inPaint = true;
|
||||
appCtx.repaintPending = false;
|
||||
JObject* gObj = vm->getObj(appCtx.sharedGraphicsRef);
|
||||
if (gObj && gObj->nativeData) {
|
||||
auto* gs = static_cast<NativeGraphicsState*>(gObj->nativeData.get());
|
||||
@@ -686,14 +721,54 @@ class RemedyEmulator {
|
||||
paintTh, rc, rm,
|
||||
{JValue::FromRef(dispRef),
|
||||
JValue::FromRef(appCtx.sharedGraphicsRef)},
|
||||
250000);
|
||||
25000000);
|
||||
}
|
||||
appCtx.repaintPending = false;
|
||||
appCtx.inPaint = false;
|
||||
}
|
||||
|
||||
// Blit 240x320 ARGB8888 canvasFb to NeoWindow's offscreen Pixmap
|
||||
std::memcpy(ximgPixels.data(), appCtx.canvasFb.pixels.data(),
|
||||
240 * 320 * sizeof(uint32_t));
|
||||
// Blit 240x320 ARGB8888 canvasFb to NeoWindow's offscreen Pixmap,
|
||||
// automatically scaling sub-QVGA games (128x128, 128x160, 176x208, 176x220)
|
||||
int maxX = appCtx.canvasFb.maxDrawnX;
|
||||
int maxY = appCtx.canvasFb.maxDrawnY;
|
||||
if (cfg.scaleMode != 2 && maxX > 0 && maxX < 192 && maxY > 0 && maxY < 236) {
|
||||
int srcW = 240;
|
||||
int srcH = 320;
|
||||
if (maxX < 128 && maxY < 128) {
|
||||
srcW = 128;
|
||||
srcH = 128;
|
||||
} else if (maxX < 128 && maxY < 160) {
|
||||
srcW = 128;
|
||||
srcH = 160;
|
||||
} else if (maxX < 176 && maxY < 208) {
|
||||
srcW = 176;
|
||||
srcH = 208;
|
||||
} else if (maxX < 176 && maxY < 220) {
|
||||
srcW = 176;
|
||||
srcH = 220;
|
||||
} else {
|
||||
srcW = maxX + 1;
|
||||
srcH = maxY + 1;
|
||||
}
|
||||
int dstW = 240;
|
||||
int dstH = (cfg.scaleMode == 1)
|
||||
? 320
|
||||
: std::min(320, (srcH * 240 + srcW / 2) / srcW);
|
||||
int dstY0 = (320 - dstH) / 2;
|
||||
std::fill(ximgPixels.begin(), ximgPixels.end(), 0xFF000000u);
|
||||
const uint32_t* srcPx = appCtx.canvasFb.pixels.data();
|
||||
for (int dy = 0; dy < dstH; ++dy) {
|
||||
int sy = (dy * srcH) / dstH;
|
||||
const uint32_t* sRow = srcPx + sy * 240;
|
||||
uint32_t* dRow = ximgPixels.data() + (dstY0 + dy) * 240;
|
||||
for (int dx = 0; dx < dstW; ++dx) {
|
||||
int sx = (dx * srcW) / dstW;
|
||||
dRow[dx] = sRow[sx];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
std::memcpy(ximgPixels.data(), appCtx.canvasFb.pixels.data(),
|
||||
240 * 320 * sizeof(uint32_t));
|
||||
}
|
||||
XPutImage(neoWin.dpy, neoWin.pix, neoWin.gc, canvasXimg, 0, 0, 0, 0, 240, 320);
|
||||
|
||||
// Optional softkey bar or command popup overlay via NeoUI
|
||||
@@ -813,6 +888,8 @@ class RemedyEmulator {
|
||||
items.push_back("Resume MIDlet");
|
||||
items.push_back("MIDlet Commands...");
|
||||
}
|
||||
const char* scModes[] = {"< Auto >", "< Full Stretch >", "< 1:1 (240x320) >"};
|
||||
items.push_back(std::string("Canvas Scale: ") + scModes[cfg.scaleMode]);
|
||||
items.push_back(std::string("3D Engine: ") +
|
||||
(m3g.useHardwareGl ? "< Adreno FD304 GL >" : "< Software 3D >"));
|
||||
const char* sbModes[] = {"< Auto >", "< Overlay >", "< Full 240x320 >"};
|
||||
@@ -1069,7 +1146,7 @@ class RemedyEmulator {
|
||||
vm->invokeManagedSync(
|
||||
keyTh, rc, rm,
|
||||
{JValue::FromRef(dispRef), JValue::FromInt(midpKeyCode)},
|
||||
100000);
|
||||
5000000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1153,21 +1230,28 @@ class RemedyEmulator {
|
||||
static_cast<size_t>(st->selectedIdx) < st->listSelected.size()) {
|
||||
st->listSelected[st->selectedIdx] = !st->listSelected[st->selectedIdx];
|
||||
}
|
||||
// Trigger IMPLICIT list command or first OK/SCREEN command
|
||||
// Trigger IMPLICIT list selectCommand (List.SELECT_COMMAND by default)
|
||||
// or first OK/SCREEN/ITEM command
|
||||
uint32_t cmdToFire = 0;
|
||||
for (const auto& c : st->commands) {
|
||||
if (c.type == 1 || c.type == 4 || c.type == 8) {
|
||||
cmdToFire = c.cmdRef;
|
||||
break;
|
||||
if (st->choiceType == 3) {
|
||||
cmdToFire = st->selectCommandRef != 0
|
||||
? st->selectCommandRef
|
||||
: appCtx.listSelectCommandRef;
|
||||
}
|
||||
if (cmdToFire == 0) {
|
||||
for (const auto& c : st->commands) {
|
||||
if (c.type == 1 || c.type == 4 || c.type == 8) {
|
||||
cmdToFire = c.cmdRef;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cmdToFire == 0) {
|
||||
cmdToFire = appCtx.listSelectCommandRef;
|
||||
}
|
||||
JThread cmdTh;
|
||||
if (cmdToFire != 0) {
|
||||
invokeCommandOnDisplayable(*vm, cmdTh, dispRef, cmdToFire);
|
||||
} else if (st->listenerRef != 0) {
|
||||
// List.SELECT_COMMAND (null or synthetic command)
|
||||
uint32_t selCmd = vm->allocObject("javax/microedition/lcdui/Command");
|
||||
invokeCommandOnDisplayable(*vm, cmdTh, dispRef, selCmd);
|
||||
}
|
||||
appCtx.repaintPending = true;
|
||||
}
|
||||
@@ -1500,7 +1584,11 @@ class RemedyEmulator {
|
||||
appCtx.repaintPending = true;
|
||||
return true;
|
||||
}
|
||||
if (choice.rfind("3D Engine:", 0) == 0) {
|
||||
if (choice.rfind("Canvas Scale:", 0) == 0) {
|
||||
int dir = (ks == XK_Left) ? 2 : 1;
|
||||
cfg.scaleMode = (cfg.scaleMode + dir) % 3;
|
||||
cfg.save();
|
||||
} else if (choice.rfind("3D Engine:", 0) == 0) {
|
||||
cfg.hwAccel3D = !cfg.hwAccel3D;
|
||||
m3g.useHardwareGl = cfg.hwAccel3D && m3g.hwInitialized;
|
||||
cfg.save();
|
||||
|
||||
Reference in new issue
Block a user